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

I need help with a C++ Ordered Binary Tree. Here is the problem: Write a program

ID: 3532583 • Letter: I

Question

I need help with a C++ Ordered Binary Tree. Here is the problem:

Write a program to read in a set of numbers and build an ordered binary tree. For the given tree, compute the following:

a. Print out the tree in inorder

b. Print out the tree in preorder

c. Print out the tree in postorder

d. Print out the number of nodes in the tree. (Traverse the tree and count the nodes)

e. Sum up all the values in the tree and compute the average

f. Count the number of leafs in the tree

g. Print the value that is the deepest in the tree

The numbers in the file are like this: 75 97 82 47 90 59 23 51 36 110 66 102 69 79 13 130 49 41 19 67 85 64 115 30 94

Don't use classes for this program if possible.

Explanation / Answer

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

struct btree

{

struct btree *left;

struct btree *right;

int no;

};

void postorder(struct btree *trav);

void inorder(struct btree *trav);

void preorder(struct btree *trav);

struct btree * create(struct btree *trav);

main()

{

struct btree *root=NULL;

char c;

clrscr();

while(1)

{

root=create(root);

cout<<"Do you want to continue : ";

cin>>c;

if(c=='n' ||c=='N')

break;

}

cout<<endl<<"Inoder is : ";inorder(root);

cout<<endl<<"Preorder is : ";preorder(root);

cout<<endl<<"Postorder is : ";postorder(root);

getch();

}

struct btree * create(struct btree *trav)

{

if(trav==NULL)

{

trav=new btree;

trav->right=NULL;

trav->left=NULL;

cout<<"Enter the no : ";

cin>>trav->no;

return(trav);

}

char choice;

cout<<"Enter the left or right child : ";

cin>>choice;

if(choice == 'r' || choice == 'R')

{

trav->right=create(trav->right);

}

if(choice=='l' || choice=='L')

{

trav->left=create(trav->left);

}

return(trav);

}

void inorder(struct btree *trav)

{

if(trav==NULL)

return ;

inorder(trav->left);

cout<<" "<<trav->no;

inorder(trav->right);

}

void preorder(struct btree *trav)

{

if(trav==NULL)

return;

cout<<" "<<trav->no;

preorder(trav->left);

preorder(trav->right);

}

void postorder(struct btree *trav)

{

if(trav==NULL)

return;

postorder(trav->left);

postorder(trav->right);

cout<<" "<<trav->no;

}

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