Professional Documents
Culture Documents
Lecture 9
Dr. Adil Yousif
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.
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.
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 node B 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).
Example of Max-Heap
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., a binary search tree). The heap relation mentioned above applies only between nodes and their immediate parents. The maximum number of children each node can have depends on the type of heap, but in many types it is at most two, which is known as a "binary heap"
Constructing a heap
A tree consisting of a single node is automatically a heap We construct a heap by adding nodes one at a time:
Add
the node just to the right of the rightmost node in the deepest level If the deepest level is full, start a new level
Examples:
8 8
10 8
2
10 5
3
10 8 12 5 8 12
10 5 8 10
12 5
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 parent got smaller, its parent is still greater than it was originally
20
20
20
20
Notice that the largest number is now in the root Suppose we discard the root:
11 22 17
19 18
22 14 21 3 9
14 11
15
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
Our tree is balanced and left-justified, but no longer a heap However, only the root lacks the heap property
11 22 17
19 18
22 14 21 3 9
14
15
We can siftUp() the root After doing this, one and only one of its children may have lost the heap property
Now the left child of the root (still the number 11) lacks the heap property
22 11 17
19 18
22 14 21 3 9
14
15
We can siftUp() this node After doing this, one and only one of its children may have lost the heap property
Now the right child of the left child of the root (still the number 11) lacks the heap property:
22 22 17
19 18
11 14 21 3 9
14
15
We can siftUp() this node After doing this, one and only one of its children may have lost the heap property but it doesnt, because its a leaf
Our tree is once again a heap, because every node in it has the heap property
22 22 17
19 18
21 14 11 3 9
14
15
Once again, the largest (or a largest) 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
20
15
15
15
15
15
15
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]
Heapsort
Goal:
Sort
Idea:
Build Swap
Repeat
39
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)
40
19
22
14
15
18
0 1
14
2 3
21
4 5 6
3
7
9
8 9
11
10 11 12
25 22 17 19 22 14 15 18 14 21 3
9 11
Notice:
The left child of index i is at index 2*i+1 The right child of index i is at index 2*i+2 Example: the children of node 3 (19) are 7 (18) and 8 (14)
The root is the first element in the array The rightmost node at the deepest level is the last element Swap them...
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3
9 11
10
11
12
11 22 17 19 22 14 15 18 14 21 3
9 25
...And pretend that the last element in the array no longer existsthat is, the last index is 11 (9)
11 22 17 19 22 14 15 18 14 21 3
0 1 2 3 4 5 6 7 8 9 10
9 25
11 12
22 22 17 19 21 14 15 18 14 11 3
9 25
10
11
12
9 22 17 19 22 14 15 18 14 21 3 22 25
...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!
Questions