Solution of Assignment 3 and Midterm CSC2100B. AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its.

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

Rizwan Rehman Centre for Computer Studies Dibrugarh University
AVL Trees binary tree for every node x, define its balance factor
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
AVL Trees Balanced Trees. AVL Tree Property A Binary search tree is an AVL tree if : –the height of the left subtree and the height of the right subtree.
Tirgul 5 AVL trees.
C++ Programming:. Program Design Including
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
CSE 326: Data Structures AVL Trees
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
AVL Trees ITCS6114 Algorithms and Data Structures.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Data Structures AVL Trees.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
AVL Tree: Balanced Binary Search Tree 9.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Balancing Binary Search Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Introduction Applications Balance Factor Rotations Deletion Example
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Red-Black Trees Motivations
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CS223 Advanced Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS223 Advanced Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL Tree By Rajanikanth B.
Data Structures Lecture 21 Sohail Aslam.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Solution of Assignment 3 and Midterm CSC2100B

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

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)

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

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

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)

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

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.

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.

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 )

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

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

Implementation of Complete Binary Tree 1 st Implementation Each node has two pointers pointing to its left and right childs

Implementation of Complete Binary Tree 2 nd Implementation A node has a pointer to its first child and a pointer to its next sibling

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 Key

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.

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.

Linked List vs Stack/Queue Access, Insertion and Deletion of data in the middle Searching without changing the data structure

Singly Linked List vs Doubly Linked List Backward traversal Finding the last k-th element in the list Insertion/Deletion of the previous element