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.be gin(),
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 { implement this } //---------------------------
------------------------------------- // Inserting into the tree using
recursion void Tree::InsertIntoTree(TreeStructPtr& Root, int x)
{ implement this } //------------------------------------------------
---------------- int Tree::FindMaxLen(TreeStructPtr Root) {
implement this } //----------------------------------------------------
------------ 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) { implement this
} //---------------------------------------------------------------- bool
Tree::Search(TreeStructPtr Root, int n) { implement this
} //---------------------------------------------------------------- void
Tree::PrintInOrderTree(TreeStructPtr Root) { implement this }
//---------------------------------------------------------------- void
Tree::PrintPreOrderTree(TreeStructPtr Root) { implement this
} //---------------------------------------------------------------- void
Tree::PrintPostOrderTree(TreeStructPtr Root) { implement this
} //---------------------------------------------------------------- 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)
{ implement this } //-------------------------------------------------
--------------- 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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
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
{
if (Root1 == NULL) //base case to end recursion when at tree
end
Root2 == NULL;
else{
Root2->Number = Root1->Number;
// Just call recursively to copy the subtrees:
Copy(Root1->Left, Root2->Left);
Copy(Root1->Right, Root2->Right);
}
}
//----------------------------------------------------------------
// Inserting into the tree using recursion
void Tree::InsertIntoTree(TreeStructPtr& Root, int x)
{
TreeStructPtr p = Root, prev=Root;
while (p != NULL)
{ prev = p;
if (x < p->Number)
p = p->Left;
else if (x > p->Number)
p = p->Right;
else
{ cout << " !!!! " << x << " is already in the tree " << endl;
return;
}
}
TreeStructPtr NewNode;
NewNode = new (TreeStruct);
NewNode->Number = x;
NewNode->Left = NULL;
NewNode->Right = NULL;
if (Root == NULL)
Root = NewNode;
else if (prev->Number < x)
prev->Right = NewNode;
else
prev->Left = NewNode;
return;
}
//----------------------------------------------------------------
int Tree::FindMaxLen(TreeStructPtr Root)
{
if (Root == NULL)
return(0);
else if (Root->Right == NULL)
return(1+FindMaxLen(Root->Left));
else if (Root->Left == NULL)
return(1+FindMaxLen(Root->Right));
else
{
int CountLeft = 1 + FindMaxLen(Root->Left);
int CountRight = 1 + FindMaxLen(Root->Right);
if (CountLeft > CountRight)
return(CountLeft);
else
return(CountRight);
}
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)
{
if(Root == NULL) { //This node doesn't exist. Therefore there
are no nodes in this 'subtree'
return 0;
} else { //Add the size of the left and right trees, then add 1
(which is the current node)
return CountNodes(Root->Left) + CountNodes(Root->Right) +
1;
}
}
//----------------------------------------------------------------
bool Tree::Search(TreeStructPtr Root, int n)
{
bool found;
TreeStructPtr curr;
curr = Root;
found = false;
while ((curr != NULL) && (! found))
if (curr->Number == n)
found = true;
else if (n < curr->Number)
curr = curr->Left;
else
curr = curr->Right;
return found;
}
//----------------------------------------------------------------
void Tree::PrintInOrderTree(TreeStructPtr Root)
{
if (Root == NULL)return;
/* first recur on Left child */
PrintInOrderTree(Root->Left);
/* then print the data of node */
printf("%d-->", Root->Number);
/* now recur on Right child */
PrintInOrderTree(Root->Right);
}
//----------------------------------------------------------------
void Tree::PrintPreOrderTree(TreeStructPtr Root)
{
if (Root == NULL)
return;
/* first print data of node */
printf("%d-->", Root->Number);
/* then recur on Left sutree */
PrintPreOrderTree(Root->Left);
/* now recur on Right subtree */
PrintPreOrderTree(Root->Right);
}
//----------------------------------------------------------------
void Tree::PrintPostOrderTree(TreeStructPtr Root)
{
if (Root == NULL)
return;
// first recur on Left subtree
PrintPostOrderTree(Root->Left);
// then recur on Right subtree
PrintPostOrderTree(Root->Right);
// now deal with the node
printf("%d-->", Root->Number);
}
//----------------------------------------------------------------
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)
{
Queue que;
TreeStructPtr p = Root;
if (p != NULL)
{
que.push(p);
while (!que.empty())
{
p = que.pop();
cout << p->Number << "-->";
if (p ->Left != NULL)
que.push(p->Left);
if (p->Right != NULL)
que.push(p->Right);
}
}
}
//----------------------------------------------------------------
int 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;
cout << "Printing Post Order " << endl;
t.PrintPostOrderTree(t.TreeRoot);
cout << " -----------------------------------------------" << endl;
cout << "Printing In Order " << endl;
t.PrintInOrderTree(t.TreeRoot);
cout << " -----------------------------------------------" << endl;
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;
cout << "Printing Pre Order with Stack " << endl;
t.PrintPreOrderTreeWithStack(t.TreeRoot);
cout << " -----------------------------------------------" << endl;
cout << "Printing level by level BreathFirst Traversal " <<
endl;
t.PrintBreadthFirstWithQueue(t.TreeRoot);
cout << " -----------------------------------------------" << endl;
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;
/*
Tree t2;
Copy(t.TreeRoot, t2.TreeRoot);
cout << "Printing In Order the copy of the tree" << endl;
t2.PrintInOrderTree(t2.TreeRoot);
cout << " -----------------------------------------------" << endl;
*/
return 0;
}
//----------------------------------------------------------------
/* 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
*/

