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? (needs to be in C#)

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

Code -

using System;
//create class Node
class Node
{
//data to store current data of node
public int data;
//left to point left node
public Node left;
//right to point right node
public Node right;
}
//crate class BinarySearchTree
class BinarySearchTree
{
//root element
public Node root;
//BinarySearchTree constructor, initialize it root to null
public BinarySearchTree()
{
root = null;
}
//function to get root node
public Node getRoot()
{
return root;
}
//function to insert node to Binary search tree
public void Insert(int no)
{
//create new node
Node newNode = new Node();
//initialize data to new node
newNode.data = no;
//check if root is NULL, Then make root as new node
if (root == null)
root = newNode;
//else
else
{
//create currentNode point to root node
Node currentNode = root;
Node parentNode;
//while loop to iterating continously
while (true)
{
//initialize parent node to current node
parentNode = currentNode;
//check if no is less then current node data
if (no < currentNode.data)
{
//the move to left of the node
currentNode = currentNode.left;
//if current node become null
if (currentNode == null)
{
//then make parent node left equal to new element
parentNode.left = newNode;
return;
}
}
//if no is greater than current node data
else
{
//move current node to right
currentNode = currentNode.right;
//if current node become null
if (currentNode == null)
{
//than initialize parentNode right to new node
parentNode.right = newNode;
return;
}
}
}
}
}
//Inorder travesal of binary search tree
public void Inorder(Node rootNode)
{
//check if root node is not null
if (rootNode != null)
{
//recurisve call left of root node
Inorder(rootNode.left);
//print node data
Console.Write(rootNode.data + " ");
//recurisve call right of root node
Inorder(rootNode.right);
}
}
}
class Program
{
static void Main(string[] args)
{
//creater Binary Search Tree object
BinarySearchTree num = new BinarySearchTree();
//insert element to it
num.Insert(43);
num.Insert(5);
num.Insert(2);
num.Insert(54);
num.Insert(64);
num.Insert(23);
num.Insert(6);
num.Insert(48);
num.Insert(30);
//print elements
Console.WriteLine("Inorder Traversal : ");
num.Inorder(num.getRoot());
}
}

Screenshots -

Inorder Traversal : 2 5 6 23 30 43 48 54 64 ...Program finished with exit code o Press ENTER to exit console.O

pls do give a like , 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