Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Red-black Trees zConsider a b-tree of order 4. yA node must have at least 2 children and as many as 4. yA node must have at least 1 key value and as.

Similar presentations


Presentation on theme: "1 Red-black Trees zConsider a b-tree of order 4. yA node must have at least 2 children and as many as 4. yA node must have at least 1 key value and as."— Presentation transcript:

1 1 Red-black Trees zConsider a b-tree of order 4. yA node must have at least 2 children and as many as 4. yA node must have at least 1 key value and as many as 3. zWe have always represented the key values as an array, but what if we did it as a tree?

2 2 Red-black Trees Example zThis is a valid b-tree of order 4; Now store key values in a binary search tree: 180, 260, 550 440, 500120, 150200600

3 3 Red-black Trees Example zOK, now link up b-tree node pointers to create a binary search tree: 260 550180 440 500 200150 120 600

4 4 Red-black Trees Example zNow color inter-node links black and intra-nodes red: 260 550180 440 500 200150 120 600

5 5 Red-black Trees Example zColor each node the color of the edge incident on it: 260 550180 440 500 200150 120 600

6 6 Red-black Trees Example zThis is a Red-black Tree. It is a height-balanced binary search tree. 260 550180 440 500 200150 120 600

7 7 Red-black Properties zA red node must have only black nodes as children. zA black node may have either red or black nodes as children. zThe path from the root to any terminal level node must pass through the same number of black nodes.

8 8 Red-black Insert zThe insert algorithm follows the rules for b- trees, but defines it in terms of node color: ySearch for place to insert; All new insertions go in as red nodes. (E.G. All insertions go into an existing b- tree node). yIf parent of new node is black, stop. (E.G. If the b- tree node is not full, no problem). yIf parent is red, see if a simple AVL-type rotation will work: look at grandparent as root of rotation.

9 9 Red-black Insert II yIf rotation doesn’t work, move the nearest black ancestor to its parent by making it red and both of its children black. (e.g. split the B-tree node & move middle key to parent). yRepeat for newly colored red node. (e.g. repeat for parent B-tree node).

10 10 Red-black Delete zSame basic idea: yFind key to delete; yIf it is not at the terminal level, replace with its in order successor & delete this value. yThus, all deletions which reduce the number of nodes occur at the terminal level of the B- tree. yThe rules follow those for deleting from a B- tree:

11 11 Red-black Delete II zIf the node is red, do your standard BST delete (e.g. the B-tree node is not empty). zIf the node is black, but has a red child, do your standard BST delete & make the red child black (e.g. again, B-tree node is not empty). zIf the node is black and has no children…

12 12 Red-black Delete III z…Attempt to “borrow” from parent’s other child. z…Failing that, “combine” nodes and repeat at (B-tree) parent.

13 13 The End Slide z


Download ppt "1 Red-black Trees zConsider a b-tree of order 4. yA node must have at least 2 children and as many as 4. yA node must have at least 1 key value and as."

Similar presentations


Ads by Google