Question: Part 3 - Binary Tree program C++ - Do NOT use ' struct ', you must use classes for each tree class and write in
Part 3 - Binary Tree program C++ - Do NOT use 'struct', you must use classes for each tree class and write in C++.( 5 points )
Program 3 - Convert program 1 the class, MyBiTree, to a template (C++ ) ( 5 points )
Here is my program 1:
#include
using namespace std;
class myBiTree{
private:
int tree[8];
int count;
public:
myBiTree(){
count = 0;
};
int root(int num){
if(tree[0] != 0)
cout << "tree already had root" << endl;
else
tree[0] = num;
return 0;
}
int rightChild(int index){
if(tree[index]!= NULL && ((2*index)+1)<=count){
return (2*index)+1;
}
else{
return 0;
}
}
int leftChild(int index){
if(tree[index]!= NULL && ((2*index))<=count){
return (2*index);
}
else{
return 0;
}
}
bool isFull(){
if(count > 7){
return true;
}
else{
return false;
}
}
bool isEmpty(){
if(count <= 0){
return true;
}
else{
return false;
}
}
void insert(int input){
if(isFull()){
cout << "tree is full..." << endl;
}
else{
count++;
tree[count] = input;
}
}
void remove(int input){
if(isEmpty()){
cout << "tree is empty..." << endl;
}
else{
int aux = 0;
for(int i = 1;i<=count;i++){
if(tree[i] == input){
aux = i;
}
}
if(aux == 0){
cout << "variable doesnt exist within tree" << endl;
}
else{
tree[aux] = tree[count];
tree[count] = NULL;
count--;
}
}
}
void preOrderPrintAll(int index){
if(index>0 && tree[index]!= NULL){
cout << tree[index] << " ";
preOrderPrintAll(leftChild(index));
preOrderPrintAll(rightChild(index));
}
}
void postOrderPrintAll(int index){
if(index>0 && tree[index]!= NULL){
postOrderPrintAll(leftChild(index));
postOrderPrintAll(rightChild(index));
cout << tree[index] << " ";
}
}
void inOrderPrintAll(int index){
if(index>0 && tree[index]!= NULL){
inOrderPrintAll(leftChild(index));
cout << tree[index] << " ";
inOrderPrintAll(rightChild(index));
}
}
};
int main() {
myBiTree T1;
T1.insert(15);
T1.insert(9);
T1.insert(21);
T1.insert(3);
T1.insert(11);
T1.insert(17);
T1.insert(42);
cout << "Preorder: ";
T1.preOrderPrintAll(1);
cout << endl;
cout << "Inorder: ";
T1.inOrderPrintAll(1);
cout << endl;
cout << "Postorder: ";
T1.postOrderPrintAll(1);
cout << endl;
T1.remove(17);
cout << "Preorder: ";
T1.preOrderPrintAll(1);
cout << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
