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

Week 7

Uploaded by

23ecuos117
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Week 7

Uploaded by

23ecuos117
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 150

EL

P T
Data Structures and Algorithms Using Java
Debasis Samanta

N
Department of Computer Science & Engineering, IIT Kharagpur

Module 08: Trees


Lecture 31 : Height Balanced Binary Search Tree
Ø Issues with Binary Search Tree

EL
T
Ø Definition of Height-Balanced Binary Search Tree

P
Ø AVL Rotations

N
Ø Left-to-Left Rotation
Ø Right-to-Right Rotation
Ø Left-to-Right Rotation
Ø Right-to-Left Rotation
EL
T
Issues with BST

P
N
Definition: Average search time calculation

• Average search time in a binary search tree:

L
n

åt

E
i
i =1
G=

T
n

P
ti = Number of comparisons for the i-th element

N
n = Total number of elements in the binary search tree

A binary search tree should be with a minimum value of average search time.
Case 1: Average search time calculation

L
apr
aug
dec

E
feb
jan
jul

T
jun
mar

P
may
nov
oct

N
sep

A skewed binary tree obtained from lexicographical order of data

G = 6.50
Case 2 & 3: Average search time calculations

jun jan

L
mar

E
jul
feb mar
jan may

T
apr jun may
feb nov

P
sep
oct aug jul
dec

N
oct
sep
aug dec
nov
apr

A binary search tree ( half skewed version) A binary search tree (obtained by inserting the
data into the order of months ).

G = 4.00 G = 3.50

12.08.09 IT 60101: Lecture #8 6


Case 4 & 5: Average search time calculations

L
mar jul

E
feb oct feb may

T
aug jul nov sep aug mar oct

P
jan

jan jun may jun nov sep

N
apr dec apr dec
A binary search tree obtained from a given
Binary search tree in the form of a complete
binary tree random ordering of data.

G = 3.08 G = 3.08

12.08.09 IT 60101: Lecture #8 7


Case 6: Average search time calculation

jan

dec

EL mar

T
aug feb jul nov

P
jun may oct

N
apr
sep

A binary search tree obtained by a special technique

G = 3.16

12.08.09 IT 60101: Lecture #8 8


Problem: Finding the best BST
• How to find a binary search tree with a minimum value of average search time ?

L
• Solution

E
• Height balanced binary search tree
• Also called AVL tree (Adelson-Velsky and Landis)

T
• Concept of balance factor of a node

P
bf = Height of the left sub-tree (hL) – Height of the right sub-tree (hR)

N
bf

hL

hR

12.08.09 IT 60101: Lecture #8 9


EL
T
Definition of Height-Balanced

P
Binary Search Tree

N
Definition: Height-balanced BST

• Definition

L
A binary search tree is said to be height balanced binary search tree

E
if all its nodes have a balance factor of 1, 0 or –1.

T
That is, for all nodes |bf| = |hL – hR| ≤ 1

N P
12.08.09 IT 60101: Lecture #8 11
Example: Height-balanced BST

L
1 2
6 6

E
1 0

T
2 8 2 8

P
0 0 0
1 0
1 4 7 1 4

N
0 0 0
3 3 5

(a) Height balanced (b) Height unbalanced

12.08.09 IT 60101: Lecture #8 12


Making a BST height-balanced

• Steps

EL
• Insert node into a binary search tree

T
• Compute the balance factors

P
• Decide the pivot node

N
• Balance the unbalance tree

12.08.09 IT 60101: Lecture #8 13


Making height-balanced BST: AVL rotations

• AVL Rotations
• G.M. Adelson-Velskii and E.M. Lendis (1962)

• Case 1: Left-to-Left rotation


• pivotàleft-sub treeàleft child

EL
T
• Case 2: Right-to-Right rotation

P
• pivotàright-sub treeàright child

N
• Case 3: Left-to-Right rotation
• pivotàleft-sub treeàright child

• Case 4: Right-to-Left rotation


• pivotàright-sub treeàleft child

12.08.09 IT 60101: Lecture #8 14


EL
T
Left-to-Left Rotation

P
N
AVL rotation: Left-to-Left

L
P A

E
A P

T
P A
R L

P
AL A A P
R R R

N
After insertion in
the left sub-tree of
the left child of pivot node (P)
After rotation

12.08.09 IT 60101: Lecture #8 16


Example: Left-to-Left AVL rotation

L
2 1
8

E
1
8
2 1
1 0
0 6 10

T
6 10
1 0
0 0 0 0

P
0 0 0 4 7 9 11
4 7 11
9
1
0
0

N
0
0 3
3 5
5
0
2
(a) A height balanced binary tree as |bf| 1 (b) After the insertion of 2 into the tree

12.08.09 IT 60101: Lecture #8 17


Example: Left-to-Left AVL rotation

L
P
8

E
8
A
6 10 4 10

T
0

P
4 7 11 3 6
9 9 11
P
0 R

N
3 5 2 5 7
A
L
P
2 A A R
R R
A
L

(c) AVL-rotation as per the LEFT-TO-LEFT (d) After AVL-rotation


(Case 1) insertion

12.08.09 IT 60101: Lecture #8 18


Example: Left-to-Left AVL rotation

L
1

E
8
0
0

T
4 10

P
1 0 0
0
3 6 9 11

N
0 0 0
2 5 7

(e) Height balanced tree with newly inserted


node 2 and after AVL-rotation

12.08.09 IT 60101: Lecture #8 19


EL
T
Right-to-Right Rotation

P
N
AVL rotation: Right-to-Right

L
P B

E
B P

T
P B
L
R

P
B B P B
L R L L

N
After insertion in the right sub-tree
of the right child of the pivot node (P)
After rotation

12.08.09 IT 60101: Lecture #8 21


Example: Right-to-Right AVL rotation

L
-1 -2 -1
7 P 7 B

E
0 -1 -2 0
5 9 P 0
5 11
B

T
0 -1
0 0 0 0
8 0 0
4 6 11 4 6 9

P
0 13
10 0 0
P 13 0
L 8 10

N
0 12
BL
12 B BR
BR P L
L

(a) 12 is inserted which makes the tree unbalanced (b) After AVL rotation

12.08.09 IT 60101: Lecture #8 22


EL
T
Left-to-Right Rotation

N P
AVL rotation: Left-to-Right

• Also called double rotation

L
• Rotation 1 (Right-to-Left)
• Left sub-tree (BL) of the right child (B) of the left child of the pivot node (P)

E
becomes the right sub-tree of the left child (A).
• Left child (A) of the pivot node (P) becomes the left child of B.

T
• Rotation 2 (Left-to-Right)

P
• Right sub-tree (BR) of the right child (B) of the left child (A) of the PIVOT node
(P) becomes the left sub-tree of P.

N
• P becomes the right child of B.

12.08.09 IT 60101: Lecture #8 24


AVL rotation: Left-to-Right

L
P B

E
2
A A P

T
P
1 R
B
B

P
AL AL L
B P
R R
B B
L R

N
After insertion in the right sub-tree
of the left child of the pivot node P After rotation

12.08.09 IT 60101: Lecture #8 25


Example: Left-to-Right AVL rotation

L
2 1
8

E
1
8
-1 0
0 0
0 10

T
10 3
3
-1 0

P
0 1 0 0
1 0 0 2
2 5 9 11
5 9 11

N
0 -1
0 0 0 0
0 1
1 4 4 6
6
0
(a) A height balanced tree 7

(b) Insertion of 7 made the tree as unbalanced


Balance factors are recomputed

12.08.09 IT 60101: Lecture #8 26


Example: Left-to-Right AVL rotation

L
A 8

E
0 5
3 B 10
3 8

T
0
0
2 9 11
5

P
2 4 6 10
PR
1
4 6 1

N
A 1 9
A B 11
L 0 L L
B
7 R PR
B B
L
R
(d) After AVL-rotation
(c) Node 8 becomes the PIVOT node

12.08.09 IT 60101: Lecture #8 27


EL
T
Right-to-Left Rotation

P
N
AVL rotation: Right-to-Left

• It is also another double rotation

L
• Rotation 1(Left-to-Right)

E
• Right sub-tree (BR) of the left child (B) of the right child (A) of the pivot node
(P) becomes the left sub-tree of A.

T
• Right child (A) of the pivot node (P) becomes the right child of B.

P
• Rotation 2 (Right-to-Left)

N
• Left sub-tree (BL) of the right child (B) of the right child (A) of the pivot rode
(P) becomes the right sub-tree of P.
• P becomes the left child of B.

12.08.09 IT 60101: Lecture #8 29


AVL rotation: Right-to-Left

P B

P
2
A

ELP A

T
L 1
B
B

P
A PL B R A
R L R
B B
L R

N
After insertion in the left sub-tree
the right child of the pivot node (P)
After rotation

12.08.09 IT 60101: Lecture #8 30


Example: Right-to-Left AVL rotation

-2

L
-1
7
7 -2 P
-1

E
0 0 5 11
9
5 A

T
0 0 2
0 0 4 9 12
8 6
4 6 12

P
P 0 1 13
1 0 10
L 11 13 8

N
P
L A
BL B
R R
A
R
10
(b) After AVL rotations

BL B
R

(a) When 10 is inserted into the tree

12.08.09 IT 60101: Lecture #8 31


Example: Right-to-Left AVL rotation

-1

L
7
0

E
0
5 11

T
0 0 -1
0

P
4 9 12
6

N
0
0 0
10 13
8

(c) Final height balanced tree after AVL rotation

12.08.09 IT 60101: Lecture #8 32


Algorithms: AVL rotations

L
For tricky ways to manage the pointers see the book

E
Classic Data Structures
Chapter 7

T
PHI, 2nd Edn., 30th Reprint

N P
12.08.09 IT 60101: Lecture #8 33
Height of an AVL tree

• Maximum height possible in an AVL tree with n number of

L
nodes is given by

E
hmax = 1.44 log 2 n

P T
N
For the proof of this property see the book
Classic Data Structures
Chapter 7
PHI, 2nd Edn., 30th Reprint

12.08.09 IT 60101: Lecture #8 34


EL
P T
For more discussion on the tree data

N
structures see the book
Classic Data Structures
Chapter 7
Prentice Hall of India, 2nd Ed.
EL
PT
N
EL
P T
Data Structures and Algorithms Using Java
Debasis Samanta

N
Department of Computer Science & Engineering, IIT Kharagpur

Module 08: Trees


Lecture 32 : Heap Tree
EL
P T
Ø Definition of Heap Tree
Ø Creating Heap Tree

N
Ø Deleting Node from Heap Tree
Ø Applications of Heap Tree
Ø Sorting
Ø Priority queue
EL
T
Definition of Heap Tree

P
N
Definition: Binary (max) heap tree

L
• A heap tree, say H, is a complete binary tree. H will be termed as heap tree, if it

E
satisfies the following properties:

T
• (i) For each node N in H, the value at N is greater than or equal to the value of
each of the children of N.

P
• (ii) Or in other words, N has the value which is greater than or equal to the value

N
of every successor of N.

• Such a heap tree is called max heap.

05.08.09 IT 60101: Lecture #6 4


Definition: Binary (min) heap tree

L
• A heap tree, say H, is a complete binary tree. H will be termed as heap tree, if it

E
satisfies the following properties:

T
• (i) For each node N in H, the value at N is less than or equal to the value of each
of the children of N.

P
• (ii) Or in other words, N has the value which is less than or equal to the value of

N
every successor of N.

• Such a heap tree is called min heap.

05.08.09 IT 60101: Lecture #6 5


Example: Heap trees

L
95 15

E
85 45 25

T
45

65

P
25 75
35 15 35
75 55

N
55 65 85 95
(a) Max heap (b) Min heap

05.08.09 IT 60101: Lecture #6 6


EL
T
Storing a Heap Tree

N P
Storing a heap tree: Array representation

