CSCI Trees and Red/Black Trees

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

CMPT 225 Red–black trees. Red-black Tree Structure A red-black tree is a BST! Each node in a red-black tree has an extra color field which is red or 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.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
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 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
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 CS302 Data Structures Dr. George Bebis.
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:
Introduction to Algorithms Jiafen Liu Sept
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
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.
CSIT 402 Data Structures II
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
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 Top-Down Insertion. Review of Bottom-Up Insertion In B-Up insertion, “ordinary” BST insertion was used, followed by correction of the.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,
Red-Black Trees Definitions and Bottom-Up Insertion.
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 Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
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.
Lecture 23 Red Black Tree Chapter 10 of textbook
AA Trees.
File Organization and Processing Week 3
Balanced Search Trees Modified from authors’ slides.
Multiway Search Trees Data may not fit into main memory
UNIT III TREES.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Balanced Trees (AVL and RedBlack)
Red-Black Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Red-Black Trees Bottom-Up Deletion.
Red Black Trees.
Red-Black Trees Motivations
Data Structures Balanced Trees CSCI
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
TCSS 342, Winter 2006 Lecture Notes
Wednesday, April 18, 2018 Announcements… For Today…
CS202 - Fundamental Structures of Computer Science II
Height Balanced Trees 2-3 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.
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
B- Trees D. Frey with apologies to Tom Anastasio
Lecture 9 Algorithm Analysis
B- Trees D. Frey with apologies to Tom Anastasio
Lecture 9 Algorithm Analysis
Data Structures and Algorithms
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.
2-3-4 Trees Red-Black Trees
B- Trees D. Frey with apologies to Tom Anastasio
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Mark Redekopp David Kempe
Red-black tree properties
Red Black Trees Top-Down Deletion.
Presentation transcript:

CSCI 104 2-3-4 Trees and Red/Black Trees Mark Redekopp David Kempe

Definition 2-3-4 trees are very much like 2-3 trees but form the basis of a balanced, binary tree representation called Red-Black (RB) trees which are commonly used [used in C++ STL map & set] We study them mainly to ease understanding of RB trees 2-3-4 Tree is a tree where Non-leaf nodes have 1 value & 2 children or 2 values & 3 children or 3 values & 4 children All leaves are at the same level Like 2-3 trees, 2-3-4 trees are always full and thus have an upper bound on their height of log2(n) a 2 Node a 3 Node 1 2 4 a 4 Node 5 10 20 a valid 2-3-4 tree 5 10 20 2 4 7 13 21

2-, 3-, & 4-Nodes 4-nodes require more memory and can be inefficient when the tree actually has many 2 nodes template <typename T> struct Item234 { T val1; T val2; T val3; Item234<T>* left; Item234<T>* midleft; Item234<T>* midright; Item234<T>* right; int nodeType; }; a 2 Node a 3 Node _ _ _ a 4 Node _ _ _

2-3-4 Search Trees Similar properties as a 2-3 Search Tree 4 Node: Left subtree nodes are < l Middle-left subtree > l and < r Right subtree nodes are > r a 2 Node a 3 Node m l r <m > m <l > l && < r > r a 4 Node l m r <l > l && < m > m && < r > r

2-3-4 Insertion Algorithm Key: Rather than search down the tree and then possibly promote and break up 4-nodes on the way back up, split 4 nodes on the way down To insert a value, 1. If node is a 4-node Split the 3 values into a left 2-node, a right 2-node, and promote the middle element to the parent of the node (which definitely has room) attaching children appropriately Continue on to next node in search order 2a. If node is a leaf, insert the value 2b. Else continue on to the next node in search tree order Insert 60, 20, 10, 30, 25, 50, 80 Key: 4-nodes get split as you walk down thus, a leaf will always have room for a value Empty Add 60 Add 20 Add 10 Add 30 20 20 60 20 60 10 20 60 10 60 10 30 60

2-3-4 Insertion Algorithm Key: Split 4 nodes on the way down To insert a value, 1. If node is a 4-node Split the 3 values into a left 2-node, a right 2-node, and promote the middle element to the parent of the node (which definitely has room) attaching children appropriately Continue on to next node in search order 2a. If node is a leaf, insert the value 2b. Else continue on to the next node in search tree order Insert 60, 20, 10, 30, 25, 50, 80 Key: 4-nodes get split as you walk down thus, a leaf will always have room for a value Add 25 Add 50 Split first, then add 50 20 30 20 20 50 10 25 50 60 10 25 30 60 10 25 30 60

