SlideShare a Scribd company logo
1 of 7
Download to read offline
Below binary program is usefell to you
#include
template
class Tree
{
// Internal class which stores only Node related information.
struct TreeNode
{
T data;
TreeNode * left;
TreeNode * right;
TreeNode(T val):data(val),left(NULL),right(NULL)
{
}
};
TreeNode * root;
void print(TreeNode*);
void freeMemory(TreeNode*);
public:
Tree();
~Tree();
void insert(T);
void print();
};
template
Tree::Tree():root(NULL){}
template
Tree::~Tree()
{
freeMemory(root);
}
template
void Tree::freeMemory(Tree::TreeNode *node)
{
if (node==NULL)
return;
if (node->left)
freeMemory(node->left);
if (root->right)
freeMemory(node->right);
delete node;
}
template
//make it return value?
void Tree::insert(T val)
{
TreeNode * treeNode = NULL;
try
{
treeNode = new TreeNode(val); // handle exception necessary?
} catch (std::bad_alloc &exception)
{
std::cerr << "bad_alloc caught: " << exception.what() << std::endl;
EXIT_FAILURE;
}
TreeNode *temp=NULL;
TreeNode *prev=NULL;
temp = root;
while(temp)
{
prev = temp;
if (temp->data < treeNode->data)
temp = temp->right;
else
temp = temp->left;
}
if (prev==NULL)
root = treeNode;
else
{
if (prev->datadata)
prev->right = treeNode; // use setter function?
else
prev->left = treeNode;
}
}
template
void Tree::print(TreeNode *root)
{
if (root==NULL)
return ;
print(root->left);
std::cout << root->data << std::endl;
print(root->right);
}
template
void Tree::print()
{
print(root);
}
int main()
{
Tree tree;
tree.insert(14);
tree.insert(12);
tree.insert(6);
tree.insert(17);
tree.insert(8);
tree.print();
}
Solution
Below binary program is usefell to you
#include
template
class Tree
{
// Internal class which stores only Node related information.
struct TreeNode
{
T data;
TreeNode * left;
TreeNode * right;
TreeNode(T val):data(val),left(NULL),right(NULL)
{
}
};
TreeNode * root;
void print(TreeNode*);
void freeMemory(TreeNode*);
public:
Tree();
~Tree();
void insert(T);
void print();
};
template
Tree::Tree():root(NULL){}
template
Tree::~Tree()
{
freeMemory(root);
}
template
void Tree::freeMemory(Tree::TreeNode *node)
{
if (node==NULL)
return;
if (node->left)
freeMemory(node->left);
if (root->right)
freeMemory(node->right);
delete node;
}
template
//make it return value?
void Tree::insert(T val)
{
TreeNode * treeNode = NULL;
try
{
treeNode = new TreeNode(val); // handle exception necessary?
} catch (std::bad_alloc &exception)
{
std::cerr << "bad_alloc caught: " << exception.what() << std::endl;
EXIT_FAILURE;
}
TreeNode *temp=NULL;
TreeNode *prev=NULL;
temp = root;
while(temp)
{
prev = temp;
if (temp->data < treeNode->data)
temp = temp->right;
else
temp = temp->left;
}
if (prev==NULL)
root = treeNode;
else
{
if (prev->datadata)
prev->right = treeNode; // use setter function?
else
prev->left = treeNode;
}
}
template
void Tree::print(TreeNode *root)
{
if (root==NULL)
return ;
print(root->left);
std::cout << root->data << std::endl;
print(root->right);
}
template
void Tree::print()
{
print(root);
}
int main()
{
Tree tree;
tree.insert(14);
tree.insert(12);
tree.insert(6);
tree.insert(17);
tree.insert(8);
tree.print();
}

More Related Content

Similar to Below binary program is usefell to you#include iostreamtem.pdf

Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfSANDEEPARIHANT
 
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdfaristogifts99
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdffashionbigchennai
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxnoreendchesterton753
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxMatthPYNashd
 
Use the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdfUse the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdfsales87
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdfdeepua8
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfamarndsons
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxdebishakespeare
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxBrianGHiNewmanv
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
 
C programming. Answer question only in C code Ninth Deletion with B.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdfC programming. Answer question only in C code Ninth Deletion with B.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdfinfo309708
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfFootageetoffe16
 
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfmain.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfpratikradia365
 
My C proggram is having trouble in the switch in main. Also the a co.pdf
My C proggram is having trouble in the switch in main. Also the a co.pdfMy C proggram is having trouble in the switch in main. Also the a co.pdf
My C proggram is having trouble in the switch in main. Also the a co.pdfmeerobertsonheyde608
 
Solve using Java programming language- ----------------------------.pdf
Solve using Java programming language-   ----------------------------.pdfSolve using Java programming language-   ----------------------------.pdf
Solve using Java programming language- ----------------------------.pdfaksahnan
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfanton291
 
