@@ -27,9 +27,9 @@ sealed class Node[A, B](var key: A, var value: B, var count: Int = 1, var red: B
27
27
class RedBlackBST [A , B ](implicit ord : Ordering [A ]) {
28
28
private var root = null .asInstanceOf [Node [A , B ]]
29
29
30
- private def isRed (x : Node [A , B ]): Boolean = if ((x == null ) || ( x.red == false )) false else true
30
+ private def isRed (x : Node [A , B ]): Boolean = x != null && x.red
31
31
32
- /** Make h.right the new root of subtree
32
+ /** Make h.right the new root of subtree and return it
33
33
*
34
34
* if h.right is red rotate left so h becomes the left child and h.right becomes the parent
35
35
*/
@@ -45,7 +45,7 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
45
45
x
46
46
}
47
47
48
- /** Make h.left the new root of subtree
48
+ /** Make h.left the new root of subtree and return it
49
49
*
50
50
* if h.left is red rotate right so h becomes the right child and h.left becomes the parent
51
51
*/
@@ -54,7 +54,7 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
54
54
val x = h.left
55
55
h.left = x.right
56
56
x.right = h
57
- x.red = x.right.red
57
+ x.red = x.right.red // equals h.red
58
58
x.right.red = true
59
59
x.count = h.count
60
60
h.count = 1 + size(h.left) + size(h.right)
@@ -82,9 +82,9 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
82
82
if (x == null ) new Node (key, value)
83
83
else {
84
84
ord.compare(key, x.key) match {
85
- case 0 => x.value = value
86
- case n if ( n < 0 ) => x.left = loop(x.left)
87
- case _ => x.right = loop(x.right)
85
+ case 0 => x.value = value
86
+ case n if n < 0 => x.left = loop(x.left)
87
+ case _ => x.right = loop(x.right)
88
88
}
89
89
x.count += 1
90
90
if (isRed(x.right) && ! isRed(x.left)) rotateLeft(x)
@@ -109,9 +109,9 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
109
109
if (x == null ) None
110
110
else {
111
111
ord.compare(key, x.key) match {
112
- case 0 => Some (x.value)
113
- case n if ( n < 0 ) => loop(x.left)
114
- case _ => loop(x.right)
112
+ case 0 => Some (x.value)
113
+ case n if n < 0 => loop(x.left)
114
+ case _ => loop(x.right)
115
115
}
116
116
}
117
117
loop(root)
@@ -125,13 +125,13 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
125
125
126
126
def loop (x : Node [A , B ], key : A ): Node [A , B ] = {
127
127
val h = ord.compare(key, x.key) match {
128
- case n if ( n < 0 ) => {
128
+ case n if n < 0 => // go left
129
129
val j = if (! isRed(x.left) && ! isRed(x.left.left)) moveRedLeft(x) else x
130
130
131
131
j.left = loop(j.left, key)
132
132
j
133
- }
134
- case _ => {
133
+
134
+ case _ =>
135
135
val j = if (isRed(x.left)) rotateRight(x) else x
136
136
137
137
ord.compare(key, j.key) match {
@@ -150,7 +150,6 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
150
150
k
151
151
}
152
152
}
153
- }
154
153
}
155
154
balance(h)
156
155
}
@@ -189,7 +188,7 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
189
188
}
190
189
191
190
private def deleteMin (h : Node [A , B ]): Node [A , B ] = {
192
- if (h.left == null ) null
191
+ if (h.left == null ) null // delete current node
193
192
else {
194
193
val j = if (! isRed(h.left) && ! isRed(h.left.left)) moveRedLeft(h) else h
195
194
j.left = deleteMin(j.left)
@@ -199,6 +198,7 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
199
198
200
199
/** delete minimum key */
201
200
def deleteMin (): Unit = {
201
+ // 确保节点为 RED
202
202
if (! isRed(root.left) && ! isRed(root.right)) root.red = true
203
203
204
204
root = deleteMin(root)
@@ -236,11 +236,11 @@ class RedBlackBST[A, B](implicit ord: Ordering[A]) {
236
236
/** is key present */
237
237
def contains (key : A ): Boolean = get(key) match {
238
238
case None => false
239
- case Some (x) => if (x == null ) false else true
239
+ case Some (x) => x != null
240
240
}
241
241
242
242
/** are any keys in tree */
243
- def isEmpty (): Boolean = if ( root == null ) true else false
243
+ def isEmpty (): Boolean = root == null
244
244
245
245
@ tailrec
246
246
private def min (x : Node [A , B ]): Node [A , B ] = {
0 commit comments