original list {67, 33,49, 21, 25, 94} pass 1 {25 33 21} 49 {67 94} pass 2 {25 21} 33 {} {} 67 {94} pass 3 {21} 25 {} ~~ ~~ ~~ ~~ ~~ sorted list {21, 25, 33, 49, 67 94}
Assignment 8 questions???
Zybook assignment before class on Thursday chapter 24 (section 1)
Remaining topics Heap Balanced binary search trees Data structure for implementing a priority queue Basis of heapsort algorithm Balanced binary search trees
a binary tree A binary tree is either Empty Consists of an item called the root and 2 binary trees called the left subtree and the right subtree
Some special binary trees A binary search tree is a binary tree that has the bst property Each item holds a value Its left child is either empty or holds a smaller value Its right child is either empty or holds a larger value A heap Is A complete binary tree that has the heap order property 45 36 56 42 25 52 60 30 50
A complete binary tree All levels except the bottom most have all possible nodes Nodes on the bottom most level fill the left most positions
Heap order property Max heap Min heap Element stored in each node is >= element stored in each of its children Min heap Element stored in each node is <= element stored in each of its children 34 25 32 15 12 27 5 13 10
A complete binary tree can be efficiently stored in an array size 12 0 1 2 3 4 5 6 7 8 9 10 11 12 -----
Allows computation of location of an element’s parent, left child and right child left child of i is at 2 * i + 1 right child of i is at 2 * i + 2 parent of i is at (i – 1) / 2 How can we tell if there is no left or right child? How can we tell if there is no parent? 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- size 12
Adding an item to a heap Put the new item at the end Increment size Do comparisons with parents to let the item percolate up to where it belongs
size 12 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Add 7
size 13 7 7 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Add 7
7 8 size 13 50 7 8 50 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Add 7
What will a min heap built from the following items look like size 0 1 2 3 4 5 6 7
2 / \ 5 3 / \ / 8 7 4 size 6 2 5 3 8 7 4 0 1 2 3 4 5 6 7
Big O of adding an item to a heap?
Big O depends on height of the binary tree A complete binary tree holding n items has a height of log2 n
Removing smallest/largest item from a heap Item being removed is in position 0 Move last item in the heap to position 0 (temporarily) decrement size Do comparisons with children to let the item trickle down to where it belongs Maximum number of comparisons is the height of the tree Height of a complete tree is (log2 n)
size 12 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Remove smallest
55 size 12 11 55 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- step 1
8 16 55 size 11 8 16 55 0 1 2 3 4 5 6 7 8 9 10 11 12 ----- Trickle down
Do 3 removes 2 / \ 5 3 / \ / 8 7 4 size 6 2 5 3 8 7 4 0 1 2 3 4 5 6 7
After first remove 3 / \ 5 4 / \ 8 7 size 5 3 5 4 8 7 4 / \ 5 4 / \ 8 7 size 5 3 5 4 8 7 4 0 1 2 3 4 5 6 7
After second remove 4 / \ 5 7 / 8 size 4 4 5 7 8 7 4 0 1 2 3 4 5 6 7
after 3 removes 5 / \ 8 7 size 3 5 8 7 8 7 4 0 1 2 3 4 5 6 7
Uses of a heap Data structure for implementing a priority queue Enqueue and deque both O(log2 N) Front O(1) How are equal priorities treated? Basis of the heapsort algorithm
Given a randomly ordered array how can we use a heap to put the array in sorted order?
Heapsort strategy Step 1: form_heap (heapify) Step 2: sort_heap Reorganize the array so it forms a heap Step 2: sort_heap Put the elements in the heap into sorted order
heapify Heap size is 0 Heap size is 1 Heap size is 2 Heap size is 3 0 1 2 3 4 5 6 7 2 7 4 8 5 3 4 7 2 8 5 3 Heap size is 0 Heap size is 3 Heap size is 1 Heap size is 2 0 1 2 3 4 5 6 7 2 5 3 8 7 4 Heap size is 6
Sort_heap Heap size is 6 Heap size is 5 Heap size is 4 Heap size is 3 0 1 2 3 4 5 6 7 2 5 3 8 7 4 Heap size is 6 0 1 2 3 4 5 6 7 3 5 4 8 7 2 Heap size is 5 0 1 2 3 4 5 6 7 4 5 7 8 3 2 Heap size is 4 0 1 2 3 4 5 6 7 5 8 7 4 3 2 Heap size is 3 Sorted array 0 1 2 3 4 5 6 7 8 7 5 4 3 2
Heapsort performance Step 1: heapify Step 2: sort_heap Reorganize the array so it forms a heap Adding an element to a heap is O(log2 n) Adding n elements is O(N log2 n) Step 2: sort_heap Put the elements in the heap into sorted order Removing an element from a heap is O(log2 n) Removing n elements is O(N log2 n) Heapsort performance is O(N log2 n)
heapsort Is it Stable? Is it Adaptive? How much extra space does it require?
heapsort Is it Stable? Is it Adaptive? no Is it Adaptive? How much extra space does it require? O(1)
balanced binary search trees How can the worst case performance of a BST be prevented? When adding and removing elements, make sure the tree remains “balanced” Requires more work when adding and removing but ensures that find and retrieve will be O(log2 n) Avl trees and red-black trees are balanced bst
Avl trees Will never have a height greater than 1.44 log2 n An avl tree is a binary tree with the bst property Additions and removals are done in such a way that the tree is kept “balanced” All other operations the same as for a bst A tree is balanced if the height of its two subtrees differ by at most 1
Is this tree balanced?
Each node of an avl tree has a balance factor data member The balance factor of a node is the height of its left subtree minus the height of its right subtree 0 – tree with that node as its root is balanced 1 – tree with that node as its root is left heavy -1 – tree with that node as its root is right heavy +2 or -2 tree with that node as its root is out of balance Needs to be rebalanced using rotations
What is the balance factor of each node?
What is the balance factor of each node? -1 -1 -1 -1
Adding an element Search exactly the same as for bst After adding a node more work to be done Balance factors of nodes on the search path need to be updated What is the balance factor of the node that was added? Back up the search path At each node update balance factor by Adding 1 if element added to its left subtree Subtracting 1 if element added to its right subtree If updating changes a balance factor to 0 quit – no rebalancing needed If updating changes a balance factor to 2 or -2 tree is no longer balanced Get it back into balance by performing a rotation
What happens after adding this element? -1 -1 -1 -1
What happens after adding this element? -1 -1 -1+1=0 -1 0+1=1
What happens after adding this element? -1 -1 -1 -1
What happens after adding this element? -1 -1+-1=-2 -1 -1 0+-1=-1
Need to rotate subtree that is out of balance -1 40 -1 -1 50 60
After rotation -1 50 -1 -1 40 60
rotations Rotations can be Left rotation Right rotation Right-left rotation Left-right rotation Rotations require extra time but big 0 of insert is still log2 n involves only the subtree whose root is the node that went out of balance Removing an element also requires rotations to keep tree balanced
Final exam Topics since exam2 (about 2/3) Hash tables Stl Graphs Sorting Balance BST Topics covered on exam 1 and exam 2 (about 1/3) Selecting the best data structure for a specific application
example A campus club is planning a fund raiser. Each member is asked to give her best idea. The club secretary enters each member’s name and idea into a dictionary. Later a report listing all the ideas will be printed. What dictionary implementation would you choose and why would you choose it?
Questions to ask What operations are needed? Add, remove, retrieve/find, traverse Are operations based on position? Or on value? How frequently are each of operations performed? Is it necessary to traverse the items in sorted order? If so, how often? Is there a maximum size for the collection? Will the size grow and shrink over time?