58.8k views
0 votes
Write a program that reads numbers from a file (or allow user to input data) and creates an ordered binary tree. The program should display the tree in a text or graphics format then allow user to add values to it, or remove/delete values from it. The program should also allow the user to print the tree in different order forms (LNR, NLR, LRN).

User Rpearce
by
5.4k points

1 Answer

4 votes

Answer:

See explaination

Step-by-step explanation:

include<bits/stdc++.h>

using namespace std;

typedef struct Node

{

int data;

struct Node *left,*right;

}Node;

bool search(Node *root,int data)

{

if(root==NULL)

return false;

if(root->data==data)

return true;

queue<Node*> q;

q.push(root);

while(!q.empty())

{

Node *temp=q.front();

q.pop();

if(temp->data==data)

return true;

if(temp->left)

q.push(temp->left);

if(temp->right)

q.push(temp->right);

}

return false;

}

Node *insert(Node *root,int data)

{

if(root==NULL)

{

Node *temp=new Node();

temp->data=data;

temp->left=NULL;

temp->right=NULL;

return temp;

}

if(data < root->data)

root->left=insert(root->left,data);

if(data>root->data)

root->right=insert(root->right,data);

return root;

}

Node *get_smallest_element_right_subtree(Node *root)

{

while(root && root->left!=NULL)

root=root->left;

return root;

}

Node *delete_node(Node *root,int data)

{

if(root==NULL)

return root;

if(data < root->data)

root->left=delete_node(root->left,data);

else if(data > root->data)

root->right=delete_node(root->right,data);

else

{

if(root->left==NULL) //If right only presents means - delete the curr node and return right node

{

Node *temp=root->right;

free(root);

return temp;

}

else if(root->right==NULL) //If left only presents means - delete the curr node and return let node

{

Node *temp=root->left;

free(root);

return temp;

}

else

{

Node *temp=get_smallest_element_right_subtree(root->right);

root->data=temp->data;

root->right=delete_node(root->right,temp->data);

}

return root;

}

}

void inorder(Node *root)

{

if(root!=NULL)

{

inorder(root->left);

cout<<root->data<<" ";

inorder(root->right);

}

}

void postorder(Node *root)

{

if(root!=NULL)

{

inorder(root->left);

inorder(root->right);

cout<<root->data<<" ";

}

}

void preorder(Node *root)

{

if(root!=NULL)

{

cout<<root->data<<" ";

inorder(root->left);

inorder(root->right);

}

}

int main()

{

fstream f;

string filename;

cout<<"\\\\1 - Input through File ";

cout<<"\\\\2 - Input through your Hand";

int h;

cout<<"\\\\\\Enter Your Choice : ";

cin>>h;

Node *root=NULL; // Tree Declaration

if(h==1)

{

cout<<"\\\\Enter the Input File Name : ";

cin>>filename;

f.open(filename.c_str());

if(!f)

cout<<"\\\\Error in Opening a file !";

else

{

cout<<"\\\\File is Being Read ........";

string num;

int value;

int node=0;

while(f>> num)

{

value=stoi(num);

root=insert(root,value);

node++;

}

cout<<"\\\\Tree has been successfully created with : "<<node<<" Nodes"<<endl;

}

}

if(h==2)

{

int y;

cout<<"\\\\Enter the Total No of Input :";

cin>>y;

int i=1,g;

while(i!=y+1)

{

cout<<"\\\\Enter Input "<<i<<" : ";

cin>>g;

root=insert(root,g);

i++;

}

cout<<"\\\\Tree has been successfully created with : "<<y<<" Nodes"<<endl;

}

if(h>=3)

{

cout<<"\\\\Invalid Choice !!! ";

return 0;

}

int n=0;

while(n!=6)

{

cout<<"\\\\\\1 - Insert Element";

cout<<"\\\\2 - Remove Element";

cout<<"\\\\3 - Inorder (LNR) Display ";

cout<<"\\\\4 - Pre (NLR) Order Display";

cout<<"\\\\5 - Post (LRN) Order Display";

cout<<"\\\\6 - Quit";

cout<<"\\\\Enter Your Choice : ";

cin>>n;

switch(n)

{

case 1:

{

int k;

cout<<"\\\\Enter Element to insert : ";cin>>k;

root=insert(root,k);

cout<<"\\\\Element Sucessfully Inserted !!!!!";

break;

}

case 2:

{

int k;

cout<<"\\\\Enter Element to Remove : ";

cin>>k;

if(search(root,k))

{

root=delete_node(root,k);

cout<<"\\\\Value Successfully Deleted !!!";

}

else

cout<<"\\\\!!!!!!!!!!!!!!!!!!!! No Such Element !!!!!!!!!!!!!!!!!!!!!!";

break;

}

case 3:

{

cout<<"\\\\The Elements (LNR) are : ";

inorder(root);

break;

}

case 4:

{

cout<<"\\\\The Elements (NLR) are : ";

preorder(root);

break;

}

case 5:

{

cout<<"\\\\The Elements (LRN) are : ";

postorder(root);

break;

}

case 6:

{

break;

}

}

}

cout<<"\\\\Bye!!!! See You !!!"<<endl;

User Flink
by
5.1k points