Create a binary search tree from the following: 43, 5, 2, 54, 64, 23, 6, 48, 30? (needs to be in C#)
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 -
pls do give a like , thanks
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#)
17) Which of the following is a valid binary search tree? 23 12 40 19 30 61 13 21 41 50 23 43 10 18 34 51 15 21 40 50 23 43 10 18 51 15 27 40 50 What is the worst-case runtime of searching for a value in a binary search tree? (a) constant O(1) (b) logarithmic O(logn) (c) linear O(n) 18) Pag
Tree & Hash Table & Heap Use the following integer keys 73, 58, 91, 42, 60, 130, 64, 87 to perform the followings: a) Binary Search Tree - Draw a binary search tree - Retrieve the integers keys in post-order - Retrieve the integers keys in pre-order - Draw a binary search tree after node 58 is deleted b) Create a Hash Table using the methods described below. Show the final array after all integer keys are inserted. Assumes that...
Create a binary search tree using the following data entered as a sequential set:14 23 7 10 33 56 80 66 70
a) Create a binary search tree with the 4 2 2 0 1 4 5 9 7 1 5 3 6 number. b) Provide the Preorder, Inorder and Postorder traversal of tree obtained in part (a). Show all the steps.
What are the main characteristics of a binary search tree? In your opinion, what advantages does a binary search tree have over a linked list or an array? Assuming you want to create a binary search tree of integers, draw the resulting tree if the integers input were as follows : 25 43 12 20 5 50 30
A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10, 6, 4, 8, 18, 15, 21 Please type...
Create a binary search tree with the following data (insert them in the order in which it is indicated data: 15, 3, 9, 2, 5, 38, 14, 96, 7, 26, 56, 76, 23 Once you finish with the insertions, return it to Inorder, Preorder and Postorder.
answer number 7 5. Draw the binary search tree that would result from inserting the following numbers into an initially empty tree in the order given: 45, 37, 62, 67, 49, 51, 16, 8, 73, 71, 64, 65 6. Given the tree created in problem 5, draw the tree that would result if you deleted the 37 from the tree? 7. Given the tree created in problem 5, draw the tree that would result if you deleted the 62 from...
Draw the binary search tree that would result from inserting the following data in the order listed into an initially empty tree 37, 19, 11, 64, 16, 73, 25, 8, 70