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

Array Implementation of Binary Trees

This document discusses the array implementation of binary trees. It explains how a binary tree can be represented using a one-dimensional array by placing the root at index 0 and following a specific pattern to determine the indices of children and parents. It provides an example of a binary tree represented as an array and discusses the advantages and disadvantages of this implementation, including faster search time but wasted space for empty nodes. Finally, it includes C++ code to demonstrate creating and traversing a binary tree stored in an array.

Uploaded by

Ammar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Array Implementation of Binary Trees

This document discusses the array implementation of binary trees. It explains how a binary tree can be represented using a one-dimensional array by placing the root at index 0 and following a specific pattern to determine the indices of children and parents. It provides an example of a binary tree represented as an array and discusses the advantages and disadvantages of this implementation, including faster search time but wasted space for empty nodes. Finally, it includes C++ code to demonstrate creating and traversing a binary tree stored in an array.

Uploaded by

Ammar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

qwertyuiopasdfghjklzxcvbnmqw

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

Submitted to: Tariku Worku

mqwertyuiopasdfghjklzxcvbnmq
Due date: 2/10/2020

wertyuiopasdfghjklzxcvbnmqwe
rtyuiopasdfghjklzxcvbnmqwerty
uiopasdfghjklzxcvbnmqwertyuio
pasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmqwertyuiopasdfghjk
Array Implementation of Binary Trees

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

Page 1 of 5
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

Page 2 of 5
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;

Page 3 of 5
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');

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

You might also like