Department of CSE
Second Year
Contents
Introduction to Trees,
Binary Trees,
Memory Representation of Binary Tree,
Traversing binary trees, Header nodes,
Binary Search Tree, Heap and heapsort,
Path length & Huffman’s algorithm.
Trees
Tree is data structure that is non linear and can be used to
represents data in hierarchy between those elements.
For example: Organization Structure,
Family Tree, the Tournament, records, and Table of
contents.
• A parent-child relationship
3
Types of Trees
There are various types of Trees Such as
Binary Tree
Complete Binary Tree
2 Tree or Extended Binary Tree
Binary Search Tree
Heap Tree
4
A Tree consist of nodes with a parent-child relationship.
● Tree data structure applications
Organization charts
File systems
Programing environment
5
Nodes
A Collection of entities called Nodes.
Tree is a Non-Linear Data Structure.
It’s a hierarchica Structure.
6
Relation Of Tree
Root-The top most Node. 1
2
Children
3
Parents
Siblings- Have same parents. 4 5 7
6 8
Leaf- Has no Child.
9 10 11
7
Tree is a sequence of nodes
There is a starting node known as a root node
Every node other than the root has a parent node.
Nodes may have any number of children
8
9
10
Some Key Terms
• Root − Node at the top of the tree is called root.
• Parent − Any node except root node has one edge upward to a node called parent.
• Child − Node below a given node connected by its edge downward is called its child node.
• Sibling – Child of same node are called siblings
• Leaf − Node which does not have any child node is called leaf node.
• Sub tree − Sub tree represents descendants of a node.
• Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child node is at level 1,
its grandchild is at level 2 and so on.
• Keys − Key represents a value of a node based on which a search operation is to be carried out for a node.
11
• Degree of a node:
• The degree of a node is the number of children of that node
• Degree of a Tree:
• The degree of a tree is the maximum degree of nodes in a given tree
• Path:
• It is the sequence of consecutive edges from source node to destination node.
• Height of a node:
• The height of a node is the max path length form that node to a leaf node.
• Height of a tree:
• The height of a tree is the height of the root
• Depth of a tree:
• Depth of a tree is the max level of any leaf in the tree
12
13
Characteristics of trees
Non-linear data structure
Combines advantages of an ordered array
Searching as fast as in ordered array
Insertion and deletion as fast as in linked list
Simple and fast
14
• A binary tree, is a tree in which no node can have more than two children.
• Consider a binary tree T, here ‘A’ is the root node of the binary tree T.
• ‘B’ is the left child of ‘A’ and ‘C’ is the right child of
‘A’
• i.e A is a father of B and C.
• The node B and C are called siblings.
• Nodes D,H,I,F,J are leaf node
15
Binary Trees
• A binary tree, T, is either empty or such that
I. T has a special node called the root node
II. T has two sets of nodes LT and RT, called the left subtree and right subtree of
T, respectively.
LT and RT are binary trees.
16
Binary Tree Cont.…
• A binary tree is a finite set of elements that are 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 of the original tree.
• A left or right sub-tree can be empty.
• Each element of a binary tree is called a node of the tree.
17
Binary Trees
The following figure shows a binary tree with 9 nodes where A is the root
18
• The root node of this binary tree is A.
• The left sub tree of the root node, which we denoted by LA, is the set
LA = {B,D,E,G} and the right sub tree of the root node,
RA is the set RA={C,F,H}
• The root node of LA is node B, the root node of RA is C and so on
19
Types of Binary Tree
Complete binary tree
Strictly binary tree
Almost complete binary tree
20
Strictly Binary Tree
• If every non-leaf node in a binary tree has nonempty left and right sub-trees, then
such a tree is called a strictly binary tree.
• Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero
or two, never degree one.
• A strictly binary tree with N leaves always contains 2N – 1 nodes.
21
Complete Binary Tree
• A complete binary tree is a binary tree in which every level, except possibly the last, is
completely filled, and all nodes are as far left as possible.
• The left child of node K is 2*K and the right child is 2*K+1 and parent is floor of K/2
2 3
4 5 6 7
11
8 9 10
22
Almost Complete Binary Tree
• An almost complete binary tree is a tree where for a right child, there is always a
left child, but for a left child there may not be a right child.
23
Tree Traversal
• Traversal is a process to visit all the nodes of a tree and may print their values too.
• All nodes are connected via edges (links) we always start from the root (head)node.
• There are three ways which we use to traverse a tree
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
• Generally we traverse a tree to search or locate given item or key in the tree
or to print all the values it contains.
24
Pre-order, In-order, Post-order
• Pre-order
<root> <left> <right>
• In-order
<left> <root> <right>
• Post-order
<left> <right> <root>
25
Pre-order Traversal
• The preorder traversal of a nonempty binary tree is defined as follows:
• Visit the root node
• Traverse the left sub-tree in preorder
• Traverse the right sub-tree in preorder
26
Example of Pre-order traversal
27
In-Order traversal
• The in-order traversal of a nonempty binary tree is defined as follows:
• Traverse the left sub-tree in in-order
• Visit the root node
• Traverse the right sub-tree in inorder
• The in-order traversal output of the given tree is
H D I B EAF C G
E A
H D I B F
C G
28
Example of In-order traversal
29
Post-order traversal
• The post-order traversal of a nonempty binary tree is defined as follows:
• Traverse the left sub-tree in post-order
• Traverse the right sub-tree in post-order
• Visit the root node
• The post-order traversal
output of the given tree is
HIDEBFGCA
30
Example of Post-order traversal
31
Binary Search Tree
• Binary Search Tree works as Binary Search Algorithm
• If value of inserted node is bigger than parent then it will be right subtree.
• If value of inserted node is smaller than parent then it will be left subtree.
• This tree is known as binary search tree.
32
H will be root Example: H A KCBLJ
A<H:
A will be left child of H
K>H:
K will be right child of H H
C < H C > A :
A K
C will be right child of A
B < H B > A B < C :
C J L
B will be left child of C L > H L > K :
L will be right child of K
B
J > H J < K :
J will be left child of K
33
Example
• Suppose the following list of letters are inserted in order into an empty
Binary Search Tree T .
• J R D G T E M H P A F Q
• Find the Final Tree T
• Find the Inorder traversal of T
Soln. – A tree T is a Binary Search Tree if each node N of T has the value at N
greater than every value in Left subtree of N & is less than every value in the
right subtree of N.
34
J R > J go to Right of J
D < J go to Left of J
D R
H < J go to left of J and H > D , D
G < J go to Left of J and G> D , D
is a Left root go to right in right H> G so
insert H as right child of G A
G M T
is a Left root
T > J go to right of J and T > R , R
F < J go to left of J and F > D , D is a Left
root again F< G , which is E H P is a right root
right child of D, go to left then E < J go to left of J and E > D , D is a Left
here F> E which is the left child of G so Q root again E< G , which is right child of D,
F
insert F at right of E insert E at Left of G
A < J go to left of J and A < D , D is a left root , insert A as the left child M > J go to right of J and M< R , R
of D is a right root , so insert it to the left of R
P > J go to right of J and P < R , R is a right root , go to the left where
P>M which is the Root so insert it to the Right of M
Q > J go to right of J and Q < R , R is a right root , go to the left where
Q > M which is the Root again Q > P so insert it to the Right of P
35
J
D R
A
G M T
P
E H
Q
D
F
A
PQ R
G H T
F J
E M
• The inorder Traversal is : A D E F G H J M P Q R T
36
Binary Search Tree
Draw the final tree T if node 20 is added 50 Compare ITEM with the root node N of the tree
to T
20 < 50 go to left
ITEM =20 25
75
Again compare 20 with 25 20 is less than 25
60
22 40 90
Go to left
At left there is 22 , here 20 is less than 22 again
15
30 44
go to left
20 At left there is 15
20 > 15 and no child is there for 15 so insert 20 as
the right child of 20
37
Extended Binary Tree
• A Binary Tree T is said to be 2 Tree or an Extended Binary Tree if each node
N has either 0 or 2 children.
• The nodes with 2 children are called Internal Nodes and the nodes with 0
children are called as External Nodes.
• Sometimes the nodes are distinguish in diagram by using circles from
Internal Nodes and squares for External Nodes. J
J
D R
D R
A
A G M T
G M T
H
H
Binary Tree T Extended 2 Tree
38
Representing Binary Tree in Memory
• A Binary Tree T is represented in two ways in memory.
• The first way is called the link representation of tree &
• another is single array called sequential representation of T.
One node in binary
tree
Left Data Field Right
Child (Info) Child
Link Representation of tree
39
Binary Tree Linked Representation
Binary Tree Binary Tree
(Linked List)
A Root
A
B
D C B
D C
E F
E F
I G
I G
H
H
40
Sequential representation of Binary Tree
• Suppose T is a binary tree which is complete or nearly complete. Then there
is an efficient way of maintaining T in memory called Sequential
Representation,
• The root is TREE[1]
• If a node N occupies TREE[K] , then left child is stored in TREE[2*K]
• right child is stored in TREE[2*K+1]
45
22 77
11
30 90
15 25 88
Binary Tree T
41
Array representation of Binary Tree
1 45
2 22
45
3 77
22 77
4 11
5 30
11
30 90 6
7 90
15 25 88
8 15
Binary Tree T 9
10 25
11
12
13
14 88
15
16
42
What is a Heap?
A heap is also known as a priority queue and can be
represented by a binary tree with the following properties:
Structure property: A heap is a completely filled binary tree with the
exception of the bottom row, which is filled from left to right
Heap Order property: For every node x in the heap, the parent of x greater
than or equal to the value of x.
(known as a maxHeap).
43
Example:
a heap 53
44 25
15 21 13 18
3 12 5 7
44
Heap data structure
• Binary tree
• Balanced
• Left-justified or Complete
• (Max) Heap property: no node has a value
greater than the value in its parent
45
Balanced binary trees
• Recall:
– The depth of a node is its distance from the root
– The depth of a tree is the depth of the deepest node
• A binary tree of depth n is balanced if all the
nodes at depths 0 through n-2 have two children
n-2
n-1
n
Balanced Balanced Not balanced
46
Left-justified binary trees
• A balanced binary tree of depth n is left-
justified if:
– it has 2n nodes at depth n (the tree is “full”), or
– it has 2k nodes at depth k, for all k < n, and all
the leaves at depth n are as far left as possible
Left-justified Not left-justified
47
Building up to heap sort
• How to build a heap
• How to maintain a heap
• How to use a heap to sort data
48
The heap property
• A node has the heap property if the value in
the node is as large as or larger than the
values in its children
12 12 12
8 3 8 12 8 14
Red node has Red node has Red node does not
heap property heap property have heap property
• All leaf nodes automatically have the heap
property
• A binary tree is a heap if all nodes in it have
49
siftUp
• Given a node that does not have the heap property, you
can give it the heap property by exchanging its value with
the value of the larger child
12 14
8 14 8 12
Blue node does not Blue node has
have heap property heap property
• This is sometimes called sifting up
50
Constructing a heap I
• A tree consisting of a single node is automatically
a heap
• We construct a heap by adding nodes one at a time:
– Add the node just to the right of the rightmost node in
the deepest level
– If the deepest level is full, start a new level
• Examples:
Add a new Add a new
node here node here
51
Constructing a heap II
• Each time we add a node, we may destroy the heap property of its
parent node
• To fix this, we sift up
• But each time we sift up, the value of the topmost node in the sift
may increase, and this may destroy the heap property of its parent
node
• We repeat the sifting up process, moving up in the tree, until either
– We reach nodes whose values don’t need to be swapped (because the
parent is still larger than both children), or
– We reach the root
52
Constructing a heap III
8 8 10 10
10 8 8 5
1 2 3
10 10 12
8 5 12 5 10 5
12 8 8
4
53
Other children are not affected
12 12 14
10 5 14 5 12 5
8 14 8 10 8 10
• The node containing 8 is not affected because its parent gets larger, not
smaller
• The node containing 5 is not affected because its parent gets larger, not
smaller
• The node containing 8 is still not affected because, although its parent got
smaller, its parent is still greater than it was originally
54
A sample heap
• Here’s a sample binary tree after it has been heapified
25
22 17
19 22 14 15
18 14 21 3 9 11
• Notice that heapified does not mean sorted
• Heapifying does not change the shape of the binary tree;
this binary tree is balanced and left-justified because it
started out that way
55
Removing the root (animated)
• Notice that the largest number is now in the root
• Suppose we discard the root:
11
22 17
19 22 14 15
18 14 21 3 9 11
• How can we fix the binary tree so it is once again balanced
and left-justified?
• Solution: remove the rightmost leaf at the deepest level
and use it for the new root
56
The reHeap method I
• Our tree is balanced and left-justified, but no longer a heap
• However, only the root lacks the heap property
11
22 17
19 22 14 15
18 14 21 3 9
• We can siftDown() the root
• After doing this, one and only one of its children may have
lost the heap property
57
The reHeap method II
• Now the left child of the root (still the number 11) lacks
the heap property
22
11 17
19 22 14 15
18 14 21 3 9
• We can siftDown() this node
• After doing this, one and only one of its children may have
lost the heap property
58
The reHeap method III
• Now the right child of the left child of the root (still the
number 11) lacks the heap property:
22
22 17
19 11 14 15
18 14 21 3 9
• We can siftDown() this node
• After doing this, one and only one of its children may have
lost the heap property —but it doesn’t, because it’s a leaf
59
The reHeap method IV
• Our tree is once again a heap, because every node in it
has the heap property
22
22 17
19 21 14 15
18 14 11 3 9
• Once again, the largest (or a largest) value is in the root
• We can repeat this process until the tree becomes empty
• This produces a sequence of values in order largest to smallest
60
Sample Run
• Start with unordered array of data
Array representation:
21 15 25 3 5 12 7 19 45 2 9
Binary tree representation:
21
15 25
3 5 12 7
19 45 2 9
61
Sample Run
• Heapify the binary tree - 21 21
21
15 25 15 25
15 25
3 5 12 7 3 9 12 7 45 9 12 7
19 45 2 9 19 45 2 5 19 3 2 5
45 21 21
21 25 45 25 15 25
19 9 12 7 19 9 12 7 45 9 12 7
15 3 2 5 15 3 2 5 19 3 2 5
62
Step 2 – perform n – 1 deleteMax(es), and replace last element in heap with
first, then re-heapify. Place deleted element in the last nodes position.
45 25
21 25 21 12
19 9 12 7 19 9 5 7
15 3 2 5 15 3 2
45 21 25 19 9 12 7 15 3 2 5 25 21 12 19 9 5 7 15 3 2 45
25 21
21 12 19 12
19 9 5 7 15 9 5 7
15 3 2 2 3
25 21 12 19 9 5 7 15 3 2 45 21 19 12 15 9 5 7 2 3 25 45
21 19
19 12 15 12
15 9 5 7 3 9 5 7
2 3 2
21 19 12 15 9 5 7 2 3 25 45 19 15 12 3 9 5 7 2 21 25 45
19 15
15 12 9 12
3 9 5 7 3 2 5 7
2
19 15 12 3 9 5 7 2 21 25 45 15 9 12 3 2 5 7 19 21 25 45
15 12
9 12 9 7
3 2 5 7 3 2 5
15 9 12 3 2 5 7 19 21 25 45 12 9 7 3 2 5 15 19 21 25 45
12 9
9 7 5 7
3 2 3 2
5
9 5 7 3 2 12 15 19 21 25 45
12 9 7 3 2 5 15 19 21 25 45
…and finally 2 3 5 7 9 12 15 19 21 25 45
Graph
Cont..
67
68
69
1