Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++program that creates a tree from given input, and prints the leaf nod

ID: 3684503 • Letter: W

Question

Write a C++program that creates a tree from given input, and prints the leaf nodesusing the level-order traversal.

The following is how we define tree nodes:

struct node

{ int item; node *l, *r;

node(int x)

{ item = x; l = 0; r = 0; }

};

typedef node* link;

The trees for this assignment have the following properties:

Each node has zero, one, or two children (left child and right child);

Each node has a unique key. That is, the item in each node is different from items in all other nodes.

Each input item is also unique.

You may find the following function useful. This function visits nodes in a tree in level order starting at the given root h using a QUEUE

(use linked list implementation for QUEUE):

void traverse(link root, int n)

{

link h;

    QUEUE q(n);

q.put(root);

while (!q.empty())

      {

        h = q.get(); // get link h at the head of the queue

if (h->l != 0) q.put(h->l);

if (h->r != 0) q.put(h->r);

      }

}

Your program will receive the following input and do the following for each input:

C : Create the root node with item r. If the tree is not empty (i.e. the root exists), return an error

L : Find the node with item p.Let u be that node. Create a new node v with item l. Make node v node u’s left child. If node u has already a left child, return an error

R : Find the node with item p. Let u be that node. Create a new node v with item r. Make node v node u’s right child. If node u has already a right child, return an error

P: Print the items in nodes in a level order traversal manner

S : Find the node with item p. Let u be that node.Print the items in nodes in the subtree rooted at u in level order traversal manner

See the example dialogue below:

C 5

Root node with item 5 has been created

C 10

Error: Tree is not empty

L 5 3

Node with item 3 has been added

R 5 4

Node with item 4 has been added

P

levelorder traversal of the entire tree: 5 3 4

S 4

Level order traversal of the asked subtree: 4

L 4 7

R 7 8

R 3 9

L 9 11

R 4 12

L 7 13

S 3

level order traversal of the asked subtree: 3 9 11

S 4

levelorder traversal of the asked subtree: 4 7 12 13 8

L 8 14

R 12 15

R 8 16

L 13 17

P

levelorder traversal of the entire tree: 5 3 4 9 7 12 11 13 8 15 17 14 16

Explanation / Answer

#include <iostream>
#include <queue>
using namespace std;

typedef struct node
{
   int item;
   node *l, *r;
}node;

typedef node* link;

link search(link root,int m)
{
   link h;
   queue<link> q;
q.push(root);
while (!q.empty())
{
    h = q.front(); // get link h at the head of the queue
    if(h->item == m) return h;
if (h->l != NULL) q.push(h->l);
if (h->r != NULL) q.push(h->r);
q.pop();
}
return NULL;
}

void print(link root)
{
   link h;
   queue<link> q;
q.push(root);
while (!q.empty())
{
    h = q.front(); // get link h at the head of the queue
if (h->l != NULL) q.push(h->l);
if (h->r != NULL) q.push(h->r);
q.pop();
cout<<h->item<<' ';
}
cout<<' ';
return;
}

void addleft(link root,int m,int n)
{
   link temp;
   temp = search(root,m);
   if(temp==NULL)
   {
       cout<<"Error: The item does not exist ";
       return;
   }
   else if(temp->l!=NULL)
   {
       cout<<"Error: The node already has a left child ";
       return;
   }
   temp-> l = (link)malloc(sizeof(node));
   temp->l->item=n;
   temp->l->l=NULL;
   temp->l->r=NULL;
   return;
}

void addright(link root,int m,int n)
{
   link temp;
   temp = search(root,m);
   if(temp==NULL)
   {
       cout<<"Error: The item does not exist ";
       return;
   }
   else if(temp->r!=NULL)
   {
       cout <<"Error: The node already has a right child ";
       return;
   }
   temp->r = (link)malloc(sizeof(node));
   temp->r->item=n;
   temp->r->l=NULL;
   temp->r->r=NULL;
   return;
}


int main() {
   // your code goes here
   link root=NULL;
   char ch;
   int m,n;
   while(1)
   {
       cin>>ch;
       if(ch=='C')
       {
           if(root==NULL)
           {
               root = (node *)malloc(sizeof(node));
               cin>>m;
               root = (link)malloc(sizeof(node));
               root->item = m;
               root->l=NULL;
               root->r=NULL;
           }
           else
           {
           cin>>m;
               cout<<"Error: Tree is not empty; ";
           }
       }
       else if(ch=='L')
       {
           if(root==NULL)
           {
               cin>>m>>n;
                   cout<<"Error: Tree is empty; ";
           }
           else
           {
           cin>>m>>n;
               addleft(root,m,n);
           }
       }
       else if(ch=='R')
       {
           if(root==NULL)
           {
               cin>>m>>n;
               cout<<"Error: Tree is empty; ";
           }
           else
           {
               cin>>m>>n;
               addright(root,m,n);
           }
       }
       else if(ch=='P')
       {
           print(root);
       }
       else if(ch=='S')
       {
           cin>>m;
           link temp = search(root,m);
           print(temp);
       }
      
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote