Practical session No. 6. AVL Tree

Similar documents
Practical session No. 6. AVL Tree

Binary Trees, Binary Search Trees

Binary search trees (BST) Binary search trees (BST)

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Trees. (Trees) Data Structures and Programming Spring / 28

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 21, Antonio Carzaniga 1

Trees. CSE 373 Data Structures

Augmenting Data Structures

CS 380 ALGORITHM DESIGN AND ANALYSIS

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Binary Trees

Binary Search Trees. Nearest Neighbor Binary Search Trees Insertion Predecessor and Successor Deletion Algorithms on Trees.

Binary Search Trees. Binary Search Trees. Nearest Neighbor. Nearest Neighbor

A set of nodes (or vertices) with a single starting point

Algorithms. AVL Tree

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

CS102 Binary Search Trees

Module 4: Dictionaries and Balanced Search Trees

Searching: Introduction

Data Structures in Java

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

TREES. Trees - Introduction

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Binary Search Trees. Analysis of Algorithms

Section 4 SOLUTION: AVL Trees & B-Trees

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

CS350: Data Structures Red-Black Trees

Lecture 6: Analysis of Algorithms (CS )

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Binary Search Trees, etc.

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Advanced Set Representation Methods

Multiway Search Trees. Multiway-Search Trees (cont d)

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Binary Search Trees (10.1) Dictionary ADT (9.5.1)

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Properties of red-black trees

Trees, Binary Trees, and Binary Search Trees

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

Module 4: Priority Queues

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

CS24 Week 8 Lecture 1

Analysis of Algorithms

INF2220: algorithms and data structures Series 1

Trees. Truong Tuan Anh CSE-HCMUT

Sample Exam 1 Questions

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

CSCI Trees. Mark Redekopp David Kempe

CMSC351 - Fall 2014, Homework #2

Programming II (CS300)

Search Trees - 1 Venkatanatha Sarma Y

SFU CMPT Lecture: Week 9

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

AVL Trees. See Section 19.4of the text, p

Lecture 23: Binary Search Trees

Search Trees. Undirected graph Directed graph Tree Binary search tree

Trees. Eric McCreath

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Chapter 5. Binary Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Binary Trees. Recursive definition. Is this a binary tree?

Self-Balancing Search Trees. Chapter 11

Lecture 13: AVL Trees and Binary Heaps

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Binary Search Trees. What is a Binary Search Tree?

8. Binary Search Tree

Multi-Way Search Tree

Outline. Binary Tree

CS350: Data Structures Binary Search Trees

AVL Trees (10.2) AVL Trees

Recursive Data Structures and Grammars

COMP171. AVL-Trees (Part 1)

Part 2: Balanced Trees

Lecture: Analysis of Algorithms (CS )

Priority Queues Heaps Heapsort

Binary Search Trees. See Section 11.1 of the text.

Friday Four Square! 4:15PM, Outside Gates

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Binary Trees. Examples:

CS 350 : Data Structures Binary Search Trees

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Midterm solutions. n f 3 (n) = 3

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Transcription:

Practical session No. 6 AVL Trees Height- Balance Property AVL Tree AVL Interface AVL Height For every internal node v of a tree T, the height of the children nodes of v differ by at most 1. Any binary search tree that satisfies the Height-Balance property. Thus, it has (log n) height, which implies (log n) worst case search and insertion times. The AVL interface supports the following operations in O(log n): insert, search, delete, maximum, minimum, predecessor and successor. Lemma: The height of an AVL tree storing n keys is O(logn) Example of AVL: 2 4 44 3 17 78 1 2 32 50 1 1 48 62 1 88 Question 1 A node in a binary tree is an only-child if it has a parent node but no sibling node (Note: The root does not qualify as an only child). The "loneliness-ratio" of a given binary tree T is defined as the following ratio: LR(T) = (The number of nodes in T that are only children) / (The number of nodes in T). a. Prove that for any nonempty AVL tree T we have that LR(T) 1/2. b. Is it true for any binary tree T, that if LR(T) 1/2 then height(t)=o(lgn)? c. Is it true for any binary tree T, that if there are Θ(n) only-children, all of which are leaves, then height(t)=o(lgn)?

a. In an AVL tree just the leaves may be only-children, and therefore for every only-child in T, there exists a unique parent node that is not an only-child. The total number of onlychildren in T is at most n/2, which means that LR(T) (n/2)/n = 1/2. b. No. Given that LR(T) 1/2, there may be n/2 only-children. This allows for a tree that is more than n/2 in depth. Also, for every full Binary tree T, LR(T)=0. c. No. There can be a tree such that height(t)=θ(n) though all only-children are leaves. Example:

