0% found this document useful (0 votes)
155 views

Tutorial 8 - Binary Tree

This document discusses a program to illustrate binary tree concepts and traversal methods. Part A shows how to create a binary search tree using a library, insert nodes, find the height, and perform an inorder traversal. Part B expands on this by adding preorder and postorder traversals. The program takes user input of numbers, inserts them into a binary search tree, then outputs the height and traversals of the tree.

Uploaded by

Syahirah Adibah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Tutorial 8 - Binary Tree

This document discusses a program to illustrate binary tree concepts and traversal methods. Part A shows how to create a binary search tree using a library, insert nodes, find the height, and perform an inorder traversal. Part B expands on this by adding preorder and postorder traversals. The program takes user input of numbers, inserts them into a binary search tree, then outputs the height and traversals of the tree.

Uploaded by

Syahirah Adibah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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 << " ";
}

You might also like