Data Structure BASICS (19) and data structure basics 19

Source: Internet
Author: User

Data Structure BASICS (19) and data structure basics 19
Double Rotation

A single rotation problem sometimes occurs (as shown in ):


(If the child node [k1] on the inner side is too deep, moving it one way will not solve the problem)

 

So there is a double rotation.

Double rotation to the right:


1. First, take k1 as the axis, and k1 and k2 rotate to the left;

2. Take k3 as the axis, and k3 and k1 after rotation rotate to the right;

/// Implement // double rotate template to the right <typename Type> void RedBlackTree <Type>: doubleRotateWithLeftChild (Node * & k3) const {// first place its left son (k1) rotate rotateWithRightChild (k3-> left) to the left, and then rotate rotateWithLeftChild (k3) to the right );}

Rotate to the left:


1. First, use k3 as the axis, and k2 and k3 rotate to the right;

2. Take k1 as the axis, and k1 and k2 after rotation rotate to the left;

/// Implement // rotate the template <typename Type> void RedBlackTree <Type>: doubleRotateWithRightChild (Node * & k1) const {// first set its right son (k2) rotate rotateWithLeftChild (k1-> right) to the right, and then rotate rotateWithRightChild (k1) to the left.} // note: in fact, the red and black trees do not use double rotation, but implement a rotate operation by themselves. // here, we only want to learn the theory of double rotation;

Test (after dual rotation ):


(Construct a binary search tree. The left side is before rotation, the right side is after rotation, and the 8 side is used as the axis for double rotation)

Int main () {// use NEG_INF to represent negative infinity const int NEG_INF =-999999; RedBlackTree <int> tree (NEG_INF); // test data tree during double spin. 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; // 12 cout <tree. header-> right-> left-> element <endl; // 8 cout <tree. header-> right-> element <endl; // 16 cout <tree. header-> right-> left-> element <endl; // 4 cout <tree. header-> right-> left-> right-> element <endl; // 10 cout <tree. header-> right-> left-> element <endl; // 14 // cout <tree. header-> right-> left-> element <endl; // 5 has been used as a sentinel // cout <tree. header-> right-> left-> right-> element <endl; // 5 cout <tree. header-> right-> left-> element <endl; // 2 cout <tree. header-> right-> left-> right-> element <endl; // 6 cout <tree. header-> right-> left-> element <endl; // 5 cout <"\ n "<endl; // rotate the tree to the right based on 8. doubleRotateWithLeftChild (tree. header-> right-> left); cout <tree. header-> right-> element <endl; // 12 cout <tree. header-> right-> left-> element <endl; // 6 cout <tree. header-> right-> element <endl; // 16 cout <tree. header-> right-> left-> element <endl; // 4 cout <tree. header-> right-> left-> right-> element <endl; // 8 cout <tree. header-> right-> left-> element <endl; // 14 cout <tree. header-> right-> left-> element <endl; // 2 cout <tree. header-> right-> left-> right-> element <endl; // 5 cout <tree. header-> right-> left-> right-> element <endl; // 10 return 0 ;}

Exception
// Exception construction used in 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 ){}};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.