Question 2 (a) Insert the following sequence of elements into an AVL tree, starting with an empty tree:,,,,, 16, 18, 19. (b) Delete in the AVL tree that you got. (a) Red dashed line signifies first part of double rotate action. 16 16 16 18 18 16 19 19 (b). 18 18 18 19 16 19 16 19 16

Question 3 In the class we have seen an implementation of AVL tree where each node v has an extra field h, the height of the sub-tree rooted at v. The height can be used in order to balance the tree. For AVL trees with n nodes, h=o(logn) thus requires O(loglogn) extra bits. 1. How can we reduce the number of extra bits necessary for balancing the AVL tree? 2. Suggest an algorithm for computing the height of a given AVL tree given in the representation you suggested in 1. 1. In addition to the regular BST fields, each node x will have 2-bits balance mark: 00 / h(x.left) > h(x.right) 01 h(x.left) = h(x.right) \ - h(x.left) < h(x.right) 2. We will follow the path from the root to the deepest leaf by following the balance property. If a sub tree is balanced to one side, the deepest leaf resides on that side. calcheight(t) if T=null return -1 if T.balance= / or T.balance= - return 1 + calcheight( T.left ) else return 1 + calcheight( T.right ) Time complexity O(h) (in the previous representation we got the height of the tree in O(1) and in a regular BST in O(n)) Question 4 a. What is the sequence of level-order traversal in the following tree:

b. Given an AVL tree T, is it always possible to build the same tree by a sequence of BSTinsert and delete operations (with no rotations)? a. Level-order traversal - visit every node on a level before going to a lower level (this is also called Breadth-first traversal) F, B, H, A, D, G, J, C, E, I b. Yes, by inserting the nodes according to the tree levels. rebuildavl(avl T) queue Q nodesbylevels(t) newt new BST while(! Q.isEmpty()) n Q.dequque() newt.insert(n) Question 5 Suggest an ADT containing integers that supports the following actions. Explain how these actions are implemented. Init() Initialize the ADT O(1) Insert(x) Insert x into the ADT, if it is not in ADT yet O(log n) Delete(x) Delete x from the ADT, if exists O(log n) Delete_in_place(i) Delete from the ADT an element, which is in the i th O(log n) place (as determined by the order of insertion) among the elements that are in the ADT at that moment. Get_place(x) Return the place (which is determined by the order of insertion) of x among the elements that are in the ADT at that moment. If x does not exist, return -1. O(log n) For example, for the following sequence of actions: Insert(3), Insert(5), Insert(11), Insert(4), Insert(7), Delete(5) Get_place(7) returns 4, and Delete_in_place(2) will delete 11 from the tree.

The ADT consists of 2 AVL trees. - T1 stores the elements by their key. - T2 stores the elements by the order of insertion (using a running counter). To this tree new element will be inserted as the greatest from all elements in this tree. The nodes of this tree store also the number of elements in their subtree x.size. Each node has a pointer to his father. - There are pointers between all the trees connecting the nodes with the same key. For the example above the trees are: Insert(3), Insert(5), Insert(11), Insert(4), Insert(7): Delete(5): Init() initialize 2 empty trees Insert(x) insert an element by key into T1, insert the element as the biggest to T2, and update the pointers. Update the in T2 the field x.size in the insertion path. (The insertion is as in AVL tree) Delete(x) find the element in T1 (regular search), and delete it from both the trees. In T2 go up from the deleted element to the root and update x.size for all the nodes in this path. (The deletion is as in AVL tree)

Delete_by_place(i) find the i th element in T2 in the following way: x T2.root if x.size<i return while(x!=null) if x.left = null z 0 else z x.left.size if (i z) x x.left else if (i = z + 1) Delete(x) and break else // i > z+1 i i (z + 1) x x.right Get_place(x) find x in T1, go by its pointer to T2, then calculate the index of x in the tree Go up from x to the root. In this path sum the number of nodes that are in the left subtree of the nodes in the path that are smaller than x: place 1 place place+x.left.size while (x.parent!= null) { if (x is left_child of x.parent) x x.parent else //x is right child place place+1+x.parent.left.size x x.parent } return place