More Related Content

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
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

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

DOCX
Complete the C++ program and implement the routines that are n.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
Write a C++ program that implements a binary search tree (BST) to man.pdf
PDF
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
PDF
Please write the C++ code that would display the exact same output a.pdf
PDF
Add these three functions to the class binaryTreeType (provided).W.pdf
DOCX
Binary Tree in C++ coding in the data structure
PPT
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
PDF
Please i need help on following program using C++ Language.Add the.pdf
DOCX
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
PDF
MAINCPP include ltiostreamgt include ltstringgt u.pdf
DOCX
C++ adt c++ implementations
PDF
In c++ format, for each function in the code, please using the comme.pdf
PDF
include ltiostreamgt include ltfstreamgt in.pdf
PDF
Data StructuresPlease I need help completing this c++ program..pdf
DOCX
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
PDF
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
PPT
computer notes - Data Structures - 13
Complete the C++ program and implement the routines that are n.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
Write a C++ program that implements a binary search tree (BST) to man.pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
Please write the C++ code that would display the exact same output a.pdf
Add these three functions to the class binaryTreeType (provided).W.pdf
Binary Tree in C++ coding in the data structure
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Please i need help on following program using C++ Language.Add the.pdf
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
MAINCPP include ltiostreamgt include ltstringgt u.pdf
C++ adt c++ implementations
In c++ format, for each function in the code, please using the comme.pdf
include ltiostreamgt include ltfstreamgt in.pdf
Data StructuresPlease I need help completing this c++ program..pdf
AvlTree.h#ifndef AVL_TREE_H#define AVL_TREE_H#include d.docx
Implement a function TNode copy_tree(TNode t) that creates a copy .pdf
computer notes - Data Structures - 13
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Basic Mud Logging Guide for educational purpose
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Computing-Curriculum for Schools in Ghana
TR - Agricultural Crops Production NC III.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Classroom Observation Tools for Teachers
Basic Mud Logging Guide for educational purpose
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Sports Quiz easy sports quiz sports quiz
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Insiders guide to clinical Medicine.pdf
Final Presentation General Medicine 03-08-2024.pptx
01-Introduction-to-Information-Management.pdf
Cell Types and Its function , kingdom of life
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Ad

