C# program to implement binary search tree Traversal
Here's an example of a C# program that implements different traversals (inorder, preorder, and postorder) for a binary search tree:
using System;
class Node
{
public int Data;
public Node Left;
public Node Right;
public Node(int data)
{
Data = data;
Left = null;
Right = null;
}
}
class BinarySearchTree
{
private Node root;
public void Insert(int data)
{
root = InsertRecursive(root, data);
}
private Node InsertRecursive(Node current, int data)
{
if (current == null)
{
return new Node(data);
}
if (data < current.Data)
{
current.Left = InsertRecursive(current.Left, data);
}
else if (data > current.Data)
{
current.Right = InsertRecursive(current.Right, data);
}
return current;
}
public void InorderTraversal()
{
InorderTraversalRecursive(root);
Console.WriteLine();
}
private void InorderTraversalRecursive(Node current)
{
if (current != null)
{
InorderTraversalRecursive(current.Left);
Console.Write(current.Data + " ");
InorderTraversalRecursive(current.Right);
}
}
public void PreorderTraversal()
{
PreorderTraversalRecursive(root);
Console.WriteLine();
}
private void PreorderTraversalRecursive(Node current)
{
if (current != null)
{
Console.Write(current.Data + " ");
PreorderTraversalRecursive(current.Left);
PreorderTraversalRecursive(current.Right);
}
}
public void PostorderTraversal()
{
PostorderTraversalRecursive(root);
Console.WriteLine();
}
private void PostorderTraversalRecursive(Node current)
{
if (current != null)
{
PostorderTraversalRecursive(current.Left);
PostorderTraversalRecursive(current.Right);
Console.Write(current.Data + " ");
}
}
}
class Program
{
static void Main(string[] args)
{
BinarySearchTree bst = new BinarySearchTree();
// Inserting elements into the binary search tree
bst.Insert(50);
bst.Insert(30);
bst.Insert(20);
bst.Insert(40);
bst.Insert(70);
bst.Insert(60);
bst.Insert(80);
// Performing inorder traversal of the binary search tree
Console.WriteLine("Inorder Traversal of the Binary Search Tree:");
bst.InorderTraversal();
// Performing preorder traversal of the binary search tree
Console.WriteLine("Preorder Traversal of the Binary Search Tree:");
bst.PreorderTraversal();
// Performing postorder traversal of the binary search tree
Console.WriteLine("Postorder Traversal of the Binary Search Tree:");
bst.PostorderTraversal();
}
}
This program creates a binary search tree class (BinarySearchTree) that contains a private Node class. The Node class represents each node in the binary search tree and holds the data and references to the left and right child nodes.
The BinarySearchTree class provides three traversal methods: InorderTraversal, PreorderTraversal, and PostorderTraversal. Each method uses recursive implementation to traverse the binary search tree in the specified order (inorder, preorder, or postorder) and print the elements.
In the Main method, we create an instance of BinarySearchTree, insert elements into the binary search tree using the Insert method, and then perform inorder, preorder, and postorder traversals using the respective traversal methods.
When you run this program, it will output the following:
Inorder Traversal of the Binary Search Tree:
20 30 40 50 60 70 80
Preorder Traversal of the Binary Search Tree:
50 30 20 40 70 60 80
Postorder Traversal of the Binary Search Tree:
20 40 30 60 80 70 50
This demonstrates the implementation of different binary search tree traversals: inorder, preorder, and postorder.