L
15

E
45 25

T
65
35 75
55

P
85 95

N
(b) Min heap

1 2 3 4 5 6 7 8 9 10 11 12 13 14

15 45 25 55 65 35 75 85 95 . . . . . .

05.08.09 IT 60101: Lecture #6 8


EL
T
Creating a Heap Tree

P
N
EL
T
Insertion Operation

N P
Inserting a node into a max heap: Case 1

L
95 95

E
85 85 45
45

T
75 25 35 15
25 35 15

P
75

55 55 65 19
65

N
(a) Max heap (b) Inclusion of 19 in the fashion of complete
binary tree and it satisfy the property of heap

Case 1 : A trivial case

05.08.09 IT 60101: Lecture #6 11


Inserting a node into a max heap: Case 2

EL
95 111 111
85 111 95 45
95 45

T
111
75 25 85 35 15 75 85 35 15

P
55 65 19 111 25 55 25

N
65 19

When 111 is inserted into the heap tree

Case 2: A non-trivial case

05.08.09 IT 60101: Lecture #6 12


Algorithm: Insertion into max heap
Algorithm InsertMaxHeap
1. If (N ≥ SIZE) then

L
2. Print “Heap tree is saturated: Insertion is void”
3. Exit

E
4. Else
5. N=N+1
6. A[N] = ITEM // Adjoin the data in the complete binary tree

T
7. i=N // Last node is the current node
8. p = i div 2. // Its parent node is at p

P
9. While (p > 0) and (A[p] < A[i]) do // Continue till the root is reached or out of order
10. temp = A[i]
11. A[i] = A[p]

N
12. A[p] = temp
13. i=p // Parent becomes child
14. p = p div 2 // Parent of parent becomes new parent
15. EndWhile
16. EndIf
17. Stop

05.08.09 IT 60101: Lecture #6 13


EL
T
Deletion Operation

N P
Deleting the root node from a max heap

• Any node can be deleted from a heap tree. But from the application
point of view, deleting the root node has some special importance.

• Copy the root node into a temporary storage, say ITEM.

EL
T
• Replace the root node by the last node in the heap tree. Then reheap the
tree as stated below:

P
• Let newly modified root node be the current node. Compare its value with the

N
value of its two child. Let X be the child whose value is the largest. Interchange
the value of X with the value of the current node.

• Make X as the current node.

• Continue reheap, if the current node is not an empty node.

05.08.09 IT 60101: Lecture #6 15


Deleting the root node from a max heap

L
26 26

E
99

45 63
45

T
63

57 42

P
57 42 35 29
35 29

N
27 12 24 26 27 12 24

Deleting the node with data 99 Heap tree after repalcing 99 by 26

Continue …

05.08.09 IT 60101: Lecture #6 16


Deleting the root node from a max heap

EL
63 63
26
45

T
63 45 26

P
57 42 42
35 29 57
35 29

N
27 12 24 27 24
12

Rebuilding after adjusting 63 Heap tree after rebuild

05.08.09 IT 60101: Lecture #6 17


Deleting the root node from a max heap

L
63
63

E
57
45 57
45 26

T
26 42

P
57 42 35 29
35 29
26
27 24

N
27 24 12
12

Heap tree after rebuild


Rebuild the tree at 26

05.08.09 IT 60101: Lecture #6 18


Deleting the root node from a max heap

L
26 63
99 63

E
57
26
45 63 45 57

P T
57 42 26 42
35 29 35 29
26

N
27 12 24 26 27 12 24

Deleting the node with data 99 After deletion of 99

05.08.09 IT 60101: Lecture #6 19


Algorithm: Deletion from max heap
Algorithm DeleteMaxHeap
1. If (N = 0) then

L
2. Print “Heap tree is exhausted: Deletion is not possible”
3. Exit

E
4. EndIf
5. ITEM = A[1] // Value at the root node under deletion
6. A[1] = A[N] // Replace the value at root by its counterpart at last node on last level

T
7. N=N–1 // Size of the heap tree is reduced by 1
8. flag = FALSE, i = 1

P
9. While(flag = FALSE) and (i < N) do // Rebuild the heap tree
10. lchild = 2 * i, rchild = 2 * i+1 // Addresses of left and right child of the current node
11. If (lchild £ N) then

N
12. x = A[lchild]
13. Else
14. x=–¥
15. EndIf
16. If (rchild £ N) then
17. y = A[rchild]
18. Else
19. y=–¥
20. EndIf

Continue …

05.08.09 IT 60101: Lecture #6 20


Algorithm: Deletion from max heap
Continue on …
21. If (A[i] > x) and (A[i] > y) then // If parent is larger than its child

L
22. flag = TRUE // Reheap is over
23. Else // Any child may have large value

E
24. If (x > y) and (A[i] < x) // If left child is larger than right child
25. Swap(A[i], A[lchild]) // Interchange the data between parent and left child
26. i = lchild // Left child becomes the current node

T
27. Else
28. If (y > x) and (A[i] < y) // If right child is larger than left child

P
29. Swap(A[i], A[rchild]) //Interchange the data between parent and right
child
30. i = rchild // Right child becomes the current node

N
31. EndIf
32. EndIf
33. EndIf
34. EndWhile
35. Stop

05.08.09 IT 60101: Lecture #6 21


EL
T
Merge Operation

N P
Merge operation

L
Consider, two heap trees H and H .1 2

• Merging the tree H2 with H1 means to include all the nodes from H2 to H1. H2 may be min

E
heap or max heap and the resultant tree will be min heap if H1 is min heap else it will be
max heap.

P
Merging operation consists of two steps:

T
N
• Continue steps 1 and 2 while H2 is not empty:
1. Delete the root node, say x, from H2.
2. Insert the node x into H1 satisfying the property of H1.

05.08.09 IT 60101: Lecture #6 23


Merge operation: An illustration

92 13

L
45 67 19 80

E
38 59 96

T
92 93
H 1: A max heap + H 2: A min heap

P
96

N
93 67

13 19
80 92

38 45 59

Resultant max heap after merging H1 and H 2

05.08.09 IT 60101: Lecture #6 24


EL
T
Applications of Heap Tree

N P
Heap tree applications

L
• Sorting

• Priority queue

T E
N P
05.08.09 IT 60101: Lecture #6 26
EL
T
Sorting Using Heap Tree

P
N
Heap tree applications: Sorting

• Step 1:

L
Build a heap tree with the given set of data.

E
• Step 2:

T
(a) Delete the root node from the heap.

P
(b) Rebuild the heap after the deletion.
(c) Place the deleted node in the output.

• Step 3:

N
Continue Step 2 until the heap tree is empty.

05.08.09 IT 60101: Lecture #6 28


Sorting using heap tree: Building heap

33, 14, 65, 02, 76, 69, 59, 85, 47, 99, 98

98
99

EL 96

P T
65 33 59
85

02 47 14 76

N
99 98 69 65 85 33 59 02 47 14 76

Building (max) heap tree from the given set of data

05.08.09 IT 60101: Lecture #6 29


Sorting using heap tree: Deleting root

L
99

E
76
98 96
98 69

T
33 59
65 85 65 85 33 59

P
02 47 14 76 02 47 14 99

99 98 69 65 85 33 59 02 47 14 76 76 98 69 65 85 33 59 02 47 14 99

N Swapping the root and the last node

05.08.09 IT 60101: Lecture #6 30


Sorting using heap tree: Rebuilding heap

L
98

E
76
98 69 85 69

T
65 85 33 59 65 76 33 59

47 14

P
02 47 14 99 02

76 98 69 65 85 33 59 02 47 14 99 98 85 69 65 76 33 59 02 47 14 99

N Rebuild the heap tree

05.08.09 IT 60101: Lecture #6 31


Sorting using heap tree: Repeating the procedure

L
98 85

E
85 69 76 69
76 33 59 14 33 59

T
65 65
02 47 14 02 47

P
98 85 69 65 76 33 59 02 47 14 99 85 76 69 65 14 33 59 02 47 98 99

N Repeat deleting root and rebuilding heap

05.08.09 IT 60101: Lecture #6 32


Sorting using heap tree: Heap sort

33 14 65 02 76 69 59 85 47 99 98

EL
T
47 65 69 98

P
02 14 33 59 76 85 99

N
Sorted list when the heap is empty

05.08.09 IT 60101: Lecture #6 33


EL
T
Priority Queue Using Heap Tree

N P
Priority queue: Concept

Consider the following processes, their arrival with their priorities:

L
Process P1 P2 P3 P4 P5 P6 P7 P8 P9 P10

E
Priority 5 4 3 4 5 5 3 2 1 5

P
Ordering of processing should be:

T
N
P1 P5 P6 P10 P2 P4 P3 P7 P8 P9

05.08.09 IT 60101: Lecture #6 35


Priority queue implementation using heap tree

L
P(5)
1
P(5)
5

E
P(5)
5
P(5)
6
P(5)
10
P(5)
6

T
P(4) P P(3) P(3) P(4) P2 (4) P(3) P(3)
10 (5) 3

P
4 3 7 4 7

P(1)

N
P(2)
8
P(1)
9 P(4) P(2)
8 9
2
(b) After the removal of P
1
(a) Priority queue heap (subscript indicates order
of process whereas number in parantheses
means its priority)

05.08.09 IT 60101: Lecture #6 36


EL
P T
For more discussion on the tree data

N
structures see the book
Classic Data Structures
Chapter 7
Prentice Hall of India, 2nd Ed.
EL
PT
N
EL
P
Data Structures and Algorithms Using Java
T
Debasis Samanta

N
Department of Computer Science & Engineering, IIT Kharagpur

Module 08: Tree


Lecture 33 : Programming for Heap Tree
EL
T
Ø Heap Tree and its Representation

Ø
Ø
Ø
Creating BST
Insertion
Deletion N P
Ø Programming for Heap Tree

Ø Traversal
EL
T
Quick Review of Heap Tree

P
N
Min heap tree

• A heap tree, say H, is a complete binary tree. H will be termed as heap tree, if it

L
satisfies the following properties:

E
• (i) For each node N in H, the value at N is less than or equal to the value of each
of the children of N.

T
• (ii) Or in other words, N has the value which is less than or equal to the value of

P
every successor of N.

N
• Such a heap tree is called min heap.

05.08.09 IT 60101: Lecture #6 4


Heap trees

L
95 15

E
85 45 25

T
45

65

P
25 75
35 15 35
75 55

N
55 65 85 95
(a) Max heap (b) Min heap

05.08.09 IT 60101: Lecture #6 5


EL
T
Programming for Heap Tree

P
N
Programming overview

L
Class TreeNode <T> {

E
Part-I : Field definition of the class

Part-II :

P T
Methods definition of the class

}
N
EL
T
Defining Structure of Heap Tree

N P
Example 33.1: Defining the heap tree structure
// This part of the program define the structure of a binary tree. */

L
public class MinHeap<T extends Comparable<T>> {
private T[] Heap;

E
private int size;
private int maxsize;

T
private static final int FRONT = 1;

P
public MinHeap(T[] arr , T node)
{

N
this.maxsize = arr.length;
this.size = 0;
Heap = arr;
Heap[0] = node;
}

// Methods of this class are defined next ...to be added here.


} // End of the program
EL
T
Defining Methods of Heap Tree

P
N
EL
T
Auxiliary Methods

N P
Example 30.2: Method for inserting a node

L
// Method to return the position of // Function to return the position of
// the parent for the node currently // the right child for the node currently

E
// at pos // at pos
private int parent(int pos) private int rightChild(int pos)
{ {

T
return pos / 2; return (2 * pos) + 1;
} }

N P
// Method to return the position of the
// left child for the node currently at
// pos
private int leftChild(int pos)
{

}
return (2 * pos);
// Function that returns true if the passed
// node is a leaf node
private boolean isLeaf(int pos)
{

}
if (pos >= (size / 2) && pos <= size) {

}
return true;

