Balanced Binary Search Trees

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
Trees Types and Operations
CS202 - Fundamental Structures of Computer Science II
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.
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
A balanced life is a prefect life.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
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!
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Introduction to Algorithms Jiafen Liu Sept
October 16, Algorithms and Data Structures Lecture IX Simonas Šaltenis Aalborg University
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
1. 2 Setting Up Deletion As with binary search trees, we can always delete a node that has at least one external child If the key to be deleted is stored.
CSIT 402 Data Structures II
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
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.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Red-Black Trees an 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.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 23 Red Black Tree Chapter 10 of textbook
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
File Organization and Processing Week 3
Balanced Search Trees Modified from authors’ slides.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees.
Red-Black Trees Motivations
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Algorithms and Data Structures Lecture VIII
CSE 332: Data Abstractions AVL Trees
CS202 - Fundamental Structures of Computer Science II
AVL-Trees.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Balanced Binary Search Trees CISC 235: Topic 4 Balanced Binary Search Trees

Outline Rationale and definitions Rotations AVL Trees, Red-Black, and AA-Trees Algorithms for searching, insertion, and deletion Analysis of complexity CISC 235 Topic 4

Balanced Binary Search Trees Purpose: To achieve a worst-case runtime of O(log n) for searching, inserting and deleting Three Types We’ll Look At : AVL Trees Red-Black Trees AA-Trees There are many types of balanced BSTs CISC 235 Topic 4

AVL Trees Invented in 1962 by Russian mathematicians Adelson-Velski and Landis An AVL tree is a binary search tree such that: The height of the left and right sub-trees of the root differ by at most 1 The left and right sub-trees are AVL trees Which of these are AVL trees, assuming that they are BSTs? CISC 235 Topic 4

Valid AVL Tree 44 32 54 82 65 97 88 17 85 99 11 14 Note: it is not a requirement that all leaves be on the same or adjacent level CISC 235 Topic 4

Minimum AVL Tree of Height H H < 1.44 log(N+2) – 1.328 SH-1 SH-2 H Let SH be the size of the smallest AVL tree of height H. Then: S0 = 1, S1 = 2 SH = SH-1 + SH-2 + 1 H-2 H-1 CISC 235 Topic 4

Rotations Right-Rotate (B) Left-Rotate (A) A B B A α γ γ β α β Rotations maintain the ordering property of BSTs. a є α, b є β, c є γ implies a ≤ A ≤ b ≤ B ≤ c A rotation is an O(1) operation CISC 235 Topic 4

Insertions: 4 Cases A B A B 4 3 1 2 When inserting into a sub-tree of A, there are 4 cases in which a height violation could occur: Inserting in the left sub-tree of the left child of A Inserting in the right sub-tree of the left child of A Inserting in the left sub-tree of the right child of A Inserting in the right sub-tree of the right child of A B A 4 3 1 A B 2 CISC 235 Topic 4

Rotations Required for the 4 Cases Case 1: Requires a single right rotation to balance Case 2 and 3: Require double rotations to balance Case 4: Requires a single left rotation to balance B A 4 3 1 A B 2 Case 2: Requires a left rotation around B followed by a right rotation around A Case 3: Requires a right rotation around B followed by a left rotation around A CISC 235 Topic 4

Insertion in an AVL Tree First insert node w in AVL tree T as for plain binary search tree Then find the first node x going up from w to the root that is unbalanced (if none, are finished) Apply appropriate rotation (single or double), which reduces height of sub-tree rooted at x by 1 Since all nodes in T that became unbalanced were on the path of T from w to the root, restoring the sub-tree rooted at x to its original height rebalances the entire tree. CISC 235 Topic 4

Insertion in an AVL Tree 44 32 54 82 65 97 88 17 What insertion value would cause a Case 1 rotation? Case 2? 3? 4? CISC 235 Topic 4

Deletion in an AVL Tree First delete node w in AVL tree T as for plain binary search tree Then find the first node x going up from w to the root that is unbalanced (if none, are finished) Apply appropriate rotation (single or double), which results either in the sub-tree rooted at x being its original height before the deletion, or in its height being decreased by 1. Balancing the sub-tree rooted at x may NOT rebalance the entire tree. O(log n) rotations may be required. CISC 235 Topic 4

