CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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 Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
BST Data Structure A BST node contains: A BST contains
CSE 326: Data Structures AVL Trees
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
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.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
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.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
Data Structures AVL Trees.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Dan Grossman Fall 2013.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Kevin Quinn Fall 2015.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CSE332: Data Abstractions Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms More Heaps; Dictionaries; Binary Search Trees Riley Porter Winter 2016 Winter 2017.
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms
Binary Search Trees.
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Hunter Zahn Summer 2016 Summer 2016.
Binary Search Trees Why this is a useful data structure. Terminology
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 332: Data Abstractions Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CSE 332: Data Abstractions AVL Trees
Binary Trees, Binary Search Trees
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Richard Anderson Spring 2016
Presentation transcript:

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1

Announcements HW2 out, due beginning of class Wednesday Two TA sessions next week – Asymptotic analysis on Tuesday – AVL Trees on Thursday MLK day Monday Winter 2014CSE373: Data Structures & Algorithms2

Previously on CSE 373 Dictionary ADT – stores (key, value) pairs – find, insert, delete Trees – terminology – traversals Winter 2014CSE373: Data Structures & Algorithms3

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } Sometimes order doesn’t matter Example: sum all elements Sometimes order matters Example: print tree with parent above indented children (pre-order) Example: evaluate an expression tree (post-order) A B D E C F G A B DE C FG Winter 20144CSE373: Data Structures & Algorithms A B DE C FG ✓ ✓✓ ✓ ✓ ✓✓

Binary Search Tree Structure property (“binary”) –Each node has  2 children –Result: keeps operations simple Order property –All keys in left subtree smaller than node’s key –All keys in right subtree larger than node’s key –Result: easy to find any given key Winter 20145CSE373: Data Structures & Algorithms

Are these BSTs? Winter 20146CSE373: Data Structures & Algorithms

Are these BSTs? Winter 20147CSE373: Data Structures & Algorithms

Find in BST, Recursive Data find(Key key, Node root){ if(root == null) return null; if(key < root.key) return find(key,root.left); if(key > root.key) return find(key,root.right); return root.data; } Winter 20148CSE373: Data Structures & Algorithms

Find in BST, Iterative Data find(Key key, Node root){ while(root != null && root.key != key) { if(key < root.key) root = root.left; else(key > root.key) root = root.right; } if(root == null) return null; return root.data; } Winter 20149CSE373: Data Structures & Algorithms

Other “Finding” Operations Find minimum node –“the liberal algorithm” Find maximum node –“the conservative algorithm” Find predecessor Find successor Winter CSE373: Data Structures & Algorithms

Insert in BST insert(13) insert(8) insert(31) (New) insertions happen only at leaves – easy! Winter CSE373: Data Structures & Algorithms

Deletion in BST Why might deletion be harder than insertion? 10 Winter CSE373: Data Structures & Algorithms

Deletion Removing an item disrupts the tree structure Basic idea: find the node to be removed, then “fix” the tree so that it is still a binary search tree Three cases: –Node has no children (leaf) –Node has one child –Node has two children Winter CSE373: Data Structures & Algorithms

Deletion – The Leaf Case delete(17) 10 Winter CSE373: Data Structures & Algorithms

Deletion – The One Child Case Winter CSE373: Data Structures & Algorithms delete(15)

Deletion – The Two Child Case What can we replace 5 with? 10 Winter CSE373: Data Structures & Algorithms delete(5)

Deletion – The Two Child Case Idea: Replace the deleted node with a value guaranteed to be between the two child subtrees Options: successor from right subtree: findMin(node.right) predecessor from left subtree: findMax(node.left) –These are the easy cases of predecessor/successor Now delete the original node containing successor or predecessor Leaf or one child case – easy cases of delete! Winter CSE373: Data Structures & Algorithms

Lazy Deletion Lazy deletion can work well for a BST –Simpler –Can do “real deletions” later as a batch –Some inserts can just “undelete” a tree node But –Can waste space and slow down find operations –Make some operations more complicated: How would you change findMin and findMax ? Winter CSE373: Data Structures & Algorithms

BuildTree for BST Let’s consider buildTree –Insert all, starting from an empty tree Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST –If inserted in given order, what is the tree? –What big-O runtime for this kind of sorted input? –Is inserting in the reverse order any better? O(n 2 ) Not a happy place Winter CSE373: Data Structures & Algorithms

BuildTree for BST Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST What we if could somehow re-arrange them –median first, then left median, right median, etc. –5, 3, 7, 2, 1, 4, 8, 6, 9 –What tree does that give us? –What big-O runtime? O(n log n), definitely better Winter CSE373: Data Structures & Algorithms

Unbalanced BST Balancing a tree at build time is insufficient, as sequences of operations can eventually transform that carefully balanced tree into the dreaded list At that point, everything is O(n) and nobody is happy –find –insert –delete Winter CSE373: Data Structures & Algorithms

