-
Notifications
You must be signed in to change notification settings - Fork 1
/
Binary Tree Array.cpp
55 lines (47 loc) · 1.04 KB
/
Binary Tree Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
using namespace std;
class BinaryTree{
int top;
int arr[20];
public:
BinaryTree(int root){
// arr = new int[maxsize];
arr[0] = root;
top = 0;
}
int size();
void setLeft(int,int);
void setRight(int,int);
void printTree();
int getParentIndex(int);
};
int BinaryTree :: size(){
return top+1;
}
void BinaryTree :: setLeft(int rootIndex,int element){
arr[(rootIndex*2)+1] = element;
}
void BinaryTree :: setRight(int rootIndex,int element){
arr[(rootIndex*2)+2] = element;
}
void BinaryTree :: printTree(){
for(int i=0;i<7;i++){
cout << arr[i] << endl;
}
cout << endl;
}
int BinaryTree :: getParentIndex(int childIndex){
int parent = (childIndex+1)/2;
return parent;
}
int main(){
BinaryTree* bt = new BinaryTree(45);
bt->setLeft(0,34);
bt->setRight(0,40);
bt->setLeft(1,23);
bt->setRight(1,16);
bt->setLeft(2,22);
bt->setRight(2,30);
bt->printTree();
return 0;
}