52
Compiled by: Dr. Mohammad Alhawarat T, Priority Queue, aps - Heapsort CHAPTER 07

Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Embed Size (px)

Citation preview

Page 1: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Compiled by: Dr. Mohammad AlhawaratBST, Priority Queue, Heaps - Heapsort

CHAPTER 07

Page 2: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Part I: Binary Search TreePart II: Priority Queue & Heaps

Part III: Heapsort

Chapter Contents2

Page 3: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Part I: Binary Search Tree

3

Page 4: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Binary Search Trees (BSTs)

A search tree is a tree whose elements are organized to facilitate finding a particular element when needed

A binary search tree is a binary tree that, for each node n the left subtree of n contains elements less

than the element stored in n

the right subtree of n contains elements greater than or equal to the element stored in n

4

Page 5: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Binary Search Trees (BSTs)5

Page 6: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Binary Search Trees (BSTs)

To determine if a particular value exists in a tree start at the root compare target to element at current node move left from current node if target is less

than element in the current node move right from current node if target is

greater than element in the current node We eventually find the target or

encounter the end of a path (target is not found)

6

Page 7: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Binary Search Trees (BSTs)

The particular shape of a binary search tree depends on the order in which the elements are added to the tree

The shape may also be dependant on any additional processing performed on the tree to reshape it

Binary search trees can hold any type of data, so long as we have a way to determine relative ordering

7

Page 8: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Adding an Element to a BST

Process of adding an element is similar to finding an element

New elements are added as leaf nodes Start at the root, follow path dictated

by existing elements until you find no child in the desired direction

Add the new element

8

Page 9: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Adding an Element to a BST

77

24 82

5817

40

97

Next to add: 77 24 5882 17 4097

9

Page 10: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Removing an Element from a BST

Removing a target in a BST is not as simple as that for linear data structures

After removing the element, the resulting tree must still be valid

Three distinct situations must be considered when removing an element The node to remove is a leaf The node to remove has one child The node to remove has two children

10

Page 11: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Removing an Element from a BST11

Page 12: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Removing an Element from a BST

Dealing with the situations Node is a leaf: it can simply be deleted Node has one child: the deleted node is

replaced by the child Node has two children: an appropriate

node is found lower in the tree and used to replace the node:Either selecting the largest element in

the left subtree.Or selecting the smallest element in the

right subtree.

12

Page 13: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

After the Root Node is Removed13

Page 14: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

14

Complexity

Logarithmic, depends on the shape of the tree O(log2 N)

In the worst case – O(N) comparisons

Page 15: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

15

Advantages of BST

Simple Efficient Dynamic

One of the most fundamental algorithms in CS

The method of choice in manyapplications

Page 16: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

16

Disadvantages of BST

The shape of the tree depends on the order of insertions, and it can be degenerated. (Becomes a Linked List)

When inserting or searching for an element, the key of each visited node has to be compared with the key of the element to be inserted/found.

Page 17: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

17

Improvements of BST

Keeping the tree balanced:

AVL trees (Adelson - Velskii and Landis)

Balance condition: left and right subtrees of each node can differ by at most one level.

It can be proved that if this condition is observed the depth of the tree is O(log2N).

Page 18: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Part II: Priority Queue & Heaps

18

Page 19: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

19

Priority Queue ADT

The data in a priority queue is (conceptually) a queue of elements

The “queue” can be thought of as sorted with the largest in front, and the smallest at the end Its physical form, however, may differ

from this conceptual view considerably

Page 20: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

20

Priority Queue ADT Operations

enqueue, an operation to add an element to the queue

dequeue, an operation to take the largest element from the queue

an operation to determine whether or not the queue is empty

an operation to empty out the queue

Page 21: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Priority Queue Implementation Priority Queue could be implemented in

different ways.

One way is to use vectors.

Another way is to use Binary Heap.

What’s a Heap?

21

Page 22: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

22

Heaps

A heap is a complete binary tree in which the value of each node is greater than or equal to the values of its children (if any)

Technically, this is called a maxheap

In a minheap, the value of each node is less than or equal to the values of its children

Page 23: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Heaps (cont.)

The element stored in each node of a heap can be an object

When we talk about the value of a node, we are really talking about some data member of the object that we want to prioritize

For example, in employee objects, we may want to prioritize age or salary

23

Page 24: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

24

Implementations

• Binary heap• Better than BST because it does not

support links.• Insert: O(log2N)

