Trees - Data Structures
Trees - Data Structures
• Objectives:
– Describe different types of trees.
– Describe how a binary tree can be
represented using an array.
– Describe how a binary tree can be
represented using an linked lists.
– Implement various operations on binary tree.
Introduction
• Arrays, linked list ,stacks and queues are
used to represent the linear and tabular
data.
• These structure are not suitable for
representing hierarchical data.
• In hierarchical data we have
– ancestor, descendant
– Superior, subordinate etc.
Family structure
Jaspal
Gurpreet Navtej
Iqbal Jaspreet
Defence Telecomm
Finance Education Railways
node
d ge
e
Tree characteristics
• Consists of nodes connected by edges.
• Nodes often represent entities (complex
objects) such as people, car parts etc.
• Edges between the nodes represent the
way the nodes are related.
• Its easy for a program to get from one node
to another if there is a line connecting them.
• The only way to get from node to node is to
follow a path along the edges.
Tree Terminology
• Subtree: tree consisting
• Root: node without parent (A)
of a node and its
• Internal node: node with at least descendants
one child (A, B, C, F)
• External node (a.k.a. leaf ): node A
without children (E, I, J, K, G, H, D)
• Ancestors of a node: parent,
grandparent, grand-grandparent, B C D
etc.
• Depth of a node: number of
ancestors E F G H
• Height of a tree: maximum depth
of any node (3)
• Descendant of a node: child, I J K
grandchild, grand-grandchild, etc.
• Degree of an element: no. of
children it has
subtree
Tree Terminology
• Path: Traversal from node to node along the edges
results in a sequence called path.
• Root: Node at the top of the tree.
• Parent: Any node, except root has exactly one edge
running upward to another node. The node above it is
called parent.
• Child: Any node may have one or more lines running
downward to other nodes. Nodes below are children.
• Leaf: A node that has no children.
• Subtree: Any node can be considered to be the root of a
subtree, which consists of its children and its children’s
children and so on.
Tree Terminology
• Visiting: A node is visited when program control
arrives at the node, usually for processing.
• Traversing: To traverse a tree means to visit all
the nodes in some specified order.
• Levels: The level of a particular node refers to
how many generations the node is from the root.
Root is assumed to be level 0.
• Keys: Key value is used to search for the item
or perform other operations on it.
Binary Trees
• Every node in a binary tree
can have at most two
children.
• The two children of each
node are called the left child
and right child
corresponding to their
positions.
• A node can have only a left
child or only a right child or
it can have no children at
all.
Binary Trees
• A binary tree is a tree with the • Applications:
following properties: – arithmetic expressions
– Each internal node has at most two – decision processes
children (exactly two for proper – searching
binary trees)
– The children of a node are an A
ordered pair
• We call the children of an internal
node left child and right child
• Alternative recursive definition: a B C
binary tree is either
– a tree consisting of a single node, or
– a tree whose root has an ordered D E F G
pair of children, each of which is a
binary tree
H I
Arithmetic Expression Tree
• Binary tree associated with an arithmetic expression
– internal nodes: operators
– external nodes: operands
• Example: arithmetic expression tree for the
expression (2 (a 1) (3 b))
2 3 b
a 1
Compiling arithmetic expressions
• We can represent an arithmetic expression such as
(A + B) * (C + D) * 2 – X / Y
as a binary tree, in which the leaves are constants or
variables and the nodes are operations:
6
4 –
5
3 * /
* 2 X Y
1 + +
2 A post order traversal then gives us
the order in which the operations have
A B C D to be carried out
Some Types of Binary Trees
(continued)
Properties of Proper Binary Trees
• Notation • Properties:
n number of nodes – ei1
e number of external – n 2e 1
nodes
i number of internal – h i
nodes – h (n 1)2
h height – e 2h
Binary Search Trees
• Binary Search Trees:
A binary search tree T is a binary tree that may be empty.
A non empty binary search tree satisfies the following
properties:
1. Every element has a key (or value) and no two
elements have the same key i.e. all keys are unique.
2. The keys, if any, in the left subtree of the root are
smaller than the key in the node.
3. The keys, if any, in the right subtree of the root are
larger than the key in the node.
4. The left and right subtrees of the root are also binary
search trees.
BST
70 40 70
60 80 10 55 80
30 62 75 92 5 12 75 92
72
80
85
Fig. (d)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
70 72 80 85
Disadvantages of array
representation
• This scheme of representation is quite
wasteful of space when binary tree is
skewed or number of elements are small
as compared to its height.
• Array representation is useful only when
the binary tree is full or complete.
Linked representation
• The most popular and practical way of representing a
binary tree is using links (pointers).
70
60 80
X 30 X X 62 X X 75 X X 92 X
40
10 X 55 X
X 5 X X 12 X
X 70
80
X 75 X X 92 X
X 70
X 72
X 80
X 85 X
Linked Representation
• In linked representation, each element is represented by
a node that has exactly two link fields.
• Field names are left and right.
• Each node has data field called info.
The node structure is defined as:
struct nodetype
{
struct nodetype *left;
int info;
struct nodetype *right;
};
Implementation of binary tree will
consider following declarartion
typedef struct nodetype
{
struct nodetype *left;
int info;
struct nodetype *right;
}BST;
BST root;
Here , I have defined a new datatype and given it
name BST, then I have declared a pointer
variable root to represent the binary (search)
tree.
Common operation on binary and
binary search trees
Some of the operations that are commonly performed on
binary as well as binary search trees:
• Creating an empty tree.
• Traverse it
• Determine its height.
• Determine the number of elements in it.
• Determine the no of internal nodes i.e. non leaf
nodes.
• Determine the no of external nodes i.e. leaf nodes.
• Determine its mirror image.
• Remove it from memory.
Operations that are performed on
only with binary search trees
1. Insert a new node.
2. Search an element.
3. Find the smallest element.
4. Find the largest element.
5. Delete a node.
1. Creating an empty Binary
(Search) Trees
void createtree(BST **tree)
{
*tree=NULL;
}
2. Traverse a binary tree
Preorder ( depth first order )
1. Visit the root
Postorder
}
}
Example
• Pre-order: A B D E G H C F
• In order: D B G E H A C F
In- Order: B D G E H A C F
RTA
LTA
D B
C F
G E H
Pre- Order: B D E G H
In- Order: D B G E H
Example
Pre- Order: B D E G H
In- Order: D B G E H
B
RTA
C F
D
G RTB
E H
Pre- Order: E G H
In- Order: G E H
Example
Pre- Order: E G H
In- Order: G E H
B
RTA
C F
D E
G H
Pre- Order: C F
In- Order: C F
Example
Pre- Order: C F
In- Order: C F
B
C
D E F
G H
Determining the height of the
binary tree
• We find the height of the left sub tree and
right sub tree of given node.
• Height of the binary tree at a given node
will be equal to maximum height of the left
and right sub tree plus 1.
• Program in the next slide shows the
implementation of various steps required.
Determining the height of the binary
tree
Int height(BST *tree)
{
int lheight,rheight;
if(tree==NULL)
{
return 0;
}
else
{
lheight=height(tree->left);
rheight=height(tree->right);
if(lheight>rheight)
return ++lheight;
else
return ++rheight;
}
}
Determining number of
nodes/elements
• Any of the traversal scheme can be used
to determine the number of element.
• The approach we use that is
– Number of elements in the tree is equal to
number of nodes in left sub tree plus number
of nodes in right sub tree plus one.
Determining number of
nodes/elements
Int totalnodes(BST *tree)
{
if(tree==NULL)
return 0;
else
return (totalnodes(tree->left)+totalnodes(tree->right) + 1);
}
Inserting a New Element
• If the binary search tree is initially empty,
then the element is inserted as root node.
• Otherwise the element is inserted as
terminal node.
• If the element is less than the element in
the root node, then the element is inserted
in the left sub tree else right sub tree.
BST
50
25 75
20 40 60 80
10 30 45 65 85
2
9
5 7 10
2
9
5 7 10
2
9
5 7 10
25 75
20 40 60 80
10 30 45 65 85
25
20 40 60 80
10 30 45 65 85
25 80
20 40 60
10 30 45 65 85
Since node has both right and left child, if right sub tree is opted find the smallest
node, if left sub tree is opted find the largest node
BST
50
25 80
20 40 60 85
10 30 45 65
25 80
20 40 60 85
10 30 45 65
Before delete(4)
After delete(4)
After delete(2)
Before delete(2)
5 7 10
Removal…
*root *root
6 6
t
2 2
9 9
t right
5 7 10 5 7 10
Set t = t right
Removal…
• If the node to be removed has two
children, the general strategy is to replace
the data of this node with the smallest data
of the right subtree.
• Then the node with the smallest data is
now removed (this case is easy since this
node cannot have two children).
Removal…
Remove the 2 again…
*root *root
6 6
2
9
3 9
1 5 7 10 1 5 7 10
3 3
4 4
The Removal Operation
delBST(root, delkey)
1. if(empty tree)
return false
endif
2. If (delkey<root)
rerturn delBST(leftsubtree, delkey)
3. else if(delkey>root)
rerturn delBST(rightsubtree, delkey)
4. Else
if(no left subtree)
make right subtree the root
return true.
else if(no right subtree)
make left subtree the root
return true.
else
save root in deletenode
set largest to largestBST(left subtree)
move data in largest to deletenode
return delBST(left subtree of deletenode, key of the largest)
endif
endif
23 is to be deleted
find delkey
17
9 23
5 11 21 27
20 22
Find largest data
22 is the largest key on the left
subtree
17
9 23
5 11 21 27
20 22
Move largest data
17
9 22
5 11 21 27
20 22
Delete largest node
17
9 23
5 11 21 27
20
AVL TREES
• We can guarantee O(log2n) performance
for each search tree operation by ensuring
that the search tree height is always
O(log2n).
• Trees with a worst case height of O(log2n)
are called balanced trees.
• One of the popular balanced tree is AVL
tree, which was introduced by Adelson-
Velskii and Landis.
AVL TREES
If T is non empty binary tree with TL and TR
as its left and right sub tree, then T is an
AVL tree if and only if:
1. |hL-hR|<=1, where hL and hR are the
heights of TL and TR respectively.
2. TL and TR are AVL trees
AVL TREES
+1
0 70
70 0
60 -1 80
+1
0
60 80 0
0 62
0 0
30 75 92
0
0
8
8
-1
-1 -1
5 10 0
5 10
0 0 0
7 15 0 0
7 9 15
Neither the balance factor nor the height of the AVL tree is affected
AVL TREES
+1
0
8
8
-1
0 -1
5 10 -1
5 10
0
7 0 0
7 15
Height remains unchanged but the balance factor of the root gets
changed
AVL TREES
+2
+1
8
8 -1
0 0
0 5 10
5 10
0 +1
0 0 3 7
3 7
0
6
-2
35 0
0 60
-1
20 60 Rotate left 0
-1
35 65
0 -1
45 65
0 0 0
20 45 70
0
70
+2
35 0
+1 30
0
30 50 Rotate right +1
0
25 35
+1 0
25 33
0 0
10 33 50 0
0
10
Total height 4 0 0
32 10
0 Rotate right
23
+1
0
20 35
0 0
10 32 50 0
B C
X F G X D X E X
X H
Left threaded binary tree (one
way threading)
A
B C
X F G X D E X
H X
Fully threaded binary tree (two
way threading)
A
B C
X F G D E X
H
Representation of threaded
binary tree in memory
typedef struct nodetype
{
struct nodetype *left;
int info;
char thread;
struct nodetype *right;
}TBST;
TBST *root;
In this representation, we have used char field thread as a
tag. The character ‘0’ will used for normal right pointer
and character ‘1’ will be used for thread.
Heaps
Objectives:
• Describe a heap
• Describe how a heap can be represented
in memory
• Implement the various operations on heap
• Describe the applications of heaps
Introduction
Heap is binary tree that satisfies the following properties:
• Shape property
• Order property
By the shape property we mean that heap must be
complete binary tree.
By order property we mean that every node in the heap,
the value stored in that node is greater than or equal to
the value in each of its children. A heap that satisfy the
above property is known as max. heap.
if every node in the heap is less than or equal to the value
in each of its children. that heap is known as min. heap.
Heaps
20
50
25 27
40 35
33 35 40 50
25 20 27 33
5 20
25 20 27 33 27
6
7 33
40 35
25 20 27 33
Starting heap
33
40 35
25 20 27
40
33 35
25 20 27
50
40 35
25 20 27
Starting heap
Inserting an element into heap
50
40 35
25 20 27 90
Add value 90
Inserting an element into heap
50
40 90
25 20 27 35
90
40 50
25 20 27 35
3 70 5 70
4 15
15 12 35 50
5 12
6
35
7 50
Building a heap
10
5 70
15 12 35 50
1 2 3 4 5 6 7
10 5 70 15 12 35 50
Array
Building a heap
10
15 70
5 12 35 50
1 2 3 4 5 6 7
10 15 70 5 12 35 50
Array
Building a heap
70
15 50
5 12 35 10
1 2 3 4 5 6 7
70 15 50 5 12 35 10
Array
Build Heap
Heapify(a,n)
Here a is the linear array with size n. this algorithm
builds max. heap
Begin
set index=parent of the node with index n
for i=index to 1 by -1 do
call reheapify upward(a,i,n)
endfor
End
Build Heap
Void heapify(int a[], int n)
{
int i,index;
index=n/2;
for(i=index;i>=1;i--);
reheapifyUpward(a,i,n);
}
Heap Sort
• General approach of heap sort:
1. From the given array build the initial max heap.
2. Interchange the root (maximum) element with
the last element.
3. Use reheapify downward operation from the
root node to rebuild the heap of the size one
less then the starting.
4. Repeat steps 1 and 2 until there are no more
elements.
Algorithm
Heapsort(a,n)
begin
call heapify(a,n) //make the max heap
for i=n to 2 by -1 do
interchange elements a[1] and a[i]
call reheapify(a,1,i-1)
endfor
end
example
Initial elements are: 10, 5, 70, 15, 12, 35, 50
After heapify operation:
1 2 3 4 5 6 7
70 15 50 5 12 35 10
Swap 10 15 50 5 12 35 70
reheapifyDownward 50 15 35 5 12 10 70
Swap 10 15 35 5 12 50 70
reheapifyDownward 35 15 10 5 12 50 70
Swap 12 15 10 5 35 50 70
reheapifyDownward 15 12 10 5 35 50 70
Swap 5 12 10 15 35 50 70
reheapifyDownward 12 5 10 15 35 50 70
Swap 10 5 12 15 35 50 70
reheapifyDownward 10 5 12 15 35 50 70
Swap 5 10 12 15 35 50 70
Final sorted array 5 10 12 15 35 50 70
Heap sort
Heapsort(a,n)
Here a is the linear array of size n in memory. This
algorithm sorts this array in ascending order using heap
sort method.
Begin
call heapify(a,n)
for i=n to 2 by -1 do
interchange elements a[1] and a[i]
call reheapifydownward(a,1,i-1)
endfor
end
Heap sort
Void heapsort(int a[],int n)
{
int i,temp;
heapify(a,n);
for(i=n;i>1;i--)
{
temp=a[1];
a[1]=a[i];
a[i]=temp;
reheapifydownward(a,1,i-1);
}
}
Analysis of heap sort
• First , to build the heap from array takes O(log 2n)
operation.
• Inside the loop we call reheapify, the loop is
executed n-1 times, and in each iteration
element in the root is swapped with the last
element of reduced size heap.
• Swap plus reheapify is O(log2n). Multiplying the
activity by (n-1) iteration shows that the sorting
loop has complexity…
O(nlog2n)