C++: Implement a remove function which is a member function of the bst class. Al
ID: 3719652 • Letter: C
Question
C++: Implement a remove function which is a member function of the bst class.
Also, some test codes should be included to demonstrate the correctness of your implementation.
_____________________________________________________________________________________________________
bst.h
class btree{
public:
btree();
void insert(int index, int entry);
int root(int index);
int left(int index);
int right(int index);
int size();
void display();
private:
int data[100];
int used;
};
_____________________________________________________________________________________________________
bst.cpp
#include <iostream>
#include "bst1.h"
#include <cmath>
using namespace std;
btree::btree() {
used=0;
for(int i=0;i<100;i++){
data[i]=0;
}
}
void btree::insert(int index, int entry) {
if(data[index]==0){
data[index]=entry;
used++;
}
else if(data[index]>entry){
insert(2*index+1, entry);
}
else if(data[index]<entry){
insert(2*index+2, entry);
}
}
int btree::root(int index) {
return data[index];
}
int btree::left(int index) {
return data[2*index+1];
}
int btree::right(int index) {
return data[2*index+2];
}
int btree::size() {
return used;
}
void btree::display(){
int row=ceil(log2(size()+1));
int space=1;
int index1=0;
for(int i=0;i<row;i++){
for(int j=0; j<20-pow(2.0,i);j++){
cout<<" ";
}
for(int k=0; k<pow(2.0,i);k++)
{
cout<<data[index1]<<" ";
index1++;
}
cout<<endl;
}
}
void pre(btree b1, int index){
//if(b1.left(index)==0 && b1.right(index)==0){
//cout<<b1.root(index)<<" ";
//}
cout<<b1.root(index)<<" "; // 3
if(b1.left(index)!=0)
pre(b1, 2*index+1); // 1
if(b1.right(index)!=0)
pre(b1, 2*index+2); // 2
}
void in(btree b1, int index){
//if(b1.left(index)==0 && b1.right(index)==0){
//cout<<b1.root(index)<<" ";
//}
if(b1.left(index)!=0)
in(b1, 2*index+1); // 1
cout<<b1.root(index)<<" "; // 3
if(b1.right(index)!=0)
in(b1, 2*index+2); // 2
}
void post(btree b1, int index){
//if(b1.left(index)==0 && b1.right(index)==0){
//cout<<b1.root(index)<<" ";
//}
if(b1.left(index)!=0)
post(b1, 2*index+1); // 1
if(b1.right(index)!=0)
post(b1, 2*index+2); // 2
cout<<b1.root(index)<<" "; // 3
}
int main(){
btree b1;
b1.insert(0, 25);
b1.insert(0, 15);
b1.insert(0, 50);
b1.insert(0, 10);
b1.insert(0, 20);
b1.insert(0, 30);
b1.insert(0, 60);
b1.display();
cout<<"pre order traversal: ";
pre(b1, 0);
cout<<endl;
cout<<"in order traversal: ";
in(b1, 0);
cout<<endl;
cout<<"post order traversal: ";
post(b1, 0);
}