Question

In: Computer Science

In C++: Write a binary search tree and include the functions to find the number of...

In C++:

Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal.

Solutions

Expert Solution

#include<bits/stdc++.h>

using namespace std;

class BST

{

int data;

BST *left, *right;

public:

BST(){}

BST(int value)

{

data = value;

left = right = NULL;

}

  

BST* Insert(BST *root, int value)

{

if(!root)

{

return new BST(value);

}

  

if(value > root->data)

{

root->right = Insert(root->right, value);

}

else

{

root->left = Insert(root->left, value);

}

return root;

}

void Inorder(BST *root)

{

if(!root)

{

return;

}

Inorder(root->left);

cout << root->data << " ";

Inorder(root->right);

}

void Preorder(BST *root)

{

if(!root)

{

return;

}

cout << root->data << " ";

Preorder(root->left);

Preorder(root->right);

}

void Postorder(BST *root)

{

if(!root)

{

return;

}

Postorder(root->left);

Postorder(root->right);

cout << root->data << " ";

}

int Count(BST *root)

{

if(root == NULL){

return 0;

}

else{

return 1 + Count(root->left) + Count(root->right);

}

}

};

int main()

{

BST *root = NULL;

int value;

int choice;

BST bst;;

  

while (1)

{

cout<<"\n\n1.Insert Node"<<endl;

cout<<"2.Count Nodes"<<endl;

cout<<"3.Inorder Traversal"<<endl;

cout<<"4.Preorder Traversal"<<endl;

cout<<"5.Postorder Traversal"<<endl;

cout<<"6.Quit"<<endl;

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

{

case 1:

cout<<"\nEnter the number to be inserted : ";

cin>>value;

root = bst.Insert(root, value);

break;

case 2:

cout << "\nTotal no of nodes : " << bst.Count(root);

break;

case 3:

cout<<"Inorder Traversal of BST:"<<endl;

bst.Inorder(root);

cout<<endl;

break;

case 4:

cout<<"Preorder Traversal of BST:"<<endl;

bst.Preorder(root);

cout<<endl;

break;

case 5:

cout<<"Postorder Traversal of BST:"<<endl;

bst.Postorder(root);

cout<<endl;

break;

case 6:

exit(1);

default:

cout<<"Wrong choice"<<endl;

}

}

}


Related Solutions

Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT