Lect 9 Heaps

download Lect 9 Heaps

of 44

Transcript of Lect 9 Heaps

  • 8/12/2019 Lect 9 Heaps

    1/44

    HEAPS

    Lecture 9 Dr. Adil Yousif

  • 8/12/2019 Lect 9 Heaps

    2/44

    Full v.s. Complete Binary Trees

    A full binary tree (sometimes proper binary tree or

    2-tree) is a tree in which every node other than the

    leaves has two children.

  • 8/12/2019 Lect 9 Heaps

    3/44

    Complete binary tree

    A complete binary tree is a binary tree in which

    every level, except possibly the last, is completely

    filled, and all nodes are as far left as possible.

  • 8/12/2019 Lect 9 Heaps

    4/44

    Heaps

    Heaps are a specialized complete tree-based data

    structure that satisfies the heap property.

    The heap property is : If A is a parent node of B then the

    key of node A is ordered with respect to the key of nodeB with the same ordering applying across the heap.

    Either the keys of parent nodes are always greater than

    or equal to those of the children and the highest key is in

    the root node (this kind of heap is called max heap)

    Or the keys of parent nodes are less than or equal to

    those of the children and the lowest key is in the root

    node (min heap).

  • 8/12/2019 Lect 9 Heaps

    5/44

    Example of Max-Heap

  • 8/12/2019 Lect 9 Heaps

    6/44

    Heap Ordering

    As shown in the graphic, there is no implied

    ordering between siblings or cousins and no

    implied sequence for an in-order traversal(as there

    would be in, e.g., abinary search tree). The heap relation mentioned above applies only

    between nodes and their immediate parents.

    The maximum number of children each node canhave depends on the type of heap, but in many

    types it is at most two, which is known as a "binary

    heap"

    http://en.wikipedia.org/wiki/Inorder_traversalhttp://en.wikipedia.org/wiki/Binary_search_treehttp://en.wikipedia.org/wiki/Binary_search_treehttp://en.wikipedia.org/wiki/Inorder_traversalhttp://en.wikipedia.org/wiki/Inorder_traversalhttp://en.wikipedia.org/wiki/Inorder_traversal
  • 8/12/2019 Lect 9 Heaps

    7/44

    7

    Max Heap with 9 Nodes

  • 8/12/2019 Lect 9 Heaps

    8/44

    8

    Min Heap with 9 Nodes

  • 8/12/2019 Lect 9 Heaps

    9/44

    9

    Array Representation of Heap

    A heap is efficiently represented as an array.

  • 8/12/2019 Lect 9 Heaps

    10/44

    Array Representation of Heap

  • 8/12/2019 Lect 9 Heaps

    11/44

    Array Representation of Heap

  • 8/12/2019 Lect 9 Heaps

    12/44

    Array Representation of Heap

  • 8/12/2019 Lect 9 Heaps

    13/44

    Constructing a heap

    A tree consisting of a single node is automatically aheap

    We construct a heap by adding nodes one at a time:

    Add the node just to the right of the rightmost node inthe deepest level

    If the deepest level is full, start a new level

    Examples:Add a newnode here Add a newnode here

  • 8/12/2019 Lect 9 Heaps

    14/44

    Constructing a heap III

    8 8

    10

    10

    8

    10

    8 5

    10

    8 5

    12

    10

    12 5

    8

    12

    10 5

    8

    1 2 3

    4

  • 8/12/2019 Lect 9 Heaps

    15/44

    Other children are not affected

    The node containing 8 is not affected because its parent gets larger,

    not smaller

    The node containing 5 is not affected because its parent gets larger,not smaller

    The node containing 8 is still not affected because, although its parentgot smaller, its parent is still greater than it was originally

    12

    10 5

    8 14

    12

    14 5

    8 10

    14

    12 5

    8 10

  • 8/12/2019 Lect 9 Heaps

    16/44

    16

    Another Example: Insertion into a

    Max Heap

    9

    8

    6

    7

    7 2 6

    5 1 5

    New element is 5

    Are we finished?

  • 8/12/2019 Lect 9 Heaps

    17/44

    17

    9

    8

    6

    7

    7 2 6

    5 1 20New element is 20Are we finished?

    Insertion into a Max Heap

  • 8/12/2019 Lect 9 Heaps

    18/44

    18

    9

    8

    6

    7

    2 6

    5 1 7

    20

    Exchange the positions with 7

    Are we finished?

    Insertion into a Max Heap

  • 8/12/2019 Lect 9 Heaps

    19/44

    19

    9

    6

    7

    2 6

    5 1 7

    8

    20

    Insertion into a Max Heap

    Exchange the positions with 8

    Are we finished?

  • 8/12/2019 Lect 9 Heaps

    20/44

    20

    6

    7

    2 6

    5 1 7

    8

    9

    20

    Insertion into a Max Heap

    Exchange the positions with 9

    Are we finished?

  • 8/12/2019 Lect 9 Heaps

    21/44

    Removing the root

    Notice that the largest number is now in the root

    Suppose we discardthe root:

    How can we fix the binary tree so it is once again balanced

    and left-justified?

    Solution: remove the rightmost leaf at the deepest level and

    use it for the new root

    19

    1418

    22

    321

    14

    119

    15

    1722

    11

  • 8/12/2019 Lect 9 Heaps

    22/44

    The reHeap method I

    Our tree is balanced and left-justified, but no longer a heap However, only the rootlacks the heap property

    We can siftUp()the root

    After doing this, one and only one of its children may have

    lost the heap property

    19

    1418

    22

    321

    14

    9

    15

    1722

    11

  • 8/12/2019 Lect 9 Heaps

    23/44

    The reHeap method II

    Now the left child of the root (still the number 11) lacksthe heap property

    We can siftUp()this node

    After doing this, one and only one of its children may have

    lost the heap property

    19

    1418

    22

    321

    14

    9

    15

    1711

    22

  • 8/12/2019 Lect 9 Heaps

    24/44

    The reHeap method III

    Now the right child of the left child of the root (still thenumber 11) lacks the heap property:

    We can siftUp()this node

    After doing this, one and only one of its children may have

    lost the heap propertybut it doesnt, because its a leaf

    19

    1418

    11

    321

    14

    9

    15

    1722

    22

  • 8/12/2019 Lect 9 Heaps

    25/44

    The reHeapmethod IV

    Our tree is once again a heap, because every node in it hasthe heap property

    Once again, the largest (oralargest) value is in the root

    We can repeat this process until the tree becomes empty

    This produces a sequence of values in order largest to smallest

    19

    1418

    21

    311

    14

    9

    15

    1722

    22

  • 8/12/2019 Lect 9 Heaps

    26/44

    26

    Another Example: Deletion from a

    Max Heap

    Max element is in the root

    What happens when we

    delete an element?

    20

    6

    7

    2 6

    5 1 7

    15

    8

    9

  • 8/12/2019 Lect 9 Heaps

    27/44

    27

    Deletion from a Max Heap

    After the max element is

    removed.

    Are we finished?

    6

    7

    2 6

    5 1 7

    15

    8

    9

  • 8/12/2019 Lect 9 Heaps

    28/44

    28

    Deletion from a Max Heap

    Heap with 10 nodes.

    Reinsert 8 into the heap.

    6

    7

    2 6

    5 1 7

    15

    8

    9

  • 8/12/2019 Lect 9 Heaps

    29/44

    29

    Deletion from a Max Heap

    Reinsert 8 into the heap.Are we finished?

    6

    7

    2 6

    5 1 7

    15

    9

    8

  • 8/12/2019 Lect 9 Heaps

    30/44

    30

    Deletion from a Max Heap

    Exchange the position with 15

    Are we finished?

    6

    7

    2 6

    5 1 7

    9

    15

    8

  • 8/12/2019 Lect 9 Heaps

    31/44

  • 8/12/2019 Lect 9 Heaps

    32/44

    32

    Max Heap Initialization

    Heap initialization means to construct a heap by

    adjusting the tree if necessary

    Example: input array = [-,1,2,3,4,5,6,7,8,9,10,11]

  • 8/12/2019 Lect 9 Heaps

    33/44

    33

    Max Heap Initialization

    - Start at rightmost array position that has a child.

    - Index is floor(n/2).

  • 8/12/2019 Lect 9 Heaps

    34/44

    34

    Max Heap Initialization

  • 8/12/2019 Lect 9 Heaps

    35/44

    35

    Max Heap Initialization

  • 8/12/2019 Lect 9 Heaps

    36/44

    36

    Max Heap Initialization

  • 8/12/2019 Lect 9 Heaps

    37/44

    37

    Max Heap Initialization

  • 8/12/2019 Lect 9 Heaps

    38/44

    38

    Max Heap Initialization

    Are we finished?

    Done!

  • 8/12/2019 Lect 9 Heaps

    39/44

    39

    Heapsort

    Goal:

    Sort an array using heap representations

    Idea:

    Build a max-heapfrom the array

    Swap the root (the maximum element) with the last

    element in the array Discard this last node by decreasing the heap size

    Call MAX-HEAPIFY on the new root

    Repeat this process until only one node remains

  • 8/12/2019 Lect 9 Heaps

    40/44

    40

    Example: A=[7, 4, 3, 1, 2]

    MAX-HEAPIFY(A, 1, 4) MAX-HEAPIFY(A, 1, 3) MAX-HEAPIFY(A, 1, 2)

    MAX-HEAPIFY(A, 1, 1)

  • 8/12/2019 Lect 9 Heaps

    41/44

    Mapping into an array

    Notice:

    The left child of indexi is at index2*i+1

    The right child of index iis at index2*i+2

    Example: the children of node 3(19) are 7(18) and 8 (14)

    19

    1418

    22

    321

    14

    119

    15

    25

    1722

    25 22 17 19 22 14 15 18 14 21 3 9 11

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

  • 8/12/2019 Lect 9 Heaps

    42/44

    Removing and replacing the root

    The root is the first element in the array The rightmost node at the deepest level is the last element

    Swap them...

    ...And pretend that the last element in the array no longer

    existsthat is, the last index is 11(9)

    25 22 17 19 22 14 15 18 14 21 3 9 11

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

    11 22 17 19 22 14 15 18 14 21 3 9 25

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

  • 8/12/2019 Lect 9 Heaps

    43/44

    Reheap and repeat

    Reheap the root node (index 0, containing 11)...

    ...And again, remove and replace the root node

    Remember, though, that the last array index is changed

    Repeat until the last becomes first, and the array is sorted!

    22 22 17 19 21 14 15 18 14 11 3 9 250 1 2 3 4 5 6 7 8 9 10 11 12

    9 22 17 19 22 14 15 18 14 21 3 22 25

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

    11 22 17 19 22 14 15 18 14 21 3 9 25

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

  • 8/12/2019 Lect 9 Heaps

    44/44

    Questions