SlideShare a Scribd company logo
// Complete the C++ program and implement the routines that
are not implemented #include <iostream> #include
<iomanip> #include <fstream> #include <string> #include
<vector> using namespace std; class TreeStruct; class Tree;
class Stack; class Queue; typedef TreeStruct* TreeStructPtr;
typedef Tree* TreePtr; class Stack { private:
vector<TreeStructPtr> s; public: void push(TreeStructPtr
ptr) {s.insert(s.begin(), ptr);} TreeStructPtr pop(){
TreeStructPtr ptr = s[0]; s.erase(s.begin()); return ptr;}
bool empty(){return (s.size()==0);} }; class Queue { private:
vector<TreeStructPtr> q; public: void push(TreeStructPtr
ptr) {q.push_back(ptr);} TreeStructPtr pop(){
TreeStructPtr ptr = q[0]; q.erase(q.begin()); return ptr;}
bool empty(){return (q.size()==0);} }; class TreeStruct {
public: int Number; TreeStructPtr Left; TreeStructPtr
Right; }; class Tree { public: TreeStructPtr TreeRoot;
Tree(); void InsertIntoTree(TreeStructPtr& Root, int num);
int FindMaxLen(TreeStructPtr Root); int
FindMinLen(TreeStructPtr Root); int
CountNodes(TreeStructPtr Root); void Copy(TreeStructPtr
Root1, TreeStructPtr& Root2); //copies one tree into another
bool Search(TreeStructPtr Root, int n); void
PrintInOrderTree(TreeStructPtr Root); void
PrintPreOrderTree(TreeStructPtr Root); void
PrintPostOrderTree(TreeStructPtr Root); void
PrintPreOrderTreeWithStack(TreeStructPtr Root); void
PrintBreadthFirstWithQueue(TreeStructPtr Root); }; // ----------
------------------------------------------------------ // tree
constructor Tree::Tree() { TreeRoot = NULL; } //----------------
------------------------------------------------ void
Copy(TreeStructPtr Root1, TreeStructPtr& Root2) //copies one
tree into another { } //----------------------------------------------
------------------ // Inserting into the tree using recursion void
Tree::InsertIntoTree(TreeStructPtr& Root, int x) { } //----------
------------------------------------------------------ int
Tree::FindMaxLen(TreeStructPtr Root) { return 0; } // ----
------------------------------------------------------------ int
Tree::FindMinLen(TreeStructPtr Root) { if (Root==NULL)
return(0); else if (Root->Right==NULL)
return(1+FindMinLen(Root->Left)); else if (Root-
>Left==NULL) return(1+FindMinLen(Root->Right)); else {
int CountLeft = 1 + FindMinLen(Root->Left); int
CountRight = 1 + FindMinLen(Root->Right); if
(CountLeft < CountRight) return(CountLeft);
else return(CountRight); } } //-------------------------
--------------------------------------- int
Tree::CountNodes(TreeStructPtr Root) { return 0; } //-----
----------------------------------------------------------- bool
Tree::Search(TreeStructPtr Root, int n) { return " "; } //-
--------------------------------------------------------------- void
Tree::PrintInOrderTree(TreeStructPtr Root) { } //-----------------
----------------------------------------------- void
Tree::PrintPreOrderTree(TreeStructPtr Root) { } // ----------------
------------------------------------------------ void
Tree::PrintPostOrderTree(TreeStructPtr Root) { } //--------------
-------------------------------------------------- void
Tree::PrintPreOrderTreeWithStack(TreeStructPtr Root) {
Stack stk; TreeStructPtr p = Root; if (p != NULL)
{ stk.push(p); while (!stk.empty())
{ p = stk.pop();
cout << p->Number << "-->"; if (p->Right !=
NULL) stk.push(p->Right);
if (p->Left != NULL) stk.push(p->Left);
} } } //------------------------------------------------------------
---- void Tree::PrintBreadthFirstWithQueue(TreeStructPtr Root)
{ } //----------------------------------------------------------------
void main() { Tree t; t.InsertIntoTree(t.TreeRoot, 15);
t.InsertIntoTree(t.TreeRoot, 20); t.InsertIntoTree(t.TreeRoot,
10); t.InsertIntoTree(t.TreeRoot, 8);
t.InsertIntoTree(t.TreeRoot, 9); t.InsertIntoTree(t.TreeRoot,
17); t.InsertIntoTree(t.TreeRoot, 21);
t.InsertIntoTree(t.TreeRoot, 22); t.InsertIntoTree(t.TreeRoot,
23); t.InsertIntoTree(t.TreeRoot, 24);
t.InsertIntoTree(t.TreeRoot, 25); t.InsertIntoTree(t.TreeRoot,
26); t.InsertIntoTree(t.TreeRoot, 11); cout << " Printing Pre
Order " << endl; t.PrintPreOrderTree(t.TreeRoot); cout << "
-----------------------------------------------" << endl; getchar();
cout << "Printing Post Order " << endl;
t.PrintPostOrderTree(t.TreeRoot); cout << " --------------------
---------------------------" << endl; getchar(); cout <<
"Printing In Order " << endl; t.PrintInOrderTree(t.TreeRoot);
cout << " -----------------------------------------------" << endl;
getchar(); cout << boolalpha << endl << endl; cout << "
Searching 30: " << t.Search(t.TreeRoot, 30) << endl; cout <<
" Searching 8: " << t.Search(t.TreeRoot, 8) << endl; cout <<
" Searching 10: " << t.Search(t.TreeRoot, 10) << endl; cout
<< "-------------------------------------------" << endl; getchar();
cout << "Printing Pre Order with Stack " << endl;
t.PrintPreOrderTreeWithStack(t.TreeRoot); cout << " ---------
--------------------------------------" << endl; getchar(); cout <<
"Printing level by level BreathFirst Traversal " << endl;
t.PrintBreadthFirstWithQueue(t.TreeRoot); cout << " ----------
-------------------------------------" << endl; getchar(); cout <<
endl; int MaxLen = t.FindMaxLen(t.TreeRoot); int MinLen =
t.FindMinLen(t.TreeRoot); int TotalNodes =
t.CountNodes(t.TreeRoot); cout << "Max is " << MaxLen <<
endl; cout << "Min is " << MinLen << endl; cout << "Num
of Nodes is " << TotalNodes << endl; cout << "-----------------
------------------------------" << endl; getchar(); Tree t2;
Copy(t.TreeRoot, t2.TreeRoot); cout << "Printing In Order the
copy of the tree" << endl; t2.PrintInOrderTree(t2.TreeRoot);
cout << " -----------------------------------------------" << endl;
} //---------------------------------------------------------------- /*
Your output should look like the foolowing Printing Pre Order
15-->10-->8-->9-->11-->20-->17-->21-->22-->23-->24-->25--
>26--> ----------------------------------------------- Printing Post
Order 9-->8-->11-->10-->17-->26-->25-->24-->23-->22-->21--
>20-->15--> ----------------------------------------------- Printing
In Order 8-->9-->10-->11-->15-->17-->20-->21-->22-->23--
>24-->25-->26--> -----------------------------------------------
Searching 30: false Searching 8: true Searching 10: true -------
------------------------------------ Printing Pre Order with Stack
15-->10-->8-->9-->11-->20-->17-->21-->22-->23-->24-->25--
>26--> ----------------------------------------------- Printing level
by level BreathFirst Traversal 15-->10-->20-->8-->11-->17--
>21-->9-->22-->23-->24-->25-->26--> -----------------------------
------------------ Max is 8 Min is 3 Num of Nodes is 13 ---------
-------------------------------------- Printing In Order the copy of
the tree 8-->9-->10-->11-->15-->17-->20-->21-->22-->23-->24-
->25-->26--> ----------------------------------------------- Press any
key to continue */
Solution
Implemented the following 4 functions
void Tree::InsertIntoTree(TreeStructPtr& Root, int x) {
if (Root == NULL) {
TreeStructPtr node = new TreeStruct();
node->Number = x;
node->Left = NULL;
node->Right = NULL;
Root = node;
} else {
if (x <= Root->Number) {
if (Root->Left != NULL)
InsertIntoTree(Root->Left, x);
else {
TreeStructPtr node = new TreeStruct();
node->Number = x;
node->Left = NULL;
node->Right = NULL;
Root->Left = node;
}
} else {
if (Root->Right != NULL)
InsertIntoTree(Root->Right, x);
else {
TreeStructPtr node = new TreeStruct();
node->Number = x;
node->Left = NULL;
node->Right = NULL;
Root->Right = node;
}
}
}
}
//----------------------------------------------------------------
void Tree::PrintInOrderTree(TreeStructPtr Root) {
if (Root != NULL) {
PrintInOrderTree(Root->Left);
cout << Root->Number << "->";
PrintInOrderTree(Root->Right);
}
}
//----------------------------------------------------------------
void Tree::PrintPreOrderTree(TreeStructPtr Root) {
if (Root != NULL) {
cout << Root->Number << "->";
PrintPreOrderTree(Root->Left);
PrintPreOrderTree(Root->Right);
}
}
//----------------------------------------------------------------
void Tree::PrintPostOrderTree(TreeStructPtr Root) {
if (Root != NULL) {
PrintPostOrderTree(Root->Left);
PrintPostOrderTree(Root->Right);
cout << Root->Number << "->";
}
}
--output with your main code for these 4 methods---------------
Printing Pre Order
15->10->8->9->11->20->17->21->22->23->24->25->26->
Printing Post Order
9->8->11->10->17->26->25->24->23->22->21->20->15->
Printing In Order
8->9->10->11->15->17->20->21->22->23->24->25->26->

