Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Binary tree program

$
0
0
Using the tree program, write a program to input twenty integers into a tree.
The program should print out the inorder, preorder and postorder traversals for the tree indicating the LEVEL of each node as it is printed out.
In addition to that there should be statements indicating :

The number of leaves in the tree
The number of nodes with only one child
The number of nodes with two children
The maximum and minimum leaf levels
The height of the tree
Also indicate if the tree is balanced

For Example…if you entered the values
45
3
66
7
8
99
23
11
44
55
6
2
88
9
0
32
14
103
92

The tree program I gave you would print out that the root is 45, that 3 is the left of 45,
66 is the right of 45, etc etc…

It would then do the inorder traversal:
0 (level 3)
2 (level2)
3 (level1)
6 (level 3)
etc

Then you would say (not necessarily correct) something like:
There are 8 leaf nodes
There are 4 nodes with one child
There are 7 nodes with two children
Maximum leaf level = 6
Maximum leaf level = 3
not balanced...height = 6


//Mike Samela
//CS 112

#include <iostream>
using namespace std;

const int nil = 0;
class treenode_type		    
{
public:						
		int info;
		treenode_type *left;
		treenode_type *right;
};

void setleft(int x);
void setright(int x);
void inorder(treenode_type *p);
void preorder(treenode_type *p);
void postorder(treenode_type *p);
treenode_type *p,*q,*root;
int number;
void main()
{
    cout << "Enter first value: \n";
	cin >> number;
	cout << number << "\n";
	root = new treenode_type;
	(*root).info = number;
	(*root).left = nil;
	(*root).right = nil;
	cout <<"Enter other numbers terminated by -999\n";
	cin >> number;
	while (number != -999)
	{
		p = root;
		q = p;
		while ((number != (*p).info) && (q!=nil))
		{
			p = q;
			if (number < (*p).info)
				q = (*p).left;
			else
				q = (*p).right;
		}
		if (number == (*p).info)
			cout << number << " is a duplicate \n";
		else if (number < (*p).info)    
		{
			setleft(number);
			cout << number <<" is a left child of "<< (*p).info << "\n";
		}
		else   
		{
			setright(number);
			cout << number <<" is a right child of "<< (*p).info << "\n";
		}
		cin >> number;
	}
	cout << "The tree traversed INORDER is \n";
	p = root;
	inorder(p);
	cout << "The tree traversed PREORDER is \n";
	p = root;
	preorder(p);
	cout << "The tree traversed POSTORDER is \n";
	p = root;
	postorder(p);
}

void setleft(int x) 
{
	treenode_type *q;
	q = new treenode_type;
	(*q).info = x;
	(*q).left = nil;
	(*q).right = nil;
	(*p).left = q;
}
void setright(int x)
{
	treenode_type *q;
	q = new treenode_type;
	(*q).info = x;
	(*q).left = nil;
	(*q).right = nil;             
	(*p).right = q;
}
void inorder(treenode_type *r)
{
  if (r != nil)
	{
	inorder((*r).left);
	cout << (*r).info << "\n";
	inorder((*r).right);
	}
}
void preorder(treenode_type *r)
{
  if (r != nil)
	{
	cout << (*r).info << "\n";
	preorder((*r).left);	
	preorder((*r).right);
	}
}
void postorder(treenode_type *r)
{
  if (r != nil)
	{
	postorder((*r).left);	
	postorder((*r).right);
	cout << (*r).info << "\n";
	}
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>