CSE 100: TREAPS and RANDOMIZED SEARCH TREES Midterm Review
Total Page:16
File Type:pdf, Size:1020Kb
CSE 100: TREAPS AND RANDOMIZED SEARCH TREES Midterm Review • Practice Midterm covered during Sunday discussion Today • Run time analysis of building the Huffman tree • AVL rotations and treaps Huffman’s algorithm 0. Determine the count of each symbol in the input message. 1. Create a forest of single-node trees containing symbols and counts for each non-zero-count symbol. 2. Loop while there is more than 1 tree in the forest: 2a. Remove the two lowest count trees 2b. Combine these two trees into a new tree (summing their counts). 2c. Insert this new tree in the forest, and go to 2. 3. Return the one tree in the forest as the Huffman code tree. How long do these steps take? N = number of distinct symbols Huffman’s algorithm K = number of symbols in the file 0. Determine the count of each symbol in the input message. 1. Create a forest of single-node trees containing symbols and counts for each non-zero-count symbol. 2. Loop while there is more than 1 tree in the forest: 2a. Remove the two lowest count trees 2b. Combine these two trees into a new tree (summing their counts). 2c. Insert this new tree in the forest, and go to 2. 3. Return the one tree in the forest as the Huffman code tree. How long do these steps take, using a heap-implemented Priority Queue? N = number of distinct symbols Huffman’s algorithm K = total number of symbols in the file 0. Determine the count of each symbol in the A. O(logN) input message. B. O(N) C. O(N*logN) 1. Create a forest of single-node trees D. O(N^2) containing symbols and counts for each E. other non-zero-count symbol. 2. Loop while there is more than 1 tree in the forest: 2a. Remove the two lowest count trees 2b. Combine these two trees into a new tree (summing their counts). 2c. Insert this new tree in the forest, and go to 2. 3. Return the one tree in the forest as the Huffman code tree. How long do these steps take, using a heap-implemented Priority Queue? N = number of distinct symbols Huffman’s algorithm K = number of symbols in the file 0. Determine the count of each symbol in the A. O(logN) input message. B. O(N) C. O(N*logN) 1. Create a forest of single-node trees D. O(N^2) containing symbols and counts for each E. O(K) non-zero-count symbol. 2. Loop while there is more than 1 tree in the forest: 2a. Remove the two lowest count trees 2b. Combine these two trees into a new tree (summing their counts). 2c. Insert this new tree in the forest, and go to 2. 3. Return the one tree in the forest as the Huffman code tree. How long do these steps take, using a heap-implemented Priority Queue? N = number of distinct symbols Huffman’s algorithm K = number of symbols in the file 0. Determine the count of each symbol in the A. O(logN) input message. B. O(N) C. O(N*logN) 1. Create a forest of single-node trees D. O(N^2) containing symbols and counts for each E. O(K) non-zero-count symbol. 2. Loop while there is more than 1 tree in the forest: 2a. Remove the two lowest count trees 2b. Combine these two trees into a new tree (summing their counts). 2c. Insert this new tree in the forest, and go to 2. 3. Return the one tree in the forest as the Huffman code tree. How long do these steps take, using a heap-implemented Priority Queue? N = number of distinct symbols Huffman’s algorithm K = number of symbols in the file 0. Determine the count of each symbol in the A. O(logN) input message. B. O(N) C. O(N*logN) 1. Create a forest of single-node trees D. O(N^2) containing symbols and counts for each E. O(K) non-zero-count symbol. 2. Loop while there is more than 1 tree in the forest: 2a. Remove the two lowest count trees 2b. Combine these two trees into a new tree (summing their counts). 2c. Insert this new tree in the forest, and go to 2. 3. Return the one tree in the forest as the Huffman code tree. How long do these steps take, using a heap-implemented Priority Queue? N = number of distinct symbols Huffman’s algorithm K = number of symbols in the file 0. Determine the count of each symbol in the A. O(logN) input message. B. O(N) C. O(N*logN) 1. Create a forest of single-node trees D. O(N^2) containing symbols and counts for each E. O(K) non-zero-count symbol. 2. Loop while there is more than 1 tree in the forest: 2a. Remove the two lowest count trees 2b. Combine these two trees into a new tree (summing their counts). 2c. Insert this new tree in the forest, and go to 2. 3. Return the one tree in the forest as the Huffman code tree. How long do these steps take, using a heap-implemented Priority Queue? N = number of distinct symbols Huffman’s algorithm K = number of symbols in the file 0. Determine the count of each symbol in the O(K) input message. 1. Create a forest of single-node trees O(N*logN) containing symbols and counts for each non-zero-count symbol. 2. Loop while there is more than 1 tree in the forest: 2a. Remove the two lowest count trees O(N*logN) 2b. Combine these two trees into a new tree (summing their counts). 2c. Insert this new tree in the forest, and go to 2. 3. Return the one tree in the forest as the O(1) Huffman code tree. How long does the whole thing take, using a heap-implemented Priority Queue? Treap insert G 50 C H 35 29 D B 8 I 24 25 F 40 A L 21 16 E 33 J 13 I am midway in the insertion of F,40 into the treap. K Have my steps been correct so far? 9 A. Yes B. No C. Not enough information to comment Treap insert G 50 C H 35 29 B E I 24 33 25 A D F L 21 8 40 16 J 13 Insert (F,40) K 9 AVL rotation to the rescue! G 50 C H 35 29 B F I 24 40 25 A E L 21 33 16 J D 13 8 K 9 Step 1: Insert using BST insert Step 2: Fix heap ordering Insert (F, 40) AVL rotation to the rescue! G 50 rotate F H 40 29 C I 35 25 B E L 24 33 16 J A D 13 21 8 K 9 How would you delete in a treap? Rotate down! G 50 F H 40 29 C I 35 25 B E L 24 33 16 J A D 13 21 8 K To delete C, rotate it? 9 A. Left B. Right C. None of these How would you delete in a treap? Rotate down! G 50 F H 40 29 E I 33 25 C L 35 16 B D 24 8 J 13 K A 9 21 Delete C How would you delete in a treap? Rotate down! G 50 F H 40 29 E I 33 25 B L 24 16 J A C 13 21 35 K D 9 8 Delete C How would you delete in a treap? Rotate down! G 50 F H 40 29 E I 33 25 B L 24 16 J D A 13 21 8 K C 9 35 Delete C How would you delete in a treap? Rotate down! G 50 F H 40 29 E I 33 25 B L 24 16 J D A 13 21 8 K 9 Delete C Why Treaps? • Treaps are worth studying because... • they permit very easy implementations of split and join operations, as well as pretty simple implementations of insert, delete, and find • they are the basis of randomized search trees, which have performance comparable to balanced search trees but are simpler to implement • they also lend themselves well to more advanced tree concepts, such as weighted trees, interval trees, etc. • We will look at the first two of these points Tree splitting • The tree splitting problem is this: • Given a tree and a key value K not in the tree, create two trees: One with keys less than K, and one with keys greater than K L Insert (K, Inf) 50 Try with K=‘K’ C M 35 29 B 24 Tree splitting • This is easy to solve with a treap, once the insert operation has been implemented: • Insert (K,INFINITY) in the treap • Since this has a higher priority than any node in the heap, it will become the root of the treap after insertion • Because of the BST ordering property, the left subtree of the root will be a treap with keys less than K, and the right subtree of the root will be a treap with keys greater than K. These subtrees then are the desired result of the split • Since insert can be done in time O(H) where H is the height of the treap, splitting can also be done in time O(H) • (yes, this same idea could be used in an ordinary BST as well...) How could you do a join? For the answer, see http://cseweb.ucsd.edu/users/kube/cls/100/Lectures/lec5/lec5.pdf Worst case time to find in a treap • The worst case time to find an element in a treap is O(H), where H is the height of the treap.