SlideShare a Scribd company logo
1 of 6
Download to read offline
Write a C++ program that implements a binary search tree (BST) to manage a number of integer
items with different priorities. In order to do so. you will need to maintain a queue as an item in
the tree. Each queue item is an integer. All items in a given queue will have the same priority. As
a new item with a user-specified priority comes in, it should be enqueued in the queue with items
of that priority. A new queue may need to created in case a queue with that priority does not
exist. Use dynamic memory to manage nodes in the queue and BST. Implement the following:
Constructor. Destructor. Overloaded copy constructor. Overloaded assignment operator.
Overloaded "==" operator to check if two trees are equal. A tree t1 is equal to t2 if for every
queue in t1, a corresponding queue with the same priority exists in t2 with the same set of
elements (the order of the elements in the queue docs not matter). Overloaded "
Solution
program using namespace std;
#define width_unit 5
class BStree
{
private:
class Node
{
public:
int data;
Node *left, *right;
Node(int d=0)
:data(d), left(NULL), right(NULL) {}
};
Node *root;
Node * trav(int, Node * &);
void chop(Node * N);
void copy(Node * N);
void print(ostream &, Node *, int) const;
void print(Node *, int) const;
public:
BStree(void);
~BStree(void);
bool find(int);
void insert(int);
void remove(int);
bool empty(void) const;
Tree(const BStree &);
const BStree & operator=(const BStree &);
friend ostream & operator<<(ostream &, const BStree &);
};
BStree::BStree(void)
{
root=NULL;
}
bool Tree::empty(void) const
{
return !root;
}
BStree::Node * BStree::trav(int foo, Node * & par)
{
Node * curr=root;
par=NULL;
while(curr && curr->data != foo)
{
par=curr;
if(foo < curr->data)
curr=curr->left;
else
curr=curr->right;
}
return curr;
}
bool BStree::find(int foo)
{
Node * par=NULL;
Node * curr=trav(foo, par);
return curr;
}
void BStree::insert(int foo)
{
Node * par=NULL;
Node * curr=trav(foo,par);
if(!curr)
{
curr= new Node(foo);
if(!par)
root=curr;
else if(foo < par->data)
par->left=curr;
else
par->right=curr;
}
}
void BStree::remove(const int foo)
{
Node * par=NULL;
Node * curr=trav(foo,par);
if(curr)
{
if(curr->left && curr->right)
{
Node * tmp=curr;
par=curr;
curr=curr->left;
while(curr->right)
{
par=curr;
curr=curr->right;
}
tmp->data=curr->data;
}
Node *tmp=(curr->left ? curr->left : curr->right);
if(!par)
root=tmp;
else if(par->data < curr->data)
par->right=tmp;
else
par->left=tmp;
delete curr;
}
}
void BStree::chop(Node *N)
{
if(N)
{
chop(N->left);
chop(N->right);
delete N;
}
}
BStree::~BStree(void)
{
chop(root);
}
BStree::BStree(const BStree & T)
{
root=NULL;
copy(T.root);
}
void BStree::copy(Node * N)
{
if(N)
{
insert(N->data);
copy(N->left);
copy(N->right);
}
}
const BStree & BStree::operator=(const BStree & T)
{
if(this != &T)
{
chop(root);
root=NULL;
copy(T.root);
}
return *this;
}
void BStree::print(ostream & ost, Node * curr, int level) const
{
if(curr)
{
print(ost,curr->right,level+1);
ost<data<left,level+1);
}
}
void BStree::print(Node * curr, int level) const
{
if(curr)
{
print(curr->right,level+1);
cout<data<left,level+1);
}
}
ostream & operator<<(ostream &ost, const BStree &t)
{
t.print(ost, t.root, 1);
return ost;
}
int main()
{
BStree mytree;
mytree.insert(5);
mytree.insert(3);
mytree.insert(2);
mytree.insert(7);
mytree.insert(0);
mytree.insert(2);
cout<

More Related Content

Similar to Write a C++ program that implements a binary search tree (BST) to man.pdf

UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptxjack881
 
Please i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfPlease i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfezzi552
 
In c++ format please, for each function in the code, using the comme.pdf
In c++ format please, for each function in the code, using the comme.pdfIn c++ format please, for each function in the code, using the comme.pdf
In c++ format please, for each function in the code, using the comme.pdfezycolours78
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfinfo114
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfherminaherman
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxVeerannaKotagi1
 
Running Head Discussion Board .docx
Running Head Discussion Board                                  .docxRunning Head Discussion Board                                  .docx
Running Head Discussion Board .docxjeanettehully
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationMoniruzzaman _
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdfMemeMiner
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 

Similar to Write a C++ program that implements a binary search tree (BST) to man.pdf (20)

UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 
linkedlist.pptx
linkedlist.pptxlinkedlist.pptx
linkedlist.pptx
 
Please i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfPlease i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdf
 
In c++ format please, for each function in the code, using the comme.pdf
In c++ format please, for each function in the code, using the comme.pdfIn c++ format please, for each function in the code, using the comme.pdf
In c++ format please, for each function in the code, using the comme.pdf
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
 
DS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docxDS UNIT5_BINARY TREES.docx
DS UNIT5_BINARY TREES.docx
 
Running Head Discussion Board .docx
Running Head Discussion Board                                  .docxRunning Head Discussion Board                                  .docx
Running Head Discussion Board .docx
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 

More from hardjasonoco14599

[Quantum Mechanics] In my class we are talking about the quantum har.pdf
[Quantum Mechanics] In my class we are talking about the quantum har.pdf[Quantum Mechanics] In my class we are talking about the quantum har.pdf
[Quantum Mechanics] In my class we are talking about the quantum har.pdfhardjasonoco14599
 
Write a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdfWrite a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdfhardjasonoco14599
 
what is tax preferenceSolutionTax preferences are measures of .pdf
what is tax preferenceSolutionTax preferences are measures of .pdfwhat is tax preferenceSolutionTax preferences are measures of .pdf
what is tax preferenceSolutionTax preferences are measures of .pdfhardjasonoco14599
 
Which of the following is true regarding homologous structuresA. .pdf
Which of the following is true regarding homologous structuresA. .pdfWhich of the following is true regarding homologous structuresA. .pdf
Which of the following is true regarding homologous structuresA. .pdfhardjasonoco14599
 
Why is it reasonable to assume that Venus, Earth, and Mars started w.pdf
Why is it reasonable to assume that Venus, Earth, and Mars started w.pdfWhy is it reasonable to assume that Venus, Earth, and Mars started w.pdf
Why is it reasonable to assume that Venus, Earth, and Mars started w.pdfhardjasonoco14599
 
Which of the following is NOT a complication in development of an HIV.pdf
Which of the following is NOT a complication in development of an HIV.pdfWhich of the following is NOT a complication in development of an HIV.pdf
Which of the following is NOT a complication in development of an HIV.pdfhardjasonoco14599
 
Which of the following groups is responsible for the actual developm.pdf
Which of the following groups is responsible for the actual developm.pdfWhich of the following groups is responsible for the actual developm.pdf
Which of the following groups is responsible for the actual developm.pdfhardjasonoco14599
 
Whats the role of the gastric caeca in a grasshopperSolutionGr.pdf
Whats the role of the gastric caeca in a grasshopperSolutionGr.pdfWhats the role of the gastric caeca in a grasshopperSolutionGr.pdf
Whats the role of the gastric caeca in a grasshopperSolutionGr.pdfhardjasonoco14599
 
what does it takes to be a living organ What does it take to be a l.pdf
what does it takes to be a living organ What does it take to be a l.pdfwhat does it takes to be a living organ What does it take to be a l.pdf
what does it takes to be a living organ What does it take to be a l.pdfhardjasonoco14599
 
ve targets. ball and sodket hinge joint condyloid joint saddle jo.pdf
ve targets. ball and sodket hinge joint condyloid joint saddle jo.pdfve targets. ball and sodket hinge joint condyloid joint saddle jo.pdf
ve targets. ball and sodket hinge joint condyloid joint saddle jo.pdfhardjasonoco14599
 
This one is for you - assume that this pedigree shows the inheritance.pdf
This one is for you - assume that this pedigree shows the inheritance.pdfThis one is for you - assume that this pedigree shows the inheritance.pdf
This one is for you - assume that this pedigree shows the inheritance.pdfhardjasonoco14599
 
There are 7 people in the elevator in a 10-story building. They are .pdf
There are 7 people in the elevator in a 10-story building. They are .pdfThere are 7 people in the elevator in a 10-story building. They are .pdf
There are 7 people in the elevator in a 10-story building. They are .pdfhardjasonoco14599
 
safety Construction Safety-Quiz 1 According to OSHA, what must man.pdf
safety Construction Safety-Quiz 1 According to OSHA, what must man.pdfsafety Construction Safety-Quiz 1 According to OSHA, what must man.pdf
safety Construction Safety-Quiz 1 According to OSHA, what must man.pdfhardjasonoco14599
 
Question 1 A ____________ is an intelligent device that controls the.pdf
Question 1 A ____________ is an intelligent device that controls the.pdfQuestion 1 A ____________ is an intelligent device that controls the.pdf
Question 1 A ____________ is an intelligent device that controls the.pdfhardjasonoco14599
 
I finished most of the program, but having trouble with some key fea.pdf
I finished most of the program, but having trouble with some key fea.pdfI finished most of the program, but having trouble with some key fea.pdf
I finished most of the program, but having trouble with some key fea.pdfhardjasonoco14599
 
In sliding window protocol the left wall of the sender sliding winod.pdf
In sliding window protocol the left wall of the sender sliding winod.pdfIn sliding window protocol the left wall of the sender sliding winod.pdf
In sliding window protocol the left wall of the sender sliding winod.pdfhardjasonoco14599
 
In mice, the black allele (B) is dominant to the recessive white all.pdf
In mice, the black allele (B) is dominant to the recessive white all.pdfIn mice, the black allele (B) is dominant to the recessive white all.pdf
In mice, the black allele (B) is dominant to the recessive white all.pdfhardjasonoco14599
 
How will the rate of diffusion of an interstitial i Impurity atom Inc.pdf
How will the rate of diffusion of an interstitial i Impurity atom Inc.pdfHow will the rate of diffusion of an interstitial i Impurity atom Inc.pdf
How will the rate of diffusion of an interstitial i Impurity atom Inc.pdfhardjasonoco14599
 
Let R be an integral domain. Prove 1R and -1R are the only units of .pdf
Let R be an integral domain. Prove 1R and -1R are the only units of .pdfLet R be an integral domain. Prove 1R and -1R are the only units of .pdf
Let R be an integral domain. Prove 1R and -1R are the only units of .pdfhardjasonoco14599
 
Kevin Keller’s aunt’s husband and his girl cousin have died of heart.pdf
Kevin Keller’s aunt’s husband and his girl cousin have died of heart.pdfKevin Keller’s aunt’s husband and his girl cousin have died of heart.pdf
Kevin Keller’s aunt’s husband and his girl cousin have died of heart.pdfhardjasonoco14599
 

More from hardjasonoco14599 (20)

[Quantum Mechanics] In my class we are talking about the quantum har.pdf
[Quantum Mechanics] In my class we are talking about the quantum har.pdf[Quantum Mechanics] In my class we are talking about the quantum har.pdf
[Quantum Mechanics] In my class we are talking about the quantum har.pdf
 
Write a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdfWrite a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdf
 
what is tax preferenceSolutionTax preferences are measures of .pdf
what is tax preferenceSolutionTax preferences are measures of .pdfwhat is tax preferenceSolutionTax preferences are measures of .pdf
what is tax preferenceSolutionTax preferences are measures of .pdf
 
Which of the following is true regarding homologous structuresA. .pdf
Which of the following is true regarding homologous structuresA. .pdfWhich of the following is true regarding homologous structuresA. .pdf
Which of the following is true regarding homologous structuresA. .pdf
 
Why is it reasonable to assume that Venus, Earth, and Mars started w.pdf
Why is it reasonable to assume that Venus, Earth, and Mars started w.pdfWhy is it reasonable to assume that Venus, Earth, and Mars started w.pdf
Why is it reasonable to assume that Venus, Earth, and Mars started w.pdf
 
Which of the following is NOT a complication in development of an HIV.pdf
Which of the following is NOT a complication in development of an HIV.pdfWhich of the following is NOT a complication in development of an HIV.pdf
Which of the following is NOT a complication in development of an HIV.pdf
 
Which of the following groups is responsible for the actual developm.pdf
Which of the following groups is responsible for the actual developm.pdfWhich of the following groups is responsible for the actual developm.pdf
Which of the following groups is responsible for the actual developm.pdf
 
Whats the role of the gastric caeca in a grasshopperSolutionGr.pdf
Whats the role of the gastric caeca in a grasshopperSolutionGr.pdfWhats the role of the gastric caeca in a grasshopperSolutionGr.pdf
Whats the role of the gastric caeca in a grasshopperSolutionGr.pdf
 
what does it takes to be a living organ What does it take to be a l.pdf
what does it takes to be a living organ What does it take to be a l.pdfwhat does it takes to be a living organ What does it take to be a l.pdf
what does it takes to be a living organ What does it take to be a l.pdf
 
ve targets. ball and sodket hinge joint condyloid joint saddle jo.pdf
ve targets. ball and sodket hinge joint condyloid joint saddle jo.pdfve targets. ball and sodket hinge joint condyloid joint saddle jo.pdf
ve targets. ball and sodket hinge joint condyloid joint saddle jo.pdf
 
This one is for you - assume that this pedigree shows the inheritance.pdf
This one is for you - assume that this pedigree shows the inheritance.pdfThis one is for you - assume that this pedigree shows the inheritance.pdf
This one is for you - assume that this pedigree shows the inheritance.pdf
 
There are 7 people in the elevator in a 10-story building. They are .pdf
There are 7 people in the elevator in a 10-story building. They are .pdfThere are 7 people in the elevator in a 10-story building. They are .pdf
There are 7 people in the elevator in a 10-story building. They are .pdf
 
safety Construction Safety-Quiz 1 According to OSHA, what must man.pdf
safety Construction Safety-Quiz 1 According to OSHA, what must man.pdfsafety Construction Safety-Quiz 1 According to OSHA, what must man.pdf
safety Construction Safety-Quiz 1 According to OSHA, what must man.pdf
 
Question 1 A ____________ is an intelligent device that controls the.pdf
Question 1 A ____________ is an intelligent device that controls the.pdfQuestion 1 A ____________ is an intelligent device that controls the.pdf
Question 1 A ____________ is an intelligent device that controls the.pdf
 
I finished most of the program, but having trouble with some key fea.pdf
I finished most of the program, but having trouble with some key fea.pdfI finished most of the program, but having trouble with some key fea.pdf
I finished most of the program, but having trouble with some key fea.pdf
 
In sliding window protocol the left wall of the sender sliding winod.pdf
In sliding window protocol the left wall of the sender sliding winod.pdfIn sliding window protocol the left wall of the sender sliding winod.pdf
In sliding window protocol the left wall of the sender sliding winod.pdf
 
In mice, the black allele (B) is dominant to the recessive white all.pdf
In mice, the black allele (B) is dominant to the recessive white all.pdfIn mice, the black allele (B) is dominant to the recessive white all.pdf
In mice, the black allele (B) is dominant to the recessive white all.pdf
 
How will the rate of diffusion of an interstitial i Impurity atom Inc.pdf
How will the rate of diffusion of an interstitial i Impurity atom Inc.pdfHow will the rate of diffusion of an interstitial i Impurity atom Inc.pdf
How will the rate of diffusion of an interstitial i Impurity atom Inc.pdf
 
Let R be an integral domain. Prove 1R and -1R are the only units of .pdf
Let R be an integral domain. Prove 1R and -1R are the only units of .pdfLet R be an integral domain. Prove 1R and -1R are the only units of .pdf
Let R be an integral domain. Prove 1R and -1R are the only units of .pdf
 
Kevin Keller’s aunt’s husband and his girl cousin have died of heart.pdf
Kevin Keller’s aunt’s husband and his girl cousin have died of heart.pdfKevin Keller’s aunt’s husband and his girl cousin have died of heart.pdf
Kevin Keller’s aunt’s husband and his girl cousin have died of heart.pdf
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
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
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
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
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
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
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

Write a C++ program that implements a binary search tree (BST) to man.pdf

  • 1. Write a C++ program that implements a binary search tree (BST) to manage a number of integer items with different priorities. In order to do so. you will need to maintain a queue as an item in the tree. Each queue item is an integer. All items in a given queue will have the same priority. As a new item with a user-specified priority comes in, it should be enqueued in the queue with items of that priority. A new queue may need to created in case a queue with that priority does not exist. Use dynamic memory to manage nodes in the queue and BST. Implement the following: Constructor. Destructor. Overloaded copy constructor. Overloaded assignment operator. Overloaded "==" operator to check if two trees are equal. A tree t1 is equal to t2 if for every queue in t1, a corresponding queue with the same priority exists in t2 with the same set of elements (the order of the elements in the queue docs not matter). Overloaded " Solution program using namespace std; #define width_unit 5 class BStree { private: class Node { public: int data; Node *left, *right; Node(int d=0) :data(d), left(NULL), right(NULL) {} }; Node *root; Node * trav(int, Node * &); void chop(Node * N); void copy(Node * N); void print(ostream &, Node *, int) const; void print(Node *, int) const; public: BStree(void); ~BStree(void); bool find(int);
  • 2. void insert(int); void remove(int); bool empty(void) const; Tree(const BStree &); const BStree & operator=(const BStree &); friend ostream & operator<<(ostream &, const BStree &); }; BStree::BStree(void) { root=NULL; } bool Tree::empty(void) const { return !root; } BStree::Node * BStree::trav(int foo, Node * & par) { Node * curr=root; par=NULL; while(curr && curr->data != foo) { par=curr; if(foo < curr->data) curr=curr->left; else curr=curr->right; } return curr; } bool BStree::find(int foo) { Node * par=NULL; Node * curr=trav(foo, par); return curr; } void BStree::insert(int foo)
  • 3. { Node * par=NULL; Node * curr=trav(foo,par); if(!curr) { curr= new Node(foo); if(!par) root=curr; else if(foo < par->data) par->left=curr; else par->right=curr; } } void BStree::remove(const int foo) { Node * par=NULL; Node * curr=trav(foo,par); if(curr) { if(curr->left && curr->right) { Node * tmp=curr; par=curr; curr=curr->left; while(curr->right) { par=curr; curr=curr->right; } tmp->data=curr->data; } Node *tmp=(curr->left ? curr->left : curr->right); if(!par) root=tmp; else if(par->data < curr->data)
  • 4. par->right=tmp; else par->left=tmp; delete curr; } } void BStree::chop(Node *N) { if(N) { chop(N->left); chop(N->right); delete N; } } BStree::~BStree(void) { chop(root); } BStree::BStree(const BStree & T) { root=NULL; copy(T.root); } void BStree::copy(Node * N) { if(N) { insert(N->data); copy(N->left); copy(N->right); } } const BStree & BStree::operator=(const BStree & T) { if(this != &T)
  • 5. { chop(root); root=NULL; copy(T.root); } return *this; } void BStree::print(ostream & ost, Node * curr, int level) const { if(curr) { print(ost,curr->right,level+1); ost<data<left,level+1); } } void BStree::print(Node * curr, int level) const { if(curr) { print(curr->right,level+1); cout<data<left,level+1); } } ostream & operator<<(ostream &ost, const BStree &t) { t.print(ost, t.root, 1); return ost; } int main() { BStree mytree; mytree.insert(5); mytree.insert(3); mytree.insert(2); mytree.insert(7); mytree.insert(0);