Search Trees. COMPSCI 355 Fall 2016

Similar documents
CS350: Data Structures Red-Black Trees

13.4 Deletion in red-black trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Algorithms. Deleting from Red-Black Trees B-Trees

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

13.4 Deletion in red-black trees

Search Trees - 1 Venkatanatha Sarma Y

Search Trees - 2. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore. M.S Ramaiah School of Advanced Studies - Bangalore

Chapter 12 Advanced Data Structures

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Lecture 6: Analysis of Algorithms (CS )

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Multiway Search Trees. Multiway-Search Trees (cont d)

Self-Balancing Search Trees. Chapter 11

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

Search Trees. Undirected graph Directed graph Tree Binary search tree

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

B Tree. Also, every non leaf node must have at least two successors and all leaf nodes must be at the same level.

Red-Black Trees. 2/24/2006 Red-Black Trees 1

Trees. (Trees) Data Structures and Programming Spring / 28

Programming II (CS300)

CHAPTER 10 AVL TREES. 3 8 z 4

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Week 3 Web site:

Lecture: Analysis of Algorithms (CS )

Analysis of Algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

Data Structures and Algorithms CMPSC 465

Algorithms. AVL Tree

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

CISC 235: Topic 4. Balanced Binary Search Trees

Binary Search Trees. Analysis of Algorithms

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

CS 3343 Fall 2007 Red-black trees Carola Wenk

CS350: Data Structures B-Trees

CIS265/ Trees Red-Black Trees. Some of the following material is from:

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Data Structure: Search Trees 2. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Ch04 Balanced Search Trees

TREES. Trees - Introduction

Multi-way Search Trees

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Properties of red-black trees

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Motivation for B-Trees

Red-Black Trees Goodrich, Tamassia. Red-Black Trees 1

CS 758/858: Algorithms

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Data Structures Week #6. Special Trees

CS 350 : Data Structures B-Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Augmenting Data Structures

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Theory & Algorithms 15/01/04. Red-Black Trees. Nicolas Wack, Sylvain Le Groux

Balanced search trees

Binary Trees, Binary Search Trees

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Balanced Trees Part Two

Recall: Properties of B-Trees

From (2,4) to Red-Black Trees

Section 4 SOLUTION: AVL Trees & B-Trees

CS 380 ALGORITHM DESIGN AND ANALYSIS

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

CSE 326: Data Structures B-Trees and B+ Trees

Advanced Tree Data Structures

Trees. Truong Tuan Anh CSE-HCMUT

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

B-Trees and External Memory

OPPA European Social Fund Prague & EU: We invest in your future.

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

B-Trees and External Memory

Data Structures Week #6. Special Trees

CSE 530A. B+ Trees. Washington University Fall 2013

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

A red-black tree is a balanced binary search tree with the following properties:

Cpt S 122 Data Structures. Data Structures Trees

Balanced Trees Part One

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Advanced Set Representation Methods

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Analysis of Algorithms

Data Structures Week #6. Special Trees

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Data Structure - Advanced Topics in Tree -

CS 331 DATA STRUCTURES & ALGORITHMS BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, THE TREE TRAVERSALS, B TREES WEEK - 7

Data Structures in Java

Trees. Eric McCreath

Binary Trees

Data Structures and Algorithms

Multi-Way Search Tree

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Transcription:

Search Trees COMPSCI 355 Fall 2016

2-4 Trees Search Trees AVL trees Red-Black trees Splay trees Multiway Search Trees (2, 4) Trees External Search Trees (optimized for reading and writing large blocks) B trees B+ trees

Red-Black Trees Same asymptotic complexity as AVL and 2-4 Trees. O(1) time for trinode restructuring O(log n) time to search, insert, delete O(log n) recoloring operations per update But at most 2 structural operations per update. More popular than AVL and 2-4 trees in practice.

Defining Properties Root is black. Every leaf is black. Children of a red node are black. Every leaf has the same black depth. 5 3 10 13 17 4 7 11 14 6 8

Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 10 13 17 4 7 11 14 6 8

Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 17 7 11 14 6 8

Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 17 7 11 14 6 8

Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 14 17 7 11 6 8

Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 14 17 7 11 6 8

Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 14 17 6 7 8 11

Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 3 4 10 13 14 17 6 7 8 11

