1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.

Slides:



Advertisements
Similar presentations
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Advertisements

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
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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):
1 CSE 326: Data Structures Part Four: Trees Henry Kautz Autumn 2002.
CSE 326: Data Structures AVL Trees
CSE 326: Data Structures Lecture #10 Balancing Act and What AVL Stands For Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
AVL Trees ITCS6114 Algorithms and Data Structures.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Splay Trees and B-Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
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.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.
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.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
CSE 326 Killer Bee-Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001 Where was that root?
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
Data Structures AVL Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
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.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSIT 402 Data Structures II
Introduction Applications Balance Factor Rotations Deletion Example
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms
Splay Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree 27th Mar 2007.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
Instructor: Lilian de Greef Quarter: Summer 2017
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
Analysis of Algorithms - Trees
Lecture 9: Self Balancing Trees
CSE 326: Data Structures Lecture #9 Amazingly Vexing Letters
CSE 326: Data Structures Lecture #8 Balanced Dendrology
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
CSE 326: Data Structures Lecture #9 AVL II
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
326 Lecture 9 Henry Kautz Winter Quarter 2002
Presentation transcript:

1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003

2 Outline Finish discussion on random binary search trees (BST) AVL trees Reading assignment for this week: Weiss: 4.3, 4.4, 4.5, and 4.7

3 The Average Depth of a BST Insert the elements 1 <2 <... < n in some order, starting with the empty tree For each permutation,  : –T  = the BST after inserting  (1),  (2),...,  (n) The Average Depth:

4 The Average Depth of a BST The average depth of a BST is: H(n) =  (log n) For some , height(T  ) = O(log n) For other , height(T  ) = O(n) But the average is O(log n) Please read the proof in the book and/or slides !

5

6 Random Input vs. Random Trees Inputs 1,2,3 3,2,1 1,3,2 3,1,2 2,1,3 2,3,1 Trees For three items, the shallowest tree is twice as likely as any other – effect grows as n increases. For n=4, probability of getting a shallow tree > 50%

7 Average cost The average, amortized cost of n insert/find operations is O(log(n)) But the average, amortized cost of n insert/find/delete operations can be as bad as  (  n) –Deletions make life harder (recall stretchy arrays) Need guaranteed cost O(log n) – next

8 Beauty is Only  (log n) Deep Binary Search Trees are fast if they’re shallow e.g.: complete Problems occur when one branch is much longer than the other How to capture the notion of a “sort of” complete tree?

9 Balance balance = height(left subtree) - height(right subtree) convention: height of a “null” subtree is -1 zero everywhere  perfectly balanced small everywhere  balanced enough:  (log n) –Precisely: Maximum depth is 1.44 log n t 5 6

10 AVL Tree (Adelson-Velskii Landis) Binary search tree properties Balance of every node is -1  b  1 Tree re-balances itself after every insert or delete 15 What is the balance of each node in this tree?

11 AVL Tree Data Structure data height children 30 0

12 Not An AVL Tree data height children

13 Bad Case #1 Insert(small) Insert(middle) Insert(tall) T M S 0 1 2

14 Single Rotation T M S M ST 00 1 Basic operation used in AVL trees: A right child could legally have its parent as its left child.

15 General Case: Insert Unbalances a X Y b Z h h - 1 h + 1 h - 1 h + 2 a X Y b Z h-1 h h + 1 a XY b Z h h - 1 h h + 1

16 Properties of General Insert + Single Rotation Restores balance to a lowest point in tree where imbalance occurs After rotation, height of the subtree (in the example, h+1) is the same as it was before the insert that imbalanced it Thus, no further rotations are needed anywhere in the tree!

17 Bad Case #2 Insert(small) Insert(tall) Insert(middle) M T S Why won’t a single rotation (bringing T up to the top) fix this?

18 Double Rotation M ST 00 1 M T S T M S 0 1 2

19 General Double Rotation Initially: insert into X unbalances tree (root height goes to h+3) “Zig zag” to pull up c – restores root height to h+2, left subtree height to h a Z b W c Y a Z b W c Y h+1 h h h h + 3 h + 2 hh h+1 h + 2 h+1 h X X

20 Another Double Rotation Case Initially: insert into Y unbalances tree (root height goes to h+2) “Zig zag” to pull up c – restores root height to h+1, left subtree height to h a Z b W c Y a Z b W c Y h+1 h h h h + 3 h + 2 h h h+1 h + 2 h+1 h X X

21 Insert Algorithm Find spot for value Hang new node Search back up looking for imbalance If there is an imbalance: “outside”: Perform single rotation and exit “inside”: Perform double rotation and exit

22 AVL Insert Algorithm Node insert(Comparable x, Node root){ if ( root == NULL ) return new Node(x); if (x == root.key) return root; if (x < root.key){ root.left = insert( x, root.left ); if (root unbalanced) { rotate... } } else { // x > root.key root.right = insert( x, root.right ); if (root unbalanced) { rotate... } } root.height = max(root.left.height, root.right.height)+1; return root; } Node insert(Comparable x, Node root){ if ( root == NULL ) return new Node(x); if (x == root.key) return root; if (x < root.key){ root.left = insert( x, root.left ); if (root unbalanced) { rotate... } } else { // x > root.key root.right = insert( x, root.right ); if (root unbalanced) { rotate... } } root.height = max(root.left.height, root.right.height)+1; return root; }

23 Deletion (Really Easy Case) Delete(17)

24 Deletion (Pretty Easy Case) Delete(15)

25 Deletion (Pretty Easy Case cont.) Delete(15)

26 Deletion (Hard Case #1) Delete(12)

27 Single Rotation on Deletion What is different about deletion than insertion?

28 Deletion (Hard Case) Delete(9)

29 Double Rotation on Deletion Not finished!

30 Deletion with Propagation We get to choose whether to single or double rotate! What different about this case?

31 Propagated Single Rotation

32 Propagated Double Rotation

33 AVL Deletion Algorithm Recursive 1.If at node, delete it 2.Otherwise recurse to find it in 3. Correct heights a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care), double rotate Iterative 1. Search downward for node, stacking parent nodes 2. Delete node 3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2 (or don’t care) double rotate

34 Pro: All operations guaranteed O(log N) The height balancing adds no more than a constant factor to the speed of insertion Con: Space consumed by height field in each node Slower than ordinary BST on random data Can we guarantee O(log N) performance with less overhead? Splay trees next time Pros and Cons of AVL Trees