Lecture No.
06
Data Structures
Tree Data Structures
• There are a number of applications where
linear data structures are not
appropriate.
• Consider a genealogy tree of a family.
Aslam Khan
Sohail Aslam Javed Aslam Yasmeen Aslam
Haaris Saad Qasim Asim Fahd Ahmad Sara Ome
r
Tree Data Structure
• A linear linked list will not be able
to capture the tree-like
relationship with ease.
• Shortly, we will see that for
applications that require
searching, linear data structures
are not suitable.
• We will focus our attention on
binary trees.
Binary Tree
• 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
subtrees.
• Each element of a binary tree is called
a node of the tree.
Binary Tree
• Binary tree with 9 nodes.
A
B C
D E F
G H I
Binary Tree
root
B C
D E F
G H I
Left subtree Right subtree
Binary Tree
• Recursive definition
A
root
B C
D E F
Left subtree G H I
Right subtree
Binary Tree
• Recursive definition
A
B C
root
D E F
G H I
Left subtree
Binary Tree
• Recursive definition
A
B C
D E F
root
G H I
Binary Tree
• Recursive definition
A
root
B C
D E F
G H I
Right subtree
Binary Tree
• Recursive definition
A
B C
root
D E F
G H I
Left subtree Right subtree
Not a Tree
• Structures that are not trees.
A
B C
D E F
G H I
Not a Tree
• Structures that are not trees.
A
B C
D E F
G H I
Not a Tree
• Structures that are not trees.
A
B C
D E F
G H I
Binary Tree: Terminology
parent
A
Left descendant B C Right descendant
D E F
G H I
Leaf nodes Leaf nodes
Binary Tree
• If every non-leaf node in a binary tree has non-empty left
and right subtrees, the tree is termed a strictly binary tree.
B C
D E J F
G K H I
Level of a Binary Tree
Node
• 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.
Level of a Binary Tree
Node
A 0 Level 0
B 1 C 1 Level 1
D 2 E 2 F 2 Level 2
G 3 H 3 I 3 Level 3
Complete Binary Tree
• A complete binary tree of depth d is the strictly
binary all of whose leaves are at level d.
0
A
B 1 C 1
D 2 E 2 F 2 G 2
H 3 I J 3 K L 3 M3 N 3 O 3
Complete Binary Tree
A Level 0: 20 nodes
B C Level 1: 21 nodes
D E F G Level 2: 22 nodes
H I J K L M N O Level 3: 23 nodes
Complete Binary Tree
• At level k, there are 2k nodes.
• Total number of nodes in the tree of
depth d:
d
2 + 2 + 2 + ………. + 2 = 2j = 2d+1 –
0 1 2 d
1 j=
0
• In a complete binary tree, there are
2d leaf nodes and (2d - 1) non-leaf
(inner) nodes.
Complete Binary Tree
• If the tree is built out of ‘n’ nodes then
n = 2d+1 – 1
or log2(n+1) = d+1
or d = log2(n+1) – 1
• I.e., the depth of the complete binary tree
built using ‘n’ nodes will be log2(n+1) – 1.
• For example, for n=1,000,000,
log2(1000001) is less than 20; the tree
would be 20 levels deep.
• The significance of this shallowness will
become evident later.
Operations on Binary Tree
• There are a number of operations that
can be defined for a binary tree.
• If p is pointing to a node in an
existing tree then
left(p) returns pointer to the left subtree
right(p) returns pointer to right subtree
parent(p) returns the father of p
brother(p) returns brother of p.
info(p) returns content of the node.
Operations on Binary Tree
• In order to construct a binary
tree, the following can be useful:
• setLeft(p,x) creates the left child
node of p. The child node contains
the info ‘x’.
• setRight(p,x) creates the right
child node of p. The child node
contains the info ‘x’.
Applications of Binary
Trees
• A binary tree is a useful data structure
when two-way decisions must be
made at each point in a process.
• For example, suppose we wanted to
find all duplicates in a list of numbers:
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Applications of Binary
Trees
• One way of finding duplicates is
to compare each number with all
those that precede it.
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
• If the list of numbers is large and is
growing, this procedure involves a
large number of comparisons.
• A linked list could handle the growth
but the comparisons would still be
large.
• The number of comparisons can be
drastically reduced by using a binary
tree.
• The tree grows dynamically like the
linked list.
Searching for Duplicates
• The binary tree is built in a special
way.
• The first number in the list is placed in
a node that is designated as the root
of a binary tree.
• Initially, both left and right subtrees of
the root are empty.
• We take the next number and compare
it with the number placed in the root.
• If it is the same then we have a
duplicate.
Searching for Duplicates
• Otherwise, we create a new tree
node and put the new number in it.
• The new node is made the left
child of the root node if the
second number is less than the
one in the root.
• The new node is made the right
child if the number is greater than
the one in the root.
Searching for Duplicates
14
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
15 14
15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
15
15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
4 14
15
4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4 15
4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
9 14
4 15
9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4 15
9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
7 14
4 15
7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4 15
7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
18 14
4 15
18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4 15
9 18
18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
3 14
4 15
9 18
3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4 15
3 9 18
3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
5 14
4 15
3 9 18
5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4 15
3 9 18
5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
16 14
4 15
3 9 18
16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4 15
3 9 18
7 16
16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
4 14
4 15
3 9 18
7 16
4, 20, 17, 9, 14, 5
Searching for Duplicates
20 14
4 15
3 9 18
7 16
20, 17, 9, 14, 5
Searching for Duplicates
14
4 15
3 9 18
7 16 20
20, 17, 9, 14, 5
Searching for Duplicates
17 14
4 15
3 9 18
7 16 20
17, 9, 14, 5
Searching for Duplicates
14
4 15
3 9 18
7 16 20
5 17
17, 9, 14, 5
Searching for Duplicates
14
4 15
3 9 18
7 16 20
5 17
9, 14, 5
C++ Implementation
#include <stdlib.h>
template <class Object>
class TreeNode {
public:
// constructors
TreeNode()
{
this->object = NULL;
this->left = this->right = NULL;
};
TreeNode( Object* object )
{
this->object = object;
this->left = this->right = NULL;
};
C++ Implementation
Object* getInfo()
{
return this->object;
};
void setInfo(Object* object)
{
this->object = object;
};
TreeNode* getLeft()
{
return left;
};
void setLeft(TreeNode *left)
{
this->left = left;
};
C++ Implementation
TreeNode *getRight()
{
return right;
};
void setRight(TreeNode *right)
{
this->right = right;
};
int isLeaf( )
{
if( this->left == NULL && this->right == NULL )
return 1;
return 0;
};
C++ Implementation
private:
Object* object;
TreeNode* left;
TreeNode* right;
}; // end class TreeNode
C++ Implementation
#include <iostream>
#include <stdlib.h>
#include "[Link]"
int main(int argc, char *argv[])
{
int x[] = { 14, 15, 4, 9, 7, 18, 3, 5, 16,4, 20, 17,
9, 14,5, -1};
TreeNode<int>* root = new TreeNode<int>();
root->setInfo( &x[0] );
for(int i=1; x[i] > 0; i++ )
{
insert(root, &x[i] );
}
}
C++ Implementation
void insert(TreeNode<int>* root, int* info)
{
TreeNode<int>* node = new TreeNode<int>(info);
TreeNode<int> *p, *q;
p = q = root;
while( *info != *(p->getInfo()) && q != NULL )
{
p = q;
if( *info < *(p->getInfo()) )
q = p->getLeft();
else
q = p->getRight();
}
C++ Implementation
if( *info == *(p->getInfo()) ){
cout << "attempt to insert duplicate: "
<< *info << endl;
delete node;
}
else if( *info < *(p->getInfo()) )
p->setLeft( node );
else
p->setRight( node );
} // end of insert
Trace of insert
p
17 q 14
4 15
3 9 18
7 16 20
17, 9, 14, 5
Trace of insert
p
17 14
4 q 15
3 9 18
7 16 20
17, 9, 14, 5
Trace of insert
17 14
4 p 15
q
3 9 18
7 16 20
17, 9, 14, 5
Trace of insert
17 14
4 p 15
3 9 q 18
7 16 20
17, 9, 14, 5
Trace of insert
17 14
4 15
3 9 p 18
q
7 16 20
17, 9, 14, 5
Trace of insert
17 14
4 15
3 9 p 18
7 q 16 20
17, 9, 14, 5
Trace of insert
17 14
4 15
3 9 18
7 p 16 20
q
5
17, 9, 14, 5
Trace of insert
17 14
4 15
3 9 18
7 p 16 20
5 q
17, 9, 14, 5
Trace of insert
14
4 15
3 9 18
7 p 16 20
5 node 17
17, 9, 14, 5 p->setRight( node );