Develop a BST implementation that omits rank and select and

Develop a BST implementation that omits rank() and select() and does not
use a count field in Node.
Please give me solution

Solution


#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *l;
struct Node *r;
};
typedef struct Node tree ;
tree *root=NULL;
class binary_tree
{
int num;
tree *p,*prev,*temp;
public:
void insertNode();
void inorderTraversal(tree *);
void postorderTraversal(tree *);
void preorderTraversal(tree *);
void display();
};
void binary_tree:: insertNode()
{
p=new(tree);
cout<<\"\ Enter number:\";
cin>>num;
p->data=num;
p->l=p->r=NULL;
if(root==NULL)
{
root=p;
return;
}
temp=root;
while(temp!=NULL)
{
if(num>=temp->data)
{
prev=temp;
temp=temp->r;
}
else
{
prev=temp;
temp=temp->l;
}
}
if(num>=prev->data)
prev->r=p;
else
prev->l=p;
}
void binary_tree::preorderTraversal(tree *temp)
{
if(temp!=NULL)
{
cout<<\" \"<<temp->data;
preorderTraversal(temp->l);
preorderTraversal(temp->r);
}
}
void binary_tree:: inorderTraversal(tree *temp)
{
if(temp!=NULL)
{
inorderTraversal(temp->l);
cout<<\" \"<<temp->data;
inorderTraversal(temp->r);
}
}
void binary_tree::postorderTraversal(tree *temp)
{
if(temp!=NULL)
{
postorderTraversal(temp->l);
postorderTraversal(temp->r);
cout<<\" \"<<temp->data;
}
}
void binary_tree:: display()
{
if(root==NULL)
{
cout<<\"\ tree is empty \ \";
return;
}
cout<<\"\ \ PREORDER : \";
preorderTraversal(root);
cout<<\"\ \ INORDER : \";
inorderTraversal(root);
cout<<\"\ \ POSTORDER : \";
postorderTraversal(root);
}
int main()
{
binary_tree obj;
int ch=1;
int count=0;
  
while(ch)
{
  
cout<<\"\ 1:INSERT DATA INTO TREE\ 2:DISPLAY TREE\ 3.QUIT\ \";
cout<<\"\ Enter your choice:\ \";
cin>>ch;
switch(ch)
{
case 1:
count++;
obj.insertNode();
break;
case 2:
cout<<\"\ \ THE COUNT OF NODES \"<< count;
obj.display();
break;
case 3:exit(0);
}
}
  
}

==============================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ g++ bst.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ g++ bst.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out

1:INSERT DATA INTO TREE
2:DISPLAY TREE
3.QUIT

Enter your choice:
1

Enter number:2

1:INSERT DATA INTO TREE
2:DISPLAY TREE
3.QUIT

Enter your choice:
2


THE COUNT OF NODES 1

PREORDER : 2

INORDER : 2

POSTORDER : 2
1:INSERT DATA INTO TREE
2:DISPLAY TREE
3.QUIT

Enter your choice:
1

Enter number:3

1:INSERT DATA INTO TREE
2:DISPLAY TREE
3.QUIT

Enter your choice:
2


THE COUNT OF NODES 2

PREORDER : 2 3

INORDER : 2 3

POSTORDER : 3 2
1:INSERT DATA INTO TREE
2:DISPLAY TREE
3.QUIT

Enter your choice:

Develop a BST implementation that omits rank() and select() and does not use a count field in Node. Please give me solutionSolution #include<bits/stdc++.h>
Develop a BST implementation that omits rank() and select() and does not use a count field in Node. Please give me solutionSolution #include<bits/stdc++.h>
Develop a BST implementation that omits rank() and select() and does not use a count field in Node. Please give me solutionSolution #include<bits/stdc++.h>
Develop a BST implementation that omits rank() and select() and does not use a count field in Node. Please give me solutionSolution #include<bits/stdc++.h>

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site