Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.

Similar presentations


Presentation on theme: "1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees."— Presentation transcript:

1 1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees

2 2 Red-Black Tree Properties A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK. The tree is approximately balanced. Each node of the tree now contains the fields color, key, left, right, and p.

3 3 If a child or the parent of a node does not exist, the corresponding pointer field of the node contains the value NIL. We shall regard these NIL's as being pointers to external nodes (leaves) of the binary search tree and the normal, key-bearing nodes as being internal nodes of the tree. Red-Black Tree Properties

4 4 A binary search tree is a red-black tree if it satisfies the following red-black properties: 1. Every node is either red or black. 2. The root is black. 3. Every leaf (NIL) is black. 4. If a node is red, then both its children are black. (Hence no two reds in a row on a simple path from the root to a leaf.) 5. For each node, all paths from the node to descendant leaves contain the same number of black nodes.

5 5 16 1410 8793 241 Color Tree Red-Black Using Properties 1.Every node is either red or black. 2.The root is black. 3.Every leaf (NIL) is black. 4.If a node is red, then both its children are black. 5.All paths from node to descendants contain the same number of black nodes.

6 6 16 1410 8793 241 Color Tree Red-Black Using Properties 1.Every node is either red or black. 2.The root is black. 3.Every leaf (NIL) is black. 4.If a node is red, then both its children are black. 5.All paths from node to descendants contain the same number of black nodes.

7 7 16 1410 8793 241 Color Tree Red-Black Using Properties 1.Every node is either red or black. 2.The root is black. 3.Every leaf (NIL) is black. 4.If a node is red, then both its children are black. 5.All paths from node to descendants contain the same number of black nodes.

8 8 16 1410 8793 241 Color Tree Red-Black Using Properties 1.Every node is either red or black. 2.The root is black. 3.Every leaf (NIL) is black. 4.If a node is red, then both its children are black. 5.All paths from node to descendants contain the same number of black nodes.

9 9 16 1410 8793 241 Color Tree Red-Black Using Properties 1.Every node is either red or black. 2.The root is black. 3.Every leaf (NIL) is black. 4.If a node is red, then both its children are black. 5.All paths from node to descendants contain the same number of black nodes.

10 10 16 1410 8793 241 Color Tree Red-Black Using Properties 1.Every node is either red or black. 2.The root is black. 3.Every leaf (NIL) is black. 4.If a node is red, then both its children are black. 5.All paths from node to descendants contain the same number of black nodes. H = 4 B = 2 H = 2 B = 2 H = 1 B = 1 H = 1 B = 1 H = 2 B = 1 H = 3 B = 2 H = 2 B = 1 H = 1 B = 1

11 11 Rotate Red-Black Trees The basic tree-restructuring operation. Needed to maintain red-black trees as balanced binary search trees. Changes the local pointer structure. (Only pointers are changed.) Won’t upset the binary-search-tree property. Have both left rotation and right rotation. They are inverses of each other. A rotation takes a red-black-tree and a node within the tree.

12 12 Rotate Red-Black Trees-Left LEFT-ROTATE(T, x) // assume right [x] ≠ nil [T] & p [root] = NIL 1. y ← right [x] //set y as right child of x 2. right [x] ← left [y] //turn y's left subtree into x's right subtree. 3. p [left [y]] ← x //set parent of y's left subtree as x 4. p [y] ← p [x] //link x's parent to y 5. if p [x] = nil [T] //if parent of x is nil….. 6. then root [T] ← y //then set the root of the tree as y 7. else if x = left [p [x]] //if x is left of parent of x….. 8. then left [p [x]] ← y //then set parent of x’s left subtree as y 9. else right [p [x]] ← y //else set parent of x's right subtree as y 10. left [y] ← x // set left child of y as x 11. p [x] ← y // set parent of x as y

13 13 Rotate Red-Black Trees-Left 7 4 11 63 18 14 9 2 19 1712 22 20 LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] if p [x] = nil [T] then root [T] ← y else if x = left [p [x]] then left [p [x]] ← y else right [p [x]] ← y left [y] ← x p [x] ← y x y We are going to rotate key 18 left to key 11’s position.

14 14 Rotate Red-Black Trees-Left 7 4 11 63 18 14 9 2 19 1712 22 20 LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] if p [x] = nil [T] then root [T] ← y else if x = left [p [x]] then left [p [x]] ← y else right [p [x]] ← y left [y] ← x p [x] ← y x y

15 15 Rotate Red-Black Trees-Left 7 4 11 63 18 14 9 2 19 1712 22 20 LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] if p [x] = nil [T] then root [T] ← y else if x = left [p [x]] then left [p [x]] ← y else right [p [x]] ← y left [y] ← x p [x] ← y x y

16 16 Rotate Red-Black Trees-Left 7 4 11 63 18 14 9 2 19 1712 22 20 LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] if p [x] = nil [T] then root [T] ← y else if x = left [p [x]] then left [p [x]] ← y else right [p [x]] ← y left [y] ← x p [x] ← y x y

17 17 Rotate Red-Black Trees-Left 7 4 1163 18 149 2 19 1712 22 20 LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] if p [x] = nil [T] then root [T] ← y else if x = left [p [x]] then left [p [x]] ← y else right [p [x]] ← y left [y] ← x p [x] ← y x y

18 18 Recolor Rotated Red-Black Trees 7 4 11 63 18 1492 19 1712 22 20

19 19 O(n) of Rotated Red-Black Trees When rotation occurs whether, left or right, only a constant numbers of pointers are altered. Therefore, the runtime of rotation is O(1).


Download ppt "1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees."

Similar presentations


Ads by Google