Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture No. 11
___________________________________________________________________
Data Structures
Lecture No. 11
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 4
4.1.1, 4.2.1
Summary
In the previous lecture, we demonstrated the technique of data structure priority queue
with the help of the example from the banking system. Data structure priority queue is
a variation of queue data structure to store the events. We have witnessed this
simulation by animation. The simulation and animation help a programmer to
understand the functionality of a program. As these are not the part of the current
course, so we will study simulation and animation in detail in the coming courses.
A queue is a FIFO structure (i.e. first in first out). But there are its variations including
the priority queue, discussed in the previous lecture. In priority queue, we were
adding data to a queue but did not get the data from the queue by First In, First Out
(FIFO) rule. We put events in the queue and got these from the queue with respect to
the time of occurrence of the event. Thus we get the items from a priority queue in a
particular order with respect to a priority given to the items (events). The priority
queue is also an important data structure. Lets see its implementation.
Tree
Now lets talk about a data structure called tree. This is an important data structure.
This data structure is used in many algorithms. We will use it in most of our
assignments. The data structures that we have discussed in previous lectures are linear
data structures. The linked list and stack are linear data structures. In these structures,
the elements are in a line. We put and get elements in and from a stack in linear order.
Queue is also a linear data structure as a line is developed in it. There are a number of
applications where linear data structures are not appropriate. In such cases, there is
need of some non-linear data structure. Some examples will show us that why nonlinear data structures are important. Tree is one of the non-linear data structures.
Look at the following figure. This figure (11.1) is showing a genealogy tree of a
family.
Page 4 of 18
T
.
Sohail Aslam
Haaris
Saad
Javed Aslam
Qasim
Asim
Yasmeen Aslam
Fahd
Ahmed
Sara
Omer
In this genealogy tree, the node at the top of the tree is Muhammad Aslam Khan i.e.
the head of the family. There are three nodes under this one. These are Sohail Aslam,
Javed Aslam and Yasmeen Aslam. Then there are nodes under these three nodes i.e.
the sons of these three family members. You may have seen the tree like this of some
other family. You can make the tree of your family too. The thing to be noted in this
genealogical tree is that it is not a linear structure like linked list, in which we have to
tell that who is the father and who is the son. We make it like a tree. We develop the
tree top-down, in which the father of the family is on the top with their children
downward. We can see the similar tree structure of a company. In the tree of a
company, the CEO of the company is on the top, followed downwardly by the
managers and assistant managers. This process goes downward according to the
administrative hierarchy of the company. Our tree structure is different from the actual
tree in the way that the actual tree grows from down to up. It has root downside and
the leaves upside. The data structure tree is opposite to the real tree as it goes upside
down. This top-down structure in computer science makes it easy to understand the
tree data structure.
There may be situations where the data, in our programs or applications, is not in the
linear order. There is a relationship between the data that cannot be captured by a
linked list or other linear data structure. Here we need a data structure like tree.
In some applications, the searching in linear data structures is very tedious. Suppose
we want to search a name in a telephone directory having 100000 entries. If this
directory is in a linked list manner, we will have to traverse the list from the starting
position. We have to traverse on average half of the directory if we find a name. We
may not find the required name in the entire directory despite traversing the whole
list. Thus it would be better to use a data structure so that the search operation does
not take a lot of time. Taking into account such applications, we will now talk about a
special tree data structure, known as binary tree.
Binary Tree
The mathematical definition of a binary tree is
A binary tree is a finite set of elements that is either empty or is partitioned into three
disjoint subsets. The first subset contains a single element called the root of the tree.
The other two subsets are themselves binary trees called the left and right sub-trees.
Each element of a binary tree is called a node of the tree.
Following figure shows a binary tree.
Page 5 of 18
Page 6 of 18
Root
Left subtree
Right subtree
Fig 11.3: Analysis of a binary tree
The same process of sub classing the tree can be done at the second level. That means
the left and right subtrees can also be divided into three parts similarly. Now consider
the left subtree of node A. This left subtree is also a tree. The node B is the root of this
tree and its left subtree is the node D. There is only one node in this left subtree. The
right subtree of the node B consists of two nodes i.e. E and G. The following figure
shows the analysis of the tree with root B.
Page 7 of 18
A
Root
B
Left subtree
Right subtree
Fig 11.4: Analysis of a binary tree
On going one step down and considering right subtree of node B, we see that E is the
root and its left subtree is the single node G. There is no right subtree of node E or we
can say that its right subtree is empty. This is shown in the following figure.
A
Root
Leftsubtree
Root
Right subtree
Fig 11.6: Analysis of a binary tree
Now we apply the same recursive definition on the level below the node C. Now the
right subtree of the node C will be considered as a tree. The root of this tree is the
node F. The left and right subtrees of this root F are the nodes H and I respectively.
The following figure depicts this.
Page 9 of 18
C
Root
Left subtree
Right subtree
Page 11 of 18
A
Left descendant
Right descendant
B
Leaf nodes
Leaf nodes
Fig 11.11: Terminologies used in a binary tree
Page 12 of 18
Page 13 of 18
Level
The level of a node in a binary tree is defined as follows:
Root has level 0,
Level of any other node is one more than the level its parent (father).
The depth of a binary tree is the maximum level of any leaf in the tree.
To understand level of a node, consider the following figure 11.13. This figure shows
the same tree of figure 11.2, discussed so far.
A
---------------------------------------- Level 0
D 2
1 ------------------- Level 1
2 ------- Level 2
-Level 3
H
3
J
3
K
3
L
3
M
3
N
3
O
3
Page 15 of 18
--------------
Level 2: 22 nodes
-----------------
O -
------------------------------------ Level 3: 23 nodes -------------------------------------------Fig 11.15: Number of nodes at each level in a complete binary tree
By observing the number of nodes at a particular level, we come to the conclusion
that the number of nodes at a level is equal to the level number raising to the power of
two. Thus we can say that in a complete binary tree at a particular level k, the number
of nodes is equal to 2k. Note that this formula for the number of nodes is for a
complete binary tree only. It is not necessary that every binary tree fulfill this
criterion. Applying this formula to each level while going to the depth of the tree (i.e.
d), we can calculate the total number of nodes in a complete binary tree of depth d by
adding the number of nodes at each level. This can be written as the following
summation.
20+ 21+ 22 + + 2d = d j=0 2j = 2d+1 1
Thus according to this summation, the total number of nodes in a complete binary tree
of depth d will be 2d+1 1. Thus if there is a complete binary tree of depth 4, the
total number of nodes in it will be calculated by putting the value of d equal to 4. It
will be calculated as under.
24+1 - 1 = 25 1 = 32 1 = 31
Thus the total number of nodes in the complete binary tree of depth 4 is 31.
We know that the total number of nodes (leaf and non-leaf) of a complete binary tree
of depth d is equal to 2d+1 1. In a complete binary tree, all the leaf nodes are at the
depth level d. So the number of nodes at level d will be 2d . These are the leaf nodes.
Thus the difference of total number of nodes and number of leaf nodes will give us
the number of non-leaf (inner) nodes. It will be (2 d+1 1) 2d i.e. 2d 1. Thus we
conclude that in a complete binary tree, there are 2d leaf nodes and 2d 1 non-leaf
(inner) nodes.
Page 16 of 18
Tips
A priority queue is a variation of queue that does not follow FIFO rule.
Tree is a non-linear data structure.
The maximum level of any leaf in a binary tree is called the depth of the tree.
Other than the root node, the level of each node is one more than the level of
its parent node.
A complete binary tree is necessarily a strictly binary tree but not vice versa.
At any level k, there are 2k nodes at that level in a complete binary tree.
The total number of nodes in a complete binary tree of depth d is 2d+1 1.
In a complete binary tree there are 2d leaf nodes and 2d 1 non-leaf nodes.
Page 17 of 18
Page 18 of 18