Question
Create a binary search tree. Write a program to test the following operations on binary search tree.
1. Determine whether the binary search tree is empty
2. Search the binary search tree for a particular item.
3. Insert an item in the binary search tree.
4. Delete an item from the binary search tree.
5. Find the height of the binary search tree.
6. Find the number of nodes in the binary search tree.
7. Find the number of leaves in the binary search tree.
8. Traverse the binary search tree(implement inorder, preorder, and postorder traversal algorithms ) (nonrecursive and recursive)
9. Copy the binary search tree. For testing create input file in.txt and take input from input file.
Output to output file
ONLY IN C++ PLEASE
i need help fast. thank you
Explanation / Answer
#include using namespace std; // Node class class Node { int key; Node* left; Node* right; public: Node() { key=-1; left=NULL; right=NULL; }; void setKey(int aKey) { key = aKey; }; void setLeft(Node* aLeft) { left = aLeft; }; void setRight(Node* aRight) { right = aRight; }; int Key() { return key; }; Node* Left() { return left; }; Node* Right() { return right; }; }; // Tree class class Tree { Node* root; public: Tree(); ~Tree(); Node* Root() { return root; }; void addNode(int key); void inOrder(Node* n); void preOrder(Node* n); void postOrder(Node* n); private: void addNode(int key, Node* leaf); void freeNode(Node* leaf); }; // Constructor Tree::Tree() { root = NULL; } // Destructor Tree::~Tree() { freeNode(root); } // Free the node void Tree::freeNode(Node* leaf) { if ( leaf != NULL ) { freeNode(leaf->Left()); freeNode(leaf->Right()); delete leaf; } } // Add a node void Tree::addNode(int key) { // No elements. Add the root if ( root == NULL ) { cout Left()); cout Left()); postOrder(n->Right()); cout addNode(10); tree->addNode(20); tree->addNode(40); tree->addNode(50); cout