Question 6 An electrician wants to represent a list of her clients records (by their ID). For each client we would like to mark whether he is a man or she is a woman. Suggest a data structure that supports the following operations in O(log n) time in the worst case, where n is the number of persons (men and women) in the data structure when the operation is executed: 1. Insert(k,c) - Insert a new client c with id = k to the data structure, at first mark the client as a woman. 2. Update(k) Update client with ID = k to be a man. 3. FindDiff(k) Find the difference between the number of women and the number of men ( #of women - #of men ) among all the clients with ID smaller than k. The data structure is an AVL tree T where each node x represents a person and has the following fields (in addition to the regular fields of a node in an AVL tree): 1. x.id - ID number (the search key for T) 2. x.client - the client record 3. x.gender- 1 (woman) or 0 (man) 4. x.women- the number of all women in the subtree rooted at x (including x). 5. x.men - the number of all men in the sub tree rooted at x (including x). Insert (k, c) create new node x x.id k x.client c x.gender 1 //(woman) x.women 1 // (a new node is always inserted as a leaf) x.men 0 AVL-Insert(x) // With rotations that update new fields // where necessary. for every node v in the path from x to the root do: v.women v.women + 1 O(1) O(logn) Time complexity : O(logn)

Update (k) x AVL-search(k) // O(logn) if x.gender = 1 x.gender 0 (man) O(1) for every node v in the path from x to the root do: v.women v.women 1 v.men v.men +1 Time complexity : O(logn) O(logn) FindDiff (k) sum-m 0 sum-w 0 O(1) T the root of the tree // search for a node with ID = k: while (T!= NULL) if (T.ID < k ) sum-m sum-m+t.left.men sum-w sum-w+t.left.women if (T.gender = 1) sum-w = sum-w + 1 else sum-m = sum-m + 1 T T.right else T T.left return sum-w sum-m O(logn) Time complexity : O(logn)

Binary Search Trees (BST) - Question 1 a. Given a BST T with unique keys, write an algorithm that prints the k smallest keys in T in O(h+k) time, where h is the height of T. If there are less than k nodes in T the algorithm should print all T s keys. b. Let x be an arbitrary node in a given BST T. Write an algorithm that finds and prints k successors of x in O(h+k) time, where h is the height of T. Example: If we run the tree contains all the numbers in the range 1 0 and key(x)= then we would want to print the numbers [11 11+k-1]. If k=4 this would mean [11 14]. inorder(x, k) In the following algorithms, we will use a local if ( x = null ) variable z to hold the number of nodes already printed return 0 of the k that we want to print. z inorder(x.left,k) a. We ll Use a modified version of inorder to scan if ( z<k ) the tree. print(x.key) First we ll print recursively print the z smallest z z+1 keys from x.left. If we haven t printed k yet we ll if ( z< k ) print the root s key and then print the (k-z) z z+inorder(x.right,k-z) remaining keys from the tree rooted in x.right. b. If the node has a right sub-tree, then the first few return z successors must be there. The rest of the successors will be the successors of the first ancestor that is larger than x. The printsucc(x,k) function The findsuccancestor(x) Finds the first prints at most k successors ancestor that is larger than x: of x in the BST: printsucc(x,k) if (k=0) return z inorder(x.right, k) if (z < k ) y findsuccancestor(x) print(y.key) z z+1 if (z < k ) printsucc(y, k-z) findsuccancestor(x) y x.parent while ((y!= NULL) & (x = y.right)) x y; y y.parent return y Time complexity: findsuccancestor O(h) time Total time - O(h+k). Total calls to inorder O(k) time

Question 2 T is a BST containing integer number values. For a given real value x SumPaths(T,x) is the number of paths in T or any subtree of T from the root down to some node for which the sum of path node keys is x. a. Write an algorithm which calculates SumPaths(T,x). b. What is the running time of your algorithm? 4 7 12-1 a. We ll use 2 functions. Sumpaths(T,)=2 The first, SumPathsRoot(T,x), will return the number of paths from the root down to some node for which the sum of path node keys is x. The second, SumPaths(T,x), will return the value we want. It will first call SumPathsRoot(T,x) to get the number of desired paths beginning at the root T, and then call itself recursively to on T.left and T.right to find the desired paths beginning below T. SumPaths(T,x) if (T = NULL) return 0 y SumPathsRoot(T,x) xl SumPaths(T.left, x) xr SumPaths(T.right, x) return y + xl + xr SumPathsRoot(T,x) if (T = NULL) return 0 xl SumPathsRoot(T.left, x T.key) xr SumPathsRoot(T.right, x T.key) if (x T.key = 0) xl xl+1 return xl+xr b. The SumPaths(T,x) algorithm s runtime is represented by the following formula: Tn Tk Tn k 1 1 Sn with k being the size of the subtree rooted in T.left, and S(n) being the runtime of the SumPathsRoot(T,x) algorithm. The SumPathsRoot(T,x) runtime is represented by the following formula Sn Sk Sn k 1 1 With k being the size of the subtree rooted in T.left. Assuming that S(0)=0 for simplicity (meaning that S(1)=1 for a leaf, which makes sense) we will prove by induction that S(n)=n. The base of induction is covered by S(0)=0. We ll assume that S(m)=m for m<n. Sn Sk Sn k 1 1 k n k 11 n We have Tn Tk Tn k 1 1Sn Tk Tn k 1 cn 2 2 By induction Tn n. Our algorithm runs in n time.