2-3-4 Insertion Algorithm Key: Split 4 nodes on the way down To insert a value, 1. If node is a 4-node Split the 3 values into a left 2-node, a right 2-node, and promote the middle element to the parent of the node (which definitely has room) attaching children appropriately Continue on to next node in search order 2a. If node is a leaf, insert the value 2b. Else continue on to the next node in search tree order Insert 60, 20, 10, 30, 25, 50, 80 Key: 4-nodes get split as you walk down thus, a leaf will always have room for a value Add 80 20 30 20 30 80 10 25 50 60 10 25 50 60 80

2-3-4 Insertion Exercise 1 Add 55 20 30 10 25 50 60 80

2-3-4 Insertion Exercise 2 Add 58 20 30 60 10 25 50 55 80

2-3-4 Insertion Exercise 3 Add 57 30 20 60 10 25 50 55 58 80

2-3-4 Insertion Exercise 3 Resulting Tree 30 20 55 60 10 25 50 57 58 80

B-Trees 2-3 and 2-3-4 trees are just instances of a more general data structure known as B-Trees Define minimum number of children (degree) for non-leaf nodes, d Non-root nodes must have at least d-1 keys and d children All nodes must have at most 2d-1 keys and 2d children 2-3-4 Tree (d=2) Used for disk-based storage and indexing with large value of d to account for large random-access lookup time but fast sequential access time of secondary storage

B Tree Resources https://www.cs.usfca.edu/~galles/visualization/BTree.html http://ultrastudio.org/en/2-3-4_tree

Tree Rotations

What values might be in the subtree rooted here BST Subtree Ranges Consider a binary search tree, what range of values could be in the subtree rooted at each node At the root, any value could be in the "subtree" At the first left child? At the first right child? What values might be in the subtree rooted here (-inf, inf) (-inf,inf) z x ( ) ( ) ( ) ( ) y d a y ( ) ( ) ( ) ( ) x c b z ( ) ( ) ( ) ( ) a b c d

BST Subtree Ranges Consider a binary search tree, what range of values could be in the subtree rooted at each node At the root, any value could be in the "subtree" At the first left child? At the first right child? (-inf, inf) (-inf, inf) z x (-inf, z) (z, inf) (-inf, x) (x, inf) y d a y (x, y) (y, inf) (-inf, y) (y,z) x c b z (-inf, x) (x,y) (y,z) (z,inf) a b c d

Right Rotation Define a right rotation as taking a left child, making it the parent and making the original parent the new right child Where do subtrees a, b, c and d belong? Use their ranges to reason about it… (-inf, inf) z y Right rotate of y & z (-inf, z) (z, inf) y d x z (-inf, y) (y,z) c ___ ___ x ___ ___ (-inf, x) (x,y) a b

Right Rotation Define a right rotation as taking a left child, making it the parent and making the original parent the new right child Where do subtrees a, b, c and d belong? Use their ranges to reason about it… (-inf, inf) z y Right rotate of y & z (-inf, z) (z, inf) y d x z (-inf, y) (y,z) a b x c c d (-inf, x) (x,y) (y,z) (z, inf) (-inf, x) (x,y) a b

Left Rotation Define a left rotation as taking a right child, making it the parent and making the original parent the new left child Where do subtrees a, b, c and d belong? Use their ranges to reason about it… (-inf, inf) y x Left rotate of y & x (-inf, x) (x, inf) x z a y (x, y) (y, inf) ___ ___ ___ ___ b z (y,z) (z,inf) c d

Left Rotation Define a left rotation as taking a right child, making it the parent and making the original parent the new left child Where do subtrees a, b, c and d belong? Use their ranges to reason about it… (-inf, inf) y x Left rotate of y & x (-inf, x) (x, inf) x z a y (x, y) (y, inf) a b c d b z (-inf, x) (x,y) (y,z) (z, inf) (y,z) (z,inf) c d

Rotations Define a right rotation as taking a left child, making it the parent and making the original parent the new right child Where do subtrees a, b, and c belong? Use their ranges to reason about it… (-inf, inf) (-inf, inf) y x Right rotate of x & y (-inf, y) (y, inf) (-inf, x) (x, inf) x c a y (x, y) (y, inf) (-inf, x) (x,y) Left rotate of y & x a b b c

Rotation's Effect on Height When we rotate, it serves to re-balance the tree z y Right rotate of y and z h+1 y x z h h+2 h+3 x c h h h h h h h For now let's always specify the two nodes involved in a rotation (i.e. right rotate y and z). We could just specify one node if we assume that node is the parent or the child, but this often gets new programmers into trouble when they forget the assumption. Better to be explicit

