2 * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef RedBlackTree_h
30 #define RedBlackTree_h
32 #include <wtf/Assertions.h>
33 #include <wtf/Noncopyable.h>
37 // This implements a red-black tree with the following properties:
38 // - The allocation of nodes in the tree is entirely up to the user.
39 // - If you are in possession of a pointer to a node, you can delete
40 // it from the tree. The tree will subsequently no longer have a
41 // reference to this node.
42 // - The key type must implement operator< and ==.
44 template<typename KeyType, typename ValueType>
46 WTF_MAKE_NONCOPYABLE(RedBlackTree);
55 friend class RedBlackTree;
58 Node(KeyType key, ValueType value)
64 const Node* successor() const
68 return treeMinimum(x->right());
69 const Node* y = x->parent();
70 while (y && x == y->right()) {
77 const Node* predecessor() const
81 return treeMaximum(x->left());
82 const Node* y = x->parent();
83 while (y && x == y->left()) {
92 return const_cast<Node*>(const_cast<const Node*>(this)->successor());
97 return const_cast<Node*>(const_cast<const Node*>(this)->predecessor());
108 m_parentAndRed = 1; // initialize to red
111 // NOTE: these methods should pack the parent and red into a single
112 // word. But doing so appears to reveal a bug in the compiler.
115 return reinterpret_cast<Node*>(m_parentAndRed & ~static_cast<uintptr_t>(1));
118 void setParent(Node* newParent)
120 m_parentAndRed = reinterpret_cast<uintptr_t>(newParent) | (m_parentAndRed & 1);
128 void setLeft(Node* node)
138 void setRight(Node* node)
145 if (m_parentAndRed & 1)
150 void setColor(Color value)
155 m_parentAndRed &= ~static_cast<uintptr_t>(1);
160 uintptr_t m_parentAndRed;
174 while (x != m_root && x->parent()->color() == Red) {
175 if (x->parent() == x->parent()->parent()->left()) {
176 Node* y = x->parent()->parent()->right();
177 if (y && y->color() == Red) {
179 x->parent()->setColor(Black);
181 x->parent()->parent()->setColor(Red);
182 x = x->parent()->parent();
184 if (x == x->parent()->right()) {
190 x->parent()->setColor(Black);
191 x->parent()->parent()->setColor(Red);
192 rightRotate(x->parent()->parent());
195 // Same as "then" clause with "right" and "left" exchanged.
196 Node* y = x->parent()->parent()->left();
197 if (y && y->color() == Red) {
199 x->parent()->setColor(Black);
201 x->parent()->parent()->setColor(Red);
202 x = x->parent()->parent();
204 if (x == x->parent()->left()) {
210 x->parent()->setColor(Black);
211 x->parent()->parent()->setColor(Red);
212 leftRotate(x->parent()->parent());
217 m_root->setColor(Black);
220 Node* remove(Node* z)
223 ASSERT(z->parent() || z == m_root);
225 // Y is the node to be unlinked from the tree.
227 if (!z->left() || !z->right())
232 // Y is guaranteed to be non-null at this point.
239 // X is the child of y which might potentially replace y in
240 // the tree. X might be null at this point.
243 x->setParent(y->parent());
244 xParent = x->parent();
246 xParent = y->parent();
250 if (y == y->parent()->left())
251 y->parent()->setLeft(x);
253 y->parent()->setRight(x);
257 if (y->color() == Black)
258 removeFixup(x, xParent);
260 y->setParent(z->parent());
261 y->setColor(z->color());
262 y->setLeft(z->left());
263 y->setRight(z->right());
266 z->left()->setParent(y);
268 z->right()->setParent(y);
270 if (z->parent()->left() == z)
271 z->parent()->setLeft(y);
273 z->parent()->setRight(y);
278 } else if (y->color() == Black)
279 removeFixup(x, xParent);
284 Node* remove(const KeyType& key)
286 Node* result = findExact(key);
289 return remove(result);
292 Node* findExact(const KeyType& key) const
294 for (Node* current = m_root; current;) {
295 if (current->m_key == key)
297 if (key < current->m_key)
298 current = current->left();
300 current = current->right();
305 Node* findLeastGreaterThanOrEqual(const KeyType& key) const
308 for (Node* current = m_root; current;) {
309 if (current->m_key == key)
311 if (current->m_key < key)
312 current = current->right();
315 current = current->left();
321 Node* findGreatestLessThanOrEqual(const KeyType& key) const
324 for (Node* current = m_root; current;) {
325 if (current->m_key == key)
327 if (current->m_key > key)
328 current = current->left();
331 current = current->right();
341 return treeMinimum(m_root);
348 return treeMaximum(m_root);
351 // This is an O(n) operation.
355 for (Node* current = first(); current; current = current->successor())
360 // This is an O(1) operation.
367 // Finds the minimum element in the sub-tree rooted at the given
369 static Node* treeMinimum(Node* x)
376 static Node* treeMaximum(Node* x)
383 static const Node* treeMinimum(const Node* x)
390 static const Node* treeMaximum(const Node* x)
397 void treeInsert(Node* z)
401 ASSERT(!z->parent());
402 ASSERT(z->color() == Red);
408 if (z->m_key < x->m_key)
417 if (z->m_key < y->m_key)
424 //----------------------------------------------------------------------
425 // Red-Black tree operations
428 // Left-rotates the subtree rooted at x.
429 // Returns the new root of the subtree (x's right child).
430 Node* leftRotate(Node* x)
433 Node* y = x->right();
435 // Turn y's left subtree into x's right subtree.
436 x->setRight(y->left());
438 y->left()->setParent(x);
440 // Link x's parent to y.
441 y->setParent(x->parent());
445 if (x == x->parent()->left())
446 x->parent()->setLeft(y);
448 x->parent()->setRight(y);
451 // Put x on y's left.
458 // Right-rotates the subtree rooted at y.
459 // Returns the new root of the subtree (y's left child).
460 Node* rightRotate(Node* y)
465 // Turn x's right subtree into y's left subtree.
466 y->setLeft(x->right());
468 x->right()->setParent(y);
470 // Link y's parent to x.
471 x->setParent(y->parent());
475 if (y == y->parent()->left())
476 y->parent()->setLeft(x);
478 y->parent()->setRight(x);
481 // Put y on x's right.
488 // Restores the red-black property to the tree after splicing out
489 // a node. Note that x may be null, which is why xParent must be
491 void removeFixup(Node* x, Node* xParent)
493 while (x != m_root && (!x || x->color() == Black)) {
494 if (x == xParent->left()) {
495 // Note: the text points out that w can not be null.
496 // The reason is not obvious from simply looking at
497 // the code; it comes about from the properties of the
499 Node* w = xParent->right();
500 ASSERT(w); // x's sibling should not be null.
501 if (w->color() == Red) {
504 xParent->setColor(Red);
506 w = xParent->right();
508 if ((!w->left() || w->left()->color() == Black)
509 && (!w->right() || w->right()->color() == Black)) {
513 xParent = x->parent();
515 if (!w->right() || w->right()->color() == Black) {
517 w->left()->setColor(Black);
520 w = xParent->right();
523 w->setColor(xParent->color());
524 xParent->setColor(Black);
526 w->right()->setColor(Black);
529 xParent = x->parent();
532 // Same as "then" clause with "right" and "left"
535 // Note: the text points out that w can not be null.
536 // The reason is not obvious from simply looking at
537 // the code; it comes about from the properties of the
539 Node* w = xParent->left();
540 ASSERT(w); // x's sibling should not be null.
541 if (w->color() == Red) {
544 xParent->setColor(Red);
545 rightRotate(xParent);
548 if ((!w->right() || w->right()->color() == Black)
549 && (!w->left() || w->left()->color() == Black)) {
553 xParent = x->parent();
555 if (!w->left() || w->left()->color() == Black) {
557 w->right()->setColor(Black);
563 w->setColor(xParent->color());
564 xParent->setColor(Black);
566 w->left()->setColor(Black);
567 rightRotate(xParent);
569 xParent = x->parent();