0% found this document useful (0 votes)
460 views

Array Representation of Binary Tree

The document discusses representing a binary tree using an array implementation, where the root node is placed at index 0 of the array, and children and parents can be found via indexing formulas; advantages are faster search time and less memory usage for balanced trees, while disadvantages include a fixed size and wasted space for incomplete trees; sample C++ code demonstrates inserting nodes and displaying the array representation of a binary tree.

Uploaded by

Ammar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
460 views

Array Representation of Binary Tree

The document discusses representing a binary tree using an array implementation, where the root node is placed at index 0 of the array, and children and parents can be found via indexing formulas; advantages are faster search time and less memory usage for balanced trees, while disadvantages include a fixed size and wasted space for incomplete trees; sample C++ code demonstrates inserting nodes and displaying the array representation of a binary tree.

Uploaded by

Ammar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Array Implementation of Binary Trees

Assignment work for Data Structures and Algorithm Analysis

Submitted By:

1.Ammar Jemal
2.Ahmed Hassen
3.Michael Daniel
4.Nahom Lulseged

Submitted to: Tariku Worku Due date: 2/11/2020


Array Representation of Binary Tree

In array representation of a binary tree, we use one-dimensional array (1-D


Array) to represent a binary tree. To represent a binary tree of depth 'n' using
array representation, we need one dimensional array with a maximum size of 2n
+ 1. Furthermore, the tree needs to be complete (otherwise it is inefficient and
problematic). If we have a binary tree with a depth of d, there will be a maximum
of 2d -1 nodes therefore we will have an array size of maximum 2d -1. In other
words, the array size will be the same as the number of nodes. Let us look at the
following two examples:

B C

D E F G

H I

The above tree is a complete binary tree therefore it can be implemented as an


array. The root is placed in position 0 of the array and then we continue by
putting elements in the array starting from top to bottom and, within the same
level, from left to right.

0 1 2 3 4 5 6 7 8
A B C D E F G H I

If a node is at ith index, the following points can be made:


• Its left child would be at [ (2 * i) + 1]
• Its right child would be at [ (2 * i) + 2]
• Its parent would be at  (i - 1) / 2 
By the formula above, we can find the parent and children of a node. For
instance, if we take node D which is at index 3, its left child will be at index
(2*3) + 1 = 7 which is node H. Right child of node D will be at index (2*3) +
2 = 8 which is node I. And finally, parent of node D will be at index (3-1) / 2
= 1 which is node B.

B C

D E F G

H I

To represent the above binary tree as an array, we first need to make it


complete. Therefore, we must insert empty nodes as children of node D. The ‘-‘
character is used to represent empty spaces. Then array representation will be
as follows

0 1 2 3 4 5 6 7 8 9 10
A B C D E F G - - H I

Advantages and Disadvantages of Array Implementation of Binary Trees

Advantages

• Faster search time


• Simplicity – it is easy to represent a binary tree as an array.
• It uses less memory space if the binary tree is balanced.
• If the child is given, its parent can be determined by the formula listed
above and vice versa.
Disadvantages
• 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>
using namespace std;
const int SIZE = 11;
char BT[SIZE];
int root(char key){
if(BT[0] != '\0')
cout<<"Tree already has a root.";
else
BT[0] = key;
return 0;
}
int leftChild(char key, int parent){
if(BT[parent] == '\0')
cout <<"\nNo parent found";
else
BT[(parent * 2) + 1] = key;
return 0;
}
int rightChild(char key, int parent){
if(BT[parent] == '\0')
cout<<"\nNo parent found";
else
BT[(parent * 2) + 2] = key;
return 0;
}
int displayTree(){
cout << "\n";
for(int i = 0; i < SIZE; i++){
if(BT[i] != '\0')
cout<<BT[i]<<',';
else
cout<<"-"<<',';
}
return 0;
}
int main(){
root('A');
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;
}

You might also like