More Related Content

DOCX
Complete the C++ program and implement the routines that are not .docx
DOCX
Complete the C++ program and implement the routines that are not .docx
DOCX
#include iostream using namespace std; const int nil = 0; cl.docx
PDF
1. Add a breadth-first (level-order) traversal function to the binar.pdf
PDF
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
PDF
Please write the C++ code that would display the exact same output a.pdf
PDF
include ltiostreamgt include ltfstreamgt in.pdf
PDF
Data StructuresPlease I need help completing this c++ program..pdf
Complete the C++ program and implement the routines that are not .docx
Complete the C++ program and implement the routines that are not .docx
#include iostream using namespace std; const int nil = 0; cl.docx
1. Add a breadth-first (level-order) traversal function to the binar.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
Please write the C++ code that would display the exact same output a.pdf
include ltiostreamgt include ltfstreamgt in.pdf
Data StructuresPlease I need help completing this c++ program..pdf

Similar to Complete the C++ program and implement the routines that are n.docx (20)

PDF
MAINCPP include ltiostreamgt include ltstringgt u.pdf
PDF
Write a C++ program that implements a binary search tree (BST) to man.pdf
PPT
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
PDF
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
PDF
In c++ format, for each function in the code, please using the comme.pdf
DOCX
Binary Tree in C++ coding in the data structure
PDF
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
DOCX
PDF
Add these three functions to the class binaryTreeType (provided).W.pdf
PPT
computer notes - Data Structures - 13
DOCX
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
DOCX
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
PDF
Please i need help on following program using C++ Language.Add the.pdf
DOCX
C++ adt c++ implementations
PPT
Computer notes - Binary Search Tree with Strings
PPT
computer notes - Data Structures - 15
PDF
Tree Traversals A tree traversal is the process of visiting.pdf
PDF
Below binary program is usefell to you#include iostreamtem.pdf
PDF
AnswerThe new program with the required constructor and with a te.pdf
DOCX
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
MAINCPP include ltiostreamgt include ltstringgt u.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
In c++ format, for each function in the code, please using the comme.pdf
Binary Tree in C++ coding in the data structure
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
Add these three functions to the class binaryTreeType (provided).W.pdf
computer notes - Data Structures - 13
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
Please i need help on following program using C++ Language.Add the.pdf
C++ adt c++ implementations
Computer notes - Binary Search Tree with Strings
computer notes - Data Structures - 15
Tree Traversals A tree traversal is the process of visiting.pdf
Below binary program is usefell to you#include iostreamtem.pdf
AnswerThe new program with the required constructor and with a te.pdf
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
Ad

