Question

In: Computer Science

In C++ with lots of comments please Complete a binary search tree of 20 numbers Show...

In C++ with lots of comments please

Complete a binary search tree of 20 numbers

Show in the output the steps while it's performing the search with 20 numbers in a text file called list.txt

The numbers will be imported to the program

Simple program that should let you have the option to search for numbers, remove numbers, print numbers, and insert numbers in the binary tree

If the number isn't there then give an error

Solutions

Expert Solution

code in c++ (code to copy)

#include<bits/stdc++.h>
using namespace std;
class Node{
        public:
        int data;
        Node* left;
        Node* right;
        Node(int x):data(x){
                left=NULL;
                right=NULL;
        }
        //inserts an element in the tree
        static Node* insert(Node *root, int val){
                if(root==NULL)
                        return new Node(val);
                else{
                        if(val<root->data){
                                root->left=Node::insert(root->left, val);
                        }else{
                                root->right=Node::insert(root->right, val);
                        }
                }
        }
        //deletes the root of the tree
        static Node* popHead(Node *root){
                if(root==NULL){
                        cout<<"List is empty!"<<endl;
                        return NULL;
                }else{
                        Node* temp=root;
                        if(root->left==NULL){
                                root=root->right;
                        }else if(root->right==NULL){
                                root=root->left;
                        }else{
                                Node *r=root->right;
                                while(r->left!=NULL){
                                        r=r->left;
                                }
                                r->left=root->left;
                                root=root->right;
                        }
                        delete temp;
                        return root;
                }
        }
        //deletes an element in the tree
        static Node* deleteElement(Node* root, int ele){
                if(root==NULL){
                        cout<<"Number not found!"<<endl;
                        return NULL;
                }
                if(root->data==ele){
                        return Node::popHead(root);
                }else if(ele<root->data){
                        root->left = deleteElement(root->left, ele);
                }else{
                        root->right = deleteElement(root->right, ele);
                }
        }
        //searches for element in the binary search tree and prints the steps in a file
        static bool search(Node* root, int ele, ofstream &outfile){
                if(root==NULL){
                        //write step in file
                        outfile<<"Encountered null node, returning false!"<<endl;
                        return false;
                }
                if(root->data==ele){
                        outfile<<ele<<" = "<<root->data<<" : Found, returning true!"<<endl;
                        return true;
                }else if(ele<root->data){
                        outfile<<ele<<" < "<<root->data<<" : Searching in left sub tree!"<<endl;
                        return search(root->left, ele, outfile);
                }else{
                        outfile<<ele<<" > "<<root->data<<" : Searching in right sub tree!"<<endl;
                        return search(root->right, ele, outfile);
                }
        }
        // prints the tree in inorder
        static void print(Node* root){
                if(root==NULL)
                return;
                print(root->left);
                cout<<root->data<<" ";
                print(root->right);
        }
};
int main()
{
        //use ofstream to write search steps to file
        ofstream outfile("list.txt");
        int choice;
        //create a tree
        Node * root=NULL;
        while(true){
                cout<<"1. Add element"<<endl;
                cout<<"2. Search element"<<endl;
                cout<<"3. Remove element"<<endl;
                cout<<"4. Print Tree"<<endl;
                cout<<"5. Exit"<<endl;
                cin>>choice;
                switch(choice){
                        case 1: {
                                int ele;
                                cout<<"Enter element: ";
                                cin>>ele;
                                root=Node::insert(root, ele);
                        }break;
                        case 2: {
                                int ele;
                                cout<<"Enter element to search: ";
                                cin>>ele;
                                outfile<<"Searching for "<<ele<<endl;
                                cout<<"Search result: "<<(Node::search(root, ele, outfile) ? "found" : "not found")<<endl;
                                outfile<<endl;
                        }break;
                        case 3: {
                                int ele;
                                cout<<"Enter element to remove: ";
                                cin>>ele;
                                root = Node::deleteElement(root, ele);
                        }break;
                        case 4: {
                                cout<<"Printing tree: "<<endl;
                                Node::print(root);
                                cout<<endl;
                        }break;
                        default: exit(0);
                }
        }
}

code screenshot

Sample Input/Output screenshot

Contents of list.txt after execution


Related Solutions

Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
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.
PLEASE READ CAREFULY AND EXPLAIN YOUR WORK: (JavaScript) only Search the Tree: A binary search tree...
PLEASE READ CAREFULY AND EXPLAIN YOUR WORK: (JavaScript) only Search the Tree: A binary search tree is a data structure that consists of JavaScript objects called "nodes". A tree always has a root node which holds its own integer value property and can have up to two child nodes (or leaf nodes), a left and right property. A leaf node holds a value attribute and, likewise, a left and right attribute each potentially pointing to another node in the binary...
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};
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.
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
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
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.
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels...
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels fully filled. Define a new class named BSTWithTestPerfect that extends BST with the following methods: (Hint: The number of nodes in a perfect binary tree is 2^(height+1) - 1.) /** Returns true if the tree is a perfect binary tree */ public boolean isPerfectBST() Class Name: Exercise25_03
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT