Case 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

16CS3202 - Data Structures II Yr CSE / III Sem

The Condition fails as the order of B tree is 5, so 4 keys and 5 child should be there. So we going to
split the B- tree into two half

UNIT 4 Page 33
16CS3202 - Data Structures II Yr CSE / III Sem

3.10 Heaps
3.10.1 Binary Heap
Heap is a complete binary tree or a almost complete binary tree in which every parent node be
either greater or lesser than its child nodes.
In this Heap can be MIN or MAX.
MAX Heap in the tree in which each node is greater than or equal to the value of the
children node
MIN Heap in the tree in which each node is lesser than or equal too the value of the
children nodes.
MAX Heap:
In MAX Heap every parent node is greater than or equal to than its child node.

18

13 4

11 10

MIN Heap:
In MIN Heap every parent node is lesser than its children.

13 14

12 20

Parental Property
Parent node being greater or lesser in heap so its called parental property.
Heap having two important property
Heap should satisfy following two conditions.
It should be a complete binary tree or almost a complete binary tree
Its should satisfy the parental property

UNIT 4 Page 34
16CS3202 - Data Structures II Yr CSE / III Sem

Almost complete binary tree:

18 18
18

13 4 13 4
13 4

11 10
11 10
In A:Binary tree
satisfying properties, its level 2 and 1 but left and right child is present for each level.
10
A B C
In B: Binary tree doesn't satisfying the 2nd property than the leaves at level 3 and level 1 are
not adjacent. So its not complete binary tree.
In C: Binary tree doesn't satisfying the 1st property than 2nd level but only right child is
present.

Properties Of Priority Queue Using Heap:


There should be either complete or almost complete binary tree.
The root of a heap always contains its smallest or largest element.
Heap sub tree in a heap is also a heap.
Heap can be implemented as array by recording its element, top-down and left -right
Array Insertion:
Root node A[0].
Left child is at 2i+1 position in array A.
Right child is at 2i+2 position in array A.
Parent node is at the position in the array A.

UNIT 4 Page 35
16CS3202 - Data Structures II Yr CSE / III Sem

Algorithm for Insertion of Element:


Insert the element at the last position in the heap.

18
18 13 4 11 10

13 4

11 10
To insert the element 19 in the given tree

18 18

13 4 13 4

11 10 11 10 19

Its not satisfying the property 2 so we swap the element.4 > 19

18
18

13 4
13 19

11 10 19
11 10 4

UNIT 4 Page 36
16CS3202 - Data Structures II Yr CSE / III Sem

Its not satisfying the property 2 so we swap the element 19 > 18

18 19

13 19 13 18

11 10 4 11 10 4

Algorithms for Deletion of Heap:


Exchange of root with the last leaf.
Decrease the heap size by 1.
Heapify smaller tree using the bottom up constructions algorithms.
Delete of key 19

19 19

13 18 13 18
4 4 4

11 10 13 4 18 13 18 13 18
11 10 4

11 10 19 11 10 19 11 10

Now we go for checking of heap property so condition fails.

13 18

UNIT 4 11 10 Page 37
16CS3202 - Data Structures II Yr CSE / III Sem

So we move the 18 as root and 4 as right child

18

13 4

11 10

3.10.2 Applications of binary heaps


Applications of Heap Data Structure
a. Heap Data Structure is generally taught with Heapsort.
b. Priority Queues: Priority queues can be efficiently implemented using Binary Heap
because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time.
c. Binomoial Heap and Fibonacci Heap are variations of Binary Heap. These variations
perform union also in O(logn) time which is a O(n) operation in Binary Heap. Heap Implemented

3.11 BINOMIAL HEAP


The main application of Binary Heap is as implement priority queue. Binomial Heap is to
extension of Binary Heap that provides faster union or merge operation together with other operations
provided by Binary Heap.
A Binomial Heap is a collection of Binomial Trees
A Binomial Tree of order 0 has 1 node. A Binomial Tree of order k can be constructed by
taking two binomial trees of order k-1, and making one as leftmost child of other.
A Binomial Tree of order k has following properties.
It has exactly 2k nodes.
a) It has depth as k.
b) There are exactly kCi nodes at depth i for i = 0, 1, . . . , k.
c) The root has degree k and children of root are themselves Binomial Trees with order k-
1, k-2,.. 0 from left to right.

UNIT 4 Page 38
16CS3202 - Data Structures II Yr CSE / III Sem

Binomial Heap:
A Binomial Heap is a set of Binomial Trees where each Binomial Tree follows Min Heap
property. And there can be at-most one Binomial Tree of any degree.

Binary Representation of a number and Binomial Heaps


A Binomial Heap with n nodes has number of Binomial Trees equal to the number of set bits in
Binary representation of n. For example let n be 13, there 3 set bits in binary representation of n

UNIT 4 Page 39
16CS3202 - Data Structures II Yr CSE / III Sem

(00001101), hence 3 Binomial Trees. We can also relate degree of these Binomial Trees with positions
of set bits. With this relation we can conclude that there are O(Logn) Binomial Trees in a Binomial

Operations of Binomial Heap:


The main operation in Binomial Heap is union(), all other operations mainly use this operation.
The union() operation is to combine two Binomial Heaps into one. Let us first discuss other operations,
we will discuss union later.
a)

b) getMin(H): A simple way to getMin() is to traverse the list of root of Binomial Trees and return
the minimum key. This implementation requires O(Logn) time. It can be optimized to O(1) by
maintaining a pointer to minimum key root.
c) extractMin(H): This operation also uses union(). We first call getMin() to find the minimum key
Binomial Tree, then we remove the node and create a new Binomial Heap by connecting all subtrees of
the removed minimum node. Finally we call union() on H and the newly created Binomial Heap. This
operation requires O(Logn) time.
d) delete(H): Like Binary Heap, delete operation first reduces the key to minus infinite, then calls
extractMin().
e) decreaseKey(H): decreaseKey() is also similar to Binary Heap. We compare the decreases key

reach a node whose parent has smaller key or we hit the root node. Time complexity of decreaseKey()
is O(Logn).

Union operation in Binomial Heap:


Given two Binomial Heaps H1 and H2, union (H1, H2) creates a single Binomial Heap.
a) The first step is to simply merge the two Heaps in non-decreasing order of degrees. In the
following diagram, figure(b) shows the result after merging.
b) After the simple merge, we need to make sure that there is at-most one Binomial Tree of any
order. To do this, we need to combine Binomial Trees of same order. We traverse the list of merged
roots, we keep track of three pointers, prev, x and next-x. There can be following 4 cases when we
traverse the list of roots.
Case 1: Orders of x and next-x are not same, we simply move ahead.
In following 3 cases orders of x and next-x are same.
Case 2: If order of next-next-x is also same, move ahead.
Case 3: If key of x is smaller than or equal to key of next-x, then make next-x as a child of x by
linking it with x.
Case 4: If key of x is greater, then make x as child of next.

UNIT 4 Page 40
16CS3202 - Data Structures II Yr CSE / III Sem

Binomial Heap Representation:


A Binomial Heap is a set of Binomial Trees. A Binomial Tree must be represented in a way that
allows sequential access to all siblings, starting from the leftmost sibling (We need this in and
extractMin() and delete()). The idea is to represent Binomial Trees as leftmost child and right-sibling
representation, i.e., every node stores two pointers, one to the leftmost child and other to the right
sibling.

UNIT 4 Page 41
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 1
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 2
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 3
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 4
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 5
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 6
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 7
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 8
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 9
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 10
16CS3202 - DATA STRUCTURES - NOTES

UNIT 4 Page 11

You might also like