L8 logn search
Binary Search Generalized
Outline:
- Red-Black Tree
Ref:
- 算法设计与分析(Algorithm design and analysis) by 黄宇
Balanced Binary Search Tree
binary search tree
- Def
- 2-Tree
- 左子树的所有值比根节点小,右子树的所有值比根节点大(如果properly drawn的话,会很清楚 )
Red-Black Tree
- Def (基于二叉搜索树,附加一些性质)
- If T is a binary search tree in which each node has a color, red or black, and all external nodes are black, then T is a red-black tree if and only if:
- [Color constraint] No red node has a red child
- [Black height constraint] The black length of all external paths from a given node u is the same(the black height of u)
- The root is black
- Almost-red-black tree(ARB tree)
- Root is red, satisfying the other constraints
- If T is a binary search tree in which each node has a color, red or black, and all external nodes are black, then T is a red-black tree if and only if:
Recursive Definition of RBT
(a red black tree of black height h is denoted as
- Def:
- An external node is an
tree, and the node is black. - A binary tree is an
( )tree if:- Its root is red,and
- Its left and right subtrees are each an
tree.
- A binary tree is an
tree if:- Its root is black, and
- Its left and right subtrees are each either an
tree or an tree.
- An external node is an
Properties of Red-Black Tree
- The black height of any
tree or is well-defined and is h. - Let T be an
tree, then:- T has at most
internal black nodes. - T has at most
in internal nodes. - The depth of any black node is at most twice its black depth.
- T has at most
- Let A be an
tree, then:- A has at least
internal black nodes. - A has at most
internal nodes. - The depth of any black node is at most twice its black depth.
- A has at least
Bound on Depth of Node in RBT
- Let T be a red-black tree with n internal nodes. Then no node has black depth greater than
, which means that the height of T in the usual sense is at most .- Proof:
- Let h be the black height of T. The number of internal nodes, n, is at least the number of internal black nodes, which is at least
, so . The node with greatest depth is some external node. All external nodes are with black depth h. So, the depth is at most .
Deletion
- Logical: 删除节点的内容
- Structural:删除节点,整棵树做修复
Complexity of Operations on RBT
- With reasonable implementation
- A new node can be inserted correctly in a red-black tree with n nodes in
time in the worst-case - Repairs for deletion do
structural changes, but may do color changes
- A new node can be inserted correctly in a red-black tree with n nodes in