§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

AVL Trees binary tree for every node x, define its balance factor
Part II. Delete an Node from an AVL Tree Consider to delete
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.
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.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
Tirgul 5 AVL trees.
C++ Programming:. Program Design Including
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Amortized Analysis Some of these lecture slides are adapted from CLRS.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
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.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
Splay Trees Splay trees are binary search trees (BSTs) that:
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees and B-Trees
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
CHAPTER 10 Search Structures All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data.
CMSC 341 Splay Trees. 8/3/2007 UMBC CMSC 341 SplayTrees 2 Problems with BSTs Because the shape of a BST is determined by the order that data is inserted,
CS Data Structures Chapter 10 Search Structures.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
Balanced Search Trees Problem: Efficiency of BST is related to tree’s height.  search, insert and remove follow a path from root to desired location 
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
CMSC420: Splay Trees Kinga Dobolyi Based off notes by Dave Mount.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Recursion and Trees Dale Roberts, Lecturer
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
Data Structures Using Java1 Chapter 10 Binary Trees.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees It’s a balancing act. Binary Tree Problems If you get either sorted or reverse-sorted input, you essentially get a linked list (always following.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL tree self-adjusting tree Lai Ah Fur. AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition:
Foundation of Computing Systems Lecture 4 Trees: Part I.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
AVL Trees CSE, POSTECH.
Binary Search Tree (BST)
Chapter 10 Search Structures
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.
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.
AVL Trees CENG 213 Data Structures.
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
Binary Search Trees Chapter 7 Objectives
AVL Trees Dynamic tables may also be maintained as binary search trees. Depending on the order of the symbols putting into the table, the resulting binary.
CSE 326: Data Structures Lecture #9 AVL II
Data Structures Using C++ 2E
Presentation transcript:

§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ), but the height can be as bad as O( N ). CHAPTER 4 Trees 1/19

§4 AVL Trees 〖 Example 〗 2 binary search trees obtained for the months of the year Nov Oct Sept May Mar June July Dec Aug Apr Feb JanJuly June Mar May Oct SeptNov Jan Feb Aug AprDec Entered from Jan to Dec A balanced tree Discussion 1: What are the average search times of these two trees? What if the months are entered in alphabetical order? 2/19

§4 AVL Trees Adelson-Velskii-Landis (AVL) Trees (1962) 【 Definition 】 An empty binary tree is height balanced. If T is a nonempty binary tree with T L and T R as its left and right subtrees, then T is height balanced iff (1) T L and T R are height balanced, and (2) | h L  h R |  1 where h L and h R are the heights of T L and T R, respectively. 【 Definition 】 The balance factor BF( node ) = h L  h R. In an AVL tree, BF( node ) =  1, 0, or The height of an empty tree is defined to be –1. 3/19

§4 AVL Trees 〖 Example 〗 Input the months Mar 0 11 May 0 Nov 0 11 22 May 0 11 Nov 00 22 11 Mar 00 0  The trouble maker Nov is in the right subtree’s right subtree of the trouble finder Mar. Hence it is called an RR rotation. In general: A 11 B 0 BLBL BRBR ALAL RR Insertion RR Rotation A 22 B 11 BLBL BRBR ALAL BLBL B 0 A ALAL BRBR 0 A is not necessarily the root of the tree Single rotation 4/19

§4 AVL Trees Aug May 0 11 Nov 00 22 11 Mar 01 1 Aug 0 Apr /19 Discussion 2: What can we do now? Discussion 2: What can we do now?

§4 AVL Trees May 0 11 Nov 00 22 11 Aug 01 1 11 Mar 0 0 Apr 0 Jan 0 1 11 2 LR Rotation Mar 0 11 May 0 11 22 11 Aug 010 11 Jan 0 0 Apr 0 Nov 0 In general: A 1 B 0 BLBL ARAR C 0 CRCR CLCL LR Insertion A 2 B 11 BLBL ARAR C 11 CRCR CLCL OR LR Rotation BLBL ARAR C 0 A  1 or 0 CRCR B 0 or 1 CLCL OR Double Rotation 6/19

§4 AVL Trees DecJuly Mar 0 11 May 0 11 22 11 Aug 01 1 11 Jan 0 Apr 0 Nov 0 July 0 Dec 0 Feb 0 11 1 22 2 Discussion 3: What can we do now? Discussion 3: What can we do now? 7/19

§4 AVL Trees July 0 Dec 0 11 Aug 01 22 11 Jan 01 0 11 Feb 0 0 Apr 0 Mar 0 11 May 0 11 22 11 1 Nov 0 JuneOctSept June 0 11 11 11 2 Nov 0 Dec 0 11 Aug 1 22 11 Feb 0 1 July 11 Apr 0 Mar 0 May 11 June 0 Jan 0 Oct 0 11 22 11 11 0 Dec 0 11 Aug 1 22 11 Feb 0 1 July 11 Apr 0 Mar 0 Nov 0 June 0 Jan 0 May 0 Sept 0 11 11 11 11 Note: Several bf’s might be changed even if we don’t need to reconstruct the tree. Another option is to keep a height field for each node. 8/19 Read the declaration and functions in Figures 4.42 – 4.48 Home work: p Create an AVL Tree p Double Rotation

§4 AVL Trees One last question: Obviously we have T p = O( h ) where h is the height of the tree. But h = ? Let n h be the minimum number of nodes in a height balanced tree of height h. Then the tree must look like A h2h2h1h1 A h2h2h1h1 OR  n h = n h  1 + n h  Fibonacci numbers: F 0 = 0, F 1 = 1, F i = F i  1 + F i  2 for i > 1  n h = F h+2  1, for h  0 Fibonacci number theory gives that 9/19

§5 Splay Trees Target : Any M consecutive tree operations starting from an empty tree take at most O(M log N) time. Does it mean that every operation takes O(log N) time? No. It means that the amortized time is O(log N). So a single operation might still take O(N) time? Then what’s the point? The bound is weaker. But the effect is the same: There are no bad input sequences. But if one node takes O(N) time to access, we can keep accessing it for M times, can’t we? Sure we can – that only means that whenever a node is accessed, it must be moved. Discussion 4: How shall we move the node? Discussion 4: How shall we move the node? 10/19

k5k5 F k4k4 E k3k3 D k2k2 A k1k1 CB §5 Splay Trees k5k5 F k4k4 E k3k3 D k2k2 BA k1k1 C k5k5 F k4k4 E k2k2 BA k1k1 k3k3 DC k5k5 F k4k4 E k2k2 BA k1k1 k3k3 DC k4k4 E k5k5 F k2k2 BA k1k1 k3k3 DC Does NOT work! 11/19

§5 Splay Trees An even worse case: Discussion 5: Try to Insert 1, 2,..., N in increasing order, and then Find them in the same order. What will happen? What is the total access time T(N)? 12/19

§5 Splay Trees Try again -- For any nonroot node X, denote its parent by P and grandparent by G : Case 1: P is the rootRotate X and P Case 2: P is not the root Zig-zag G D P A X BC X G CD P AB Double rotation Zig-zig G D P C X AB Single rotation X A P B G DC 13/19

§5 Splay Trees k5k5 F k4k4 E k3k3 D k2k2 A k1k1 CB k5k5 F k4k4 E k1k1 k3k3 DC k2k2 BA k1k1 k2k2 BA k4k4 k5k5 FE k3k3 DC Splaying not only moves the accessed node to the root, but also roughly halves the depth of most nodes on the path. 14/19

§5 Splay Trees Insert: 1, 2, 3, 4, 5, 6, Find: Read the 32-node example given in Figures 4.52 – /19

Discussion 6: Then what must we do? Please complete the algorithm description for Deletion. §5 Splay Trees Deletions:  Step 1: Find X ; Are splay trees really better than AVL trees? Home work: p Access a splay tree 16/19

 Other Operations on Binary Search Trees  Sort: List the elements in increasing order Solution: inorder traversal.  Get Height: Compute the heights of the nodes Solution: postorder traversal.  Get Depth: Compute the depths of the nodes Solution: preorder traversal. 17/19

Research Project 1 Binary Search Trees (25) This project requires you to implement operations on unbalanced binary search trees, AVL trees, and splay trees. You are to analyze and compare the performances of a sequence of insertions and deletions on these search tree structures. Detailed requirements can be downloaded from 18/19

Research Project 2 Population (25) It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are distributed in this continent. The continent is divided into square regions, each has a center with integer coordinates (x, y). Hence all the people coming into that region are considered to be settled at the center position. Given the positions of the corners of a rectangle region, you are supposed to count the number of people living in that region. Note: there are up to different regions and possibly even more queries. Detailed requirements can be downloaded from 19/19