Presentation is loading. Please wait.

Presentation is loading. Please wait.

Red Black Trees https://www.cs.usfca.edu/~galles/visualization/RedBlack.html.

Similar presentations


Presentation on theme: "Red Black Trees https://www.cs.usfca.edu/~galles/visualization/RedBlack.html."— Presentation transcript:

1 Red Black Trees

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. Constraining the node colors on any simple path from the root to a leaf, no such path is more than twice as long as any other, so that the tree is approximately balanced. Each node of the tree now contains the attributes color, key, left, right, and p (parent).

3 Properties of red black trees
A red-black tree is a binary tree that satisfies the following red-black properties: 1. Every node is either red or black. 2. The root is always black. 3. Every leaf (NIL) is black. 4. If a node is red, then both its children are black. 5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

4 black-height: We call the number of black nodes on any simple path from, but not including, a node x down to a leaf the black-height of the node, denoted bh(x). By property 5, the notion of black-height is well defined, since all descending simple paths from the node have the same number of black nodes.

5 A red-black tree with n internal nodes has height at most 2 log (n + 1).
we can implement SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR in O(log n) time on red-black trees.

6 The search-tree operations TREE-INSERT and TREE-DELETE, when run on a red-black tree with n keys, take O(log n) time. they modify the tree, the result may violate the red-black properties. change the colors of some of the nodes in the tree and also rotate.

7 HERE..

8

9

10 Insertion We can insert a node into an n-node red-black tree in O(log n) time. 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. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

11 Grandparent of z uncle of z

12 not the same color uncle of z with its parent

13

14 Deletion …


Download ppt "Red Black Trees https://www.cs.usfca.edu/~galles/visualization/RedBlack.html."

Similar presentations


Ads by Google