The following four steps are taken to expand the data structure:
1) Select the basic data structure;
2) determine the information to be added to the basic data structure;
3) Verify that the newly added information can be maintained using the basic modification operation on the basic data structure;
4) design new operations.
The introduction to algorithms provides an expansion of the red and black trees-dynamic sequence statistics.
Measure the values of random samples with a capacity of N (x1, x2 ,......, XN) in ascending order. The sequential position of Xi is its Ordered Statistics.
Dynamic sequence statistics mean that any sequence statistics can be determined within O (lgn) time in a dynamically disordered set (linear structure requires O (N) ).
As for the expansion of the red and black trees, the final point is that after expansion, maintenance of new domains can be controlled within the original complexity.
The extension steps are as follows (C ++ implementation ):
1. Add an int type size change to the members of the Red/black tree.
class BRTreeNode{private:friend class BRTree;int key;int size;bool color;BRTreeNode* left;BRTreeNode* right;BRTreeNode* parent;}
2. Maintain data domains, including left and right.
// Left-side node nodebool leftrotate (brtreenode * node) {brtreenode * Y; If (node-> right = nil) {cout <"can't left rotate! "<Endl; return 0;} y = node-> right; node-> right = Y-> left; If (Y-> left! = Nil) {Y-> left-> parent = node;} y-> parent = node-> parent; If (node-> parent = nil) {root = y;} else if (node-> parent-> left = node) {node-> parent-> left = y ;} else {node-> parent-> right = y;} y-> left = node; node-> parent = y; // maintain the size field y-> size = node-> size; node-> size = node-> left-> size + node-> right-> size + 1; return 1;} // right-hand node bool rightrotate (brtreenode * node) {If (node-> left = nil) {cout <"can't rightrotate! "<Endl; return 0;} brtreenode * X; X = node-> left; node-> left = x-> right; if (X-> right! = Nil) {X-> right-> parent = node;} X-> parent = node-> parent; If (node-> parent = nil) {root = x;} else if (node-> parent-> left = node) {node-> parent-> left = x ;} else {node-> parent-> right = x;} node-> parent = x; X-> right = node; // maintain the size field node-> size = x-> size; X-> size = x-> left-> size + X-> right-> size + 1; return 1 ;}
3. define new operations
1) first root Traversal
void Preorder(BRTreeNode*node){if(node!=nil){cout<<node->key<<" "<<"size:"<<node->size<<" ";Preorder(node->left);Preorder(node->right);}}
2) Search for the Order Statistic with a smaller num value in the tree.
BRTreeNode* GetIthnode(BRTreeNode*node,int num){int r=node->left->size+1;if(r==num){ cout<<"Got it!"<<node->key<<endl;return node;}else if(r>num){return GetIthnode(node->left,num);}else{return GetIthnode(node->right,num-r);}}
3) determine the rank of an element in the tree whose key value is K.
int getRank(BRTreeNode*node,int key){ int r=node->left->size+1; int k=node->key; if(k==key) { return r; } else if(key<k) { return getRank(node->left,key)-1; } else { return r+getRank(node->right,key); }}
Finally, let's test:
int mian(){ BRTree tree; tree.init(); cout<<"Insert 8 numbers:"<<endl; int a[8]={1,2,3,4,5,6,7,8}; int i; for(i=0;i<8;i++) { tree.Insert(a[i]); tree.Preorder(tree.Getroot()); cout<<"Insert finish"<<endl; } cout<<"Get No.6 node"<<endl; tree.GetIthnode(tree.Getroot(),6); cout<<"Get rank of node whick key-value is 6:"<<endl; cout<<tree.getRank(tree.Getroot(),6)<<endl; return 0;}
Running result:
For other operations, refer to the previous article:
Introduction to algorithms-red/black tree C ++ implementation
Refer:
Introduction to algorithms Version 2