return false;
Example 30.2: Method for inserting a node

// Function to print the contents of the heap

L
public void print()
{
for (int i = 1; i <= size / 2; i++) {

E
System.out.print(" PARENT : " + Heap[i]
+ " LEFT CHILD : " + Heap[2 * i]
+ " RIGHT CHILD :" + Heap[2 * i + 1]);
System.out.println();

T
}
}

P
// Function to swap two nodes of the heap

N
private void swap(int fpos, int spos)
{
T tmp;
tmp = Heap[fpos];
Heap[fpos] = Heap[spos];
Heap[spos] = tmp;
}
EL
T
Method for Heapify

P
N
Example 30.3: Method for heapify

// Function to heapify the node at pos

L
private void minHeapify(int pos)
{

E
// If the node is a non-leaf node and greater
// than any of its child
if (!isLeaf(pos)) {
if (Heap[pos].compareTo(Heap[leftChild(pos)]) > 0

T
|| Heap[pos].compareTo(Heap[rightChild(pos)]) > 0) {

// Swap with the left child and heapify

P
// the left child
if (Heap[leftChild(pos)].compareTo(Heap[rightChild(pos)]) < 0) {
swap(pos, leftChild(pos));

N
minHeapify(leftChild(pos));
}

// Swap with the right child and heapify


// the right child
else {
swap(pos, rightChild(pos));
minHeapify(rightChild(pos));
}
}
}
}
EL
T
Method for Insertion

P
N
Example 33.4: Method for inserting a node into a heap tree

L
// Function to insert a node into the heap

E
public void insert(T element)
{
if (size >= maxsize) {

T
return;
}

P
Heap[++size] = element;
int current = size;

N
while (Heap[current].compareTo(Heap[parent(current)]) < 0 ){
swap(current, parent(current));
current = parent(current);
}
}
EL
T
Method for Deletion

P
N
Example 33.5: Method for inserting a node into a heap tree

EL
// Function to remove and return the minimum
// element from the heap
public T remove()

T
{
T popped = Heap[FRONT];

P
Heap[FRONT] = Heap[size--];
minHeapify(FRONT);

N
return popped;
}
EL
Method for Building a

P T
(Min) Heap

N
Example 33.6: Method for creating a heap

// Function to build the min heap using


// the minHeapify

EL
T
public void minHeap()
{

P
for (int pos = (size / 2); pos >= 1; pos--) {
minHeapify(pos);
}

N
}
EL
T
Working with Heap Tree

P
N
Example 33.7: Main method
public static void main(String[] arg)
{

L
System.out.println("The Min Heap is ");
Integer[] a = new Integer[15];
MinHeap minHeap = new MinHeap(a , 5 );

E
//minHeap.insert(5);
minHeap.insert(3);

T
minHeap.insert(17);
minHeap.insert(10);

P
minHeap.insert(84);
minHeap.insert(19);
minHeap.insert(6);

N
minHeap.insert(22);
minHeap.insert(9);
minHeap.minHeap();

minHeap.print();
System.out.println("The Min val is " + minHeap.remove());
} // End of all methods of this program
EL
P T
Ø Classic Data Structures, Debasis Samanta, 2nd Edition, Prentice Hall of India

N
Ø https://cse.iitkgp.ac.in/~dsamanta/javads/index.html
EL
PT
N
EL
P T
Data Structures and Algorithms Using Java
Debasis Samanta

N
Department of Computer Science & Engineering, IIT Kharagpur

Module 08: Trees


Lecture 34 : Huffman Tree
EL
T
Ø Concept of Weighted Binary Tree

P
Ø Internal Path Length

N
Ø External Path Length
Ø Weighted External Path Length
Ø Creating Huffman Tree
Ø Applications
EL
T
Weighted Binary Tree

P
N
Definition: Extended binary tree

N1

L
N1 N2 N4

N4

E
N2 N3 L1 N5 N7

N3 N5 N7 L2 L3 N6 N8 L6

T
L7

P
N6 N8 L4 L5 N9 L 10

N
N9 L8 L9

A binary tree An extended binary tree

05.08.09 IT 60101: Lecture #6 4


Definition: Internal and external nodes
N1

N2 N4

L
Internal node: A node with one or more children is called
an internal node. N3 L1 N5 N7

E
L2 L3 N6 N8 L6 L7

T
External node: A node with zero children is called an L4 L5 N9

P
L 10
external node.

N
L8 L9

05.08.09 IT 60101: Lecture #6 5


Definition: Path length

N1

L
Path length: Path length of a node in a tree is defined as
N2 N4
the number of edges that has to be traversed from the

E
node to the root node. N3 N5
L1 N7

T
L2 L3 N6 N8 L6 L7

P
Path length of N1 = 0
L4 L5 N9 L 10
Path length of N9 = 4

N
Path length of L5 = 4
Path length of L8 = 5 L8 L9

05.08.09 IT 60101: Lecture #6 6


Definition: Internal and extern path lengths

Internal path length: Internal path length (let it be


denoted as I) of a binary tree can be defined analogously as N1

L
the sum of path lengths of all internal nodes in the tree. N2 N4

E
N N N N N N N N N
1 2 3 4 5 6 7 8 9
N3 L1 N5 N7
I = 0 + 1 + 2 + 1 + 2 + 3 + 2 + 3 + 4 = 18

T
L2 L3 N6 N8 L6 L7

P
L4 L5 N9 L
External path length: External path length (let it be denoted as 10

N
E) of a binary tree is defined as the sum of all path lengths,
summed over each path from the root node of the tree to an L8 L9
external node.

L L L L L L L L L L
1 2 3 4 5 6 7 8 9 10

E = 2 + 3 + 3 + 4 + 4 + 3 + 3 + 5 + 5 + 4 = 36

05.08.09 IT 60101: Lecture #6 7


Properties: Relation between internal and extern path lengths

In a binary tree with n internal nodes, if I denotes the


