Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Balanced Binary Search Tree
Algorithm : Design & Analysis[8]
In the last class…
Finding max and minFinding the second largest keyAdversary argument and lower boundSelection Problem – MedianA Linear Time Selection AlgorithmAnalysis of Selection AlgorithmA Lower Bound for Finding the Median
Balanced Binary Search Tree
Definition of red-black treeBlack heightInsertion into a red-black treeDeletion from a red-black tree
Binary Search Tree Revisited
30
20
40
50
80
60
40
20 60
50 8030
Poor balancingΘ(n)
•Each node has a key, belonging to a linear ordered set•An inorder traversal produces a sorted list of the keys
•Each node has a key, belonging to a linear ordered set•An inorder traversal produces a sorted list of the keys
In a properly drawn tree, pushing forward to get the ordered list.
Good balancingΘ(logn)
Node Group in a binTree
65
60
70
90
80
75
60
70
75
80
50
15
10 25
20
30
40
5 principal subtrees5 principal subtrees
Node groupNode group
As in 2-tree, the number of external node is one more than that of internal node
Improving the Balancing by Rotation
50
15
10 25
20
30
40
25
4015
2010 30
50The node group to be rotated
Root of the group is changed.
The middle principal subtree changes parent
Red-Black Tree: the Definition
If T is a binary 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 constrain] 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.
Balancing is under controlledBalancing is under controlled
Recursive Definition of Red-Black Tree
(A red-black tree of black height h is denoted as RBh)
Definition:An external node is an RB0 tree, and the node is black.A binary tree is an ARBh (h≥1) tree if:
Its root is red, andIts left and right subtrees are each an RBh-1 tree.
A binary tree is an RBh (h≥1) tree if:Its root is black, andIts left and right subtrees are each either an RBh-1 tree or an ARBhtree.
No ARB0
RBi and ARBi
RB0
ARB1
RB1
(1) (2)
(4)(3)
Red-Black Tree with 6 Nodes40
60
805030
20
40
30 80
60
50
20
30
20
80
60
40
50
poorest balancing: height(normal) is 4
Black edge
Black-Depth Convention
40
30
8050
20
20
5040
30
60
40
6020 8030 5060
80
All with the same largest black depth: 2All with the same
largest black depth: 2
ARB TreesARB Trees
Properties of Red-Black Tree
The black height of any RBh tree or ARBh tree is well defind and is h.
Let T be an RBh tree, then:T has at least 2h-1 internal black nodes.T has at most 4h-1 internal nodes.The depth of any black node is at most twice its black depth.
Let A be an ARBh tree, then:A has at least 2h-2 internal black nodes.A has at most (4h)/2-1 internal nodes.The depth of any black node is at most twice its black depth.
Well-Defined Black HeightThat “the black height of any RBh tree or ARBh tree is well defind”means the black length of all external paths from the root is the same. Proof: induction on hBase case: h=0, that is RB0 (there is no ARB0)In ARBh+1, its two subtrees are both RBh.Since the root is red, the black length of all external paths from the root is h, that’s the same as its two subtrees.In RBh+1:
Case 1: two subtrees are RBh’sCase 2: two subtrees are ARBh+1’sCase 3: one subtree is an RBh(black height=h), and the another is an ARBh+1(black height=h+1)
Bound on Depth of Node in RBTree
Let T be a red-black tree with n internal nodes. Then no node has depth greater than 2lg(n+1), which means that the height of T in the usual sense is at most 2lg(n+1).
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 2h-1, so h≤lg(n+1). The node with greatest depth is some external node. All external nodes are with black depth h. So, the depth is at most 2h.
Influences of Insertion into an RB TreeBlack height constrain:
No violation if inserting a red node.Color constraint:
40
6020 8030 50
40
20 30 60 7050 80Inserting 70
Critical clusters(external nodes excluded), which originated by color violation, with 3 or 4 red nodes
Critical clusters(external nodes excluded), which originated by color violation, with 3 or 4 red nodes
Repairing 4-node Critical Cluster
40
20 30 60 7050 80
Color flip:Root of the critical cluster exchanges color with its subtrees
Color flip:Root of the critical cluster exchanges color with its subtrees
40
20 30
60
7050 80
No new critical cluster occurs, inserting finished.
No new critical cluster occurs, inserting finished.
Repairing 4-node Critical Cluster2 more insertions
40
20 30
60
7050 80 85 90
Critical cluster
90
40
20 30
60
7050
80
85
New critical cluster with 3 nodes.Color flip doesn’t work,Why?
New critical cluster with 3 nodes.Color flip doesn’t work,Why?
Patterns of 3-Node Critical Cluster
85
L
RLLL
M
RRLR
R L
RLLL
M
RRLR
R
L
RLLL
M
RRLR
R L
RLLL
M
RRLR
R
Shown as properly drawn
C
A B
D
Repairing 3-Node Critical Cluster
L
RLLL
M
RRLR
RRoot of the critical cluster is changed to M, and the parentship is adjusted accordingly
90
40
20 30
60
7050
80
85
The incurred critical cluster is of pattern A
All into one pattern
Implementing Insertion: Class
class RBtreeElement root;RBtree leftSubtree;RBtree rightSubtree;int color; /* red, black */
static class InsReturnpublic RBtree newTree;public int status /* ok, rbr, brb, rrb, brr */
class RBtreeElement root;RBtree leftSubtree;RBtree rightSubtree;int color; /* red, black */
static class InsReturnpublic RBtree newTree;public int status /* ok, rbr, brb, rrb, brr */
Color pattern
Implementing Insertion: ProcedureRBtree rbtInsert (RBtree oldRBtree, Element newNode)
InsReturn ans = rbtIns(oldREtree, newNode);If (ans.newTree.color ≠ black)
ans.newTree.color = black;return ans.newTree;
RBtree rbtInsert (RBtree oldRBtree, Element newNode)InsReturn ans = rbtIns(oldREtree, newNode);If (ans.newTree.color ≠ black)
ans.newTree.color = black;return ans.newTree;
InsReturn rbtIns(RBtree oldRBtree, Element newNode)InsReturn ans, ansLeft, ansRight;if (oldRBtree = nil) then <Inserting simply>;else
if (newNode.key <oldRBtree.root.key)ansLeft = rbtIns (oldRBtree.leftSubtree, newNode);ans = repairLeft(oldRBtree, ansLeft);
else ansRight = rbtIns(oldRBtree.rightSubtree, newNode);ans = repairRight(oldRBtree, ansRight);
return ans the recursive function
the wrapper
Correctness of InsertionIf the parameter oldRBtree of rbtIns is an RBh tree or an ARBh+1 tree(which is true for the recursive calls on rbtIns), then the newTree and status fields returned are one of the following combinations:
Status=ok, and newTree is an RBh or an ARBh+1 tree,Status=rbr, and newTree is an RBh, Status=brb, and newTree is an ARBh+1 tree,Status=rrb, and newTree.color=red, newTree.leftSubtree is an ARBh+1tree and newTree.rightSubtree is an RBh tree,Status=brr, and newTree.color=red, newTree.rightSubtree is an ARBh+1tree and newTree.leftSubtree is an RBh tree
For those cases with red root, the color will be changed to black, with other constraints satisfied by repairing subroutines.
Deletion: Logical and Structralu: to be deleted logicallyu: to be deleted logically
σ: tree successor of u, to be deleted structurally, with information moved into u
σ: tree successor of u, to be deleted structurally, with information moved into u
90
40
20 30
60
7050
80
85
π
S right subtree of S, to replacing S
right subtree of S, to replacing S
90
40
20 30
70
50
80
85
π
After deletion
π is parent of σπ is parent of σ
Deletion from RBTree: Examples
90
40
20
60
7050 8530
80
90
40
20
60
705030
85
90
50
20
60
70 8530
80
u,π
80
40
u,π 90
40
20
60
705030
80u,π
85
Original tree
Deletion in a Red-Black Tree
90
40
20 30
60
7050
80
85
To be deletedTo be deleted
90
40
20 30
70
50
80
85
π The black height of πis not well-defined !
black depth=1
black depth=2
one deletion
Procedure of Red-Black Deletion
1. Do a standard BST search to locate the node to be logically deleted, call it u
2. If the right child of u is an external node, identify u as the node to be structurally deleted.
3. If the right child of u is an internal node, find the tree successor of u , call it σ, copy the key and information from σ to u. (color of u not changed) Identify σ as the node to be deleted structurally.
4. Carry out the structural deletion and repair any imbalance ofblack height.
Imbalance of Black Height60
70 90
80
85
40
20 30 50
9070
85deleting 80
80
90
deleting 85
50
20 30
deleting 40
70
90
80
85
deleting 60
Black height has to be restored
Analysis of Black Imbalance
The imbalance occurs when:A black node is delete structurally, and Its right subtree is black (external)
The result is:An RBh-1 occupies the position of an RBh as required by its parent, coloring it as a “gray” node.
Solution:Find a red node and turn it black as locally as possible.The gray color might propagate up the tree.
Propagation of Gray Node
s
l r
g
p
Map of the vicinity of g, the gray node
s
r
g
p
l
g l r
p s
The pattern for which propagation is needed
g-subtree gets well-defined black height, but that is less than that required by its parent
g-subtree gets well-defined black height, but that is less than that required by its parent
Gray UpIn the worst case, up to the root of the tree, and successful
Repairing without Propagation
g
p
s
a b r
g a b
l
l
Deletion Rebalance group
4 principal subtrees, RBh-1
r
p s
Restructured
Restructuring the deletion rebalance group:•Red p: form an RB1 or ARB2 tree•Black p: form an RB2 tree
Restructuring the deletion rebalance group:•Red p: form an RB1 or ARB2 tree•Black p: form an RB2 tree
RB2
Repairing without Propagation
g
p
s
a b r
g a b
l
l
Deletion Rebalance group
4 principal subtrees, RBh-1
r
p s
Restructured
Restructuring the deletion rebalance group:•Red p: form an RB1 or ARB2 tree•Black p: form an RB2 tree
Restructuring the deletion rebalance group:•Red p: form an RB1 or ARB2 tree•Black p: form an RB2 tree
ARB2
Complexity of Operations on RBTree
With reasonable implementationA new node can be inserted correctly in a red-black tree with n nodes in Θ(logn) time in the worst case.Repairs for deletion do O(1) structural changes, but may do O(logn) color changes.
Home Assignments
pp.302-6.4-66.11-136.17