Advantages/Disadvantage of AVL Trees O(log n) worst-case searches, insertions and deletions Disadvantages Complicated Implementation Must keep balancing info in each node To find node to balance, must go back up in the tree: easy if pointer to parent, otherwise difficult Deletion complicated by numerous potential rotations CISC 235 Topic 4

Red-Black Trees Improvement over AVL Trees: A single top-down pass can be used during insertion and deletion routines However, the implementation and number of rotation cases is still complex, so we will only look at Red-Black Trees as a step towards considering AA-Trees. CISC 235 Topic 4

Red-Black Trees A Red-Black Tree is a binary search tree with the following ordering properties: Every node is colored either red or black. The root is black If a node is red, its children must be black. All simple paths from any node x to a descendent leaf must contain the same number of black nodes = black-height(x) CISC 235 Topic 4

A Red-Black Tree Every node is colored either red or black 30 5 15 10 20 70 50 65 85 60 55 40 80 90 Every node is colored either red or black The root is black CISC 235 Topic 4

A Red-Black Tree 3. If a node is red, its children must be black 30 5 15 10 20 70 50 65 85 60 55 40 80 90 3. If a node is red, its children must be black CISC 235 Topic 4

A Red-Black Tree 30 5 15 10 20 70 50 65 85 60 55 40 80 90 bh = 3 bh = 2 bh = 1 bh = 1 bh = 0 4. All simple paths from any node x to a descendent leaf must contain the same number of black nodes = black-height(x) CISC 235 Topic 4

Height of a Red-Black Tree Theorem. A red-black tree with n keys has height h ≤ 2 lg(n + 1). INTUITION: • Merge red nodes into their black parents. CISC 235 Topic 4

Height of a Red-Black Tree Theorem. A red-black tree with n keys has height h ≤ 2 lg(n + 1). INTUITION: • Merge red nodes into their black parents. CISC 235 Topic 4

Height of a Red-Black Tree Theorem. A red-black tree with n keys has height h ≤ 2 lg(n + 1). INTUITION: • Merge red nodes into their black parents. This process produces a tree in which each node has 2, 3, or 4 children. The 2-3-4 tree has uniform depth h′ of leaves. h´ CISC 235 Topic 4

AA-Trees Improvement over Red-Black Trees: Fewer rotation cases, so easier to code, especially deletions (eliminates about half of the restructuring cases) AA-Trees still have O(log n) searches in the worst-case, although they are slightly less efficient. CISC 235 Topic 4

AA-Tree Ordering Properties An AA-Tree is a binary search tree with the same ordering properties as a red-black tree: Every node is colored either red or black. The root is black If a node is red, its children must be black. All simple paths from any node x to a descendent leaf must contain the same number of black nodes = black-height(x) PLUS 5. Left children may not be red CISC 235 Topic 4

An AA-Tree No left red children 30 15 5 20 35 50 65 85 60 40 10 70 90 55 80 No left red children CISC 235 Topic 4

Representation of Balancing Info The level of a node is stored instead of its color. 65 55 85 90 80 30 15 5 20 35 50 40 10 70 60 30 70 15 50 60 5 10 20 35 40 Red children are considered to be at the level of their parent. Note that this is the same tree as that on the previous slide. CISC 235 Topic 4

Redefinition of “Leaf” Both the terms leaf and level are redefined: A leaf in an AA-Tree is a node with no black children. 65 55 85 90 80 30 15 5 20 35 50 40 10 70 60 CISC 235 Topic 4

Redefinition of “Level” The level of a node in an AA-Tree is: Level 1, if the node is a leaf The level of its parent, if the node is red One less than the level of its parent, if the node is black 65 55 85 90 80 30 15 5 20 35 50 40 10 70 60 Level 1 Level 2 Level 3 CISC 235 Topic 4

Implications of Ordering Properties Horizontal links are right links because only right children may be red There may not be two consecutive horizontal links because there cannot be consecutive red nodes 65 55 85 90 80 30 15 5 20 35 50 40 10 70 60 CISC 235 Topic 4

Implications of Ordering Properties 3. Nodes at level 2 or higher must have two children. 4. If a node does not have a right horizontal link, its two children are at the same level. 65 55 85 90 80 30 15 5 20 35 50 40 10 70 60 Level 1 Level 2 Level 3 CISC 235 Topic 4

Implications of Ordering Properties 5. Any simple path from a black node to a leaf contains one black node on each level. 65 55 85 90 80 30 15 5 20 35 50 40 10 70 60 Level 1 Level 2 Level 3 CISC 235 Topic 4

