Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees

Similar presentations


Presentation on theme: "Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees"— Presentation transcript:

1 Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Deleting a node from a Binary Search Tree Case 1: The node to be deleted has no children. Just delete the node. Case 2: The node to be deleted has one child. Splice out node by making link from its parent to its child. Case 3: The node to be deleted has two children. Replace node's data with data from its successor. Splice out the successor (which has no left child). 10 6 4 5 7 8 12 14 Example: Case 1: Remove 5 Case 2: Remove 8 Case 3: Remove 10 Running time?

2 Tree-Delete Pseudocode
Tree-Delete(T, z) if z.left == NIL or z.right == NIL y = z //y is node to be deleted else y = Tree-Successor(z) if y.left != NIL x = y.left //x is node we splice to x = y.right if x != NIL x.p = y.p //bypass y if y.p == NIL //If node to be deleted (y) was root T.root = x //Set new root else if y == (y.p).left //If y was on left branch of parent (y.p).left = x //attach x to left branch of parent (y.p).right = x if y != z //We spliced out successor z.key = y.key //Copy y's data into z return y //return spliced-out node

3 Problem with Binary Search Trees
We saw that the search, minimum, maximum, successor, predecessor, insert and delete functions all run in O(h) time for binary search trees (where h is the height of the tree). Problem: Sometimes the height of the search tree is n: 3 In this case, the running time for the tree is no better than for a linked list. 5 8 Solution: Find a way to guarantee that the binary search tree is more balanced. 10 13

4 Red-Black Trees Red-black trees are binary search trees with an added piece of information at each node: the color. Each node in a red-black tree has information on: color, key, left, right, and p. If a node does not have a child, the pointer will point to an external node, T.nil, which is a leaf on the tree. The root's p field also points to T.nil All normal nodes (with a key value and pointers) are internal nodes of the tree.

5 Properties of Red-black trees
Every node is either red or black The root is black Every leaf NIL is black If a node is red, both children are black For each node, all paths from that node to the descendent leaves contain the same number of black nodes. 26 Usually omitted when drawing trees 17 41 14 21 30 NIL NIL NIL NIL NIL NIL NIL

6 Black Height The black height, bh(x) of a node, x, is the number of black nodes on a path from (but not including) x down to the leaf. A leaf (NIL) has bh = 0 We can show that the height of a red-black tree with n internal nodes is at most: 2lg(n+1)

7 Height of Red-Black tree
To show that a red-black tree with n nodes has height <= 2lg(n+1), we show the following: Show, by induction, that a subtree rooted at x has at least 2bh(x)-1 internal nodes. At least half the nodes from root to leaf must be black nodes. Therefore bh >= h/2 We will work out the rest in class.

8 Proof by induction To prove part a) by induction:
Show that if height of x, h(x), is zero, a) is true. If height of x is zero, then x is a leaf and bh(x) = 0 2bh(x)-1 = 20-1 = 0. Therefore a) is true (sub tree has at least 0 internal nodes). 2) Assume show that if a) holds for tree of height h(x) -1 then it also holds for tree of the height of h(x). Assume x is internal node with height > 0 and with 2 children. Each child has black height = bh(x) OR black height = bh(x) -1 Part a) holds for both children, so each child has at least 2bh(x)-1-1 nodes. Therefore, tree rooted at x has at least: (2bh(x)-1 - 1) + (2bh(x)-1-1) + 1 = 2(2bh(x)-1) -1 = 2bh(x)-1 Therefore part a) holds for node x.

9 Running time of Red-black tree functions
We can use the same search, minimum, maximum, successor and predecessor functions that we used for binary search trees. These ran in O(h) time. Since h<= 2lg(n+1) for red-black trees, all of the above functions will run in O(lgn) time. We can run Tree-insert and Tree-delete on red-black trees, but they may not maintain the red-black tree properties. Next lecture we will examine new insert and delete functions for red-black trees that do maintain the red-black tree properties.

10 Rotations To insert and delete items in Red-Black trees while maintaining all the Red-Black Tree properties, we must be able to change node colors and sometimes we need to re-arrange the structure of the trees. We change the pointer structure through Rotations. A rotation exchanges the level of 2 adjacent nodes (parent and child) and rearranges pointers to maintain the binary search tree property.

11 Left Rotation y x Left Rotation a, b, g, are subtrees. a g x y
Right Rotation a b g b x is a node whose right child, y, is not T.nil We exchange the levels of x and y x becomes y's left subtree (Check: is x < y?) x's left subtree remains x's left subtree y's left subtree becomes x's right subtree (Check: are all b >x and b<y?) y's right subtree remain's y's subtree. Left Rotation Right rotation is symmetric to Left Rotation.

12 Pseudocode for Left Rotation
Left-Rotate(T, x) y = x.right //Set y to be x's right child x.right = y.left //x's right subtree assigned y's left subtree if y.left != T.nil (y.left).p = x //x becomes parent of y's left subtree y.p = x.p //y's parent is assigned x's parent if x.p == T.nil //if x was root, set y to root T.root = y else if x == (x.p).left //if x was left subchild, now y is left subchild (x.p).left = y else (y.p).right = y //Otherwise, y is now right subchild y.left = x //x is now left child of y x.p =y //y is parent of x //Right-Rotate(T, x) is similar //Running time?


Download ppt "Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees"

Similar presentations


Ads by Google