More from ShiraPrater50 (20)

DOCX
Read Chapter 3. Answer the following questions1.Wha.docx
DOCX
Read Chapter 15 and answer the following questions 1.  De.docx
DOCX
Read Chapter 2 and answer the following questions1.  List .docx
DOCX
Read chapter 7 and write the book report  The paper should be .docx
DOCX
Read Chapter 7 and answer the following questions1.  What a.docx
DOCX
Read chapter 14, 15 and 18 of the class textbook.Saucier.docx
DOCX
Read Chapter 10 APA FORMAT1. In the last century, what historica.docx
DOCX
Read chapter 7 and write the book report  The paper should b.docx
DOCX
Read Chapter 14 and answer the following questions1.  Explain t.docx
DOCX
Read Chapter 2 first. Then come to this assignment.The first t.docx
DOCX
Journal of Public Affairs Education 515Teaching Grammar a.docx
DOCX
Learner Guide TLIR5014 Manage suppliers TLIR.docx
DOCX
Lab 5 Nessus Vulnerability Scan Report © 2012 by Jone.docx
DOCX
Leveled and Exclusionary Tracking English Learners Acce.docx
DOCX
Lab 5 Nessus Vulnerability Scan Report © 2015 by Jone.docx
DOCX
MBA 6941, Managing Project Teams 1 Course Learning Ou.docx
DOCX
Inventory Decisions in Dells Supply ChainAuthor(s) Ro.docx
DOCX
It’s Your Choice 10 – Clear Values 2nd Chain Link- Trade-offs .docx
DOCX
MBA 5101, Strategic Management and Business Policy 1 .docx
DOCX
MAJOR WORLD RELIGIONSJudaismJudaism (began .docx
Read Chapter 3. Answer the following questions1.Wha.docx
Read Chapter 15 and answer the following questions 1.  De.docx
Read Chapter 2 and answer the following questions1.  List .docx
Read chapter 7 and write the book report  The paper should be .docx
Read Chapter 7 and answer the following questions1.  What a.docx
Read chapter 14, 15 and 18 of the class textbook.Saucier.docx
Read Chapter 10 APA FORMAT1. In the last century, what historica.docx
Read chapter 7 and write the book report  The paper should b.docx
Read Chapter 14 and answer the following questions1.  Explain t.docx
Read Chapter 2 first. Then come to this assignment.The first t.docx
Journal of Public Affairs Education 515Teaching Grammar a.docx
Learner Guide TLIR5014 Manage suppliers TLIR.docx
Lab 5 Nessus Vulnerability Scan Report © 2012 by Jone.docx
Leveled and Exclusionary Tracking English Learners Acce.docx
Lab 5 Nessus Vulnerability Scan Report © 2015 by Jone.docx
MBA 6941, Managing Project Teams 1 Course Learning Ou.docx
Inventory Decisions in Dells Supply ChainAuthor(s) Ro.docx
It’s Your Choice 10 – Clear Values 2nd Chain Link- Trade-offs .docx
MBA 5101, Strategic Management and Business Policy 1 .docx
MAJOR WORLD RELIGIONSJudaismJudaism (began .docx
Ad

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Structure & Organelles in detailed.
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems
TR - Agricultural Crops Production NC III.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Module 4: Burden of Disease Tutorial Slides S2 2025
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Microbial diseases, their pathogenesis and prophylaxis
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Renaissance Architecture: A Journey from Faith to Humanism
Anesthesia in Laparoscopic Surgery in India
GDM (1) (1).pptx small presentation for students
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Structure & Organelles in detailed.
Sports Quiz easy sports quiz sports quiz
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

Complete the C++ program and implement the routines that are n.docx

  • 1. // Complete the C++ program and implement the routines that are not implemented #include <iostream> #include <iomanip> #include <fstream> #include <string> #include <vector> using namespace std; class TreeStruct; class Tree; class Stack; class Queue; typedef TreeStruct* TreeStructPtr; typedef Tree* TreePtr; class Stack { private: vector<TreeStructPtr> s; public: void push(TreeStructPtr ptr) {s.insert(s.begin(), ptr);} TreeStructPtr pop(){ TreeStructPtr ptr = s[0]; s.erase(s.begin()); return ptr;} bool empty(){return (s.size()==0);} }; class Queue { private: vector<TreeStructPtr> q; public: void push(TreeStructPtr ptr) {q.push_back(ptr);} TreeStructPtr pop(){ TreeStructPtr ptr = q[0]; q.erase(q.begin()); return ptr;} bool empty(){return (q.size()==0);} }; class TreeStruct { public: int Number; TreeStructPtr Left; TreeStructPtr Right; }; class Tree { public: TreeStructPtr TreeRoot; Tree(); void InsertIntoTree(TreeStructPtr& Root, int num); int FindMaxLen(TreeStructPtr Root); int FindMinLen(TreeStructPtr Root); int CountNodes(TreeStructPtr Root); void Copy(TreeStructPtr Root1, TreeStructPtr& Root2); //copies one tree into another bool Search(TreeStructPtr Root, int n); void PrintInOrderTree(TreeStructPtr Root); void PrintPreOrderTree(TreeStructPtr Root); void PrintPostOrderTree(TreeStructPtr Root); void PrintPreOrderTreeWithStack(TreeStructPtr Root); void PrintBreadthFirstWithQueue(TreeStructPtr Root); }; // ---------- ------------------------------------------------------ // tree constructor Tree::Tree() { TreeRoot = NULL; } //---------------- ------------------------------------------------ void Copy(TreeStructPtr Root1, TreeStructPtr& Root2) //copies one tree into another { } //---------------------------------------------- ------------------ // Inserting into the tree using recursion void Tree::InsertIntoTree(TreeStructPtr& Root, int x) { } //---------- ------------------------------------------------------ int
  • 2. Tree::FindMaxLen(TreeStructPtr Root) { return 0; } // ---- ------------------------------------------------------------ int Tree::FindMinLen(TreeStructPtr Root) { if (Root==NULL) return(0); else if (Root->Right==NULL) return(1+FindMinLen(Root->Left)); else if (Root- >Left==NULL) return(1+FindMinLen(Root->Right)); else { int CountLeft = 1 + FindMinLen(Root->Left); int CountRight = 1 + FindMinLen(Root->Right); if (CountLeft < CountRight) return(CountLeft); else return(CountRight); } } //------------------------- --------------------------------------- int Tree::CountNodes(TreeStructPtr Root) { return 0; } //----- ----------------------------------------------------------- bool Tree::Search(TreeStructPtr Root, int n) { return " "; } //- --------------------------------------------------------------- void Tree::PrintInOrderTree(TreeStructPtr Root) { } //----------------- ----------------------------------------------- void Tree::PrintPreOrderTree(TreeStructPtr Root) { } // ---------------- ------------------------------------------------ void Tree::PrintPostOrderTree(TreeStructPtr Root) { } //-------------- -------------------------------------------------- void Tree::PrintPreOrderTreeWithStack(TreeStructPtr Root) { Stack stk; TreeStructPtr p = Root; if (p != NULL) { stk.push(p); while (!stk.empty()) { p = stk.pop(); cout << p->Number << "-->"; if (p->Right != NULL) stk.push(p->Right); if (p->Left != NULL) stk.push(p->Left); } } } //------------------------------------------------------------ ---- void Tree::PrintBreadthFirstWithQueue(TreeStructPtr Root) { } //---------------------------------------------------------------- void main() { Tree t; t.InsertIntoTree(t.TreeRoot, 15); t.InsertIntoTree(t.TreeRoot, 20); t.InsertIntoTree(t.TreeRoot, 10); t.InsertIntoTree(t.TreeRoot, 8); t.InsertIntoTree(t.TreeRoot, 9); t.InsertIntoTree(t.TreeRoot, 17); t.InsertIntoTree(t.TreeRoot, 21);
  • 3. t.InsertIntoTree(t.TreeRoot, 22); t.InsertIntoTree(t.TreeRoot, 23); t.InsertIntoTree(t.TreeRoot, 24); t.InsertIntoTree(t.TreeRoot, 25); t.InsertIntoTree(t.TreeRoot, 26); t.InsertIntoTree(t.TreeRoot, 11); cout << " Printing Pre Order " << endl; t.PrintPreOrderTree(t.TreeRoot); cout << " -----------------------------------------------" << endl; getchar(); cout << "Printing Post Order " << endl; t.PrintPostOrderTree(t.TreeRoot); cout << " -------------------- ---------------------------" << endl; getchar(); cout << "Printing In Order " << endl; t.PrintInOrderTree(t.TreeRoot); cout << " -----------------------------------------------" << endl; getchar(); cout << boolalpha << endl << endl; cout << " Searching 30: " << t.Search(t.TreeRoot, 30) << endl; cout << " Searching 8: " << t.Search(t.TreeRoot, 8) << endl; cout << " Searching 10: " << t.Search(t.TreeRoot, 10) << endl; cout << "-------------------------------------------" << endl; getchar(); cout << "Printing Pre Order with Stack " << endl; t.PrintPreOrderTreeWithStack(t.TreeRoot); cout << " --------- --------------------------------------" << endl; getchar(); cout << "Printing level by level BreathFirst Traversal " << endl; t.PrintBreadthFirstWithQueue(t.TreeRoot); cout << " ---------- -------------------------------------" << endl; getchar(); cout << endl; int MaxLen = t.FindMaxLen(t.TreeRoot); int MinLen = t.FindMinLen(t.TreeRoot); int TotalNodes = t.CountNodes(t.TreeRoot); cout << "Max is " << MaxLen << endl; cout << "Min is " << MinLen << endl; cout << "Num of Nodes is " << TotalNodes << endl; cout << "----------------- ------------------------------" << endl; getchar(); Tree t2; Copy(t.TreeRoot, t2.TreeRoot); cout << "Printing In Order the copy of the tree" << endl; t2.PrintInOrderTree(t2.TreeRoot); cout << " -----------------------------------------------" << endl; } //---------------------------------------------------------------- /* Your output should look like the foolowing Printing Pre Order 15-->10-->8-->9-->11-->20-->17-->21-->22-->23-->24-->25-- >26--> ----------------------------------------------- Printing Post Order 9-->8-->11-->10-->17-->26-->25-->24-->23-->22-->21--
  • 4. >20-->15--> ----------------------------------------------- Printing In Order 8-->9-->10-->11-->15-->17-->20-->21-->22-->23-- >24-->25-->26--> ----------------------------------------------- Searching 30: false Searching 8: true Searching 10: true ------- ------------------------------------ Printing Pre Order with Stack 15-->10-->8-->9-->11-->20-->17-->21-->22-->23-->24-->25-- >26--> ----------------------------------------------- Printing level by level BreathFirst Traversal 15-->10-->20-->8-->11-->17-- >21-->9-->22-->23-->24-->25-->26--> ----------------------------- ------------------ Max is 8 Min is 3 Num of Nodes is 13 --------- -------------------------------------- Printing In Order the copy of the tree 8-->9-->10-->11-->15-->17-->20-->21-->22-->23-->24- ->25-->26--> ----------------------------------------------- Press any key to continue */ Solution Implemented the following 4 functions void Tree::InsertIntoTree(TreeStructPtr& Root, int x) { if (Root == NULL) { TreeStructPtr node = new TreeStruct(); node->Number = x; node->Left = NULL; node->Right = NULL; Root = node; } else {
  • 5. if (x <= Root->Number) { if (Root->Left != NULL) InsertIntoTree(Root->Left, x); else { TreeStructPtr node = new TreeStruct(); node->Number = x; node->Left = NULL; node->Right = NULL; Root->Left = node; } } else { if (Root->Right != NULL) InsertIntoTree(Root->Right, x); else { TreeStructPtr node = new TreeStruct(); node->Number = x; node->Left = NULL; node->Right = NULL; Root->Right = node; } } } } //---------------------------------------------------------------- void Tree::PrintInOrderTree(TreeStructPtr Root) {
  • 6. if (Root != NULL) { PrintInOrderTree(Root->Left); cout << Root->Number << "->"; PrintInOrderTree(Root->Right); } } //---------------------------------------------------------------- void Tree::PrintPreOrderTree(TreeStructPtr Root) { if (Root != NULL) { cout << Root->Number << "->"; PrintPreOrderTree(Root->Left); PrintPreOrderTree(Root->Right); } } //---------------------------------------------------------------- void Tree::PrintPostOrderTree(TreeStructPtr Root) { if (Root != NULL) { PrintPostOrderTree(Root->Left); PrintPostOrderTree(Root->Right); cout << Root->Number << "->"; } } --output with your main code for these 4 methods---------------
  • 7. Printing Pre Order 15->10->8->9->11->20->17->21->22->23->24->25->26-> Printing Post Order 9->8->11->10->17->26->25->24->23->22->21->20->15-> Printing In Order 8->9->10->11->15->17->20->21->22->23->24->25->26->