CSE2331/5331 Topic 7: Balanced search trees Rotate operation

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
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.
CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures.
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
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.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Binary Search Trees What is a binary search tree?
Lecture 23 Red Black Tree Chapter 10 of textbook
Analysis of Algorithms CS 477/677
File Organization and Processing Week 3
BCA-II Data Structure Using C
Data Structures – LECTURE Balanced trees
Insertion/Deletion in binary trees
Balancing Binary Search Trees
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
FLIPPED CLASSROOM ACTIVITY CONSTRUCTOR – USING EXISTING CONTENT
Red-Black Trees.
Summary of General Binary search tree
Design and Analysis of Algorithms
Slide Sources: CLRS “Intro. To Algorithms” book website
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Red-Black Trees Motivations
CS200: Algorithms Analysis
Data Structures Lecture 4 AVL and WAVL Trees Haim Kaplan and Uri Zwick
Augmenting Data Structures
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Data Structures and Algorithms
Red-Black Trees.
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VIII
CS 583 Analysis of Algorithms
Introduction to Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Red-Black Trees.
Binary SearchTrees [CLRS] – Chap 12.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Augmenting Data Structures: Interval Trees
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Binary Search Trees < > = Dictionaries
Red-Black Trees CS302 Data Structures
Presentation transcript:

CSE2331/5331 Topic 7: Balanced search trees Rotate operation Red-black tree Augmenting data struct. CSE 2331/5331

Set Operations Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor CSE 2331/5331

Motivation Using binary search trees, Want to keep h small most operations take time O(h) with h being the height of the tree Want to keep h small ℎ=Θ( log 𝑛) the best one can hope for. (why?) We can sort the elements, and construct a balanced binary search tree But then how do we maintain it under dynamic operations (insertion / deletion)? CSE 2331/5331

Balanced Search Tree CSE 2331/5331

Un-balanced Search Tree CSE 2331/5331

