Array Implementation of Binary Trees
Array Implementation of Binary Trees
ertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopa
sdfghjklzxcvbnmqwertyuiopasdf
Array Implementation of Binary Trees
ghjklzxcvbnmqwertyuiopasdfghj
Assignment work for Data Structures and Algorithm
Analysis
klzxcvbnmqwertyuiopasdfghjklz
Submitted By:
xcvbnmqwertyuiopasdfghjklzxcv
1.Ammar Jemal
2.Ahmed Hassen
3.Michael Daniel
bnmqwertyuiopasdfghjklzxcvbn
4.Nahom Lulseged
mqwertyuiopasdfghjklzxcvbnmq
Due date: 2/10/2020
wertyuiopasdfghjklzxcvbnmqwe
rtyuiopasdfghjklzxcvbnmqwerty
uiopasdfghjklzxcvbnmqwertyuio
pasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmqwertyuiopasdfghjk
Array Implementation of Binary Trees
B C
D E F G
H I
0 1 2 3 4 5 6 7 8
A B C D E F G H I
Page 1 of 5
If a node is at ith index, the following points can be made:
B C
D E F G
H I
0 1 2 3 4 5 6 7 8 9 10
A B C D E F G - - H I
Page 2 of 5
Advantages and Disadvantages of Array Implementation of Binary
Trees
Advantages
• It has a fixed size. This is a problem since data may overflow the array if
too little space is allocated or memory maybe wasted if too much memory
is allocated.
• Since we must insert empty nodes to make the tree complete, the array
will have empty elements and as a result, memory is wasted.
Implementation in C++
#include <iostream>
char BT[SIZE];
if(BT[0] != '\0')
else
BT[0] = key;
return 0;
Page 3 of 5
int leftChild(char key, int parent){
if(BT[parent] == '\0')
else
BT[(parent * 2) + 1] = key;
return 0;
if(BT[parent] == '\0')
else
BT[(parent * 2) + 2] = key;
return 0;
int displayTree(){
if(BT[i] != '\0')
cout<<BT[i]<<',';
else
cout<<"-"<<',';
return 0;
int main(){
root('A');
Page 4 of 5
leftChild('B', 0);
rightChild('C', 0);
leftChild('D', 1);
rightChild('E', 1);
leftChild('F', 2);
rightChild('G', 2);
leftChild('H', 4);
rightChild('I', 4);
displayTree();
return 0;
Output
Page 5 of 5