📄 fig12_14.cpp
字号:
template <typename Comparable>
class RedBlackTree
{
public:
explicit RedBlackTree( const Comparable & negInf );
RedBlackTree( const RedBlackTree & rhs );
~RedBlackTree( );
const Comparable & findMin( ) const;
const Comparable & findMax( ) const;
bool contains( const Comparable & x ) const;
bool isEmpty( ) const;
void printTree( ) const;
void makeEmpty( );
void insert( const Comparable & x );
void remove( const Comparable & x );
enum { RED, BLACK };
const RedBlackTree & operator=( const RedBlackTree & rhs );
private:
struct RedBlackNode
{
Comparable element;
RedBlackNode *left;
RedBlackNode *right;
int color;
RedBlackNode( const Comparable & theElement = Comparable( ),
RedBlackNode *lt = NULL, RedBlackNode *rt = NULL,
int c = BLACK )
: element( theElement ), left( lt ), right( rt ), color( c ) { }
};
RedBlackNode *header; // The tree header (contains negInf)
RedBlackNode *nullNode;
// Used in insert routine and its helpers (logically static)
RedBlackNode *current;
RedBlackNode *parent;
RedBlackNode *grand;
RedBlackNode *great;
// Usual recursive stuff
void reclaimMemory( RedBlackNode *t );
void printTree( RedBlackNode *t ) const;
RedBlackNode * clone( RedBlackNode * t ) const;
// Red-black tree manipulations
void handleReorient( const Comparable & item );
RedBlackNode * rotate( const Comparable & item, RedBlackNode *theParent );
void rotateWithLeftChild( RedBlackNode * & k2 );
void rotateWithRightChild( RedBlackNode * & k1 );
};
/**
* Construct the tree.
* negInf is a value less than or equal to all others.
*/
explicit RedBlackTree( const Comparable & negInf )
{
nullNode = new RedBlackNode;
nullNode->left = nullNode->right = nullNode;
header = new RedBlackNode( negInf );
header->left = header->right = nullNode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -