{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

dis8

dis8 - CS32 Discussion Sec.on 1B Week 9 TA: Brian...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS32 Discussion Sec.on 1B Week 9 TA: Brian Choi Reminder •  Project 4 –  Hash table vs. binary search tree •  Just use a hash table Heaps •  A heap is a –  complete binary tree –  every node carries a value greater than or equal to its children’s (maxHeap). –  usually implemented as an array 9 7 6 1 5 2 Heaps: opera.ons •  3 opera.ons for heaps –  ﬁndMax (search) –  insertNode (insert) –  deleteMax (remove) 9 7 6 1 5 2 ﬁndMax •  What do you think? 9 7 6 1 5 2 insertNode •  Not so trivial •  We ﬁrst add the new node and ﬁx it 9 7 6 1 5 2 insertNode 1.  Add the new node to the tail. 2.  Ask: –  Is the new value greater than its parent? –  If so: ?? –  Else: ?? 9 7 6 1 5 2 8 insertNode 1.  Add the new node to the tail. 2.  Ask: –  Is the new value greater than its parent? –  If so: swap –  Else: done 9 7 8 1 5 2 6 insertNode •  What is the index of node i’s parent in the array? 9 7 8 1 5 2 6 insertNode •  What is the index of node i’s parent in the array? –  parent = (i  ­ 1) / 2 9 7 8 1 5 2 6 insertNode •  Running .me? 9 7 8 1 5 2 6 insertNode •  Running .me? –  propor.onal to the height of the tree: O(log n) 9 7 8 1 5 2 6 deleteMax •  Again, take the ac.on ﬁrst and ﬁx it. •  Fill in the void ﬁrst. deleteMax •  Now compare the values of the two children, take the greater of the two (why?), and swap. •  What are the indices of –  Lea child: –  Right child: of the node i? deleteMax •  Now compare the values of the two children, take the greater of the two (why?), and swap. •  What are the indices of –  Lea child: 2 * i + 1 –  Right child: 2* i + 2 of the node i? deleteMax •  Now compare the values of the two children, take the greater of the two (why?), and swap. •  What are the indices of –  Lea child: 2 * i + 1 –  Right child: 2* i + 2 of the node i? Heapsort • Can you use a heap to sort a set of elements? Heapsort •  Can you use a heap to sort a set of elements? –  Insert all elements into a heap –  Extract the maximum element from the heap one by one Heapsort •  Can you use a heap to sort a set of elements? –  Insert all elements into a heap –  Extract the maximum element from the heap one by one •  Running .me? Heapsort •  Can you use a heap to sort a set of elements? –  Insert all elements into a heap –  Extract the maximum element from the heap one by one •  Running .me? Inser.ng n items: n x O(log n) = O(n log n) Extrac.ng n items: n x O(log n) = O(n log n) O(n log n) + O(n log n) = O(n log n) In ­place Heapsort •  Heapsort is an in ­place sor.ng algorithm – you don’t need an auxiliary structure for the sor.ng opera.on. •  Let us try using a maxHeap to sort the elements in an array in an increasing order. In ­place Heapsort build the maxHeap extract 2 3 1 5 4 4 3 1 2 5 2 3 1 5 4 3 2 1 4 5 3 2 1 5 4 2 1 3 4 5 3 2 1 5 4 1 2 3 4 5 5 3 1 2 4 1 2 3 4 5 5 4 1 2 3 part of the maxHeap Hash Func.ons •  Hashing –  Take a “key” and map it to a number “David Smallberg” Hash Func.on H 4531 A requirement for hash func.on H: should return the same value for the same key. •  A good hash func.on –  spreads out the values: two diﬀerent keys are likely to result in diﬀerent hash values –  computes each value quickly •  Hash Table … 4530 4531 D Hash func 4531 4532 4533 B Hash func 4533 D … array B Hash Table … 4530 4531 C Hash func 4531 D C 4532 4533 B … array May collide, so make a linked list! Hash Table •  Running .me –  Insert? –  Remove? –  Search? … 4530 4531 D C 4532 4533 B … array May collide, so make a linked list! Hash Table •  Running .me –  Insert? O(1) –  Remove? O(1) –  Search? O(1) … 4530 4531 D C 4532 4533 B … array May collide, so make a linked list! ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online