码迷,mamicode.com
首页 > 其他好文 > 详细

Red Black Tree

时间:2017-11-03 21:50:52      阅读:490      评论:0      收藏:0      [点我收藏+]

标签:ast   tree   diff   end   including   ber   nbsp   root   and   

https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/video-lectures/lecture-10-red-black-trees-rotations-insertions-deletions/lec10.pdf

 

技术分享

Example of a red-black tree
 

This data structure requires an extra one-bit color field in each node.
Red-black properties:
1. Every node is either red or black.
2. The root and leaves (NIL’s) are black.
3. If a node is red, then its parent is black.
4. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x).

 

 技术分享

 

 

 

http://pages.cs.wisc.edu/~skrentny/cs367-common/readings/Red-Black-Trees/index.html

 

Recall that, for binary search trees, although the average-case times for the lookup, insert, and delete methods are all O(log N), where N is the number of nodes in the tree, the worst-case time is O(N). We can guarantee O(log N) time for all three methods by using a balanced tree -- a tree that always has height O(log N)-- instead of a binary search tree.

A number of different balanced trees have been defined, including AVL trees, 2-4 trees, and B trees. You might learn about the first two in an algorithms class and the third in a database class. Here we will look at yet another kind of balanced tree called a red-black tree.

The important idea behind all of these trees is that the insert and delete operations may restructure the tree to keep it balanced. So lookup, insert, and delete will always be logarithmic in the number of nodes but insert and delete may be more complicated than for binary search trees.

red-black tree is a binary search tree in which

  • each node has a color (red or black) associated with it (in addition to its key and left and right children)
  • the following 3 properties hold:
    1. (root property) The root of the red-black tree is black
    2. (red property) The children of a red node are black.
    3. (black property) For each node with at least one null child, the number of black nodes on the path from the root to the null child is the same.

An example of a red-black tree is shown below:

技术分享

 

 

 

 

 

 

 

 

Red Black Tree

标签:ast   tree   diff   end   including   ber   nbsp   root   and   

原文地址:http://www.cnblogs.com/yuanjiangw/p/7780299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!