Balanced Trees Balanced trees have height O(lg n).

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Red-Black tree Recall binary search tree –Key values in the left subtree = the node value Operations:
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Computer Science Red-Black CS 330: Algorithms and Red-Black Trees Gene Itkis.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
13. Red-Black Tree Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P.2 One of many search-tree schemes that are “ balanced ” in order to guarantee that.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
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.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
Balanced Binary Search Trees
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
© 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1.
Red-Black tree Recall binary search tree –Key values in the left subtree = the node value Operations:
Red-Black Trees CS302 Data Structures Dr. George Bebis.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Introduction to Algorithms Jiafen Liu Sept
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Red-Black Trees Red-black trees: –Binary search trees augmented with node color –Operations designed to guarantee that the height h = O(lg n)
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in They came up with the AVL tree. In.
Data StructuresData Structures Red Black Trees. Red-black trees: Overview Red-black trees are a variation of binary search trees to ensure that the tree.
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
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Definitions and Bottom-Up Insertion
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Summary of General Binary search tree
Design and Analysis of Algorithms
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees Motivations
CS200: Algorithms Analysis
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
CS 583 Analysis of Algorithms
Analysis of Algorithms CS 477/677
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

Balanced Trees Balanced trees have height O(lg n). Height-balanced trees At each node, height of left and right subtrees are “close”. e.g. AVL trees, B-trees, red-black trees, splay trees Weight-balanced trees At each node, number of nodes in left and right subtrees are “close”. Search, Predecessor, Successor, Minimum, Maximum O(lg n) time. Insert and Delete may have to rebalance the tree.

Rebalancing Heuristic -- Rotation Right-Rotate(T, y) y x y C C Left-Rotate(T, x) x A A B B Preserves the inorder key sequence. Rotation Takes O(1) time. To be used by insertion and deletion on a red-black tree.

An Example of Rotation 7 6 2 3 4 x y 7 6 2 3 4 11 9 9 18 19 20 22 12 17 14 19 20 22 12 17 14 Left-Rotate(x) 18 11 y x

Red-Black Trees A “balanced” binary search tree with height (lg n). Basic dynamic-set operations: Search, Predecessor, Successor, Minimum Maximum, Insert, Delete all take O(lg n) time.

Node of a Red-Black Tree color key left right parent NIL as pointers to external nodes (leaves) of the tree. Key-bearing nodes as internal nodes of the tree.

Red-Black Properties 12 internal node 8 14 NILl 4 9 15 NILl NILl NILl NILl NILl 5 external node (requiring no extra storage) NILl NILl 1. Every node is either red or black. No path is more than twice as long as any other. 2. The root is black. 3. Every leaf (NIL) is black. 4. If a node is red, then both of its children are black. 5. Every simple path from the root to a descendant leaf contains the same number of black nodes.

Sentinel (Save Storage) parent 12 8 14 4 9 15 5 nil(T)

All Nodes Are Black A complete binary tree! 12 8 14 4 9 13 20 NILl NILl NILl NILl NILl NILl NILl NILl Red nodes may be seen as “fill-ins” to a complete binary search tree.

Black-Height The black-height of a node x, denoted bh(x), is the number of black nodes on any path from x (excluded) to a leaf. bh = 3 17 2 1 bh = 2 14 21 10 23 19 16 7 20 NILl NILl 12 15 NILl NILl 3 NILl NILl NILl NILl NILl NILl NILl A node at height h has black-height ≥ h/2. NILl NILl

#Internal Nodes vs Black Height Claim Subtree rooted at node x contains  2 – 1 internal nodes. bh(x) Proof By induction on the height h of x. h = 0 xx 2 – 1 = 0 internal node. h > 0 x x or ≥bh(x)–1 bh(x)–1 black height height ≤ h – 1 ≥ 2 – 1 bh(x)–1 ≥ 2 – 1 #internal nodes (by induction) ≥ 2 – 1 2 – 1 + 2 – 1 + 1 = 2 – 1 bh(x) bh(x)–1 internal node total: ≥

#Internal Nodes vs Height Lemma A red-black tree with n internal nodes has height at most 2lg (n+1). Proof The root of a red-black tree of height h has black-height  h/2. This is because on any path down from the root a red node is always followed by a black node (but not necessarily vice versa). h/2 By the claim there are at least 2 – 1 internal nodes in the tree. Therefore n  2 – 1 and h  2 lg(n+1). Corollary Search, Minimum, Maximum, Successor, and Predecessor can be done in O(lg n) time.