Balanced BST Observation BST: the shallower the better! For a BST with n nodes inserted in arbitrary order –Average height is O( log n) – see text for proof –Worst case height is O(n) Simple cases, such as inserting in key order, lead to the worst-case scenario Solution: Require a Balance Condition that 1.Ensures depth is always O( log n) – strong enough! 2.Is efficient to maintain – not too strong! Winter CSE373: Data Structures & Algorithms

Potential Balance Conditions 1.Left and right subtrees of the root have equal number of nodes 2.Left and right subtrees of the root have equal height Too weak! Height mismatch example: Too weak! Double chain example: Winter CSE373: Data Structures & Algorithms

Potential Balance Conditions 3.Left and right subtrees of every node have equal number of nodes 4.Left and right subtrees of every node have equal height Too strong! Only perfect trees (2 n – 1 nodes) Too strong! Only perfect trees (2 n – 1 nodes) Winter CSE373: Data Structures & Algorithms

25 The AVL Balance Condition Left and right subtrees of every node have heights differing by at most 1 Definition: balance(node) = height(node.left) – height(node.right) AVL property: for every node x, –1  balance(x)  1 Ensures small depth –Will prove this by showing that an AVL tree of height h must have a number of nodes exponential in h Efficient to maintain –Using single and double rotations Winter 2014CSE373: Data Structures & Algorithms

26 The AVL Tree Data Structure Structural properties 1.Binary tree property 2.Balance property: balance of every node is between -1 and 1 Result: Worst-case depth is O(log n) Ordering property –Same as for BST 15 Winter 2014CSE373: Data Structures & Algorithms

An AVL tree? Winter 2014CSE373: Data Structures & Algorithms27

An AVL tree? Winter 2014CSE373: Data Structures & Algorithms28

29 The shallowness bound Let S(h) = the minimum number of nodes in an AVL tree of height h –If we can prove that S(h) grows exponentially in h, then a tree with n nodes has a logarithmic height Step 1: Define S(h) inductively using AVL property –S(-1)=0, S(0)=1, S(1)=2 –For h  1, S(h) = 1+S(h-1)+S(h-2) Step 2: Show this recurrence grows really fast –Can prove for all h, S(h) >  h – 1 where  is the golden ratio, (1+  5)/2, about 1.62 –Growing faster than 1.6 h is “plenty exponential” It does not grow faster than 2 h h-1 h-2 h Winter 2014CSE373: Data Structures & Algorithms

Before we prove it Good intuition from plots comparing: –S(h) computed directly from the definition –((1+  5)/2) h S(h) is always bigger, up to trees with huge numbers of nodes –Graphs aren’t proofs, so let’s prove it Winter CSE373: Data Structures & Algorithms

31 The Golden Ratio This is a special number Aside: Since the Renaissance, many artists and architects have proportioned their work (e.g., length:height) to approximate the golden ratio: If (a+b)/a = a/b, then a =  b We will need one special arithmetic fact about  :  2 = ((1+5 1/2 )/2) 2 = (1 + 2*5 1/2 + 5)/4 = (6 + 2*5 1/2 )/4 = ( /2 )/2 = 1 + ( /2 )/2 = 1 +  Winter 2014CSE373: Data Structures & Algorithms

The proof Theorem: For all h  0, S(h) >  h – 1 Proof: By induction on h Base cases: S(0) = 1 >  0 – 1 = 0 S(1) = 2 >  1 – 1  0.62 Inductive case (k > 1): Show S(k+1) >  k+1 – 1 assuming S(k) >  k – 1 and S(k-1) >  k-1 – 1 S(k+1) = 1 + S(k) + S(k-1)by definition of S > 1 +  k – 1 +  k-1 – 1by induction =  k +  k-1 – 1 by arithmetic (1-1=0) =  k-1 (  + 1) – 1by arithmetic (factor  k-1 ) =  k-1  2 – 1 by special property of  =  k+1 – 1 by arithmetic (add exponents) Winter CSE373: Data Structures & Algorithms S(-1)=0, S(0)=1, S(1)=2 For h  1, S(h) = 1+S(h-1)+S(h-2)

Good news Proof means that if we have an AVL tree, then find is O( log n) –Recall logarithms of different bases > 1 differ by only a constant factor But as we insert and delete elements, we need to: 1.Track balance 2.Detect imbalance 3.Restore balance Winter CSE373: Data Structures & Algorithms Is this AVL tree balanced? How about after insert(30) ?

An AVL Tree … 3 value height children Track height at all times! 10 key Winter 2014CSE373: Data Structures & Algorithms34

AVL tree operations AVL find : –Same as BST find AVL insert : –First BST insert, then check balance and potentially “fix” the AVL tree –Four different imbalance cases AVL delete : –The “easy way” is lazy deletion –Otherwise, do the deletion and then have several imbalance cases (we will likely skip this but post slides for those interested) Winter 2014CSE373: Data Structures & Algorithms35