I have C++ question that I do not know how to do, Can you teach me t.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdfI have C++ question that I do not know how to do, Can you teach me t.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdffasttrackscardecors
 

Similar to Below binary program is usefell to you#include iostreamtem.pdf (20)

Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdf
 
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdf
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docx
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
Use the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdfUse the singly linked list class introduced in the lab to implement .pdf
Use the singly linked list class introduced in the lab to implement .pdf
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdf
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
Chtp412
Chtp412Chtp412
Chtp412
 
C programming. Answer question only in C code Ninth Deletion with B.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdfC programming. Answer question only in C code Ninth Deletion with B.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdf
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
 
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfmain.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
 
My C proggram is having trouble in the switch in main. Also the a co.pdf
My C proggram is having trouble in the switch in main. Also the a co.pdfMy C proggram is having trouble in the switch in main. Also the a co.pdf
My C proggram is having trouble in the switch in main. Also the a co.pdf
 
Solve using Java programming language- ----------------------------.pdf
Solve using Java programming language-   ----------------------------.pdfSolve using Java programming language-   ----------------------------.pdf
Solve using Java programming language- ----------------------------.pdf
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
linkedlist.pptx
linkedlist.pptxlinkedlist.pptx
linkedlist.pptx
 
I have C++ question that I do not know how to do, Can you teach me t.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdfI have C++ question that I do not know how to do, Can you teach me t.pdf
I have C++ question that I do not know how to do, Can you teach me t.pdf
 

More from anandinternational01

Sorry, the correct answer is D .pdf
                     Sorry, the correct answer is D                   .pdf                     Sorry, the correct answer is D                   .pdf
Sorry, the correct answer is D .pdfanandinternational01
 
1) Aspartylglycosaminuriaa) What is the inheritance pattern of thi.pdf
1) Aspartylglycosaminuriaa) What is the inheritance pattern of thi.pdf1) Aspartylglycosaminuriaa) What is the inheritance pattern of thi.pdf
1) Aspartylglycosaminuriaa) What is the inheritance pattern of thi.pdfanandinternational01
 
mollity = moles of solutemass of the solvent .pdf
                     mollity = moles of solutemass of the solvent    .pdf                     mollity = moles of solutemass of the solvent    .pdf
mollity = moles of solutemass of the solvent .pdfanandinternational01
 
import required package file import java.io.File; The Filed.pdf
 import required package file import java.io.File; The Filed.pdf import required package file import java.io.File; The Filed.pdf
import required package file import java.io.File; The Filed.pdfanandinternational01
 
Devaluation Revaluation Increases imports Increases exports In.pdf
    Devaluation Revaluation   Increases imports Increases exports   In.pdf    Devaluation Revaluation   Increases imports Increases exports   In.pdf
Devaluation Revaluation Increases imports Increases exports In.pdfanandinternational01
 
Project #4 Urban Population Dynamics This project will acquaint y.pdf
  Project #4 Urban Population Dynamics   This project will acquaint y.pdf  Project #4 Urban Population Dynamics   This project will acquaint y.pdf
Project #4 Urban Population Dynamics This project will acquaint y.pdfanandinternational01
 
Triple bond complexes Cobalt forms complexes wit.pdf
                     Triple bond complexes  Cobalt forms complexes wit.pdf                     Triple bond complexes  Cobalt forms complexes wit.pdf
Triple bond complexes Cobalt forms complexes wit.pdfanandinternational01
 
KNO2 is a strong salt KNO2 K+ + NO2- NO2- +.pdf
                     KNO2 is a strong salt  KNO2  K+ + NO2-  NO2- +.pdf                     KNO2 is a strong salt  KNO2  K+ + NO2-  NO2- +.pdf
KNO2 is a strong salt KNO2 K+ + NO2- NO2- +.pdfanandinternational01
 
In general, the more hydrophobic ,less polar, and.pdf
                     In general, the more hydrophobic ,less polar, and.pdf                     In general, the more hydrophobic ,less polar, and.pdf
In general, the more hydrophobic ,less polar, and.pdfanandinternational01
 
group IB, IIA, and IIB metals .pdf
                     group  IB, IIA, and IIB metals                   .pdf                     group  IB, IIA, and IIB metals                   .pdf
group IB, IIA, and IIB metals .pdfanandinternational01
 
the units should be kJmolSolutionthe units should be kJmol.pdf
the units should be kJmolSolutionthe units should be kJmol.pdfthe units should be kJmolSolutionthe units should be kJmol.pdf
the units should be kJmolSolutionthe units should be kJmol.pdfanandinternational01
 
the correct options are --1) There are several observations t.pdf
the correct options are --1)  There are several observations t.pdfthe correct options are --1)  There are several observations t.pdf
the correct options are --1) There are several observations t.pdfanandinternational01
 
