David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

David Luebke 1 6/7/2014 CS 332: Algorithms Skip Lists Introduction to Hashing.
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
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.
CS3424 AVL Trees Red-Black Trees. Trees As stated before, trees are great ways of holding hierarchical data Insert, Search, Delete ~ O(lgN) But only if.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  Implement red-black.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
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.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Balanced Trees Balanced trees have height O(lg n).
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.
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 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
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.
CSIT 402 Data Structures II
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 4: Data Structures.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
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.
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: 
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION.
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.
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)
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
Binary Search Trees What is a binary search tree?
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Design and Analysis of Algorithms
Red-Black Trees Motivations
CS200: Algorithms Analysis
Red-Black Trees.
CMSC 341 (Data Structures)
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Algorithms and Data Structures Lecture VIII
Red-Black Trees.
Augmenting Data Structures: Interval Trees
Analysis of Algorithms CS 477/677
Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II
Red-black tree properties
Red-Black Trees CS302 Data Structures
Presentation transcript:

David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists

David Luebke 2 3/20/2016 Administration ● Hand back homework 3 ● Hand back exam 1 ● Go over exam

David Luebke 3 3/20/2016 Review: Red-Black Trees ● Red-black trees: ■ Binary search trees augmented with node color ■ Operations designed to guarantee that the height h = O(lg n) ● We described the properties of red-black trees ● We proved that these guarantee h = O(lg n) ● Next: describe operations on red-black trees

David Luebke 4 3/20/2016 Review: Red-Black Properties ● The red-black properties: 1. Every node is either red or black 2.Every leaf (NULL pointer) is black ○ Note: this means every “real” node has 2 children 3.If a node is red, both children are black ○ Note: can’t have 2 consecutive reds on a path 4.Every path from node to descendent leaf contains the same number of black nodes 5.The root is always black

David Luebke 5 3/20/2016 Review: RB Trees: Rotation ● Our basic operation for changing tree structure is called rotation: ● Preserves BST key ordering ● O(1) time…just changes some pointers y x C AB x A y BC rightRotate(y) leftRotate(x)

David Luebke 6 3/20/2016 Review: Red-Black Trees: Insertion ● Insertion: the basic idea ■ Insert x into tree, color x red ■ Only r-b property 3 might be violated (if p[x] red) ○ If so, move violation up tree until a place is found where it can be fixed ■ Total time will be O(lg n)

rbInsert(x) treeInsert(x); x->color = RED; // Move violation of #3 up tree, maintaining #4 as invariant: while (x!=root && x->p->color == RED) if (x->p == x->p->p->left) y = x->p->p->right; if (y->color == RED) x->p->color = BLACK; y->color = BLACK; x->p->p->color = RED; x = x->p->p; else // y->color == BLACK if (x == x->p->right) x = x->p; leftRotate(x); x->p->color = BLACK; x->p->p->color = RED; rightRotate(x->p->p); else // x->p == x->p->p->right (same as above, but with “right” & “left” exchanged) Case 1 Case 2 Case 3

rbInsert(x) treeInsert(x); x->color = RED; // Move violation of #3 up tree, maintaining #4 as invariant: while (x!=root && x->p->color == RED) if (x->p == x->p->p->left) y = x->p->p->right; if (y->color == RED) x->p->color = BLACK; y->color = BLACK; x->p->p->color = RED; x = x->p->p; else // y->color == BLACK if (x == x->p->right) x = x->p; leftRotate(x); x->p->color = BLACK; x->p->p->color = RED; rightRotate(x->p->p); else // x->p == x->p->p->right (same as above, but with “right” & “left” exchanged) Case 1: uncle is RED Case 2 Case 3

David Luebke 9 3/20/2016 Review: RB Insert: Case 1 if (y->color == RED) x->p->color = BLACK; y->color = BLACK; x->p->p->color = RED; x = x->p->p; ● Case 1: “uncle” is red ● In figures below, all  ’s are equal-black-height subtrees C A D  B   C A D  B   x y new x Change colors of some nodes, preserving #4: all downward paths have equal b.h. The while loop now continues with x’s grandparent as the new x case 1

David Luebke 10 3/20/2016 B  x Review: RB Insert: Case 2 if (x == x->p->right) x = x->p; leftRotate(x); // continue with case 3 code ● Case 2: ■ “Uncle” is black ■ Node x is a right child ● Transform to case 3 via a left-rotation C A  C B y A  x  case 2  y  Transform case 2 into case 3 (x is left child) with a left rotation This preserves property 4: all downward paths contain same number of black nodes

David Luebke 11 3/20/2016 Review: RB Insert: Case 3 x->p->color = BLACK; x->p->p->color = RED; rightRotate(x->p->p); ● Case 3: ■ “Uncle” is black ■ Node x is a left child ● Change colors; rotate right B A x  case 3 C B A  x  y  C  Perform some color changes and do a right rotation Again, preserves property 4: all downward paths contain same number of black nodes

David Luebke 12 3/20/2016 Red-Black Trees ● Red-black trees do what they do very well ● What do you think is the worst thing about red- black trees? ● A: coding them up

David Luebke 13 3/20/2016 Skip Lists ● A relatively recent data structure ■ “A probabilistic alternative to balanced trees” “A probabilistic alternative to balanced trees” ■ A randomized algorithm with benefits of r-b trees ○ O(lg n) expected time for Search, Insert ○ O(1) time for Min, Max, Succ, Pred ■ Much easier to code than r-b trees ■ Fast!

David Luebke 14 3/20/2016 Linked Lists ● Think about a linked list as a structure for dynamic sets. What is the running time of: ■ Min() and Max() ? ■ Successor() ? ■ Delete() ? ○ How can we make this O(1)? ■ Predecessor() ? ■ Search() ? ■ Insert() ? Goal: make these O(lg n) time in a linked-list setting So these all take O(1) time in a linked list. Can you think of a way to do these in O(1) time in a red-black tree?