Complete the C++ program and implement the routines that are not .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.be gin(), 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 { implement this } //--------------------------- ------------------------------------- // Inserting into the tree using recursion void Tree::InsertIntoTree(TreeStructPtr& Root, int x) { implement this } //------------------------------------------------
  • 2. ---------------- int Tree::FindMaxLen(TreeStructPtr Root) { implement this } //---------------------------------------------------- ------------ 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) { implement this } //---------------------------------------------------------------- bool Tree::Search(TreeStructPtr Root, int n) { implement this } //---------------------------------------------------------------- void Tree::PrintInOrderTree(TreeStructPtr Root) { implement this } //---------------------------------------------------------------- void Tree::PrintPreOrderTree(TreeStructPtr Root) { implement this } //---------------------------------------------------------------- void Tree::PrintPostOrderTree(TreeStructPtr Root) { implement this } //---------------------------------------------------------------- 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) { implement this } //------------------------------------------------- --------------- 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,
  • 3. 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
  • 4. 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 #include <iostream> #include <iomanip> #include <fstream> #include <string> #include <vector> #include <stdio.h> using namespace std; class TreeStruct;
  • 5. 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);}
  • 6. }; 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);
  • 7. 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 { if (Root1 == NULL) //base case to end recursion when at tree end Root2 == NULL; else{ Root2->Number = Root1->Number; // Just call recursively to copy the subtrees: Copy(Root1->Left, Root2->Left); Copy(Root1->Right, Root2->Right); } } //----------------------------------------------------------------
  • 8. // Inserting into the tree using recursion void Tree::InsertIntoTree(TreeStructPtr& Root, int x) { TreeStructPtr p = Root, prev=Root; while (p != NULL) { prev = p; if (x < p->Number) p = p->Left; else if (x > p->Number) p = p->Right; else { cout << " !!!! " << x << " is already in the tree " << endl; return; } } TreeStructPtr NewNode; NewNode = new (TreeStruct); NewNode->Number = x; NewNode->Left = NULL; NewNode->Right = NULL; if (Root == NULL) Root = NewNode; else if (prev->Number < x) prev->Right = NewNode; else
  • 9. prev->Left = NewNode; return; } //---------------------------------------------------------------- int Tree::FindMaxLen(TreeStructPtr Root) { if (Root == NULL) return(0); else if (Root->Right == NULL) return(1+FindMaxLen(Root->Left)); else if (Root->Left == NULL) return(1+FindMaxLen(Root->Right)); else { int CountLeft = 1 + FindMaxLen(Root->Left); int CountRight = 1 + FindMaxLen(Root->Right); if (CountLeft > CountRight) return(CountLeft); else return(CountRight); } return 0; } //----------------------------------------------------------------
  • 10. 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) { if(Root == NULL) { //This node doesn't exist. Therefore there are no nodes in this 'subtree' return 0;
  • 11. } else { //Add the size of the left and right trees, then add 1 (which is the current node) return CountNodes(Root->Left) + CountNodes(Root->Right) + 1; } } //---------------------------------------------------------------- bool Tree::Search(TreeStructPtr Root, int n) { bool found; TreeStructPtr curr; curr = Root; found = false; while ((curr != NULL) && (! found)) if (curr->Number == n) found = true; else if (n < curr->Number) curr = curr->Left; else curr = curr->Right; return found; } //----------------------------------------------------------------
  • 12. void Tree::PrintInOrderTree(TreeStructPtr Root) { if (Root == NULL)return; /* first recur on Left child */ PrintInOrderTree(Root->Left); /* then print the data of node */ printf("%d-->", Root->Number); /* now recur on Right child */ PrintInOrderTree(Root->Right); } //---------------------------------------------------------------- void Tree::PrintPreOrderTree(TreeStructPtr Root) { if (Root == NULL) return; /* first print data of node */ printf("%d-->", Root->Number); /* then recur on Left sutree */ PrintPreOrderTree(Root->Left);
  • 13. /* now recur on Right subtree */ PrintPreOrderTree(Root->Right); } //---------------------------------------------------------------- void Tree::PrintPostOrderTree(TreeStructPtr Root) { if (Root == NULL) return; // first recur on Left subtree PrintPostOrderTree(Root->Left); // then recur on Right subtree PrintPostOrderTree(Root->Right); // now deal with the node printf("%d-->", Root->Number); } //---------------------------------------------------------------- void Tree::PrintPreOrderTreeWithStack(TreeStructPtr Root) { Stack stk; TreeStructPtr p = Root; if (p != NULL) {
  • 14. 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) { Queue que; TreeStructPtr p = Root; if (p != NULL) { que.push(p); while (!que.empty()) { p = que.pop(); cout << p->Number << "-->"; if (p ->Left != NULL)
  • 15. que.push(p->Left); if (p->Right != NULL) que.push(p->Right); } } } //---------------------------------------------------------------- int 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);
  • 16. cout << " -----------------------------------------------" << endl; cout << "Printing Post Order " << endl; t.PrintPostOrderTree(t.TreeRoot); cout << " -----------------------------------------------" << endl; cout << "Printing In Order " << endl; t.PrintInOrderTree(t.TreeRoot); cout << " -----------------------------------------------" << endl; 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; cout << "Printing Pre Order with Stack " << endl; t.PrintPreOrderTreeWithStack(t.TreeRoot); cout << " -----------------------------------------------" << endl; cout << "Printing level by level BreathFirst Traversal " << endl; t.PrintBreadthFirstWithQueue(t.TreeRoot);
  • 17. cout << " -----------------------------------------------" << endl; 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; /* Tree t2; Copy(t.TreeRoot, t2.TreeRoot); cout << "Printing In Order the copy of the tree" << endl; t2.PrintInOrderTree(t2.TreeRoot); cout << " -----------------------------------------------" << endl; */ return 0; } //---------------------------------------------------------------- /* Your output should look like the foolowing Printing Pre Order
  • 18. 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--> -----------------------------------------------
  • 19. 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 */