Question

In: Computer Science

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.

Solutions

Expert Solution

Note: here all code are done as per your request in the question including comment details of each logic code, but if you have any doubt or issue feel free to ask me or post the issue.

BST.cpp

#include <iostream> 
using namespace std; 

class BST 
{ 
        int data; 
        BST *left, *right; 

        public: 
           // default constructor
                BST(){
                    data = 0;
                    left = NULL;
                    right = NULL;
            }
            
            // parameterized constructor
            BST(int value) { 
                data = value; 
                    left = right = NULL; 
            }  
        
               // Insert function. 
           BST* Insert(BST *root, int value) { 
                // Insert the first node, if root is NULL.
                if(!root) 
                    return new BST(value); 

                    //  Insert Right node data, if the 'value' is greater than 'root' node data.  
                if(value > root->data) 
                            root->right = Insert(root->right, value); 
                
                 //     Insert Left node data, if the 'value' is smaller than 'root' node data.
                    else
                            root->left = Insert(root->left, value); 
                return root; 
            } 
            
    int numOfNodes(BST* root){
        int count = 1;
    
        // if left is not null goto left node and increment count
        if (root->left != NULL) 
           count += numOfNodes(root->left);
    
           // if right is not null goto left node and increment count
        if (root->right != NULL) 
           count += numOfNodes(root->right);
    
        return count;
   }

    int height(BST* root){
        if(root == NULL)
             return 0;
    
        else { 
             // compute the depth of each subtree 
          int lDepth = height(root->left); 
          int rDepth = height(root->right); 
  
            // return  the max depth 
          if (lDepth > rDepth)  
            return(lDepth+1); 
          else return(rDepth+1); 
        }
    }
        
        // Preorder traversal. 
        void Preorder(BST *root){
            if(!root) 
           return; 
        
        cout << root->data << " "; 
        Inorder(root->left); 
        Inorder(root->right); 
    } 
        
        // Inorder traversal. 
        void Inorder(BST *root){
            if(!root) 
           return; 
        
        Inorder(root->left); 
        cout << root->data << " "; 
        Inorder(root->right); 
    } 
    
    // Postorder traversal. 
        void Postorder(BST *root){
            if(!root) 
           return; 
        
        Inorder(root->left); 
        Inorder(root->right);
    cout << root->data << " ";  
    } 
}; 


// Driver code 
int main() 
{ 
        BST b, *root = NULL; 
        int n;
/*      
        root = b.Insert(root, 50); 
        b.Insert(root, 30); 
        b.Insert(root, 20); 
        b.Insert(root, 40); 
        b.Insert(root, 70); 
        b.Insert(root, 60); 
        b.Insert(root, 80); 
*/      
        cout << "Enter size of n : ";
        cin >> n;
        for(int i=0; i<n; i++){
            int x;
            cin>>x;
            
            if(root == NULL)
           root = b.Insert(root, x); 
        else
           b.Insert(root, x);
    }
        
        int totalNodes = b.numOfNodes(root);
        cout << "Total Number of Nodes present in BST : " << totalNodes << endl;
        
        int ht = b.height(root);
        cout << "Height of BST : " << ht << endl;
        
   // Display BST
    cout << endl << "Display PreOrder BST : " << endl;
        b.Preorder(root);
    
    cout << endl << "Display InOrder BST : " << endl;
        b.Inorder(root);
    
    cout << endl << "Display PostOrder BST : " << endl;
        b.Postorder(root); 
        return 0; 
} 

Related Solutions

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.
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
Give a proof by induction that the number of nodes of a full binary tree with...
Give a proof by induction that the number of nodes of a full binary tree with a height of "h" is: (2^(h+1))-1.
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
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
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 . •...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT