Você está na página 1de 6

Tutorial 8 Binary Tree

A.
Write a program to illustrates how to use the Binary Tree
concept in a program. Use the library binarySearchTree.h and
binaryTree.h to run the program.
//

Use the print node function below.

#include <iostream>
#include<stdlib.h>

//Include the binarySearchTree.h library


#include "binarySearchTree.h"

using namespace std;

void print(int& x);

int main()
{
//my info
cout<<endl;
cout<<"Name :"<<endl;
cout<<"Matric No "<<endl;
cout<<"Task: Tutorial 8A"<<endl;
cout<<"*******************"<<endl;

//Declare treeRoot to be a binary search tree object

bSearchTreeType<int> treeRoot;

//Declare variables num as an integer variables.


int num;

//Print out an instruction for user to input a numbers ending with


-999
cout<<"Input number end with -999"<<endl;
cout<<endl;

//Get a numbers from user input


cin>>num;

//Use a while loop to insert the numbers into treeRoot ending with
-999
while (num != -999)
{
treeRoot.insert(num);
cin>>num;
}

//Use the member function treeHeight of treeRoot


//to measure the tree height of treeRoot
cout<<"The tree height is:"
<<treeRoot.treeHeight()
<<endl;

//Use the member function inorderTraversal of treeRoot


//to traverse the binary search tree treeRoot
//and print the inorder Traversal.
cout << endl<< " Tree nodes in inorder: "<<endl;
treeRoot.inorderTraversal(print);
cout<<endl;
cout<<endl;

system ("PAUSE");
return 0;
}

//Print node function


void print(int& x)
{
cout << x << " ";
}

B.
Expand the program above A. to illustrate
Preorder and Postorder Traversal in a program.
#include <iostream>
#include<stdlib.h>
//Include the binarySearchTree.h library
#include "binarySearchTree.h"
using namespace std;
void print(int& x);
int main()
{
//my info
cout<<endl;
cout<<"Name : "<<endl;
cout<<"Matric No.: "<<endl;
cout<<"Task: Tutorial 8B"<<endl;
cout<<"*******************"<<endl;
//Declare treeRoot to be a binary search tree
object
bSearchTreeType<int> treeRoot;
//Declare variables num as an integer variables.
int num;
//Print out an instruction for user to input a
numbers ending with -999
cout<<"Input number end with -999"<<endl;
cout<<endl;
//Get a numbers from user input
cin>>num;
//Use a while loop to insert the numbers into
treeRoot ending with -999
while (num != -999)
{
treeRoot.insert(num);
cin>>num;
}
//Use the member function treeHeight of treeRoot
//to measure the tree height of treeRoot
cout<<"The tree height is:"

<<treeRoot.treeHeight()
<<endl;
//Use the member function preorderTraversal of
treeRoot
//to traverse the binary search tree treeRoot
//and print the preorder Traversal.
cout << endl<< "Tree nodes in preorder: "<<endl;
treeRoot.preorderTraversal(print);
cout<<endl;
cout<<endl;
//Use the member function inorderTraversal of
treeRoot
//to traverse the binary search tree treeRoot
//and print the inorder Traversal.
cout << endl<< "Tree nodes in inorder: "<<endl;
treeRoot.inorderTraversal(print);
cout<<endl;
cout<<endl;
//Use the member function postorderTraversal of
treeRoot
//to traverse the binary search tree treeRoot
//and print the postorder Traversal.
cout << endl<< "Tree nodes in postorder:
"<<endl;
treeRoot.postorderTraversal(print);
cout<<endl;
cout<<endl;

system ("PAUSE");
return 0;
}
//Print node function
void print(int& x)
{
cout << x << " ";
}

Você também pode gostar