Red-Black 2-4 Collapse each red child into its parent. Height of red-black tree with n nodes is at most 2lg(n+1). 5 10 3 4 6 7 8 11 13 14 17

2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. 50 70 20 30 55 60 65 80 25 40 53 56 63 68 69 75 90

2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 30 55 60 65 80 25 40 53 56 63 68 69 75 90

2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 55 60 65 80 30 53 56 63 68 69 75 90 25 40

2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 55 60 65 80 30 53 56 63 68 69 75 90 25 40

2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 60 80 30 55 65 75 90 25 40 53 56 63 68 69

2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 70 20 60 80 30 55 65 75 90 25 40 53 56 63 68 69

2-4 Red-Black Color all nodes black. Keep the children of a 2-node black. Split up 3-nodes and 4-nodes. 50 20 70 30 60 80 25 40 55 65 75 90 53 56 63 68 69

Insertions Perform BST insertion. If new node is root, color it black; otherwise, color it red. 5 3 10 13 17 insert 2 4 7 11 14 6 8 insert 16

Insertions Perform BST insertion. If new node is root, color it black; otherwise, color it red. 5 3 10 13 17 insert 2 2 4 7 11 14 16 6 8 insert 16 Simple. Is that all there is to it?

Fixing a Double Red (Okasaki's Method) z z x x x D y D A y A z A y x C B z y D B C A B C D B C y Preserves postorder traversal: AxByCzD x z A B C D

Fixing a Double Red (Okasaki's Method) z y x z x y x z y x y z x y z A A B A A B B B C C C C D D D D A B C D Another double red might result, but it will be two levels closer to the root. Repeat as needed or until reaching the root, which may then be colored black.

Fixing a Double Red (Okasaki's Method) z z x x x D y D A y A z A y x C B z y D B C A B C D B C Another double red might result, but it will be two levels closer to the root. Repeat as needed or until reaching the root, which may then be colored black. y x z A B C D But amazingly, a double red can be fixed with a single trinode restructuring!

Fixing a Double Red 30 30 10 10 20 10 20 30 10 20 30 20 20 10 30 Parent of new node has black sibling: trinode restructuring.

Fixing a Double Red 30 30 10 20 10 20 Parent of new node has red sibling: recolor. And if this creates a new double red...?

Illustration 4 2 10 13 17 1 3 7 11 14 16 6 8 Insert 5 5 The top node of the double-red has a red sibling, so we recolor.

Illustration 4 2 10 13 17 1 3 7 11 14 16 6 8 Insert 5 5 The top node of the double-red has a red sibling, so we recolor.

Illustration The top node of the double-red has a black sibling, so we restructure. 4 2 10 13 17 1 3 7 11 14 16 6 8 5

Illustration The top node of the double-red has a black sibling, so we restructure. 7 4 10 13 17 2 6 8 11 14 16 1 3 5

Deletion Remove key 5 5 3 10 13 17 2 4 8 11 14 16 6 9 Copy successor key to node and delete successor node.

Deletion 6 3 10 13 17 2 4 8 11 14 16 9 Copy successor key to node and delete successor node. Simple! Now suppose we want to delete key 9. Well, just delete it!

Deletion 6 3 10 13 17 2 4 8 11 14 16 But what if we want to delete key 8, 10, or 11?

Deletion 6 3 10 13 17 2 4 11 14 16 The black depth property has been violated. In this case, we can just recolor.

Deletion 6 3 10 13 17 2 4 11 14 16 The black depth property has been violated. In this case, we can just recolor.

Deletion 6 3 10 13 17 2 4 11 14 16 But suppose we delete key 11 and then we delete key 10.

Deletion 6 3 13 17 2 4 14 16 The black depth property no longer holds.

Removing a Double Black 6 3 13 17 2 4 14 16 The basic idea is to double count the blackness of the problem node and look for a nearby red node to absorb the extra blackness.

Removing a Double Black 6 3 13 17 2 4 14 16 In this case, the sibling of the double black node is black and has a red child, so we can rotate and recolor...

Removing a Double Black 3 2 6 13 17 4 14 16

Removing a Double Black There are two other cases to consider. Double black may propagate up to root. If you really want to delete a key from a RBT by hand without memorizing all the details: Transform the RBT to a (2, 4) tree. Delete the desired key. Transform the (2, 4) tree back to a RBT.