subject

For this assignment, build on the bst code posted today (nov 15). support the following operations:
(1) a parameterized constructor that initializes the tree to a valid user-specified capacity. you will need to update the add function to ensure that you do not surpass the tree’s capacity.
(2) a copy constructor.
(3) overloaded assignment operator.
(4) overloaded == operator. two trees are equal if they contain the exact same elements.
(5) a get_sibling class function that takes a number as input and returns its sibling. suppose 100 is a parent, and 50 is its left child and 200 is its right child. the sibling of 50 is 200 (because 50 and 200 are the children of 100). the function assumes that the input value exists in the tree and that a sibling exists for the input value.
(6) use const keyword as appropriate for class functions and class function inputs, including for those that we have already implemented.
demonstrate that your code works using a main function.
#include
using namespace std;
class node
{
private:
int val;
node* left;
node* right;
public:
node();
node(int);
~node();
bool set_val(int);
bool set_left(node*);
bool set_right(node*);
int get_val();
node* get_left();
node* get_right();
void display();
};
node: : node()
{
val=0;
left=null;
right=null;
}
node: : node(int v)
{
val=v;
left=null;
right=null;
}
node: : ~node()
{
}
bool node: : set_val(int v)
{
val=v;
return true;
}
int node: : get_val()
{
return val;
}
bool node: : set_left(node* n)
{
left=n;
}
node* node: : get_left()
{
return left;
}
bool node: : set_right(node* p)
{
right=p;
}
node* node: : get_right()
{
return right;
}
void node: : display()
{
cout < < val < < " ";
}
class bst
{
private:
node* root;
void preorder(node*);
void postorder(node*);
void inorder(node*);
void cleanup(node*);
public:
bst();
~bst();
bool add(int);
bool is_present(int);
void display();
};
void bst: : cleanup(node* curr)
{
if (curr==null)
return;
cleanup(curr-> ;
cleanup(curr-> ;
cout < < "deallocating the node with value: " < < curr-> get_val() < < endl;
delete curr;
}
bst: : ~bst()
{
cleanup(root);
}
void bst: : display()
{
preorder(root);
cout < < endl;
postorder(root);
cout < < endl;
inorder(root);
cout < < endl;
}
void bst: : inorder(node* curr)
{
if (curr==null)
return;
inorder(curr-> ;
cout < < curr-> get_val() < < " ";
inorder(curr-> ;
}
void bst: : postorder(node* curr)
{
if (curr==null)
return;
postorder(curr-> ;
postorder(curr-> ;
cout < < curr-> get_val() < < " ";
}
void bst: : preorder(node* curr)
{
if (curr==null)
return;
cout < < curr-> get_val() < < " ";
preorder(curr-> ;
preorder(curr-> ;
}
bst: : bst()
{
root=null;
}
bool bst: : add(int v)
{
if (is_present(v))
return false;
node* temp=new node(v);
if (root==null)
{
root=temp;
return true;
}
node* curr=root;
while(curr! =null)
{
if (v> curr->
{
if (curr-> get_right()==null)
{
curr-> set_right(temp);
return true;
}
else
curr=curr-> get_right();
}
else
{
if (curr-> get_left()==null)
{
curr-> set_left(temp);
return true;
}
curr=curr-> get_left();
}
}
}
bool bst: : is_present(int v)
{
node* curr=root;
while(curr! =null)
{
if (curr-> get_val()==v)
return true;
else if (v> curr->
curr=curr-> get_right();
else
curr=curr-> get_left();
}
return false;
}
int main()
{
bst b;
b. add(100);
b. add(90);
b. add(150);
b. add(80);
b. add(95);
b. add(125);
b. add(175);
b. add(120);
b. add(127);
b. add(160);
b. add(200);
// for (int i=0; i< =200; i++)
// cout < < i < < ": " < < b. is_present(i) < < endl
b. display();
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 03.02.2019 00:27
You should hand write your references on your resume.
Answers: 1
question
Computers and Technology, 29.01.2019 00:28
Which statistical function in a spreadsheet you to see how far each number varies, on average, from the average value of the list?
Answers: 2
question
Computers and Technology, 24.01.2019 19:56
Which steps will open the system so that you can enter a question and do a search for
Answers: 1
question
Computers and Technology, 23.01.2019 23:08
Can someone work out the answer as it comes up in one of my computer science exams and i don't understand the cryptovariables
Answers: 1
You know the right answer?
For this assignment, build on the bst code posted today (nov 15). support the following operations:...
Questions
question
Biology, 04.02.2016 05:07
Questions on the website: 6675995