1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
aliina [53]
3 years ago
8

Write a program that reads numbers from a file (or allow user to input data) and creates an ordered binary tree. The program sho

uld 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).
Computers and Technology
1 answer:
IgorLugansk [536]3 years ago
7 0

Answer:

See explaination

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<<"\n\n1 - Input through File ";

cout<<"\n\n2 - Input through your Hand";

int h;

cout<<"\n\n\nEnter Your Choice : ";

cin>>h;

Node *root=NULL; // Tree Declaration

if(h==1)

{

cout<<"\n\nEnter the Input File Name : ";

cin>>filename;

f.open(filename.c_str());

if(!f)

cout<<"\n\nError in Opening a file !";

else

{

cout<<"\n\nFile is Being Read ........";

string num;

int value;

int node=0;

while(f>> num)

{

value=stoi(num);

root=insert(root,value);

node++;

}

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

}

}

if(h==2)

{

int y;

cout<<"\n\nEnter the Total No of Input :";

cin>>y;

int i=1,g;

while(i!=y+1)

{

cout<<"\n\nEnter Input "<<i<<" : ";

cin>>g;

root=insert(root,g);

i++;

}

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

}

if(h>=3)

{

cout<<"\n\nInvalid Choice !!! ";

return 0;

}

int n=0;

while(n!=6)

{

cout<<"\n\n\n1 - Insert Element";

cout<<"\n\n2 - Remove Element";

cout<<"\n\n3 - Inorder (LNR) Display ";

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

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

cout<<"\n\n6 - Quit";

cout<<"\n\nEnter Your Choice : ";

cin>>n;

switch(n)

{

case 1:

{

int k;

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

root=insert(root,k);

cout<<"\n\nElement Sucessfully Inserted !!!!!";

break;

}

case 2:

{

int k;

cout<<"\n\nEnter Element to Remove : ";

cin>>k;

if(search(root,k))

{

root=delete_node(root,k);

cout<<"\n\nValue Successfully Deleted !!!";

}

else

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

break;

}

case 3:

{

cout<<"\n\nThe Elements (LNR) are : ";

inorder(root);

break;

}

case 4:

{

cout<<"\n\nThe Elements (NLR) are : ";

preorder(root);

break;

}

case 5:

{

cout<<"\n\nThe Elements (LRN) are : ";

postorder(root);

break;

}

case 6:

{

break;

}

}

}

cout<<"\n\nBye!!!! See You !!!"<<endl;

You might be interested in
What is a method whereby new problems are solved based on the solutions from similar cases solved in the past?
mario62 [17]

Answer:

"Case-Based Reasoning" is the answer for the above question.

Explanation:

  • Case-Based Reasoning is a process of decision-making theory in which the new problems were solved based on the previously solved problem.
  • It is used in artificial intelligence and robots. This helps to make any AI and robots to do the work and take decisions on its own.
  • The theory is used to make any computer that behaves like humans. It can take decisions like a human.
  • The above question asked about the method by which the new problem is solved on behalf of the old problem. Hence the answer is "Case-Based Reasoning".
4 0
3 years ago
How many mb are in a gb?
frosja888 [35]

Answer:

1000 megabytes are in a gigabyte

Explanation:

3 0
2 years ago
Describe the components of the Web. Your friend wants to create and publish a Web page. He tells you that he wants to learn the
iren [92.7K]

Answer:

The answer is "HTML".

Explanation:

We will suggest him to learn HTML language because It stands for HyperText Markup Language. It is a markup language that uses a build a web page and these sites are normally viewed on the web browser.

  • It is the language for development used in the World Wide Web.
  • This language includes writing, links, images, videos, and audio files.  
  • Each one of these kinds of content is tagged and described by HTML, which uses a web browser can display it correctly.
3 0
2 years ago
WHO LOVES THE KREW LOOK IT UP ON YOU_TUBE IF UR NOT SURE BUT PLZ SUBSCRIBE TO HER SHE DA BEST "YOOT" the one with pink and golde
kodGreya [7K]

Answer:

i like her mate :3 glad im not alone!

Explanation:

7 0
2 years ago
Read 3 more answers
List 2 advantages Cryptocurrency have over regular bank transactions
ratelena [41]

Answer:

low transaction fees and speedier processing

Explanation:

5 0
2 years ago
Read 2 more answers
Other questions:
  • Which IPv6 transition technology can be used with NAT routers and has the address prefix 2001::32?
    10·1 answer
  • Which of the following applications can be used as a data source for a Microsoft® Word® mail merge? A.Outlook Contacts B.Microso
    9·2 answers
  • You use different office apps to accomplish specific tasks, such a creating a newsletter or producing a sales presentation, yet
    8·1 answer
  • A function may return a pointer, but the programmer must ensure that the pointer:
    14·1 answer
  • What is an efficient way to ensure that the code is working as per the acceptance criteria/business requirements? (1 correct ans
    11·2 answers
  • sqrt is being defined as a variable name, how do I get its math function to... well... function? (Python)
    11·1 answer
  • What is a word processor in ms word​
    9·2 answers
  • Which is NOT a benefit of using visual aids?
    9·2 answers
  • Problem 1: you must write a method for this problem called sentenceAnalyzer Write a program that reads a sentence from the keybo
    10·1 answer
  • You can open a movie maker project file any time in a media player. (1 point) true false
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!