heaps definition

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: joseph-benjamin

Post on 02-Jan-2016

32 views

Category:

Documents


1 download

DESCRIPTION

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. Heaps Definition. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Heaps Definition

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: Heaps Definition

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

Page 3: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

HeapsHeapsComplete binary tree.

Page 12: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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

root.

19

4222135

23

27

42

Page 24: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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: Heaps Definition

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