"Balanced" Binary Search Trees Red Black Trees

Red Black Trees A red-black tree is a binary search tree Only 2 nodes (no 3- or 4-nodes) Can be built from a 2-3-4 tree directly by converting each 3- and 4- nodes to multiple 2-nodes All 2-nodes means no wasted storage overheads Yields a "balanced" BST "Balanced" means that the height of an RB-Tree is at MOST twice the height of a 2-3-4 tree Recall, height of 2-3-4 tree had an upper bound of log2(n) Thus height or an RB-Tree is bounded by 2*log2n which is still O(log2(n))

Red Black and 2-3-4 Tree Correspondence Every 2-, 3-, and 4-node can be converted to… At least 1 black node and 1 or 2 red children of the black node Red nodes are always ones that would join with their parent to become a 3- or 4-node in a 2-3-4 tree a 2-node m m m a 4 Node S = Small M = Median L = Large s m l s l a b c d s l a 3 Node l s a b c or

Red Black Trees Below is a 2-3-4 tree and how it can be represented as a directly corresponding RB-Tree Notice at most each 2-3-4 node expands to 2 level of red/black nodes Q: Thus if the height of the 2-3-4 tree was bound by log2n, then the height of an RB-tree is bounded by? A: 2*log2n = O(log2n) 20 20 30 10 25 50 60 80 Equivalent RB-Tree 10 30 25 60 50 80

Red-Black Tree Properties Valid RB-Trees maintain the invariants that… 1. No path from root to leaf has two consecutive red nodes (i.e. a parent and its child cannot both be red) Since red nodes are just the extra values of a 3- or 4-node from 2-3-4 trees you can't have 2 consecutive red nodes 2. Every path from leaf to root has the same number of black nodes Recall, 2-3-4 trees are full (same height from leaf to root for all paths) Also remember each 2, 3-, or 4- nodes turns into a black node plus 0, 1, or 2 red node children 3. At the end of an operation the root should always be black 4. We can imagine leaf nodes as having 2 non-existent (NULL) black children if it helps

Red-Black Insertion Insertion Algorithm: fixTree involves either 1. Insert node into normal BST location (at a leaf location) and color it RED 2a. If the node's parent is black (i.e. the leaf used to be a 2-node) then DONE (i.e. you now have what was a 3- or 4-node) 2b. Else perform fixTree transformations then repeat step 2 on the parent or grandparent (whoever is red) fixTree involves either recoloring or 1 or 2 rotations and recoloring Which case of fixTree you perform depends on the color of the new node's "aunt/uncle" Insert 10 grandparent 30 aunt/ uncle parent 20 40 x 10

fixTree Cases 1. G G G P U P U P G U N P U N N G 2. G G P G U P N U P Recolor P U P U N P G U N P U N c N c a b a b G 2. G G Recolor P G U N P N U P U P U a N a N b c b c Note: For insertion/removal algorithm we consider non-existent leaf nodes as black nodes 3. Recolor Root R R

fixTree Cases 4. G P P G N P G P U N G N c U c U N U 5. G G N P U N U 1 Rotate / Recolor Right rotate of P,G P U N G N c U c U a b N c a b c U a b a b 5. G G N 2 Rotates / Recolor Right rotate of N,G & Recolor Left rotate of N,P P U N U P G a N P c a b c U b c a b P G P N G a N U a b c U b c www.cse.ohio-state.edu/~gurari/course/cis680Ch11.html

Insertion Insert 10, 20, 30, 15, 25, 12, 5, 3, 8 Empty Insert 10 Insert 20 Insert 30 Case 4: Left rotate and recolor Violates consec. reds 10 10 10 20 30 20 20 10 30 Insert 15 Insert 25 20 20 20 20 10 30 10 30 10 30 10 30 Case 2: Recolor Case 3: Recolor root 15 15 15 15 25

Insertion Insert 10, 20, 30, 15, 25, 12, 5, 3, 8 Insert 12 20 20 20 30 10 30 10 12 30 12 15 25 25 10 15 25 12 Case 5: Right Rotate… 15 Case 5: … Right Rotate and recolor Insert 5 20 20 12 30 12 30 10 15 25 10 15 25 Case 1: Recolor 5 Recursive call "fix" on 12 but it's parent is black so we're done 5

