Double Rotation
Single rotation sometimes occurs with a problem (as shown):
(If the inner descendant node [K1] is too deep, moving it one-way will not solve the problem)
So there's a double spin.
Double rotation to the right:
1. First take K1 as the axis, K1 and K2 rotate to the left;
2. Then take K3 as the axis, K3 and rotate the K1 to the right;
Implement//Right double rotate template <typename type>void redblacktree<type>::d oublerotatewithleftchild (Node *& K3) const{ //First turn its left son (K1) to the left single rotation rotatewithrightchild (k3->left); Then turn yourself (K3) to the right single-spin rotatewithleftchild (K3);}
Double rotation to the left:
1. First Take K3 as the axis , K2 and K3 rotate to the right ;
2. Then take K1 as the axis , K1 and rotate the K2 to the left ;
Implement//Left double rotate template <typename type>void redblacktree<type>::d oublerotatewithrightchild (Node *& K1) const{ //First turn its right son (K2) to the right by single rotation rotatewithleftchild (k1->right); Then turn yourself (K1) to the left single-spin rotatewithrightchild (K1);} Note: In fact, the red and black trees do not use the double rotation, but their own implementation of a rotate operation,//In this only to learn the theory of double rotation;
Test (after completing the double rotation):
(Constructs a binary lookup tree, the left side is not rotated before, the right is rotated, the axis is double rotated by 8)
int main () {//uses Neg_inf to represent the negative Infinity const int neg_inf =-999999; Redblacktree<int> tree (Neg_inf);//test data for double rotation tree.insert (12); Tree.insert (16); Tree.insert (8); Tree.insert (10); Tree.insert (4); Tree.insert (14); Tree.insert (2); Tree.insert (6); Tree.insert (5); cout << tree.header->right->element << Endl; cout << tree.header->right->left->element << Endl; 8 cout << tree.header->right->right->element << Endl; cout << tree.header->right->left->left->element << Endl; 4 cout << tree.header->right->left->right->element << Endl; Ten cout << tree.header->right->right->left->element << Endl; 14//cout << tree.header->right->left->right->left->element << Endl; 5 have been Sentinel//cout << tree.header->right->left->right->right->element << endl 5 cout << tree.header->right->left->left->left->element << Endl; 2 cout << tree.header->right->left->left->right->element << Endl; 6 cout << tree.header->right->left->left->right->left->element << Endl; 5 cout << "\ n Double turn" << Endl; Double rotation tree.doublerotatewithleftchild (tree.header->right->left) to the right with 8 as the benchmark; cout << tree.header->right->element << Endl; cout << tree.header->right->left->element << Endl; 6 cout << tree.header->right->right->element << Endl; cout << tree.header->right->left->left->element << Endl; 4 cout << tree.header->right->left->right->element << Endl; 8 cout << tree.header->right->right->left->element << Endl; cout << Tree.header->right->left->leFt->left->element << Endl; 2 cout << tree.header->right->left->left->right->element << Endl; 5 cout << tree.header->right->left->right->right->element << Endl; return 0;}
Exception
Exception constructs used during the implementation of the red-black Tree: Class Dsexception{public: dsexception (const string &_message = String ()) : Message (_ Message) {} ~dsexception () {} Virtual string what () const { return message; } Virtual string toString () const { return "Exception:" + what (); } Private: std::string message;}; Class Duplicateitemexception:public Dsexception{public: duplicateitemexception (const string &_msg = String ( ) : Dsexception (_msg) {}};
Data Structure Basics (19)--design and implementation of red and Black trees (2)