1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Splay Trees Binary search trees.
Trees Types and Operations
1 Finger search trees. 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that.
Outline Scapegoat Trees ( O(log n) amortized time)
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.
1 B trees Nodes have more than 2 children Each internal node has between k and 2k children and between k-1 and 2k-1 keys A leaf has between k-1 and 2k-1.
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.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Trees and Red-Black Trees Gordon College Prof. Brinton.
Red Black Trees Colored Nodes Definition Binary search tree.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
1 Binomial heaps, Fibonacci heaps, and applications.
Weight balance trees (Nievergelt & Reingold 73)
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:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
B + -Trees Same structure as B-trees. Dictionary pairs are in leaves only. Leaves form a doubly-linked list. Remaining nodes have following structure:
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
1 Biased 2-b trees (Bent, Sleator, Tarjan 1980). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)
Balanced Binary Search Trees
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
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
More Trees. Outline Tree B-Tree 2-3 Tree Tree Red-Black Tree.
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.
Chapter 47 Red Black Trees
AA Trees.
File Organization and Processing Week 3
Chapter 48 Red Black Trees
G64ADS Advanced Data Structures
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red Black Trees Colored Nodes Definition Binary search tree.
Red Black Trees
Splay Trees Binary search trees.
Finger search trees.
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
AVL Tree.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Splay Trees Binary search trees.
Red-Black Trees Motivations
Data Structures Lecture 4 AVL and WAVL Trees Haim Kaplan and Uri Zwick
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Advanced Associative Structures
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Chapter 43 Red Black Trees
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Binary Search Trees.
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red-Black Trees Bottom-Up Deletion.
Red Black Trees (Guibas Sedgewick 78)
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Red-black tree properties
Red Black Trees.
Splay Trees Binary search trees.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

1 Red Black Trees (Guibas Sedgewick 78)

2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2) : Assumes that all items in L2 are greater than all items in L1. split(x,L) : returns two lists one with all item less than or equal to x and the other with all items greater than x.

3 Binary search trees Binary search tree: All internal nodes have degree = 2 Items are at the leaves. All items in left subtree are smaller than all items in right subtree (symmetric order)

4 Red Black trees - definition Binary search tree each node is colored red or black such that 1) Leaves are black 2) All paths from the root to an external node contain the same number of black nodes (black rule) 3) Any red node, if has a parent, has a black parent (red rule)

5 Red Black trees - example

6 Red Black Trees -properties The depth is O(log n) -- prove as an excercise ==> find takes O(log n) time How do we do the other operations and keep the trees Red- Black ?

7 Insert

8 Insert (cont)

9

10 Insert (cont)

11 Insert (cont)

12 Use rotations x y B C y A x BC A x y z y zx ===>

13 Insert (cont) x y z y zx ====>

14 Insert (cont)

15 Insert (cont)

16 Insert (cont)

17 Insert (cont)

18 Insert (cont)

19 Insert (cont)

20 Insert -- definition Convert a leaf to a red internal node with two leaves. This may create violation to property 2. To restore it we walk up towards the root applying one of the following cases (each case has a symmetric version)

21 Insert -- non terminal cases z y x AB C DE z y x AB C DE z y x BC A DE ===> (1) (2) y x BC A DE ===>

22 Insert -- terminal cases z y x AB C DE y z x AB C z y x BC A DE ===> (3) (4) x B x B ===> w DE w (5) ===> wz y AB C DE w x z y x AB C DE w

23 Insert - analysis O(log n) time worst case, since the height is O(log n) Suppose you start with an empty tree and do m insertions such that the point of insertion is given to you each time, how much time does it take ? Obviously O(mlog n), but maybe we can prove it cannot be that bad ?

24 Amortized analysis We are interested in the worst case running time of a sequence of operations. Example: binary counter single operation -- increment

25 Amortized analysis (Cont.) On the worst case increment takes O(k). k = #digits What is the complexity of a sequence of increments (on the worst case) ? Define a potential of the counter: Amortized(increment) = actual(increment) +   (c) = ?

26 Amortized analysis (Cont.) Amortized(increment 1 ) = actual(increment 1 ) +  1 -  0 Amortized(increment 2 ) = actual(increment 2 ) +  2 -  1 Amortized(increment n ) = actual(increment n ) +  n -  (n-1) ………… +  i Amortized(increment i ) =  i actual(increment i ) +  n -  0  i Amortized(increment i )   i actual(increment i ) if  n -  0  0

27 Amortized analysis (Cont.) Define a potential of the counter:  (c) = #(ones) Amortized(increment) = actual(increment) +  Amortized(increment) = 1+ #(1 => 0) #(1 => 0) = O(1) ==> Sequence of n increments takes O(n) time

28 Insert - analysis Each time we do a color-flip-step the number of red nodes decreases by one.  (tree) = #red nodes Actual(insert) = O(1) + #color-flips-steps  (insert) = O(1) - #color-flips-steps ==> amortized(insert) = O(1) and the sequence actually takes O(m) time.

29 Delete -- example

30 Delete -- example (cont) -

31 Delete -- example (cont)

32 Delete -- example2 (cont)

33 Delete -- example2 (cont) -

34 Delete -- example2 (cont) -

35 Delete -- example2 (cont)

36 Delete -- definition Replace the parent of the external node containing the item with the sibling subtree of the deleted item If the parent of the deleted item is black then we create a short node To restore the black constraint we go bottom up applying one of the following cases.

37 Delete -- fixing a short node - - ====> (1) - ====> (2)

38 Delete -- fixing a short node (cont) - ====> (3) x y z wx y w z - ====> (4) x y z w A x y v v BA - x y z w v B A - w B

39 Delete -- fixing a short node (cont) - ====> (5) x y z w y z v w x v - And apply one of the previous 3 cases.

40 Delete + insert -- analysis O(log n) time, since the height is O(log n) Suppose you start with an empty tree and do m insertions and deletions such that the point of insertion is given to you each time, how much time does it take ? Obviously O(mlog n), but maybe we can prove it cannot be that bad ?

41 Delete + insert - analysis The previous potential won’t do the trick  (tree) = #red nodes Here are the transformation that we want to release potential

42 Delete + insert -- analysis - - ====> ===>

43 Delete + insert -- analysis  (tree) = #() + 2 #() ==> amortized(delete) = O(1) amortized(insert) = O(1) sequence of m delete and inserts, starting from an empty tree takes O(m) time

44 Concatenation + = Define the rank of a node v as the number of black nodes from v to a leaf. Assume T1 has a black root. Look on the left spine of T2 for a node x of the same rank as the root of T1 T1 T2

45 Concatenation (cont) + = T1 T2 x y Make y a child of p(x) Continue as for insert Allocate y make the root of T1 and x children of y. Color y red

46 Concatenation (analysis) O(|r1-r2| + 1) = O(log n) worst case. If the right point on the spine of the taller tree is given then its O(1) amortized

47 Split x Concatenate all trees to the left of the path from the root to x to from the left tree T1 (including x). Concatenate all trees to the right of the path from the root to x to from the right tree T2

48 Split -- analysis. Can prove that split takes O(log n) time.