intro to heap & adding and removing a node presented to: sir ahsan raza presented by: shah rukh...

31
INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll # 07-22 Semester BIT 3 rd Session 2007— 2011 Department of Computer Science, B.Z.University Multan

Upload: audrey-henry

Post on 21-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

INTRO TO HEAP & Adding and Removing a NODE

Presented to: Sir AHSAN RAZA

Presented by: SHAH RUKH Roll # 07-22Semester BIT 3rd

Session 2007—2011

Department of Computer Science,B.Z.University Multan

Page 2: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Heaps DefinitionHeaps DefinitionA heap is a certain kind of complete binary tree.

Page 3: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Binary TREE

It has a Root and has two CHILDREN.

Left child or left subtree.

Right child or right subtree.

Every child has its two children (left & right) then this child is called a node.

If a node has no children then this node is said to be as a leaf.

Page 4: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsA heap is a certain kind of complete binary tree.

When a completebinary tree is built,

its first node must bethe root.

When a completebinary tree is built,

its first node must bethe root.

Root

Page 5: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsComplete binary tree.

Left childof theroot

The second node isalways the left child

of the root.

The second node isalways the left child

of the root.

Page 6: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsComplete binary tree.

Right childof the

root

The third node isalways the right child

of the root.

The third node isalways the right child

of the root.

Page 7: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsComplete binary tree.

The next nodesalways fill the next

level from left-to-right..

The next nodesalways fill the next

level from left-to-right..

Page 8: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsComplete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

Page 9: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsComplete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

Page 10: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsComplete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

Page 11: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsComplete binary tree.

Page 12: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsA heap is a certain kind of complete binary tree.

Each node in a heapcontains a key thatcan be compared toother nodes' keys.

Each node in a heapcontains a key thatcan be compared toother nodes' keys.

19

4222127

23

45

35

Page 13: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

HeapsHeapsA heap is a certain kind of complete binary tree.

The "heap property"requires that each

node's key is >= thekeys of its children

The "heap property"requires that each

node's key is >= thekeys of its children

19

4222127

23

45

35

Page 14: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

What it is not: It is not a BSTIn a binary search tree, the entries of the nodes can be compared

with a strict weak ordering. Two rules are followed for every node n:The entry in node n is NEVER less than an entry in its left subtreeThe entry in the node n is less than every entry in its right

subtree.BST is not necessarily a complete tree

Page 15: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

What it is: Heap DefinitionA heap is a binary tree where the entries of the nodes can be

compared with the less than operator of a strict weak ordering. In addition, two rules are followed:The entry contained by the node is NEVER less than the entries

of the node’s childrenThe tree is a COMPLETE tree.

Page 16: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Application : Priority QueuesA priority queue is a container class that allows entries to be

retrieved according to some specific priority levelsThe highest priority entry is removed firstIf there are several entries with equally high priorities, then the

priority queue’s implementation determines which will come out first (e.g. FIFO)

Heap is suitable for a priority queue

Page 17: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

The Priority Queue ADT with HeapsThe entry with the highest priority is always at the root nodeFocus on two priority queue operations

adding a new entryremove the entry with the highest priority

In both cases, we must ensure the tree structure remains to be a heapwe are going to work on a conceptual heap without worrying about

the precise implementation

Page 18: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Adding a Node to a HeapAdding a Node to a HeapPut the new node in the next

available spot.Push the new node upward,

swapping with its parent until the new node reaches an acceptable location.

19

4222127

23

45

35

42

Page 19: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Adding a Node to a HeapAdding a Node to a HeapPut the new node in the next

available spot.Push the new node upward,

swapping with its parent until the new node reaches an acceptable location.

19

4222142

23

45

35

27

Page 20: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Adding a Node to a HeapAdding a Node to a HeapPut the new node in the next

available spot.Push the new node upward,

swapping with its parent until the new node reaches an acceptable location.

19

4222135

23

45

42

27

Page 21: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Adding a Node to a HeapAdding a Node to a HeapThe parent has a key that is

>= new node, orThe node reaches the root.The process of pushing the

new node upward is called reheapification upward.

19

4222135

23

45

42

27

Note: we need to easily go from child to parent as well as parent to child.

Page 22: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Removing the Top of a HeapRemoving the Top of a HeapMove the last node onto the

root.

19

4222135

23

45

42

27

Page 23: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Removing the Top of a HeapRemoving the Top of a HeapMove the last node onto the

root.

19

4222135

23

27

42

Page 24: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Removing the Top of a HeapRemoving the Top of a HeapMove the last node onto the

root.Push the out-of-place node

downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222135

23

27

42

Page 25: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Removing the Top of a HeapRemoving the Top of a HeapMove the last node onto the

root.Push the out-of-place node

downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222135

23

42

27

Page 26: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Removing the Top of a HeapRemoving the Top of a HeapMove the last node onto the

root.Push the out-of-place node

downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222127

23

42

35

Page 27: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Removing the Top of a HeapRemoving the Top of a HeapThe children all have keys

<= the out-of-place node, or

The node reaches the leaf.The process of pushing the

new node downward is called reheapification downward.

19

4222127

23

42

35

Page 28: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Priority Queues RevisitedA priority queue is a container class that allows entries to be

retrieved according to some specific priority levelsThe highest priority entry is removed firstIf there are several entries with equally high priorities, then the priority

queue’s implementation determines which will come out first (e.g. FIFO)

Heap is suitable for a priority queue

Page 29: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Adding a Node: same priority Adding a Node: same priority Put the new node in the next

available spot.Push the new node upward,

swapping with its parent until the new node reaches an acceptable location.

19

4222127

23

45

35

45*

Page 30: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Adding a Node : same priority Adding a Node : same priority Put the new node in the next

available spot.Push the new node upward,

swapping with its parent until the new node reaches an acceptable location.

19

4222145*

23

45

35

27

Page 31: INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department

Adding a Node : same priority Adding a Node : same priority Put the new node in the next

available spot.Push the new node upward,

swapping with its parent until the new node reaches an acceptable location.

19

4222135

23

45

45*

27