internal path length, then external path length, N1

L
N2 N4
E = I + 2n.

E
N3 L1 N5 N7

T
L2 L3 N6 N8 L6 L7
n=9

P
I = 18 L4 L5 N9 L 10

N
E = 18 + 2 × 9 = 36
L8 L9

05.08.09 IT 60101: Lecture #6 8


Definition: Weighted binary tree

Suppose, all internal nodes have unit values but each of the external nodes are assigned a
(non-negative) number called weights.

EL
The external path length with these weights are called external weighted path length (or, simply
weighted path length) and the corresponding binary tree is termed as extended weighted binary

T
tree (or, simply weighted binary tree).

P
if Wi is a weight of an external node ni and its path length is Li then, weighted path length, P is:

N
n
P = å Wi Li
i =1

05.08.09 IT 60101: Lecture #6 9


Weighted path length: Illustration
n
P = å Wi Li
i =1

EL
T
W =2 W2 =5
1
P = W1L1+ W2L2 + W3L3 + W4L4 + W5L5 + W6L6 + W7L7

P
=2×2+5×2+3×4+4×4+1×3+7×3+6×3 W5 =1 W =7 W =6
6 7
= 4 + 10 + 12 + 16 + 3 + 21 + 18

N
= 84 W3 =3 W4 =4

05.08.09 IT 60101: Lecture #6 10


Problem: Building a weighted binary tree with minimum weighted path length

Given a set of weights (for the external nodes), how a weighted binary tree can be constructed, so that it has
minimum weighted path length

EL
T
2 9

P
2 3 5 9 9 5

N
5 3 2 3
(a) T1

(b) T (c) T
2 3

P(T1) = 38 P(T2) = 44 P(T3) = 34

05.08.09 IT 60101: Lecture #6 11


EL
T
Huffman Algorithm

P
N
Huffman algorithm

L
• An elegant solution to the problem of finding a binary tree with minimum weighted external path
length was given by D. Huffman.

E
• The tree that can be constructed with the Huffman’s algorithm is guaranteed to be a minimum

T
weighted binary tree and to honour the inventor, such trees are alternatively termed as Huffman

P
tree.

N
• The algorithm is an iterative method with greedy strategy.

05.08.09 IT 60101: Lecture #6 13


The algorithm

Input:

L
n weights W1, W2, . . ., Wn.

E
Steps:

T
1. Sort the weights in ascending order.
2. Obtain a sub-tree with two minimum weights as the weights of external nodes.

P
3. Include the weighted path length of the sub-tree so obtained into the list of weights.
4. Repeat the procedure until the list contains single weight.

N
05.08.09 IT 60101: Lecture #6 14
Huffman algorithm: Illustration

L
External node: A B C D E F
Weight: 2 5 3 4 1 7

Huffman tree

T E
P
22

N
8 14

3 5 7 7
B F
1 2 3 4
E A C D

05.08.09 IT 60101: Lecture #6 15


Huffman algorithm: Illustration of creating

L
1 2 3 4 5 7 3* 3 4 5 7 4 5 6* 7

E
3* 6* 9*

T
1 2 3* 3 4 5

P
E A C D B
1 2

N
E A

(a) (b) (c)

Continued …

05.08.09 IT 60101: Lecture #6 16


Huffman algorithm: Illustration of creating

L
6* 7 9* 9* 13*
13*

E
22*
6* 7 13*
9*

T
F
3* 3 4 5 6* 7

P
C D B F
1 2 3* 3

N
E A
C
1 2
E A
(d) (e)

P = 22 + 9 + 13 + 6 + 3 = 54

05.08.09 IT 60101: Lecture #6 17


Huffman algorithm: Huffman tree may not be unique

L
22* 22

E
9* 13*
8 14
4 5

T
6* 7
D 3 5 7 7
B F

P
3* 3 B F
C 1 2 3 4

N
1 2
E A C D
E A

P = 22 + 9 + 13 + 6 + 3 = 54 P = 22 + 8 + 14 + 3 + 7 = 54

05.08.09 IT 60101: Lecture #6 18


Huffman algorithm
Algorithm BuildHuffmanTree (W)
1. For i = 1 to N do // Read the external nodes and initialize the PARRAY

L
2. ptr = GetNode(NODE)
3. ptr→WEIGHT = W[i] // To read the weight for the external node

E
4. ptr→LCHILD = NULL // Link fields of external nodes are empty
5. ptr→RCHILD = NULL

T
6. PARRAY[i] = ptr // Store the node into the PARRAY
7. EndFor
8. i=1

P
9. While (i < N) do
10. Sort_PARRAY(i, N) // Sort the weights from i to N in ascending order

N
11. ptr = GetNode(NODE) // Get a node for internal node
12. ptr→DATA = NULL
13. ptr→LCHILD = PARRAY[i] // Include the lowest weight as the left sub-tree
14. ptr→RCHILD = ptr→PARRAY[i + 1] // Include the second lowest weight as the right sub-tree
15. ptr→WEIGHT = (ptr→LCHILD) →WEIGHT + (ptr→RCHILD) →WEIGHT
// Path length of sub-tree
16. PARRAYE[i + 1] = ptr // Store the pointer to the sub-trees into the list of weights
17. i=i+1
18. EndWhile
19. ROOT = PARRAY[N] // Root of the Huffman tree
20. Stop

05.08.09 IT 60101: Lecture #6 19


EL
T
Application of Huffman Tree

P
N
Application of Huffman tree: Optimum coding for message

One very useful application is to obtain an optimal set of codes for symbols S1, S2, ..., Sn which

L
constitute messages. Each code is a binary string (combinations of 0’s and 1’s) which will be used
for transmission of messages.

T E
Suppose, there are n symbols to constitute a message. We are to code these symbols by means of

P
strings of bits. One simple way to do this is to code each symbol by a r-bit string where

2r – 1 < n ≤ 2r

N
05.08.09 IT 60101: Lecture #6 21
Optimum coding for message: Fixed length coding

