Download presentation
Presentation is loading. Please wait.
Published bySuharto Setiawan Modified over 6 years ago
1
original list {67, 33,49, 21, 25, 94} pass { } {67 94} pass {25 21} {} {} {94} pass {21} {} ~~ ~~ ~~ ~~ ~~ sorted list {21, 25, 33, 49, }
2
Assignment 8 questions???
3
Zybook assignment before class on Thursday chapter 24 (section 1)
4
Remaining topics Heap Balanced binary search trees
Data structure for implementing a priority queue Basis of heapsort algorithm Balanced binary search trees
5
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
6
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
7
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
8
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
9
A complete binary tree can be efficiently stored in an array
size 12
10
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? size 12
11
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
12
size 12 Add 7
13
size 13 7 7 Add 7
14
7 8 size 13 50 7 8 50 Add 7
15
What will a min heap built from the following items look like
size
16
2 / \ / \ / size 6
17
Big O of adding an item to a heap?
18
Big O depends on height of the binary tree
A complete binary tree holding n items has a height of log2 n
19
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)
20
size 12 Remove smallest
21
55 size 12 11 55 step 1
22
8 16 55 size 11 8 16 55 Trickle down
23
Do 3 removes 2 / \ / \ / size 6
24
After first remove 3 / \ 5 4 / \ 8 7 size 5 3 5 4 8 7 4
/ \ / \ size 5
25
After second remove 4 / \ / 8 size 4
26
after 3 removes 5 / \ size 3
27
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
28
Given a randomly ordered array how can we use a heap to put the array in sorted order?
29
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
30
heapify Heap size is 0 Heap size is 1 Heap size is 2 Heap size is 3
Heap size is 0 Heap size is 3 Heap size is 1 Heap size is 2 Heap size is 6
31
Sort_heap Heap size is 6 Heap size is 5 Heap size is 4 Heap size is 3
Heap size is 6 Heap size is 5 Heap size is 4 Heap size is 3 Sorted array
32
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)
33
heapsort Is it Stable? Is it Adaptive?
How much extra space does it require?
34
heapsort Is it Stable? Is it Adaptive?
no Is it Adaptive? How much extra space does it require? O(1)
35
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
36
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
37
Is this tree balanced?
38
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
39
What is the balance factor of each node?
40
What is the balance factor of each node?
-1 -1
41
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
42
What happens after adding this element?
-1 -1
43
What happens after adding this element?
-1 =0 -1 0+1=1
44
What happens after adding this element?
-1 -1
45
What happens after adding this element?
-1 -1+-1= -1 0+-1=-1
46
Need to rotate subtree that is out of balance
-1 -1 50 60
47
After rotation -1 -1 40 60
48
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
49
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
50
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?
51
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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.