Adjustments to AA-Trees: Split (Color no longer shown for AA-Trees, since only the level is stored) Problem: With G inserted, there are two reds in a row The split procedure is a simple left rotation between X and R X Red-Black Tree G A R B G X R G AA-Tree A B CISC 235 Topic 4

Adjustments to AA-Trees: Split Problem: With G inserted, there are two reds in a row The split procedure is a simple left rotation between X and R R G A X Red-Black Tree G B R X G AA-Tree A B CISC 235 Topic 4

Adjustments to AA-Trees: Split Note that R’s level increases in the AA-Tree R Red-Black Tree A X G G B R X G AA-Tree A B CISC 235 Topic 4

Adjustments to AA-Trees: Skew Problem: Horizontal left link in AA-Tree (too many black nodes on one path) The skew procedure is a simple right rotation between X and P X P Red-Black Tree G C A B P X AA-Tree A B C CISC 235 Topic 4

Adjustments to AA-Trees: Skew Problem: Horizontal left link in AA-Tree (too many black nodes on one path) The skew procedure is a simple right rotation between X and P A P X C Red-Black Tree G B P X AA-Tree A B C CISC 235 Topic 4

Adjustments to AA-Trees: Skew Problem: Horizontal left link in AA-Tree (too many black nodes on one path) The skew procedure is a simple right rotation between X and P A P X Red-Black Tree G C B P X AA-Tree A B C CISC 235 Topic 4

Example: Insert 45 First, insert as for simple binary search tree 45 30 70 15 50 60 85 5 10 20 35 40 55 65 80 90 First, insert as for simple binary search tree CISC 235 Topic 4

Example: Insert 45 After insert to right of 40: 30 70 15 50 60 85 5 20 10 35 40 45 55 65 80 90 Problem: Consecutive horizontal links starting at 35, so need split CISC 235 Topic 4

Example: Insert 45 After split at 35: 30 70 15 40 50 60 85 5 20 10 35 45 55 65 80 90 Problem: Left horizontal link at 50 is introduced, so need skew CISC 235 Topic 4

Example: Insert 45 After skew at 50: 30 70 15 40 50 60 85 5 20 10 35 45 55 65 80 90 Problem: Consecutive horizontal links starting at 40, so need split CISC 235 Topic 4

Example: Insert 45 After split at 40: 30 70 50 15 40 60 85 5 20 10 35 45 55 65 80 90 Problem: Left horizontal link at 70 is introduced (50 is now on same level as 70), so need skew CISC 235 Topic 4

Example: Insert 45 After skew at 70: 30 70 50 15 40 60 85 5 20 10 35 45 55 65 80 90 Problem: Consecutive horizontal links starting at 30, so need split CISC 235 Topic 4

Example: Insert 45 After split at 30: Insertion is complete (finally!) 50 30 70 15 40 60 85 5 20 10 35 45 55 65 80 90 Insertion is complete (finally!) CISC 235 Topic 4

AA-Tree Insertion Algorithm // Inserts node y into AA-Tree rooted at node x // Only for tree nodes with no pointer to parent AAInsert ( x, y ) if ( x = NIL ) // have found where to insert y then x  y else if key[ y ] < key[ x ] then AAInsert( left[ x ], y ) else if key[ y ] > key[ x ] then AATInsert( right[ x ], y ) else y is a duplicate; handle duplicate case skew ( x ) // Do skew and split at each level split ( x ) CISC 235 Topic 4

Deletion Two-Child Case, e.g., 15 50 30 70 15 40 60 85 5 20 10 35 45 55 65 80 90 Same as for simple BST: replace with smallest right child or largest left child and recursively call delete CISC 235 Topic 4

Deletion One-Child & No-Child Cases, e.g., 5 50 30 70 15 40 60 85 5 20 10 35 45 55 65 80 90 Note that these are all at level one, so everything boils down to deleting a level one node CISC 235 Topic 4

Deletion at Level 1 In the worst case, deleting one leaf node, e.g., 15, could cause six nodes to all become at one level, introducing horizontal left links. 30 70 15 50 60 85 90 However, it turns out that all cases can be handled by three calls to skew, followed by two calls to split (implementation can be found in various texts if you need it someday). CISC 235 Topic 4

BST Applets http://people.ksp.sk/~kuko/bak/index.html http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html http://www.cis.ksu.edu/~howell/viewer/viewer.html CISC 235 Topic 4