Q1. The answer is true. This is because there are over a million kno.pdf
Q1. The answer is true. This is because there are over a million kno.pdfQ1. The answer is true. This is because there are over a million kno.pdf
Q1. The answer is true. This is because there are over a million kno.pdfanandinternational01
 
Stakeholders in the compensation arena are as followingShareholde.pdf
Stakeholders in the compensation arena are as followingShareholde.pdfStakeholders in the compensation arena are as followingShareholde.pdf
Stakeholders in the compensation arena are as followingShareholde.pdfanandinternational01
 
problems while testing a program are Testing issues include the d.pdf
problems while testing a program are Testing issues include the d.pdfproblems while testing a program are Testing issues include the d.pdf
problems while testing a program are Testing issues include the d.pdfanandinternational01
 
Occipital lobe-Houses area responsible for perception of vision and .pdf
Occipital lobe-Houses area responsible for perception of vision and .pdfOccipital lobe-Houses area responsible for perception of vision and .pdf
Occipital lobe-Houses area responsible for perception of vision and .pdfanandinternational01
 
long term business benefits of integrated dataStrategic thinking .pdf
long term business benefits of integrated dataStrategic thinking .pdflong term business benefits of integrated dataStrategic thinking .pdf
long term business benefits of integrated dataStrategic thinking .pdfanandinternational01
 

More from anandinternational01 (20)

Sorry, the correct answer is D .pdf
                     Sorry, the correct answer is D                   .pdf                     Sorry, the correct answer is D                   .pdf
Sorry, the correct answer is D .pdf
 
1) Aspartylglycosaminuriaa) What is the inheritance pattern of thi.pdf
1) Aspartylglycosaminuriaa) What is the inheritance pattern of thi.pdf1) Aspartylglycosaminuriaa) What is the inheritance pattern of thi.pdf
1) Aspartylglycosaminuriaa) What is the inheritance pattern of thi.pdf
 
mollity = moles of solutemass of the solvent .pdf
                     mollity = moles of solutemass of the solvent    .pdf                     mollity = moles of solutemass of the solvent    .pdf
mollity = moles of solutemass of the solvent .pdf
 
import required package file import java.io.File; The Filed.pdf
 import required package file import java.io.File; The Filed.pdf import required package file import java.io.File; The Filed.pdf
import required package file import java.io.File; The Filed.pdf
 
Devaluation Revaluation Increases imports Increases exports In.pdf
    Devaluation Revaluation   Increases imports Increases exports   In.pdf    Devaluation Revaluation   Increases imports Increases exports   In.pdf
Devaluation Revaluation Increases imports Increases exports In.pdf
 
Project #4 Urban Population Dynamics This project will acquaint y.pdf
  Project #4 Urban Population Dynamics   This project will acquaint y.pdf  Project #4 Urban Population Dynamics   This project will acquaint y.pdf
Project #4 Urban Population Dynamics This project will acquaint y.pdf
 
Triple bond complexes Cobalt forms complexes wit.pdf
                     Triple bond complexes  Cobalt forms complexes wit.pdf                     Triple bond complexes  Cobalt forms complexes wit.pdf
Triple bond complexes Cobalt forms complexes wit.pdf
 
THE IMAGE IS NT HERE!!! .pdf
                     THE IMAGE IS NT HERE!!!                          .pdf                     THE IMAGE IS NT HERE!!!                          .pdf
THE IMAGE IS NT HERE!!! .pdf
 
KNO2 is a strong salt KNO2 K+ + NO2- NO2- +.pdf
                     KNO2 is a strong salt  KNO2  K+ + NO2-  NO2- +.pdf                     KNO2 is a strong salt  KNO2  K+ + NO2-  NO2- +.pdf
KNO2 is a strong salt KNO2 K+ + NO2- NO2- +.pdf
 
In general, the more hydrophobic ,less polar, and.pdf
                     In general, the more hydrophobic ,less polar, and.pdf                     In general, the more hydrophobic ,less polar, and.pdf
In general, the more hydrophobic ,less polar, and.pdf
 
group IB, IIA, and IIB metals .pdf
                     group  IB, IIA, and IIB metals                   .pdf                     group  IB, IIA, and IIB metals                   .pdf
group IB, IIA, and IIB metals .pdf
 
trapeziumSolutiontrapezium.pdf
trapeziumSolutiontrapezium.pdftrapeziumSolutiontrapezium.pdf
trapeziumSolutiontrapezium.pdf
 
the units should be kJmolSolutionthe units should be kJmol.pdf
the units should be kJmolSolutionthe units should be kJmol.pdfthe units should be kJmolSolutionthe units should be kJmol.pdf
the units should be kJmolSolutionthe units should be kJmol.pdf
 