Goal: Maintain a balanced binary search tree with height Θ( lg 𝑛) such that all operations take 𝑂( lg 𝑛) Maintain means that the tree is always of height Θ( lg (#𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠)) after any operation supported, especially insertion and deletion. There are multiple such balanced trees We focus on the so-called Red-black trees CSE 2331/5331

Rotation Operation A key operation in maintaining the “balance” of a binary search tree Locally rotate subtree, while maintain binary search tree property x y A B C y x right rotation A B C left rotation CSE 2331/5331

Right Rotation Examples Right rotation at 9. Does it change Binary search tree property? Right rotation at 13 ? Right rotation at 7 ? CSE 2331/5331

Left Rotation Examples Left rotation at node 3. what if we then right-rotate at node 6 ? left and right rotations are inverse of each other. CSE 2331/5331

Recall: Transplant CSE 2331/5331

Implementation of Right Rotation Running time: Θ(1) CSE 2331/5331

Implementation of Left Rotation Running time: Θ(1) CSE 2331/5331

Rotation does not change binary search tree property! x y A B C y x right rotation A B C left rotation CSE 2331/5331

Rotation to Re-balance Apply rotation to node 5 Reduce height to 3! CSE 2331/5331

Rotation to Re-balance Apply rotation at node 5 Height not reduced! Need a double-rotation (first at 16, then at 5) In general, how and when? CSE 2331/5331

Red-Black Trees Compared to an ordinary binary tree, a red-black binary tree: A leaf node is NIL Hence all nodes are either NIL or have exactly two children! Each node will have a color, either red or black CSE 2331/5331

Definition A Red-Black tree is a binary tree with the following properties: Every node is either red or black Root is black Every leaf is NIL and is black If a node is red, then both its children are black For each node, all simple paths from this node to its decedent leaves contain same number of black nodes CSE 2331/5331

No two consecutive red nodes. An Example Double nodes are black. No two consecutive red nodes. CSE 2331/5331

Skipping Leaves CSE 2331/5331

Is This a Red-Black Tree? CSE 2331/5331

Exercise Color the following tree to make a valid red-black tree CSE 2331/5331

Given a tree node x size(x): the total number of internal nodes contained in the subtree rooted at x bh(x): the number of black nodes on the path from x to leaf (not counting x) CSE 2331/5331

Balancing Property of RB-tree Lemma [BN-Bound]: Let r be the root of tree T. Then 𝑠𝑖𝑧𝑒 𝑟 ≥ 2 𝑏ℎ 𝑟 −1 . Proof: Since every root-leaf path has 𝑏ℎ(𝑟) black nodes, 𝑇 contains a complete binary tree of height 𝑏ℎ(𝑟) as subtree. For a complete binary tree of height 𝑏ℎ(𝑟), its size is 2 𝑏ℎ 𝑥 −1 Hence 𝑠𝑖𝑧𝑒 𝑟 ≥ 2 𝑏ℎ 𝑟 −1 CSE 2331/5331

Balancing Property of RB-tree Theorem [RB-Height] A red-black tree with n internal nodes has height ℎ≤2 log 2 (𝑛+1) . Proof: Let r be the root of this red-black tree Since no red node has a red child, 𝑏ℎ 𝑟 ≥ ℎ 2 By Lemma [BN-Bound], 𝑛≥ 2 𝑏ℎ 𝑟 −1 ≥ 2 ℎ/2 −1 Thus, ℎ≤2 log 2 𝑛+1 CSE 2331/5331

Implication of the Theorem In other words, if we can maintain a RB-tree under every operation, then the tree always has Θ( lg 𝑛) height, and All operations thus all take 𝑂( lg 𝑛) time. How to maintain RB-tree under Operations? CSE 2331/5331

Set Operations Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor Only need to consider Insert / delete CSE 2331/5331

Insertion Example Insert 36? Insert 2? Insert 24? CSE 2331/5331

Red-Black Tree Insert CSE 2331/5331

Insert Fixup: Case 3 (z is the new node) CSE 2331/5331

Example: Case 3 Insert 12? CSE 2331/5331 The other child of 15 is a leaf so thus black Insert 12? CSE 2331/5331

Implementation of Fixup Case 3 CSE 2331/5331

Remarks Running time of RBInsertFixupC Θ 1 If the tree has red-black properties if not counting violation caused by z Then after RBInsertFixupC the tree is a red-black tree! No more operations needed. CSE 2331/5331

Insert Fixup: Case 2 CSE 2331/5331

Example: Case 2 Insert 14? CSE 2331/5331

Implementation of Case 2 Running time Θ 1 CSE 2331/5331

Insert Fixup: Case 1: (z is new node) Go back to the “insert 36” example before. The parent and uncle of z are red: Color the parent and uncle of z both black Color the grandparent of z red Continue with the grandparent of z CSE 2331/5331

Sibling Takes Θ 1 time CSE 2331/5331

Implementation of Case 1 Running time: Θ ℎ =Θ( lg 𝑛) When does this procedure terminate? CSE 2331/5331

Red-black Tree Insert Fixup Function RBInsertFixup (T, z) RBInsertFixupA (T, z); RBInsertFixupB (T, z); RBInsertFixupC (T, z); T.root.color = Black; Note z changes after each call. Total time copmlexity: Θ( lg 𝑛) Total # rotations: At most 2 CSE 2331/5331

Example Insert 21? CSE 2331/5331

Augmenting Data Structure CSE 2331/5331

Balanced Binary Search Tree Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor Also support Select operation ? CSE 2331/5331

Augment Tree Structure Select ( T, k ) Goal: Augment the binary search tree data structure so as to support Select ( T, k ) efficiently Ordinary binary search tree O(h) time for Select(T, k) Red-black tree (balanced search tree) O(lg n) time for Select(T, k) CSE 2331/5331

How To Augment Tree Structure? At each node x of the tree T store x.size = # nodes in the subtree rooted at x Include x itself If a node (leaf) is NIL, its size is 0. Space of an augmented tree: Θ 𝑛 Basic property: 𝑥.𝑠𝑖𝑧𝑒=𝑥.𝑙𝑒𝑓𝑡.𝑠𝑖𝑧𝑒+𝑥.𝑟𝑖𝑔ℎ𝑡.𝑠𝑖𝑧𝑒+1 CSE 2331/5331

Example M 9 C 5 P 3 T 2 F 3 A 1 D 1 H 1 Q 1 CSE 2331/5331

How to Setup Size Information? procedure AugmentSize( 𝑡𝑟𝑒𝑒𝑛𝑜𝑑𝑒 𝑥 ) If (𝑥≠𝑁𝐼𝐿 ) then 𝐿𝑠𝑖𝑧𝑒 = AugmentSize( 𝑥.𝑙𝑒𝑓𝑡 ); 𝑅𝑠𝑖𝑧𝑒 = AugmentSize( 𝑥.𝑟𝑖𝑔ℎ𝑡); 𝑥.𝑠𝑖𝑧𝑒=𝐿𝑠𝑖𝑧𝑒+𝑅𝑠𝑖𝑧𝑒+1; Return( 𝑥.𝑠𝑖𝑧𝑒 ); end Return (0); Postorder traversal of the tree ! CSE 2331/5331

Augmented Binary Search Tree Let T be an augmented binary search tree OS-Select(x, k): Return the k-th smallest element in the subtree rooted at x OS-Select(T.root, k) returns the k-th smallest elements in the entire tree. OS-Select(T.root, 5) ? CSE 2331/5331

Correctness? Running time? O(h) CSE 2331/5331

OS-Rank(T, x) Return the rank of the element x in the linear order determined by an inorder walk of T CSE 2331/5331

Example M 9 C 5 P 3 T 2 F 3 A 1 OS-Rank(T, D) ? OS-Rank(T, M) ? D 1 H Q 1 CSE 2331/5331

Correctness ? Time complexity? O(h) CSE 2331/5331

Need to maintain augmented information under dynamic operations Are we done ? Need to maintain augmented information under dynamic operations Insert / delete Extract-Max can be implemented with delete CSE 2331/5331

Example M 9 C 5 P 3 T 2 F 3 A 1 Insert(J) ? D 1 H 1 Q 1 CSE 2331/5331

During the downward search, increase the size attribute of each node visited along the path from root to the final insert location. Time complexity: O(h) However, if we have to maintain balanced binary search tree, say Red-black tree Also need to adjust size attribute after rotation CSE 2331/5331

Left-Rotate y.size = x.size x.size = x.left.size + x.right.size + 1 O(1) time per rotation CSE 2331/5331

Right-rotate can be done similarly. Overall: Two phases: Update size for all nodes along the path from root to insertion location O(h) = O(lg n) time Update size for the Fixup stage involving O(1) rotations O(1) + O(lg n) = O(lg n) time O(h) = O(lg n) time to insert in a Red-Black tree Same asymptotic time complexity as the non-augmented version CSE 2331/5331

Delete Two phases: Decrement size in each node on the path from the root to the node to be deleted O(h) = O(lg n) time During Fixup (to maintain balanced binary search tree property), update size for O(1) rotations O(1) + O(lg n) time Overall: CSE 2331/5331

Summary Simple example of augmenting data structures In general, the augmented information can be quite complicated Can be a separate data structure! Need to consider how to maintain such information under dynamic changes CSE 2331/5331