• Find minimum O(log2N)

• Deleting the minimal element takes a constant time, however after that the heap structure has to be adjusted, and this requires O(log2N) time.

Page 25: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

25

Binary Heap

• Heap-Structure Property: • Complete Binary Tree - Each node

has two children, except for the last two levels.

• The nodes at the last level do not have children. New nodes are inserted at the last level from left to right.

• Heap-Order Property: • Each node has a higher priority than

its children

Page 26: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

26

Binary Heap

6

1012

15 17 18 23

20 19 34

Next node to be inserted - right child of the yellow node

Page 27: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

27

Basic Operations

Build the heap

Insert a node – Percolate Up

Delete a node – Percolate Down

Page 28: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

28

Build Heap - O(N)

• Given an array of elements to be inserted in the heap,

• treat the array as a heap with order property violated,

• and then do operations to fix the order property.

Page 29: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

29

150 80 40 30 10 70 110 100 20 90 60 50 120 140 130

150

80 40

30 1070 110

100 20 90 60 50 120 140 130

Example

Page 30: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

30

Example (cont)

150

80 40

20 1050 110

100 30 90 60 70 120 140 130

After processing height 1

Page 31: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

31

Example (cont)

150

10 40

20 6050 110

100

30 90 80 70 120

140

130

After processing height 2

Page 32: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

32

Example (cont)

10

20 40

30 6050 110

100 150 90 80 70 120 140 130

After processing height 3

Page 33: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

33

Percolate Up – Insert a Node

• A hole is created at the bottom of the tree, in the next available position.

6

1012

15 17 18 23

20 19 34

Page 34: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

34

Percolate Up

Insert 206

1012

15 17 18 23

21 19 34 20

Page 35: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

35

Insert 16 6

1012

15 18 23

21 19 34 17

Percolate Up

Page 36: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

36

6

1012

15 18 23

21 19 34 17

16

Complexity of insertion: O(log2N)

Percolate Up

Page 37: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

37

Percolate Down – Delete a Node

1016

13 12 18 23

15 19 30 34

Page 38: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

38

Percolate Down – the wrong way

10

16

13 12 18 23

15 19 30 34

Page 39: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

39

Percolate Down – the wrong way

16

13 18 23

15 19 34

10

12

30

The empty hole violates the heap-structure property

Page 40: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

40

Percolate Down

Last element - 20. The hole at the root.

1016

12 13 18 23

15 19 30 20

We try to insert 20 in the hole by percolating the hole down

Page 41: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

41

Percolate Down

16

12 13 18 23

15 19 3020

10

Page 42: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

42

Percolate Down

16

13 18 23

15 19 3020

10

12

Page 43: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

43

Percolate Down

16

13 18 23

20 19 30

10

12

15

Complexity of deletion: O(logN)

Page 44: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

44

Example of a Heap

39

16

14 3

46

28

2

root

32

29

24

Only the data member is shown that we want to prioritize

Page 45: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

45

Example of a Heap (cont.)

39

16

14 3

46

28

2

root

32

29

24

Where is the greatest value in a heap always found?

Page 46: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

46

Example of a Heap (cont.)

39

16

14 3

46

28

2

root

32

29

24

Where is the greatest value in a heap always found?

Page 47: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

47

Dequeue

Dequeuing the object with the greatest value appears to be a ( 1 ) operation

However, after removing the object, we must turn the resultant structure into a heap again, for the next dequeue

Fortunately, it only takes O( log2n ) time to turn the structure back into a heap again (which is why dequeue in a heap is a O( log2n ) operation

Page 48: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

48

The process of swapping downwards to form a new heap is called heapifying

When, we heapify, it is important that the rest of the structure is a heap, except for the root node that we are starting off with; otherwise, a new heap won’t be formed

A loop is used for heapifying; the number of times through the loop is always lg n or less, which gives the O( lg n ) complexity

Each time we swap downwards, the number of nodes we can travel to is reduced by approximately half

Heapify

Page 49: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Part III: Heapsort

49

Page 50: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Heapsort

Consider a priority queue with n items implemented by means of a heap

• The space used is O(n)

• Methods enqueue and removeMax take O(log n) time

Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time

The resulting algorithm is called heap-sort

50

Page 51: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Heapsort

• Build a binary heap of N elements – O(N) time

• Then perform N deleteMax operations– log(N) time per deleteMax

• Total complexity O(N log N)

51

Page 52: Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07

Questions52