• Suppose a message consists of one or more of the 11 symbols, say S1, S2, ..., S11.

L
• Then the number of binary bits required to encode each symbol is 4 (since 23 < 11 ≤ 24) and a
coding scheme is presented .

Symbol Binary code Symbol

T E
Binary code

P
S1 0000 S7 0110
S2 0001 S8 0111

N
S3 0010 S9 1000
S4 0011 S10 1001
S5 0100 S11 1010
S6 0101

Number of bits required to code a message of 100 symbols = 400

05.08.09 IT 60101: Lecture #6 22


Optimum coding for message: Variable length coding

Symbol Probability Weight Code

L
S1 1/4 16 00
S2 3/16 12 10

E
S3 1/8 8 111
S4 1/8 8 010

T
S5 1/16 4 1101

P
S6 1/16 4 01110
S7 1/16 4 01111
S8 1/16 4 0110

N
S9 1/32 2 11001
S10 1/64 1 110000
S11 1/64 1 110001

Number of bits required to code a message of 100 symbols = 243

05.08.09 IT 60101: Lecture #6 23


Optimum coding for message using Huffman tree: Huffman coding

L
0
1
0 0
1

E
1
12 0
16 S2
S2 8 S1 0
S1 1 1 S3

T
S3 0
8 S4
0
S4 4 1 1 S5

P
0
4 S5
2 S 0
8 1 1 S9
S8 0
S9

N
4 4 1 1 S6 S7 S
S
S6 S7 10 11
S S
11
10 (b) Huffman coding
(a) Huffman tree

05.08.09 IT 60101: Lecture #6 24


EL
P T
For more discussion on the tree data

N
structures see the book
Classic Data Structures
Chapter 7
Prentice Hall of India, 2nd Ed.
EL
PT
N
EL
P T
Data Structures and Algorithms Using Java
Debasis Samanta

N
Department of Computer Science & Engineering, IIT Kharagpur

Module 09: Graph Data Structures


Lecture 35 : Graph Structures
Ø Concept of Graph

EL
T
Ø Representation of Graphs

P
Ø Set Representation

N
Ø Linked List Representation
Ø Matrix representation
Ø Operations on Graphs
Ø Insertion
Ø Deletion
Ø Traversals
EL
T
Concept of Graph Structure

P
N
Tree and Graph

EL
P T Graph

N
Tree

• Two structures are similar in the sense that they represent connectivity among the
nodes
• Both belong to the category of non-linear data structures
• They are different in the sense that tree is acyclic where as a graph usably have cycle(s).

05.08.09 4
Example of a graph structure B
B
....... .....
.. .. . .
..
. . .... . . . . .......... . . . A
.. .. .. . . ... A . D
.. . . . . . D
Karachi . . . . . . . . .. . ... ... .
.. . . ...

L
C
Lahore C
Guwahati
Delhi

E
(c) Konigsberg's bridges and their graphical representation

Calcutta

T
Mumbai Hyderabad

D1 D2 D3
Pune

P
Chennai

N
Bangalore Hong Kong

Gas
Colombo Water Electricity (d) Flowchart
(b) Graphical representation of 3 commodities in 3 destinations
(a) Graphical representation of airlines in East Asia

• Other example includes:


• Flowchart, Electrical network, transportation network (road, rail, bus, etc.), postal
communication, courier service, supply-chain, etc.

05.08.09 5
Some terminologies
v1

L
v2 v3
v1

E
v4
v2 v3

T
v5 v6
v4 v5 v6 v7

P
v7

N
G1 G2

5
A B
A B 1
2 3 6 7
E 4
C D
C D
8
G3 G4

05.08.09 6
EL
T
Representations of a Graph

N P
Representations of graphs

L
A graph can be represented in many ways. Some of these representations are as under:

E
1. Set representation

T
2. Linked representation

P
3. Sequential (matrix) representation.

N
05.08.09 8
Set Representations v1

Graph G1 v2 v3
V(G1) = {v1, v2, v3, v4, v5, v6, v7} v1
v4
E(G1) = {(v1, v2), (v1, v3), (v2, v4), (v2, v5), (v3, v6), (v3, v7)}

L
v2 v3

Graph G2 v5 v6 v7
v5 v6

E
v4
V(G2) = {v1, v2, v3, v4, v5, v6, v7} v7
E(G2) = {(v1, v2), (v1, v3), (v2, v4), (v2, v5), (v3, v4), (v3, v6), (v4, v7), (v5, v7), (v6, v7)}
G2

T
G1

Graph G3
5
V(G3) = {A, B, C, D, E}

P
A B
A B
E(G3) = {(A, B),(A, C), (C, B), (C, A), (D, A), (D, B), (D, C), (D, E), (E, B)} 2 3 1
6 7
E 4
C D

N
Graph G4 C D
8
V(G4) = {A, B, C, D}
G3 G4
E(G4) = {(3, A, C), (5, B, A), (1, B, C), (7, B, D), (2, C, A), (4, C, D), (6, D, B), (8, D, C)}

05.08.09 9
Linked list representation v1

v2 v3
v1
v4
v2 v3

v v2 v ^ v v v3 ^ v5 v6

L
1 3 1 2 v7
v4 v5 v6
v v v ^ v v4 v ^
2 4 5 2 5 v7
v v v v v v
3 ^ 3 6 ^

E
6 7 4
v v G1 G2
4 ^ 4 v7 ^
v5 ^ v5
v ^ 5
7 A
v v B
^

T
6 6 v ^ A B 1
7 3
v ^ v ^
2 6 7
7 7 E 4 D
C

P
(a) Representation of graph G1 (b) Representation of graph G2 C D
8
G3 G4

N
A B C ^ ^ A 3 C ^
B ^
^ B ^ 1 C 7 D 5 A ^
C A B ^
^ C 2 A
D A E ^
4 D ^
B
^ D 6 B ^
E B ^ 8 C ^
(c) Representation of graph G3
(d) Representation weighted digraph G4

05.08.09 10
Matrix representation v1

