1 / 19

Red-Black Trees By Mary Hudachek-Buswell

Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees. Red-Black Trees By Mary Hudachek-Buswell. 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 .

Download Presentation

Red-Black Trees By Mary Hudachek-Buswell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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. Red-Black Tree Properties

  3. Red-Black Tree Properties • 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.

  4. A binary search tree is a red-black tree if it satisfies the following red-black properties: Every node is either red or black. The root is black. Every leaf (NIL) is black. 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.) For each node, all paths from the node to descendant leaves contain the same number of black nodes. Red-Black Tree Properties

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

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

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

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

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

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

  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. Rotate Red-Black Trees-Left LEFT-ROTATE(T, x) // assume right [x] ≠ nil [T] & p [root] = NIL • y ← right [x] //set y as right child of x • right [x] ← left [y] //turn y's left subtree into x's right subtree. • p [left [y]] ← x//set parent of y's left subtree as x • p [y] ← p [x] //link x's parent to y • ifp [x] = nil [T] //if parent of x is nil….. • thenroot [T] ← y//then set the root of the tree as y • else ifx = left [p [x]] //if x is left of parent of x….. • thenleft [p [x]] ← y //then set parent of x’s left subtree as y • elseright [p [x]] ← y //else set parent of x's right subtree as y • left [y] ← x// set left child of y as x • p [x] ← y// set parent of x as y

  13. Rotate Red-Black Trees-Left We are going to rotate key 18 left to key 11’s position. 7 11 4 x 18 9 3 6 y LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] ifp [x] = nil [T] thenroot [T] ← y else ifx = left [p [x]] thenleft [p [x]] ← y elseright [p [x]] ← y left [y] ← x p [x] ← y 14 19 2 12 17 22 20

  14. Rotate Red-Black Trees-Left 7 11 4 x 18 9 3 6 y LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] ifp [x] = nil [T] thenroot [T] ← y else ifx = left [p [x]] thenleft [p [x]] ← y elseright [p [x]] ← y left [y] ← x p [x] ← y 14 19 2 12 17 22 20

  15. Rotate Red-Black Trees-Left 7 11 4 x 18 9 3 6 y LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] ifp [x] = nil [T] thenroot [T] ← y else ifx = left [p [x]] thenleft [p [x]] ← y elseright [p [x]] ← y left [y] ← x p [x] ← y 14 19 2 12 17 22 20

  16. Rotate Red-Black Trees-Left 7 11 4 x 18 9 3 6 y LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] ifp [x] = nil [T] thenroot [T] ← y else ifx = left [p [x]] thenleft [p [x]] ← y elseright [p [x]] ← y left [y] ← x p [x] ← y 14 19 2 12 17 22 20

  17. Rotate Red-Black Trees-Left 7 y 18 4 x 19 3 6 11 22 LEFT-ROTATE(T, x) y ← right [x] right [x] ← left [y] p [left [y]] ← x p [y] ← p [x] ifp [x] = nil [T] thenroot [T] ← y else ifx = left [p [x]] thenleft [p [x]] ← y elseright [p [x]] ← y left [y] ← x p [x] ← y 9 14 2 20 12 17

  18. Recolor Rotated Red-Black Trees 7 4 18 19 3 6 11 22 2 9 14 20 12 17

  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).

More Related