the correct options are --1) There are several observations t.pdf
the correct options are --1)  There are several observations t.pdfthe correct options are --1)  There are several observations t.pdf
the correct options are --1) There are several observations t.pdf
 
Q1. The answer is true. This is because there are over a million kno.pdf
Q1. The answer is true. This is because there are over a million kno.pdfQ1. The answer is true. This is because there are over a million kno.pdf
Q1. The answer is true. This is because there are over a million kno.pdf
 
Stakeholders in the compensation arena are as followingShareholde.pdf
Stakeholders in the compensation arena are as followingShareholde.pdfStakeholders in the compensation arena are as followingShareholde.pdf
Stakeholders in the compensation arena are as followingShareholde.pdf
 
PART 2SolutionPART 2.pdf
PART 2SolutionPART 2.pdfPART 2SolutionPART 2.pdf
PART 2SolutionPART 2.pdf
 
problems while testing a program are Testing issues include the d.pdf
problems while testing a program are Testing issues include the d.pdfproblems while testing a program are Testing issues include the d.pdf
problems while testing a program are Testing issues include the d.pdf
 
Occipital lobe-Houses area responsible for perception of vision and .pdf
Occipital lobe-Houses area responsible for perception of vision and .pdfOccipital lobe-Houses area responsible for perception of vision and .pdf
Occipital lobe-Houses area responsible for perception of vision and .pdf
 
long term business benefits of integrated dataStrategic thinking .pdf
long term business benefits of integrated dataStrategic thinking .pdflong term business benefits of integrated dataStrategic thinking .pdf
long term business benefits of integrated dataStrategic thinking .pdf
 

Recently uploaded

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Below binary program is usefell to you#include iostreamtem.pdf

  • 1. Below binary program is usefell to you #include template class Tree { // Internal class which stores only Node related information. struct TreeNode { T data; TreeNode * left; TreeNode * right; TreeNode(T val):data(val),left(NULL),right(NULL) { } }; TreeNode * root; void print(TreeNode*); void freeMemory(TreeNode*); public: Tree(); ~Tree(); void insert(T); void print(); }; template Tree::Tree():root(NULL){} template Tree::~Tree()
  • 2. { freeMemory(root); } template void Tree::freeMemory(Tree::TreeNode *node) { if (node==NULL) return; if (node->left) freeMemory(node->left); if (root->right) freeMemory(node->right); delete node; } template //make it return value? void Tree::insert(T val) { TreeNode * treeNode = NULL; try { treeNode = new TreeNode(val); // handle exception necessary? } catch (std::bad_alloc &exception) { std::cerr << "bad_alloc caught: " << exception.what() << std::endl; EXIT_FAILURE; } TreeNode *temp=NULL; TreeNode *prev=NULL; temp = root; while(temp) { prev = temp;
  • 3. if (temp->data < treeNode->data) temp = temp->right; else temp = temp->left; } if (prev==NULL) root = treeNode; else { if (prev->datadata) prev->right = treeNode; // use setter function? else prev->left = treeNode; } } template void Tree::print(TreeNode *root) { if (root==NULL) return ; print(root->left); std::cout << root->data << std::endl; print(root->right); } template void Tree::print() { print(root); } int main() { Tree tree; tree.insert(14); tree.insert(12); tree.insert(6); tree.insert(17);
  • 4. tree.insert(8); tree.print(); } Solution Below binary program is usefell to you #include template class Tree { // Internal class which stores only Node related information. struct TreeNode { T data; TreeNode * left; TreeNode * right; TreeNode(T val):data(val),left(NULL),right(NULL) { } }; TreeNode * root; void print(TreeNode*); void freeMemory(TreeNode*); public: Tree(); ~Tree(); void insert(T); void print(); };
  • 5. template Tree::Tree():root(NULL){} template Tree::~Tree() { freeMemory(root); } template void Tree::freeMemory(Tree::TreeNode *node) { if (node==NULL) return; if (node->left) freeMemory(node->left); if (root->right) freeMemory(node->right); delete node; } template //make it return value? void Tree::insert(T val) { TreeNode * treeNode = NULL; try { treeNode = new TreeNode(val); // handle exception necessary? } catch (std::bad_alloc &exception) { std::cerr << "bad_alloc caught: " << exception.what() << std::endl; EXIT_FAILURE; } TreeNode *temp=NULL;
  • 6. TreeNode *prev=NULL; temp = root; while(temp) { prev = temp; if (temp->data < treeNode->data) temp = temp->right; else temp = temp->left; } if (prev==NULL) root = treeNode; else { if (prev->datadata) prev->right = treeNode; // use setter function? else prev->left = treeNode; } } template void Tree::print(TreeNode *root) { if (root==NULL) return ; print(root->left); std::cout << root->data << std::endl; print(root->right); } template void Tree::print() { print(root); } int main() {