v2 v3
v v . . . . . . .v . . . . . . . .v v1
1 2 j n v4
v .
1 v2 v3
v .
2 .
. v5 v6

L
. v4 v5 v6 v7
. . v7
. .

E
v . . . . . . . . G1 G2
i .a
ij
.
5
. A B

T
A B 1
. 2 3 6 7
v E 4
n C D

P
nxn C D
8
G3 G4
v1 v2 v3 v 4v 5v 6v 7 v1 v2 v3 v4 v5 v 6v 7

N
v1 01 1 0 0 0 0 v 01 1 0 0 0 0
1
v2 10 0 1 1 0 0 v2 10 0 1 1 0 0 A B C D E
v 10 0 0 0 1 1 v 10 0 1 0 1 0
3 3 A 0 1 1 0 0 A B C D
v 01 0 0 0 0 0 v 01 1 0 0 0 1
4 4 B 0 0 0 0 0 A 0 0 3 0
v5 01 0 0 0 0 0 v5 01 0 0 0 0 1 C 1 1 0 0 0 B 5 0 1 7
v6 00 1 0 0 0 0 v6 00 1 0 0 0 1 D 1 1 1 0 1 C 2 0 0 4
v 00 1 0 0 0 0 v 00 0 1 1 1 0
7 7 E 0 1 0 0 0 D 0 6 8 0

G1 G2 G3 G4

05.08.09 11
EL
T
Operations on Graph

P
N
Operations of graphs

Insertion
To insert a vertex and hence establishing connectivity with other vertices in the

L
existing graph.

E
To insert an edge between two vertices in the graph.

T
Deletion
To delete a vertex from the graph.

P
To delete an edge from the graph.

N
Merging
To merge two graphs G1 and G2 into a single graph.

Traversal
To visit all the vertices in the graph.

05.08.09 13
EL
T
Insertion into a Graph

N P
Insertion into a linked representation of a undirected graphs

L
v1
VGptr v - v - v3 - v8 ^
1 2

E
v - v - v
v2 v8 v 2 4 5
3
v - v - v

T
3 4 6
v4 v
4 - v - v - v ^ - v
2 3 7 8

P
v5 - v - v ^
v5 v6 2 7
v - - v
6 v ^
3 7

N
v7 v - v - v - v
7 4 5 6 ^

v8 - v1 - v ^
4

(a)

05.08.09 15
Insertion into a linked representation of a directed graphs

v1

L
DGptr v - v - v ^
1 2 3

E
v - v - v ^
v2 v8 v 2 4 5
3
v - v4 - v
3 ^

T
6
v4 v
4 - v ^

P
8
v5 - v ^
v6 7
v5
v

N
6 - v ^
7
v7 v - v
7 4
v8 - v ^
1

(b)

05.08.09 16
Insertion into a matrix representation of graphs

EL
v1 v1
1 2 3 4 5 6 7 88 1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 11 1 0 1 1 0 0 0 0 0

T
v8 v2 v8 v 2 0 0 0 1 1 0 0 0
v2 v
3
2 1 0 0 1 0
1 0 0 00 3
3 1 0 0 1 0 1 0 00 3 0 0 0 1 0 1 0 0

P
v4 4 0 1 1 0 0 0 1 11 v4 4 0 0 0 0 0 0 0 1
5 0 1 0 0 0 0 1 00 5 0 0 0 0 0 0 1 0
6 0 0 1 0 0 0 1 00 6 0 0 0 0 0 0 1 0

N
v6 v5 v6 0 0 0 1 0 0 0 0
v5 7 0 0 0 1 1 1 0 00 7
8 1 0 0 1 0 0 0 0 8 1 0 0 0 0 0 0 0

v7 v7

05.08.09 17
EL
Deletion from a Graph

P T
N
Deletion from a linked representation of a undirected graphs

L
v1
VGptr v - v - v3 - v8 ^
1 2

E
v - v - v
v2 v8 v 2 4 5
3
v - v - v

T
3 4 6
v4 v
4 - v - v - v ^ - v
2 3 7 8

P
v5 - v - v ^
v5 v6 2 7
v - - v
6 v ^
3 7

N
v7 v - v - v - v
7 4 5 6 ^

v8 - v1 - v ^
4

(a)

05.08.09 19
EL
T
Graph Traversals

N P
Traversals of graphs
Depth first traversal
Depth first search (DFS) traversal is similar to the inorder traversal of a binary tree. Starting

L
from a given node, this traversal visits all the nodes up to the deepest level and so on..

E
Breadth first traversal
The breadth first search (BFS) traversal is very similar to the level-by-level traversal of a tree.

P T
N
Tree Graph

05.08.09 21
DFS traversal of graphs

v1 v1 v1 v1

L
v2 v8 v v2 v8 v v8 v v2 v8 v
3 3 v2 3 3

E
DFS DFS
v4 v4 v4 v4

T
v5 v6 v5 v6 v6 v6
v5 v5

P
v7 v7 v7 v7

G1 G2

N
DFS(G1) = v1-v2-v5-v7-v4-v8-v6-v3 DFS(G2) = v1-v2-v5-v7-v4-v8-v3-v6

Note:
• A stack can be used to maintain the track of all paths from any vertex so as to help
backtracking.

05.08.09 22
BFS traversal of graphs

v1 v1 v1 v1

L
v2 v8 v v2 v8 v v8 v v2 v8 v
v2

E
3 3 3 3
BFS BFS
v4 v4 v4 v4

T
v5 v6 v5 v6 v6 v6
v5 v5

P
v7 G1 v7 G2 v7
v7

N
BFS(G1) = v1-v2-v8-v3-v5-v4-v6-v7 BFS(G2) = v1-v2-v3-v5-v4-v6-v7-v8

Note:
• A queue can be used to maintain the track of all paths from any vertex so as to help
backtracking.

05.08.09 23
EL
P T
For more discussion on the graph

N
data structure see the book
Classic Data Structures
Chapter 8
Prentice Hall of India, 2nd Ed.
EL
PT
N

You might also like