Answer to Question #281323 in C++ for Zain

Question #281323

Program in c++


create a node for binary tree. you are required to create 2 projects



1. only binary tree



2. binary search tree



perform the operations for



1. constructor



is empty



isFull



hasLeft



hasRight



2. insert (remember insertion in Binary tree is different from insertion in binary search tree)



search in BT and BST



-BFS



-DFS

1
Expert's answer
2021-12-20T10:01:29-0500
#include <iostream>
using namespace std;
 
class BST
{
    int data;
    BST *left, *right;
 
public:
    
    BST();
 
   
    BST(int);
 
    
    BST* Insert(BST*, int);
 
  
    void Inorder(BST*);
};
 

BST ::BST()
    : data(0)
    , left(NULL)
    , right(NULL)
{
}

BST ::BST(int value)
{
    data = value;
    left = right = NULL;
}

BST* 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 BST ::Inorder(BST* root)
{
    if (!root) {
        return;
    }
    Inorder(root->left);
    cout << root->data << endl;
    Inorder(root->right);
}
 

int main()
{
    BST b, *root = NULL;
    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);
 
    b.Inorder(root);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog