SlideShare a Scribd company logo
1. Add a breadth-first (level-order) traversal function to the binary tree code.
2. Add a function to find the height of a tree.
3. Re-implement one of the depth-first traversal methods using a stack instead of recursion.
4. Add a link to each nodes parent node.
#include
#include
#include
using namespace std;
template < typename T >
class TreeNode
{
public:
T element; //
TreeNode < T > * left; //
TreeNode < T > * right; //
TreeNode * next;
TreeNode() //
{
left = NULL;
next = NULL;
}
TreeNode(T element) // Constructor
{
this->element = element;
left = NULL;
right = NULL;
}
};
template < typename T >
class BinaryTree
{
public:
BinaryTree();
BinaryTree(T elements[], int arraySize);
bool insert(T element);
void inorder();
void preorder();
void postorder();
int getSize();
bool search(T element);
void breadthFirstTraversal();
int depth();
private:
TreeNode < T > * root;
int size;
void inorder(TreeNode < T > * root);
void postorder(TreeNode < T > * root);
void preorder(TreeNode < T > * root);
bool search(T element, TreeNode < T > * root);
int depth(TreeNode * root);
};
template < typename T >
BinaryTree < T >::BinaryTree()
{
root = NULL;
size = 0;
}
template < typename T >
BinaryTree < T >::BinaryTree(T elements[], int arraySize)
{
root = NULL;
size = 0;
for (int i = 0; i < arraySize; i++)
{
insert(elements[i]);
}
}
template < typename T >
bool BinaryTree < T >::insert(T element)
{
if (root == NULL)
root = new TreeNode < T > (element); // Create a new root
else
{
// Locate the parent node
TreeNode < T > * parent = NULL;
TreeNode < T > * current = root;
while (current != NULL)
if (element < current->element)
{
parent = current;
current = current->left;
}
else if (element > current->element)
{
parent = current;
current = current->right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent->element)
parent->left = new TreeNode < T > (element);
else
parent->right = new TreeNode < T > (element);
}
size++;
return true; // Element inserted
}
/* Inorder traversal */
template < typename T >
void BinaryTree < T >::inorder()
{
inorder(root);
}
/* Inorder traversal from a subtree */
template < typename T >
void BinaryTree < T >::inorder(TreeNode < T > * root)
{
if (root == NULL) return;
inorder(root->left);
cout << root->element << " ";
inorder(root->right);
}
/* Postorder traversal */
template < typename T >
void BinaryTree < T >::postorder()
{
postorder(root);
}
/** Inorder traversal from a subtree */
template < typename T >
void BinaryTree < T >::postorder(TreeNode < T > * root)
{
if (root == NULL) return;
postorder(root->left);
postorder(root->right);
cout << root->element << " ";
}
/* */
template < typename T >
void BinaryTree < T >::preorder()
{
preorder(root);
}
/* */
template < typename T >
void BinaryTree < T >::preorder(TreeNode < T > * root)
{
if (root == NULL) return;
cout << root->element << " ";
preorder(root->left);
preorder(root->right);
}
/**/
template < typename T >
int BinaryTree < T >::getSize()
{
return size;
}
template < typename T >
bool BinaryTree < T >::search(T element)
{
return search(element, root);
}
template < typename T >
bool BinaryTree < T >::search(T element, TreeNode < T > * root)
{
if (root == NULL)
return false;
else if (root->element == element)
return true;
else if (root->element > element)
return search(element, root->right);
else
return search(element, root->left);
}
int main()
{
BinaryTree < string > tree1;
tree1.insert("George");
tree1.insert("Michael");
tree1.insert("Tom");
tree1.insert("Adam");
tree1.insert("Jones");
tree1.insert("Peter");
tree1.insert("Daniel");
cout << "Inorder (sorted): ";
tree1.inorder();
cout << " Postorder: ";
tree1.postorder();
cout << " Preorder: ";
tree1.preorder();
cout << " The number of nodes is " << tree1.getSize();
int numbers[] =
{
2, 4, 3, 1, 8, 5, 6, 7
};
BinaryTree < int > tree2(numbers, 8);
cout << " Inorder (sorted): ";
tree2.inorder();
cout << " search 2 " << tree2.search(2) << endl;
cout << " search 99 " << tree2.search(99) << endl;
cout << " search 8 " << tree2.search(8) << endl;
return 0;
}
Solution
#include
#include
#include
using namespace std;
template < typename T >
class TreeNode
{
public:
T element; //
TreeNode < T > * left; //
TreeNode < T > * right; //
TreeNode * next;
TreeNode() //
{
left = NULL;
next = NULL;
}
TreeNode(T element) // Constructor
{
this->element = element;
left = NULL;
right = NULL;
}
};
template < typename T >
class BinaryTree
{
public:
BinaryTree();
BinaryTree(T elements[], int arraySize);
bool insert(T element);
void inorder();
void preorder();
void postorder();
int getSize();
bool search(T element);
void breadthFirstTraversal();
int depth();
void BFS();
private:
TreeNode < T > * root;
int size;
void inorder(TreeNode < T > * root);
void postorder(TreeNode < T > * root);
void preorder(TreeNode < T > * root);
bool search(T element, TreeNode < T > * root);
int depth(TreeNode * root);
void printLevelOrder(TreeNode < T > * root);
void printGivenLevel(TreeNode < T > * root, int level);
};
template < typename T >
int BinaryTree < T >::depth()
{
depth(root);
}
template < typename T >
void BinaryTree < T >::BFS()
{
printLevelOrder(root);
}
template < typename T >
void BinaryTree < T >::printLevelOrder(TreeNode < T > * root)
{
int h = depth(root);
int i;
for (i=1; i<=h; i++)
printGivenLevel(root, i);
}
/* Print nodes at a given level */
template < typename T >
void BinaryTree < T >::printGivenLevel(TreeNode < T > * root, int level)
{
if (root == NULL)
return;
if (level == 1)
cout<<" "<element;
else if (level > 1)
{
printGivenLevel(root->left, level-1);
printGivenLevel(root->right, level-1);
}
}
template < typename T >
int BinaryTree < T >::depth(TreeNode < T > * element)
{
if (element==NULL)
return 0;
else
{
int lDepth = depth(element->left);
int rDepth = depth(element->right);
// use the larger one
if (lDepth > rDepth)
return(lDepth+1);
else return(rDepth+1);
}
}
template < typename T >
BinaryTree < T >::BinaryTree()
{
root = NULL;
size = 0;
}
template < typename T >
BinaryTree < T >::BinaryTree(T elements[], int arraySize)
{
root = NULL;
size = 0;
for (int i = 0; i < arraySize; i++)
{
insert(elements[i]);
}
}
template < typename T >
bool BinaryTree < T >::insert(T element)
{
if (root == NULL)
root = new TreeNode < T > (element); // Create a new root
else
{
// Locate the parent node
TreeNode < T > * parent = NULL;
TreeNode < T > * current = root;
while (current != NULL)
if (element < current->element)
{
parent = current;
current = current->left;
}
else if (element > current->element)
{
parent = current;
current = current->right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent->element)
parent->left = new TreeNode < T > (element);
else
parent->right = new TreeNode < T > (element);
}
size++;
return true; // Element inserted
}
/* Inorder traversal */
template < typename T >
void BinaryTree < T >::inorder()
{
inorder(root);
}
/* Inorder traversal from a subtree */
template < typename T >
void BinaryTree < T >::inorder(TreeNode < T > * root)
{
if (root == NULL) return;
inorder(root->left);
cout << root->element << " ";
inorder(root->right);
}
/* Postorder traversal */
template < typename T >
void BinaryTree < T >::postorder()
{
postorder(root);
}
/** Inorder traversal from a subtree */
template < typename T >
void BinaryTree < T >::postorder(TreeNode < T > * root)
{
if (root == NULL) return;
postorder(root->left);
postorder(root->right);
cout << root->element << " ";
}
/* */
template < typename T >
void BinaryTree < T >::preorder()
{
preorder(root);
}
/* */
template < typename T >
void BinaryTree < T >::preorder(TreeNode < T > * root)
{
if (root == NULL) return;
cout << root->element << " ";
preorder(root->left);
preorder(root->right);
}
/**/
template < typename T >
int BinaryTree < T >::getSize()
{
return size;
}
template < typename T >
bool BinaryTree < T >::search(T element)
{
return search(element, root);
}
template < typename T >
bool BinaryTree < T >::search(T element, TreeNode < T > * root)
{
if (root == NULL)
return false;
else if (root->element == element)
return true;
else if (root->element > element)
return search(element, root->right);
else
return search(element, root->left);
}
int main()
{
BinaryTree < string > tree1;
tree1.insert("George");
tree1.insert("Michael");
tree1.insert("Tom");
tree1.insert("Adam");
tree1.insert("Jones");
tree1.insert("Peter");
tree1.insert("Daniel");
cout << "Inorder (sorted): ";
tree1.inorder();
cout << " Postorder: ";
tree1.postorder();
cout << " Preorder: ";
tree1.preorder();
cout << " The number of nodes is " << tree1.getSize();
int numbers[] =
{
2, 4, 3, 1, 8, 5, 6, 7
};
BinaryTree < int > tree2(numbers, 8);
cout << " Inorder (sorted): ";
tree2.inorder();
cout << " search 2 " << tree2.search(2) << endl;
cout << " search 99 " << tree2.search(99) << endl;
cout << " search 8 " << tree2.search(8) << endl;
cout<<"Height is "<

More Related Content

PDF
MAINCPP include ltiostreamgt include ltstringgt u.pdf
PDF
Add these three functions to the class binaryTreeType (provided).W.pdf
PDF
Please i need help on following program using C++ Language.Add the.pdf
PDF
Tree Traversals A tree traversal is the process of visiting.pdf
DOCX
#include iostream using namespace std; const int nil = 0; cl.docx
PDF
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
DOCX
Binary Tree in C++ coding in the data structure
DOCX
Complete the C++ program and implement the routines that are not .docx
MAINCPP include ltiostreamgt include ltstringgt u.pdf
Add these three functions to the class binaryTreeType (provided).W.pdf
Please i need help on following program using C++ Language.Add the.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
#include iostream using namespace std; const int nil = 0; cl.docx
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
Binary Tree in C++ coding in the data structure
Complete the C++ program and implement the routines that are not .docx

Similar to 1. Add a breadth-first (level-order) traversal function to the binar.pdf (20)

DOCX
Complete the C++ program and implement the routines that are not .docx
PDF
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
PDF
include ltiostreamgt include ltfstreamgt in.pdf
PPT
computer notes - Data Structures - 13
PDF
Write a function in C++ to generate an N-node random binary search t.pdf
DOCX
Complete the C++ program and implement the routines that are n.docx
DOCX
Complete the C++ program and implement the routines that are n.docx
DOCX
Complete the C++ program and implement the routines that are n.docx
DOCX
Complete the C++ program and implement the routines that are n.docx
DOCX
Complete the C++ program and implement the routines that are n.docx
DOCX
Complete the C++ program and implement the routines that are n.docx
DOCX
Complete the C++ program and implement the routines that are n.docx
DOCX
Complete the C++ program and implement the routines that are n.docx
DOCX
Complete the C++ program and implement the routines that are n.docx
PDF
Modify this code to do an Insert function for an AVL tree, instead o.pdf
PDF
Binary Tree - Algorithms
PDF
create a binary search tree from an empty one by adding the key valu.pdf
PPT
Algorithm and Data Structure - Binary Trees
PDF
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
PDF
Please write the C++ code that would display the exact same output a.pdf
Complete the C++ program and implement the routines that are not .docx
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
include ltiostreamgt include ltfstreamgt in.pdf
computer notes - Data Structures - 13
Write a function in C++ to generate an N-node random binary search t.pdf
Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
Modify this code to do an Insert function for an AVL tree, instead o.pdf
Binary Tree - Algorithms
create a binary search tree from an empty one by adding the key valu.pdf
Algorithm and Data Structure - Binary Trees
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
Please write the C++ code that would display the exact same output a.pdf
Ad

More from arjuncp10 (20)

PDF
I l Show that if X has the discrete topology, then its only conn.pdf
PDF
In 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdf
PDF
How would a CFE devise a plan to prevent subsequent employee fraud.pdf
PDF
Hilary rode her horse for 8 miles until it was hurt.Then she walked .pdf
PDF
fully comments for my program, thank you will thumb up#include io.pdf
PDF
Find the admittance Yab in the circuit seen in the figure. Take that.pdf
PDF
Diversity Paper Each student will complete a diversity research assig.pdf
PDF
Detailed solutions please 1. Let R and S be commutative rings and le.pdf
PDF
Describe the mechanisms of asexual reproduction inProkaryotesPr.pdf
PDF
Define intermediate phenotype and then imagine some intermediate phe.pdf
PDF
Consider a population of lizards living on the coast of Africa. A sto.pdf
PDF
Can “discovery science” (for example, the discovery of a new species.pdf
PDF
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
PDF
All of the following are features or functions of nanobodies except _.pdf
PDF
A girl running at a constant speed of 1.4ms in a straight line thro.pdf
PDF
You have a rural dial-up customer who complains that a large number .pdf
PDF
You are given a mixed culture containing a anaerobic thermophile, ae.pdf
PDF
Woyld removing phenylethyl alcohol from PEA alter the mediumsS.pdf
PDF
Which abstraction uses slates, state transitions, inputs and outputs .pdf
PDF
What is the target cell for the hormone AngiotensionogenSolut.pdf
I l Show that if X has the discrete topology, then its only conn.pdf
In 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdf
How would a CFE devise a plan to prevent subsequent employee fraud.pdf
Hilary rode her horse for 8 miles until it was hurt.Then she walked .pdf
fully comments for my program, thank you will thumb up#include io.pdf
Find the admittance Yab in the circuit seen in the figure. Take that.pdf
Diversity Paper Each student will complete a diversity research assig.pdf
Detailed solutions please 1. Let R and S be commutative rings and le.pdf
Describe the mechanisms of asexual reproduction inProkaryotesPr.pdf
Define intermediate phenotype and then imagine some intermediate phe.pdf
Consider a population of lizards living on the coast of Africa. A sto.pdf
Can “discovery science” (for example, the discovery of a new species.pdf
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
All of the following are features or functions of nanobodies except _.pdf
A girl running at a constant speed of 1.4ms in a straight line thro.pdf
You have a rural dial-up customer who complains that a large number .pdf
You are given a mixed culture containing a anaerobic thermophile, ae.pdf
Woyld removing phenylethyl alcohol from PEA alter the mediumsS.pdf
Which abstraction uses slates, state transitions, inputs and outputs .pdf
What is the target cell for the hormone AngiotensionogenSolut.pdf
Ad

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Complications of Minimal Access Surgery at WLH
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
01-Introduction-to-Information-Management.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Anesthesia in Laparoscopic Surgery in India
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Basic Mud Logging Guide for educational purpose
FourierSeries-QuestionsWithAnswers(Part-A).pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Renaissance Architecture: A Journey from Faith to Humanism
Microbial disease of the cardiovascular and lymphatic systems
Complications of Minimal Access Surgery at WLH
O5-L3 Freight Transport Ops (International) V1.pdf
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Insiders guide to clinical Medicine.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

1. Add a breadth-first (level-order) traversal function to the binar.pdf

  • 1. 1. Add a breadth-first (level-order) traversal function to the binary tree code. 2. Add a function to find the height of a tree. 3. Re-implement one of the depth-first traversal methods using a stack instead of recursion. 4. Add a link to each nodes parent node. #include #include #include using namespace std; template < typename T > class TreeNode { public: T element; // TreeNode < T > * left; // TreeNode < T > * right; // TreeNode * next; TreeNode() // { left = NULL; next = NULL; } TreeNode(T element) // Constructor { this->element = element; left = NULL; right = NULL; } }; template < typename T > class BinaryTree { public: BinaryTree(); BinaryTree(T elements[], int arraySize);
  • 2. bool insert(T element); void inorder(); void preorder(); void postorder(); int getSize(); bool search(T element); void breadthFirstTraversal(); int depth(); private: TreeNode < T > * root; int size; void inorder(TreeNode < T > * root); void postorder(TreeNode < T > * root); void preorder(TreeNode < T > * root); bool search(T element, TreeNode < T > * root); int depth(TreeNode * root); }; template < typename T > BinaryTree < T >::BinaryTree() { root = NULL; size = 0; } template < typename T > BinaryTree < T >::BinaryTree(T elements[], int arraySize) { root = NULL; size = 0; for (int i = 0; i < arraySize; i++) { insert(elements[i]); } } template < typename T > bool BinaryTree < T >::insert(T element) {
  • 3. if (root == NULL) root = new TreeNode < T > (element); // Create a new root else { // Locate the parent node TreeNode < T > * parent = NULL; TreeNode < T > * current = root; while (current != NULL) if (element < current->element) { parent = current; current = current->left; } else if (element > current->element) { parent = current; current = current->right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent->element) parent->left = new TreeNode < T > (element); else parent->right = new TreeNode < T > (element); } size++; return true; // Element inserted } /* Inorder traversal */ template < typename T > void BinaryTree < T >::inorder() { inorder(root); } /* Inorder traversal from a subtree */
  • 4. template < typename T > void BinaryTree < T >::inorder(TreeNode < T > * root) { if (root == NULL) return; inorder(root->left); cout << root->element << " "; inorder(root->right); } /* Postorder traversal */ template < typename T > void BinaryTree < T >::postorder() { postorder(root); } /** Inorder traversal from a subtree */ template < typename T > void BinaryTree < T >::postorder(TreeNode < T > * root) { if (root == NULL) return; postorder(root->left); postorder(root->right); cout << root->element << " "; } /* */ template < typename T > void BinaryTree < T >::preorder() { preorder(root); } /* */ template < typename T > void BinaryTree < T >::preorder(TreeNode < T > * root) { if (root == NULL) return; cout << root->element << " "; preorder(root->left);
  • 5. preorder(root->right); } /**/ template < typename T > int BinaryTree < T >::getSize() { return size; } template < typename T > bool BinaryTree < T >::search(T element) { return search(element, root); } template < typename T > bool BinaryTree < T >::search(T element, TreeNode < T > * root) { if (root == NULL) return false; else if (root->element == element) return true; else if (root->element > element) return search(element, root->right); else return search(element, root->left); } int main() { BinaryTree < string > tree1; tree1.insert("George"); tree1.insert("Michael"); tree1.insert("Tom"); tree1.insert("Adam"); tree1.insert("Jones"); tree1.insert("Peter"); tree1.insert("Daniel");
  • 6. cout << "Inorder (sorted): "; tree1.inorder(); cout << " Postorder: "; tree1.postorder(); cout << " Preorder: "; tree1.preorder(); cout << " The number of nodes is " << tree1.getSize(); int numbers[] = { 2, 4, 3, 1, 8, 5, 6, 7 }; BinaryTree < int > tree2(numbers, 8); cout << " Inorder (sorted): "; tree2.inorder(); cout << " search 2 " << tree2.search(2) << endl; cout << " search 99 " << tree2.search(99) << endl; cout << " search 8 " << tree2.search(8) << endl; return 0; } Solution #include #include #include using namespace std; template < typename T > class TreeNode { public: T element; // TreeNode < T > * left; // TreeNode < T > * right; // TreeNode * next; TreeNode() // {
  • 7. left = NULL; next = NULL; } TreeNode(T element) // Constructor { this->element = element; left = NULL; right = NULL; } }; template < typename T > class BinaryTree { public: BinaryTree(); BinaryTree(T elements[], int arraySize); bool insert(T element); void inorder(); void preorder(); void postorder(); int getSize(); bool search(T element); void breadthFirstTraversal(); int depth(); void BFS(); private: TreeNode < T > * root; int size; void inorder(TreeNode < T > * root); void postorder(TreeNode < T > * root); void preorder(TreeNode < T > * root); bool search(T element, TreeNode < T > * root); int depth(TreeNode * root); void printLevelOrder(TreeNode < T > * root); void printGivenLevel(TreeNode < T > * root, int level);
  • 8. }; template < typename T > int BinaryTree < T >::depth() { depth(root); } template < typename T > void BinaryTree < T >::BFS() { printLevelOrder(root); } template < typename T > void BinaryTree < T >::printLevelOrder(TreeNode < T > * root) { int h = depth(root); int i; for (i=1; i<=h; i++) printGivenLevel(root, i); } /* Print nodes at a given level */ template < typename T > void BinaryTree < T >::printGivenLevel(TreeNode < T > * root, int level) { if (root == NULL) return; if (level == 1) cout<<" "<element; else if (level > 1) { printGivenLevel(root->left, level-1); printGivenLevel(root->right, level-1); } } template < typename T > int BinaryTree < T >::depth(TreeNode < T > * element)
  • 9. { if (element==NULL) return 0; else { int lDepth = depth(element->left); int rDepth = depth(element->right); // use the larger one if (lDepth > rDepth) return(lDepth+1); else return(rDepth+1); } } template < typename T > BinaryTree < T >::BinaryTree() { root = NULL; size = 0; } template < typename T > BinaryTree < T >::BinaryTree(T elements[], int arraySize) { root = NULL; size = 0; for (int i = 0; i < arraySize; i++) { insert(elements[i]); } } template < typename T > bool BinaryTree < T >::insert(T element) { if (root == NULL)
  • 10. root = new TreeNode < T > (element); // Create a new root else { // Locate the parent node TreeNode < T > * parent = NULL; TreeNode < T > * current = root; while (current != NULL) if (element < current->element) { parent = current; current = current->left; } else if (element > current->element) { parent = current; current = current->right; } else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent->element) parent->left = new TreeNode < T > (element); else parent->right = new TreeNode < T > (element); } size++; return true; // Element inserted } /* Inorder traversal */ template < typename T > void BinaryTree < T >::inorder() { inorder(root); } /* Inorder traversal from a subtree */ template < typename T >
  • 11. void BinaryTree < T >::inorder(TreeNode < T > * root) { if (root == NULL) return; inorder(root->left); cout << root->element << " "; inorder(root->right); } /* Postorder traversal */ template < typename T > void BinaryTree < T >::postorder() { postorder(root); } /** Inorder traversal from a subtree */ template < typename T > void BinaryTree < T >::postorder(TreeNode < T > * root) { if (root == NULL) return; postorder(root->left); postorder(root->right); cout << root->element << " "; } /* */ template < typename T > void BinaryTree < T >::preorder() { preorder(root); } /* */ template < typename T > void BinaryTree < T >::preorder(TreeNode < T > * root) { if (root == NULL) return; cout << root->element << " "; preorder(root->left); preorder(root->right);
  • 12. } /**/ template < typename T > int BinaryTree < T >::getSize() { return size; } template < typename T > bool BinaryTree < T >::search(T element) { return search(element, root); } template < typename T > bool BinaryTree < T >::search(T element, TreeNode < T > * root) { if (root == NULL) return false; else if (root->element == element) return true; else if (root->element > element) return search(element, root->right); else return search(element, root->left); } int main() { BinaryTree < string > tree1; tree1.insert("George"); tree1.insert("Michael"); tree1.insert("Tom"); tree1.insert("Adam"); tree1.insert("Jones"); tree1.insert("Peter"); tree1.insert("Daniel"); cout << "Inorder (sorted): "; tree1.inorder();
  • 13. cout << " Postorder: "; tree1.postorder(); cout << " Preorder: "; tree1.preorder(); cout << " The number of nodes is " << tree1.getSize(); int numbers[] = { 2, 4, 3, 1, 8, 5, 6, 7 }; BinaryTree < int > tree2(numbers, 8); cout << " Inorder (sorted): "; tree2.inorder(); cout << " search 2 " << tree2.search(2) << endl; cout << " search 99 " << tree2.search(99) << endl; cout << " search 8 " << tree2.search(8) << endl; cout<<"Height is "<