Download presentation
Presentation is loading. Please wait.
Published byGuy Durrell Modified over 9 years ago
1
Solution of Assignment 3 and Midterm CSC2100B
2
AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its left subtree and smaller key than the nodes in its right subtree AVL tree is a balanced binary search tree such that for every node, the heights of its two subtrees differ by at most one
3
AVL Tree After insertion/deletion, a node may become unbalanced because of change in the height of its subtree We can restore the balance by repeatedly doing rotation at the LOWEST unbalanced node until all nodes are balanced There are four possible cases –Left Left (Right Right) –Left Right (Right Left)
4
Left Call the unbalanced node a Left Left means the left subtree (C) of the left child of a is causing the unbalance The balance can be restored by a single rotation a b A B C h h+1 b a BA C h
5
Left Right Left Right means the right subtree (C) of the left child of a is causing the unbalance We can restore the unbalance by a double rotation a c BC h h+1 b A D Either B or C is too tall a A b c BCD h
6
Special Case After deletion, the height of the left subtree of a node may decrease by one and Left Left and Left Right may happen at the same time In this case, we can always use single rotation (for details see the post “answer for written assignment” in newsgroup)
7
Intermediate Steps Required in Exam In Exam, you need to show the intermediate steps, but don’t need to show the change of pointers, like this one For insertion/deletion, first show me the tree immediately after the insertion/deletion before any rotation is done; if rotation is needed, then also show the tree after the rotation. Between each state, you should draw an arrow and label it with Insert k, Delete k or LL Rotate
8
3.15 Let P(h) be the proposition that at most h/2 rotations are needed after deleting a key from a AVL tree of height h. We claim that P(h) is true. Base case: When h<= 1, no rotation is needed, so the claim is true. Assume that P(h) is true for 2 <= h <= k. Consider the case when h = k+1. Case 1 (The key to delete is at the root): By assumption h >= 2, so the root has 2 children. We replace the key of the root by the smallest key in the right subtree of root and then remove that key. This is reduced to the case 2 or case 3. Case 2 (The key to delete is at a child c of the root): Suppose c has only one child d. In this case, we remove c and append the subtree rooted d to the root. The only node that can be unbalanced is the root and at most one rotation is needed. So assume that c has two children. This case, same as case 1, can be reduced to case 3.
9
3.15 Case 2 (The key to delete is at a child c of the root): Suppose c has only one child d. In this case, we remove c and append the subtree rooted d to the root. The only node that can be unbalanced is the root and at most one rotation is needed. So assume that c has two children. This case, same as case 1, can be reduced to case 3. Case 3 (The key to delete is in a subtree T’ rooted at a node of depth 2): The height of T’ is at most k-1. By the inductive assumption, at most(k- 1)/2 rotations is needed to restore the AVL property of T’. If the height of T’ remains the same after these rotations, then T is also balanced. In case the height of T’ decreases by one after restoring the AVL property of T’, then one more rotation is needed. So in total at most (k- 1)/2+1 = (k+1)/2 rotations are needed to restore the AVL tree property of T after the deletion of a key.
10
Big-Oh Notation Some useful rules about big oh notation –If a term has some constant in the front, drop the constant. e.g. O(3n) = O(n), O(25) = O(1) –Remember the order O(1) < O(log n) < O(n 1/2 ) < O(n) < O(n log n) < O(n 2 ) < O(n 3 ) < O(2 n ) < O(n!) < O(n n ) –If there are more than one term, just keep the highest order one. e.g. O(log n + n 2 + n log n) = O(n 3 ) –If g(n) > O(1), O( f(n) g(n) ) > O( f(n) ). e.g. O(n 2 log n) > O(n 2 )
11
Most Crucial Function Suppose we use D 1 in a program for solving some problem In this program, three operations are used (called for some constant number of times) and they have worst case time complexities f 1 = n 2 log n, f 2 = n! and f 3 = n log n respectively So the worst case total running time is c 1 n 2 log n + c 2 n! + c n n log n where c 1, c 2, c 3 are some constants As n! grows much faster than n 2 log n and n log n (for n=100, n!= 9.33e+157, n 2 log n=66438 and n log n=664.38), the other two terms become insignificant when n is large Therefore the most crucial function is f 2
12
Finding Interception Point To find the interception point of two functions f 3 D1 (n) and f 3 D2 (n), just solve the equation f 3 D1 (n) = f 3 D2 (n) 10 n = n 2 => n = 0 or n = 10 Since the time complexity is undefined for n<0, the first case is rejected
13
Implementation of Complete Binary Tree 1 st Implementation Each node has two pointers pointing to its left and right childs
14
Implementation of Complete Binary Tree 2 nd Implementation A node has a pointer to its first child and a pointer to its next sibling
15
Implementation of Complete Binary Tree 3 rd implementation Implementation using array The parent, left child and right child of the node at position k are at positions ceiling(k/2)-1, 2k+1 and 2(k+1) respectively. Address 0123456 Key 7361254
16
Solving Recurrence 1) Expand T(n) by applying the definition of T(n) for several times. You may also start by evaluating T(1), T(2), … but starting from T(n) may be easier 2) Find the general pattern. Note the second to the last term all have the form ba i (n-i). Pay attention to the first term and last term to find the first and last index of the summation. When solving a recurrence, unless specified, otherwise you DON’T need to simplify it to get a closed form expression. Just giving the summation is okay. But T(n) should not appear in the last line.
17
Evaluating Nested Loops 1) First you evaluate the innermost loop. When you evaluate it, you can ignore the outer loops and consider i, j as fixed. So It is just summing up 1 for in times. These correspond to the first, second, and third loops. 2) Similarly in this summation, i is fixed so this is just summing up in for (n-i+1) times 3) A series of the sum of two terms is the same as the sum of two series of the individual terms, i.e.
18
Linked List vs Stack/Queue Access, Insertion and Deletion of data in the middle Searching without changing the data structure
19
Singly Linked List vs Doubly Linked List Backward traversal Finding the last k-th element in the list Insertion/Deletion of the previous element
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.