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]
4 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]4 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
1) In your own words, explain what a browser is. 2) Why is it important to keep your browser up to date?
marta [7]

Answer:

A browser is a software that is used to access the internet. A browser lets you visit websites and do activities within them like login, view multimedia, link from one site to another, visit one page from another, print, send and receive emails, among many other activities

The most important reason to keep your browser up-to-date is for your own safety and security, and that of your computer. ... If you're a Firefox user, your browser will remind you to update your add-ons whenever new versions become available.

Malicious hackers and Web sites can exploit security flaws in browsers and infect your computer with harmful programs, trojans, and viruses. Regular updates to your browser fix security problems when they are detected, and make your computer safer from those types of attacks.

6 0
4 years ago
All digital images are made up from varying rectangles of color, called _____________.
Dmitry_Shevchenko [17]
<span>A Pixel (a word invented from "picture element") is the basic unit of programmable color on a computer display or in a computer image.</span>
4 0
3 years ago
Read 2 more answers
Help me with this, please. Are vacuum cleaners, Cd players, and telephones considered computers? Do they store any data or proce
OleMash [197]

Answer:

search it up on google

Explanation:

5 0
4 years ago
Read 2 more answers
De que manera se beneficiaria una presentación en powtoon​
melisa1 [442]
Puedes crear videos y presentaciones
4 0
3 years ago
Adding a form into an App maybe a good idea, however, you must consider how you design it. Why do you think there is a good and
emmasim [6.3K]

Answer:

To inform you (the reader) of what to do and what not to do.

Explanation:

By starting it off by saying "..you must consider how you design it" and then following it up with an image of a good and bad form for an App, it gives a visual idea of the Do's and Don'ts, which can help you out much more than explaining it in text.

Hope this helped!

Source(s): None, except my mind. Hope this helped!

3 0
3 years ago
Read 2 more answers
Other questions:
  • Mary’s computer is running slow and will not load programs or videos. She has tried restarting her computer yet it is still not
    15·1 answer
  • What feature did the 32X add to the Sega Genesis?
    13·1 answer
  • An independent frame that holds a program, document, or folder contents in windows 7 is called a ____.
    11·1 answer
  • Convot the following biliary number into decimal form using any method ? (1010.100)2
    8·1 answer
  • Write two versions of a program that reads a sequence of positive integers from the user, calculates their geometric mean, and p
    13·1 answer
  • Nicole wants to create a database to collect information about videos in her video rental store. She would like to use the datab
    9·1 answer
  • Which of the following is a Web browser? Exit Test A Windows B Comcast High-Speed C Microsoft Internet Explorer D Modem
    7·1 answer
  • Which of the following statements about creating arrays and initializing their elements is false?
    6·1 answer
  • Which type of file can be opened directly into Excel?
    15·1 answer
  • Please explain what is Ribbon?  give examples​
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!