Question

Create a binary search tree from the following: 43, 5, 2, 54, 64, 23, 6, 48,...

Create a binary search tree from the following: 43, 5, 2, 54, 64, 23, 6, 48, 30 and write its preorder, inorder and postorder traversal. (Needs to be in C#)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

using System;
  
/* Class containing left and
right child of current
node and key value*/
class Node
{
public int key;
public Node left, right;
  
public Node(int item)
{
key = item;
left = right = null;
}
}
  
class BinaryTree
{
// Root of Binary Tree
Node root;
  
BinaryTree()
{
root = null;
}
  
/* Given a binary tree, print
its nodes according to the
"bottom-up" postorder traversal. */
void printPostorder(Node node)
{
if (node == null)
return;
  
// first recur on left subtree
printPostorder(node.left);
  
// then recur on right subtree
printPostorder(node.right);
  
// now deal with the node
Console.Write(node.key + " ");
}
  
/* Given a binary tree, print
its nodes in inorder*/
void printInorder(Node node)
{
if (node == null)
return;
  
/* first recur on left child */
printInorder(node.left);
  
/* then print the data of node */
Console.Write(node.key + " ");
  
/* now recur on right child */
printInorder(node.right);
}
  
/* Given a binary tree, print
its nodes in preorder*/
void printPreorder(Node node)
{
if (node == null)
return;
  
/* first print data of node */
Console.Write(node.key + " ");
  
/* then recur on left sutree */
printPreorder(node.left);
  
/* now recur on right subtree */
printPreorder(node.right);
}
  
// Wrappers over above recursive functions
void printPostorder() {printPostorder(root);}
void printInorder() {printInorder(root);}
void printPreorder() {printPreorder(root);}
  
// Driver Code
static public void Main(String []args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(43);
tree.root.left = new Node(5);
tree.root.right = new Node(54);
tree.root.left.left = new Node(2);
tree.root.right.right = new Node(64);
tree.root.right.left = new Node(23);
tree.root.left.right = new Node(6);
tree.root.right.right.left = new Node(48);
tree.root.left.right.right = new Node(30);
Console.WriteLine("Preorder traversal " +
"of binary tree is ");
tree.printPreorder();
  
Console.WriteLine("\nInorder traversal " +   
"of binary tree is ");
tree.printInorder();
  
Console.WriteLine("\nPostorder traversal " +
"of binary tree is ");
tree.printPostorder();
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Create a binary search tree from the following: 43, 5, 2, 54, 64, 23, 6, 48,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT