Lecture 10: BST and AVL Trees

Slides:



Advertisements
Similar presentations
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Advertisements

AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
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):
AVL Trees ITCS6114 Algorithms and Data Structures.
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.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
COMP261 Lecture 23 B Trees.
Part-D1 Binary Search Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
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.
Search Trees.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
Chapter 29 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
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
Binary Search Trees.
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
Introduction to Hash Tables
Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Binary Search Tree AVL Tree
Data Structures and Algorithms
Instructor: Lilian de Greef Quarter: Summer 2017
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
B-Tree Insertions, Intro to Heaps
AVL Trees Lab 11: AVL Trees.
CSE 373: Data Structures and Algorithms
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
CSE 373: Data Structures and Algorithms
Data Structures and Algorithms
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures and Algorithms
Lecture 9: Self Balancing Trees
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
Lecture 9: Intro to Trees
1 Lecture 13 CS2013.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Lecture 10: BST and AVL Trees CSE 373: Data Structures and Algorithms CSE 373 19 SP - Kasey Champion

Warm Up 6 Is valid BST? No Height? 2 2 8 8 1 4 7 12 6 11 10 13 Yes 7 15 Height? 3 9 9 > 8 CSE 373 19 SP - Kasey Champion

Administrivia CSE 373 19 SP - Kasey Champion

Trees CSE 373 SP 18 - Kasey Champion

Binary Search Trees A binary search tree is a binary tree that contains comparable items such that for every node, all children to the left contain smaller data and all children to the right contain larger data. 10 9 15 7 12 18 8 17 CSE 373 SP 18 - Kasey Champion

Implement Dictionary Binary Search Trees allow us to: 10 “foo” quickly find what we’re looking for add and remove values easily Dictionary Operations: Runtime in terms of height, “h” get() – O(h) put() – O(h) remove() – O(h) What do you replace the node with? Largest in left sub tree or smallest in right sub tree 7 “bar” 12 “baz” 5 “fo” 9 “sho” 15 “sup” 1 “burp” 8 “poo” 13 “boo” CSE 373 SP 18 - Kasey Champion

Height in terms of Nodes For “balanced” trees h ≈ logc(n) where c is the maximum number of children Balanced binary trees h ≈ log2(n) Balanced trinary tree h ≈ log3(n) Thus for balanced trees operations take Θ(logc(n)) CSE 373 SP 18 - Kasey Champion

Unbalanced Trees Is this a valid Binary Search Tree? Yes, but… 10 Is this a valid Binary Search Tree? Yes, but… We call this a degenerate tree For trees, depending on how balanced they are, Operations at worst can be O(n) and at best can be O(logn) How are degenerate trees formed? insert(10) insert(9) insert(7) insert(5) 9 7 5 CSE 373 SP 18 - Kasey Champion

Implementing Dictionary with BST 2 Minutes Implementing Dictionary with BST public boolean contains(K key, BSTNode node) { if (node == null) { return false; } int compareResult = compareTo(key, node.data); if (compareResult < 0) { returns contains(key, node.left); } else if (compareResult > 0) { returns contains(key, node.right); } else { returns true; +C1 +C2 +T(n/2) best + T(n-1) worst Best Case (assuming key is at the bottom) +T(n/2) best + T(n-1) worst 𝑻 𝒏 = 𝑪 𝒘𝒉𝒆𝒏 𝒏<𝟎 𝒐𝒓 𝒌𝒆𝒚 𝒇𝒐𝒖𝒏𝒅 𝑻 𝒏 𝟐 +𝑪 𝒐𝒕𝒉𝒆𝒓𝒘𝒊𝒔𝒆 +C3 Worst Case (assuming key is at the bottom) 𝑻 𝒏 = 𝑪 𝒘𝒉𝒆𝒏 𝒏<𝟎 𝒐𝒓 𝒌𝒆𝒚 𝒇𝒐𝒖𝒏𝒅 𝑻 𝒏−𝟏 +𝑪 𝒐𝒕𝒉𝒆𝒓𝒘𝒊𝒔𝒆 CSE 373 SP 18 - Kasey Champion

Measuring Balance 2 Minutes Measuring balance: For each node, compare the heights of its two sub trees Balanced when the difference in height between sub trees is no greater than 1 8 10 8 7 7 9 15 7 Balanced Balanced Balanced 12 18 Unbalanced CSE 373 SP 18 - Kasey Champion

Meet AVL Trees AVL Trees must satisfy the following properties: binary trees: all nodes must have between 0 and 2 children binary search tree: for all nodes, all keys in the left subtree must be smaller and all keys in the right subtree must be larger than the root node balanced: for all nodes, there can be no more than a difference of 1 in the height of the left subtree from the right. Math.abs(height(left subtree) – height(right subtree)) ≤ 1 AVL stands for Adelson-Velsky and Landis (the inventors of the data structure) CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 7 Is it… Binary BST Balanced? yes yes 4 10 3 5 9 12 2 6 8 11 13 14 CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 2 Minutes 6 Is it… Binary BST Balanced? yes 8 no Height = 0 Height = 2 1 4 7 12 3 5 10 13 9 11 CSE 373 SP 18 - Kasey Champion

Is this a valid AVL tree? 2 Minutes 8 Is it… Binary BST Balanced? yes no 6 11 yes 2 7 15 -1 5 9 9 > 8 CSE 373 SP 18 - Kasey Champion

Implementing an AVL tree dictionary Dictionary Operations: get() – same as BST containsKey() – same as BST put() - ??? remove() - ??? Add the node to keep BST, fix AVL property if necessary Replace the node to keep BST, fix AVL property if necessary Unbalanced! 1 2 2 1 3 3 CSE 373 SP 18 - Kasey Champion

Rotations! Balanced! Unbalanced! a b a Insert ‘c’ X b X b Z a Y Z Y Z CSE 373 SP 18 - Kasey Champion

Rotate Left parent’s right becomes child’s left, child’s left becomes its parent Balanced! Unbalanced! a b a Insert ‘c’ X b X b Z a Y Z Y Z X Y c c CSE 373 SP 18 - Kasey Champion

Rotate Right parent’s left becomes child’s right, child’s right becomes its parent 4 3 15 height put(16); 2 2 3 8 22 Unbalanced! 1 1 2 4 10 19 24 1 3 6 17 20 16 CSE 373 SP 18 - Kasey Champion

Rotate Right parent’s left becomes child’s right, child’s right becomes its parent 15 3 height put(16); 2 2 8 19 1 1 1 10 17 22 4 3 6 16 20 24 CSE 373 SP 18 - Kasey Champion

So much can go wrong Unbalanced! Unbalanced! Unbalanced! 3 1 1 3 3 1 Rotate Left Rotate Right 2 2 2 Parent’s right becomes child’s left Child’s left becomes its parent Parent’s left becomes child’s right Child’s right becomes its parent CSE 373 SP 18 - Kasey Champion

Two AVL Cases Kink Case Line Case Solve with 2 rotations 3 1 1 3 2 2 3 1 1 3 2 2 Rotate Right Parent’s left becomes child’s right Child’s right becomes its parent Rotate Left Parent’s right becomes child’s left Child’s left becomes its parent Right Kink Resolution Rotate subtree left Rotate root tree right Left Kink Resolution Rotate subtree right Rotate root tree left CSE 373 SP 18 - Kasey Champion

Double Rotations 1 Unbalanced! a a a Insert ‘c’ X e W d W e d Z Y e d CSE 373 SP 18 - Kasey Champion

Double Rotations 2 d Unbalanced! a e a W d Y X Z W Y e X Z c c CSE 373 SP 18 - Kasey Champion

Implementing Dictionary with AVL 2 Minutes Implementing Dictionary with AVL public boolean contains(K key, AVLNode node) { if (node == null) { return false; } int compareResult = compareTo(key, node.data); if (compareResult < 0) { returns contains(key, node.left); } else if (compareResult > 0) { returns contains(key, node.right); } else { returns true; +C1 +C2 +T(n/2) +T(n/2) Worst Case Improvement Guaranteed with AVL +C3 𝑻 𝒏 = 𝑪 𝒘𝒉𝒆𝒏 𝒏<𝟎 𝒐𝒓 𝒌𝒆𝒚 𝒇𝒐𝒖𝒏𝒅 𝑻 𝒏 𝟐 +𝑪 𝒐𝒕𝒉𝒆𝒓𝒘𝒊𝒔𝒆 CSE 373 SP 18 - Kasey Champion

How long does AVL insert take? AVL insert time = BST insert time + time it takes to rebalance the tree = O(log n) + time it takes to rebalance the tree How long does rebalancing take? Assume we store in each node the height of its subtree. How long to find an unbalanced node: Just go back up the tree from where we inserted. How many rotations might we have to do? Just a single or double rotation on the lowest unbalanced node. AVL insert time = O(log n)+ O(log n) + O(1) = O(log n)  O(log n)  O(1) CSE 373 AU 18 – Shri mare

AVL wrap up Pros: - O(log n) worst case for find, insert, and delete operations. - Reliable running times than regular BSTs (because trees are balanced) Cons: - Difficult to program & debug [but done once in a library!] - (Slightly) more space than BSTs to store node heights. CSE 373 AU 18 – Shri mare

Lots of cool Self-Balancing BSTs out there! Popular self-balancing BSTs include: AVL tree Splay tree 2-3 tree AA tree Red-black tree Scapegoat tree Treap (Not covered in this class, but several are in the textbook and all of them are online!) (From https://en.wikipedia.org/wiki/Self-balancing_binary_search_tree#Implementations) CSE 373 SU 17 – lilian de Greef