Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one...

9
Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4- nodes as mini- binary trees, bound by “red” links. Red-black trees have the following properties: 1 3-nodes can be represented in two different ways. 2 Two red links never follow one another in a row. 3 All paths in the red-black tree have the same number of black links. 4 One path containing red and black links can be twice as long as another path containing only black links. 5 All path lengths are proportional to log N. 6 Nodes with data items equal to a given node can fall on both sides of that node.

description

Operations on red-black trees Search: same as in binary search trees, except for the need of a boolean attribute black that each node must maintain. Insert: same as in trees, except that the colors of the links must be taken into consideration. Delete: similar to deletion in binary search trees. We can always delete a node that has at least one external child. However, if the key to be deleted is stored in a node with no external children, we move there the key of its inorder predecessor (or successor), and delete that node instead. Example:

Transcript of Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one...

Page 1: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

Red-Black trees

Red-black trees are 2-3-4 trees represented as binary trees at the expense ofone extra bit per node. The idea is to represent 3- and 4-nodes as mini-binary trees, bound by “red” links.

Red-black trees have the following properties:

1 3-nodes can be represented in two different ways.2 Two red links never follow one another in a row.3 All paths in the red-black tree have the same number of black links.4 One path containing red and black links can be twice as long as

another path containing only black links.5 All path lengths are proportional to log N.6 Nodes with data items equal to a given node can fall on both sides of

that node.

Page 2: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

Examples

1 2 32

1 3

9 1515 9

10 16 20

3 7 8 12 13 18

9 15

25

16

10 20

7 12 18 25

3 8 13

OR

Page 3: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

Operations on red-black trees

Search: same as in binary search trees, except for the need of a boolean attribute black that each node must maintain.

Insert: same as in 2-3-4 trees, except that the colors of the links must be taken into consideration.

Delete: similar to deletion in binary search trees. We can always delete a node that has at least one external child. However, if the key to be deleted is stored in a node with no external children, we move there the key of its inorder predecessor (or successor), and delete that node instead.

Example: 7 5

4 8 4 8

2 5 9 2 9

Page 4: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

Insert operation: example

Insert 15

16

10 20

12 18 25

13

7

3 8 16

10 20

7 13 18 25

3 8 15

Split

12

Page 5: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

Splitting 4-nodes without rotation

Case1: Splitting a 4-node connected to a 2-node

Page 6: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

Splitting 4-nodes without rotation (contd.)

Case2A: Splitting a 4-node connected to a 3-node

Page 7: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

Splitting 4-nodes with single rotation

Case2B: Splitting a 4-node connected to a 3-node

Page 8: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

Splitting 4-nodes with double rotationCase2C: Splitting a 4-node connected to a 3-node

Reducedto case 2B

Page 9: Red-Black trees Red-black trees are 2-3-4 trees represented as binary trees at the expense of one extra bit per node. The idea is to represent 3- and 4-nodes.

The insertRB methodAlgorithm insertRB (T, newData, precedes)

boolean success := falseif (T = null) { NodeRBtree node = new NodeRBtree(newData) success := true }else { if (nodeType(T)= 4) splitroot(T) NodeRBtree p := T; NodeRBtree parent := null; boolean done := false while (! done) { if (nodeType(p) = 4) { if (nodeType (parent) = 2) then splitChildOf2 (p, parent) else splitChildOf3 (p, parent) } int compareResult := compare (p, newData, precedes) if (compareResult < 0) then success := false; done := true else if (compareResult = 0) then insertData (p, newData, precedes); success:=true; done:=true else advance (p, parent, compareResult) } // end while } // end else