Insertion Insert 10, 20, 30, 15, 25, 12, 5, 3, 8 Insert 3 20 20 12 30 12 30 10 15 25 5 15 25 5 3 Case 4: Rotate 10 3

Insertion Insert 10, 20, 30, 15, 25, 12, 5, 3, 8 Insert 8 20 20 12 12 30 12 30 5 20 5 15 25 5 15 25 3 10 15 30 3 10 3 10 8 25 8 Case 2: Recolor 8 Case 4: Rotate 12

Insertion Exercise 1 Insert 27 12 5 20 G 3 10 15 30 P 8 25 N 27

Insertion Exercise 1 G P N This is case 5. Left rotate around P 12 12 5 5 20 G 20 3 10 15 30 3 10 15 27 P 8 25 8 25 30 N 27 This is case 5. Left rotate around P Right rotate around N Recolor

Insertion Exercise 2 Insert 40 12 5 20 G 3 10 15 27 P A 8 25 30 N 40

Insertion Exercise 2 G P A N Aunt and Parent are the 12 12 5 5 20 20 G 3 10 15 27 3 10 15 27 P A 8 25 30 8 25 30 N 40 40 Aunt and Parent are the same color. So recolor aunt, parent, and grandparent.

Insertion Exercise 2 G A P N Aunt and Parent are the 12 12 A P 5 5 20 20 N 3 10 15 27 3 10 15 27 8 25 30 8 25 30 40 40 Aunt and Parent are the same color. So recolor aunt, parent, and grandparent.

Insertion Exercise 3 Insert 50 12 5 20 3 10 15 27 G 8 25 30 P 40 N 50

Insertion Exercise 3 G P N Remember, empty nodes are black. 12 12 5 5 20 20 3 10 15 27 3 10 15 27 G 8 25 30 8 25 40 P 40 30 50 N 50 Remember, empty nodes are black. Do a left rotation around P and recolor.

Insertion Exercise 4 G A P N 12 5 20 3 10 15 27 8 25 40 30 50 45

Insertion Exercise 4 G A P N Aunt and Parent are the same color. 12 12 5 5 20 20 3 10 15 27 3 10 15 27 G 8 8 25 40 25 40 A P 30 50 30 50 45 45 N Aunt and Parent are the same color. Just recolor.

Insertion Exercise 4 12 G 5 20 A P 3 10 15 27 8 25 40 N 30 50 45

Final Result 12 5 27 40 3 10 20 15 25 30 50 8 45

Insertion Exercise 5 G A P N 12 5 27 40 3 10 20 8 15 25 30 50 9 45

Insertion Exercise 5 12 5 27 40 3 9 20 15 25 30 50 8 10 45

RB-Tree Visualization & Links https://www.cs.usfca.edu/~galles/visualization/RedBlack.html

RB Tree Implementation

Hints Implement private methods: findMyUncle() AmIaRightChild() AmIaLeftChild() RightRotate LeftRotate Need to change x's parent, y's parent, b's parent, x's right, y's left, x's parent's left or right, and maybe root (-inf, inf) (-inf, inf) y x Right rotate of x,y (-inf, y) (y, inf) (-inf, x) (x, inf) x c a y Left rotate of y,x (x, y) (y, inf) (-inf, x) (x,y) a b b c

Hints You have to fix the tree after insertion if… Watch out for traversing NULL pointers node->parent->parent However, if you need to fix the tree your grandparent… Cases break down on uncle's color If an uncle doesn't exist (i.e. is NULL), he is (color?)… (-inf, inf) (-inf, inf) y x Right rotate of x,y (-inf, y) (y, inf) (-inf, x) (x, inf) x c a y Left rotate of y,x (x, y) (y, inf) (-inf, x) (x,y) a b b c

For Print

fixTree Cases 1. G G G P U P U P G U N P U N N G 2. G G P G U P N U P Recolor P U P U N P G U N P U N c N c a b a b G 2. G G Recolor P G U N P N U P U P U a N a N b c b c Note: For insertion/removal algorithm we consider non-existent leaf nodes as black nodes 3. Recolor Root R R

fixTree Cases 4. G P P G N P G P U N G N c U c U N U 5. G G N P U N U 1 Rotate / Recolor Right rotate of P,G P U N G N c U c U a b N c a b c U a b a b 5. G G N 2 Rotates / Recolor Right rotate of N,G & Recolor Left rotate of N,P P U N U P G a N P c a b c U b c a b P G P N G a N U a b c U b c www.cse.ohio-state.edu/~gurari/course/cis680Ch11.html