The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 398 Weeks Ago, 2 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
This example shows how to pass a user-defined function as a parameter to the binary tree traversal algorithms. For illustration purposes, we show how to use only the inorder traversal function. The following program uses the class bSearchTreeType, which is derived from the class binaryTreeType, to build the binary tree. The traversal functions are included in the class binaryTreeType, which are then inherited by the class bSearchTreeType.
#include <iostream>
#include "binarySearchTree.h"
using namespace std;
void print(int& x);
void update(int& x);
int main()
{
bSearchTreeType<int> treeRoot; //Line 1
int num; //Line 2
cout << "Line 3: Enter numbers ending "
<< "with -999." << endl; //Line 3
cin >> num; //Line 4
while (num != -999) //Line 5
{
treeRoot.insert(num); //Line 6
cin >> num; //Line 7
}
cout << endl
<< "Line 8: Tree nodes in inorder: "; //Line 8
treeRoot.inorderTraversal(print); //Line 9
cout << endl << "Line 10: Tree Height: "
<< treeRoot.treeHeight()
<< endl << endl; //Line 10
cout << "Line 11: ******* Update Nodes "
<< "*******" << endl; //Line 11
treeRoot.inorderTraversal(update); //Line 12
cout << "Line 13: Tree nodes in inorder "
<< "after the update: " << endl
<< " "; //Line 13
treeRoot.inorderTraversal(print); //Line 14
cout << endl << "Line 15: Tree Height: "
<< treeRoot.treeHeight() << endl; //Line 15
return 0; //Line 16
}
void print(int& x) //Line 17
{
cout << x << " "; //Line 18
}
void update(int& x) //Line 19
{
x = 2 * x; //Line 20
Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- -----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------sol-----------uti-----------on.-----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- ca-----------tch-----------