Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
aa:lab:08 [2020/11/20 21:08]
calin_andrei.bucur
aa:lab:08 [2020/11/20 22:31] (current)
calin_andrei.bucur
Line 5: Line 5:
 Binary Heaps are binary trees with the following properties: Binary Heaps are binary trees with the following properties:
  
- ​**I.**  ​forall nodes n: if c is a child of n, then n.value ​>= c.value+ ​**I.**  ​Any node's $math[n] value is greater ​then his child'​s value $math[c]. ($math[n.value ​\geq c.value])
    
- ​**II.** ​the tree is almost complete (missing elements are possible only on the last level)+ ​**II.** ​The tree is almost complete (missing elements are possible only on the last level)
  
          15          15
Line 18: Line 18:
    15 9 5 2 3 1    15 9 5 2 3 1
  
-This corresponds to a BF traversal of the tree, and it guarantees that the children nodes corresponding to v[i] are v[2i + 1] and v[2i + 2]+This corresponds to a BF traversal of the tree, and it guarantees that the children nodes corresponding to **v[i]** are **v[2i + 1]** and **v[2i + 2]**
  
-**1** Basic operations. Implement and analyse the complexity for the following operations:+---- 
 + 
 + 
 +**1.** Basic operations. Implement and analyse the complexity for the following operations: 
 + 
 +**1.1** **empty_heap()** - creates an empty heap. 
 + 
 +**1.2** **insert(v, h)** - inserts $math[v] in $math[h]. At the end of the procedure , $math[h] is the updated heap. 
 + 
 +**1.3** **get_max(h)** - return the maximum value from the heap. 
 + 
 +**1.4** **delete(pos,​h)** - deletes element at position $math[pos] from the heap. 
 + 
 +       ​algorithm:​ 
 +          swap the last element with the element whose position is to be deleted 
 +          the resulting tree may no longer be a heap 
 +          (!!) make sure the heap property is preserved  
 + 
 + 
 +Deleting 9: 
 + 
 +        15 
 +      /    \ 
 +     ​9 ​     5 
 +    / \    / 
 +   ​2 ​  ​3 ​ 1   
 + 
 +9 is swapped with 1, then deleted 
 + 
 +        15 
 +      /    \ 
 +     ​1 ​     5 
 +    / \    / 
 +   ​2 ​  ​3 ​ 9   
 + 
 +Here, we make sure the heap property is preserved by swapping the largest of the children to be 
 +with the current root and repeating the process all over for the modified subtree: 
 + 
 +        15 
 +      /    \ 
 +     ​1 ​     5 
 +    / \  
 +   ​2 ​  ​3 ​  
 + 
 +        15 
 +      /    \ 
 +     ​3 ​     5 
 +    / \  
 +   ​2 ​  1  
 + 
 +---- 
 + 
 +**2.** Using heaps: 
 + 
 +**2.1** Implement a procedure which constructs a heap from an arbitrary array, using the insert operation. 
 + 
 +**2.2** Implement heapsort using the previous procedure. What's the complexity?​ 
 + 
 + 
 +       ​algorithm(v):​ 
 +          build a heap from the array 
 +          extract the top and insert it at the end of the array 
 +          repeat the previous step until the heap is empty 
 + 
 +**2.3** Implement the following heap-creation procedure, due to Robert W. Floyd: 
 + 
 +Suppose we have an unsorted array: 
 + 
 +   n = 10 
 +   5 3 2 1 4 8 6 7 0 9 
 + 
 +which we treat as a tree, not yet a heap: 
 + 
 +                     5 
 +                   / ​  \ 
 +                 ​2 ​     3 
 +               / ​  ​\ ​  / ​ \ 
 +              1     9 8    6 
 +             / \   / 
 +            7   0 4 
 + 
 +We start from the last level, which in this case is $math[ceil(log(10)) = 4] 
 +from $math[j = 2^{(4-1)}] (which is 8) to n-1. On the last level, each tree is a heap. 
 + 
 +We now move to a previous level. Say the level is i: 
 + 
 +from $math[j = 2^{(i-1)}] to $math[2^i-1]:​ 
 + 
 +we send-down each value on this level, just like in the deletion algorithm, by keeping as the current root, 
 +the largest between the two children. The sending down procedure repeats recursively,​ until we have heaps on level i. Example: 
 + 
 +                     5 
 +                   / ​  \ 
 +                 ​2 ​     3 
 +               / ​  ​\ ​  / ​ \ 
 +              7     9 8    6 
 +             / \   / 
 +            1   0 4 
 + 
 + 
 +we repeat the process on the next level: 
 + 
 +                     5 
 +                   / ​  \ 
 +                 ​9 ​     8 
 +               / ​  ​\ ​  / ​ \ 
 +              7     4 3    6 
 +             / \   / 
 +            1   0 2 
 + 
 + 
 +until we reach the first level: 
 + 
 +                     9 
 +                   / ​  \ 
 +                 ​7 ​     8 
 +               / ​  ​\ ​  / ​ \ 
 +              5     4 3    6 
 +             / \   / 
 +            1   0 2 
 + 
 +Notice that 5 is sent-down on its respective level. 
 + 
 +Analyse the complexity.
  
-**1.1** **empty_heap()** - creates an empty heap