!!!Chapter 12 Advanced Data Structure and Implementation

来源:互联网 发布:好用的补水乳液 知乎 编辑:程序博客网 时间:2024/05/17 06:58

12.2 Red Black Tree

Operations on red black trees take O(logN) time in the worst case, and a careful nonrecursive implementation can be done relatively effortlessly.

A red black tree is a binary search tree with the following coloring properties:

1. Every node is colored either red or black.

2. The root is black.

3. If a node is red, its children must be black.

4. Every path from a node to a NULL pointer must contain the same number of black nodes.

(5). All leaves (NULL node) are black.

The height of a red black tree is at most 2log(N+1). Height of AVL tree & red black tree:http://en.wikipedia.org/wiki/AVL_tree

12.2.1 Bottom-Up Insertion

When insert a new node, it must be red. Otherwise, it will break rule 4. When we add a red node, there are two scenarios:

1. The parent node is black: In this case, we are done

2. The parent node is red: In this case, we need to change color and rotate tree






原创粉丝点击