Re-learn the data structure series--Balance tree sb tree (Size blanced tree)

Source: Internet
Author: User

Balance Tree
1. Definition
for each node,The absolute value of the height difference of the left and right two subtrees is not more than 1, or the depth difference is not more than 1
Why is there such a tree?

If we insert into the two-fork sort tree in the order of 1-n, then the two-fork sort tree is degenerate into an ordered list, and the efficiency is greatly reduced.


2. Related Concepts

All balance trees are basically made up of the following three characteristics:

1. Self-balancing conditions
2. Rotation operation
3. Triggering of rotation

Balanced tree by setting reasonable self-balance conditions, so that the performance of the binary sorting tree search, insertion and other operations do not degenerate to O (n) o (n), and in the binary sorting tree to find, insert and other operations to judge, if the trigger condition of one of the rotation is satisfied, then the corresponding rotation operation.


Left-and right-handed: (My understanding)

L: The right depth is greater than 2 of the left depth, turning it into a balance tree, called the left side (because you want to move some elements on the right hand side)

Right: The left depth is greater than 2 of the right depth, turning it into a balance tree, called right-handed (because you want to move some elements on the left to the right)

Multi-spin: 1. Turn left and right after 2. Turn right at the left hand first


SB Tree
This is not very familiar, the procedure is a garlic customer, probably understand it, do some notes, but the comments are their own understanding
1. Definition
He is also a kind of balance tree. The number of nodes in the subtree where each node is located is not less than the number of nodes in the subtree of the two children of their siblings. There are also sbtree that can trigger adjustments only if they are inserted.

It is said that he has a special feature that can find the element of the K-large, why this feature is. First of all, a concept: the size of the tree: the number of nodes included, and the other is that SB tree is also the data structure of the node of the sort tree sb tree, the size of the tree that is the root of the current node is stored with size, then its father is in the size+1 bit, the specific code to understand better
2. Implement
Here is an annotated function see this (binary sort tree): http://blog.csdn.net/u012763794/article/details/50967631
#include <iostream>using namespace Std;class sbtnode {public://size: Subtree size (that is, the number of nodes that are the root of the current node)//data: Weights,    Is the value of the node stored in the tree//value: should be the temporary storage value of the int data, size, value; Sbtnode * lchild, * rchild, * father;//constructor, parameters for weights, tree size with current node root, Father node Sbtnode (int init_data, int init_size = 0, Sbtnod    E * init_father = NULL);    ~sbtnode ();//The following sequence is the insertion of a//two fork sorting tree, search, find a precursor, find a successor, remove a node with a degree of 0 or 1, remove a point of a certain weight, find the element of k large void insert (int value);    Sbtnode * Search (int value);    Sbtnode * predecessor ();    Sbtnode * successor ();    void Remove_node (Sbtnode * delete_node);    BOOL Remove (int value); int select (int k);};    Class BinaryTree {Private:sbtnode * Root;public:binarytree ();    ~binarytree ();//The following is the insertion of the//two fork tree to find the delete node to find the K-large tree, which is based on the function of the above node class void insert (int value);    BOOL Find (int value);    BOOL Remove (int value); int select (int k);};/ /Here's a null//with a weight of 0, which is the equivalent of our own, but instead of the null pointer here is the address of zero (the node with a weight of 0), and we see that the constructor of the following Sbtnode is initialized with the zptr pointer//here with this, Binary tree and with null, seems a bit contradictory, know the reason can tell me, thank you Sbtnode ZERO (0); Sbtnode * zptr = &Amp ZERO;    Sbtnode::sbtnode (int init_data, int init_size, Sbtnode * init_father) {data = Init_data;    size = Init_size;    Lchild = zptr;    Rchild = zptr; Father = Init_father;}    Sbtnode::~sbtnode () {if (lchild! = zptr) {delete lchild;    } if (rchild! = zptr) {delete rchild; }}//left: The right child into a "root node" (the root node of the current subtree), the right child's child will become the original root node of the right child//The following comments: node (the original root node) of the right child with "root node" for the Sbtnode * left_rotate (Sbtnode * node) {//Save "root node" with temp Sbtnode * temp = node->rchild;//"root node" left child becomes node (original root node) right child Node->rchild = TEMP-&GT;LC hild;//Update "root node" the original left child's father is node (the original root node) Temp->lchild->father = Node;//node (the original root node) becomes the "root node" of the left child Temp->lchild = node;//"root node" father updated to node (original root node) father Temp->father = Node->father;//node (original root node) of the father updated to "root node" node->father = Te The size of the mp;//"root node" is updated to the size of node (the original root node) (where the size is the number of nodes of the tree that is the root of the node) Temp->size = Node->size;//node (the original root node) is updated to the size of Its left child and right child size again on itself 1 node->size = node->lchild->size + node->rchild->size + 1;//return left-handed root knotPoint return temp;} Right: Turn the left child into "root node" (the root node of the current subtree), the left child's right child becomes the original root node of the left child//The following comment: node (the original root node) of the left child with "root node" to say//anyway here with almost opposite Sbtnode * right_ Rotate (Sbtnode * node) {//Save "root node" with temp Sbtnode * temp = node->lchild;//"root node" right child becomes node (original root node) of the left child Node->lch    ILD = temp->rchild;//Update "root node" the original right child's father is node (the original root node) Temp->rchild->father = Node;//node (the original root node) becomes the "root node" of the right child Temp->rchild = node;//"root node" of Father Update to node (original root node) father Temp->father = Node->father;//node (original root node) of the father updated to "root node" nod E->father = temp;//the size of the "root node" is updated to the size of node (the original root node) (where the size is the number of nodes of the tree that is the root of the node) Temp->size = node->size;// The size of node (the original root node) is updated to its left child and right child size again on itself 1 node->size = node->lchild->size + node->rchild->size + 1;//back to the root after right rotation node return temp;} Use the left and right rotation to adjust the function//flag to false: Handle the higher left subtree situation, otherwise, the case of higher//node: The root node of the subtree to be adjusted Sbtnode * Maintain (SBTNODE * node, BOOL flag) { Supi Right Sub-tree height (or depth) if (flag = = False) {//ll: Zuozi the number of elements of the left subtree is greater than the number of elements of the right subtree, the right-hand if (node->lchild->lchild->si Ze > Node->rchilD->size) {//right-click and update the root node of the subtree node = right_rotate (node); }//LR: The number of elements in the right subtree of the Zuozi is greater than the number of elements in the right subtree//Then the Zuozi is left-handed, then the LL-shape, then the other right immediately can if (Node->lchild->rchild->size > node-> Rchild->size) {//L and update the root node of the left subtree Node->lchild = left_rotate (node->lchild);//Right-click and update root nodes = RI        Ght_rotate (node);        The else {//description is balanced, returning the root node to return to node; }//right subtree is greater than Gow (or depth)} else {//rr: Right subtree has more elements than the left subtree, should be left-handed if (Node->rchild->rchild->size > nod        E->lchild->size) {//L and Update root node = left_rotate (node); }//RL: The number of elements in the left subtree of the right subtree is greater than the number of elements of the left subtree//Then the right subtree is first right-handed, to RR, and to the left immediately else if (Node->rchild->lchild->size > node-> Lchild->size) {//right-scroll and update the root node of the left subtree Node->rchild = right_rotate (node->rchild);//L and Update root nodes = L        Eft_rotate (node);        The else {//description is balanced, returning the root node to return to node; The following is a recursive call, because sometimes after the above adjustment, a node in the right subtree of Saozi is still unbalanced//recursive call, which handles the Zuozi height of the left subtree//false means that the left sub-tree is higher NODE-&GT;LCHild = Maintain (Node->lchild, false);//The right sub-tree of its right subtree is higher in height Node->rchild = Maintain (Node->rchild, true);//    Finally, the left and right sub-tree of the root node is adjusted node = Maintain (node, false); node = maintain (node, true);//returns the root node of the adjusted subtree return node;    Sbtnode * INSERT (SBTNODE * node, int value) {if (value = = node->data) {return node;        } else {node->size++; if (value > Node->data) {if (Node->rchild = = zptr) {node->rchild = new Sbtnode (val            UE, 1, node);            } else {node->rchild = insert (node->rchild, value);            }} else {if (Node->lchild = = zptr) {node->lchild = new Sbtnode (value, 1, node);            } else {node->lchild = insert (node->lchild, value); }}} return maintain (node, value > Node->data);}    Sbtnode * Sbtnode::search (int value) {if (data = = value) {return this; } else if (Value > DatA) {if (rchild = = zptr) {return zptr;        } else {return rchild->search (value);        }} else {if (lchild = = zptr) {return zptr;        } else {return lchild->search (value);    }}}sbtnode * Sbtnode::p redecessor () {Sbtnode * temp = lchild;    while (temp! = zptr && Temp->rchild! = zptr) {temp = temp->rchild; } return temp;}    Sbtnode * Sbtnode::successor () {Sbtnode * temp = rchild;    while (temp! = zptr && Temp->lchild! = zptr) {temp = temp->lchild; } return temp;}    void Sbtnode::remove_node (Sbtnode * delete_node) {Sbtnode * temp = zptr;        if (delete_node->lchild! = zptr) {temp = delete_node->lchild;        Temp->father = delete_node->father;    Delete_node->lchild = zptr;        } if (Delete_node->rchild! = zptr) {temp = delete_node->rchild;        Temp->father = delete_node->father; DElete_node->rchild = zptr;    } if (Delete_node->father->lchild = = Delete_node) {delete_node->father->lchild = temp;    } else {delete_node->father->rchild = temp;    } temp = Delete_node;        while (temp! = NULL) {temp->size--;    temp = temp->father; } delete Delete_node;}    BOOL Sbtnode::remove (int value) {Sbtnode * delete_node, * current_node;    Current_node = search (value);    if (Current_node = = Zptr) {return false;    } size--;    if (current_node->lchild! = zptr) {Delete_node = Current_node->predecessor ();    } else if (current_node->rchild! = zptr) {Delete_node = Current_node->successor ();    } else {delete_node = Current_node;    } Current_node->data = delete_node->data;    Remove_node (Delete_node); return true;} int sbtnode::select (int k) {//rank represents the current node in the sub-tree's rank int rank = lchild->size + 1;//if rank equals K of K, the description is the value to be found, the direct return weight can be the if (rank = = k) {return data;}else if (K < rank) {///is less than rank, which indicates that it is smaller than the current node, look for return Lchild->select (k) on the Left;} else{//is greater than on the right side//Here why look at K-rank, because we have put the former rank ruled out,//equivalent to We want to in the right sub-tree (see him as a new tree to find), so the rank of course to subtract rank return rchild->select (K-rank);}} Binarytree::binarytree () {root = NULL;}    Binarytree::~binarytree () {if (root! = NULL) {Delete root;    }}void binarytree::insert (int value) {if (root = NULL) {//Initialize only the root node, so the subtree size is 1 root = new Sbtnode (value, 1);    } else {root =:: Insert (root, value);    }}bool binarytree::find (int value) {if (Root->search (value) = = NULL) {return false;    } else {return true; }}bool binarytree::remove (int value) {return root->remove (value);} int binarytree::select (int k) {return root->select (k);}    int main () {BinaryTree BinaryTree;    int arr[10] = {16, 9, 20, 3, 18, 15, 6, 30, 7, 25};    for (int i = 0; i < i++) {Binarytree.insert (arr[i]);    }cout<< "Please enter the element K-large:" <<endl;    int k; Cin >> K;cout<<endl<< "<<k<<" large elements are: "<<endl;    cout << Binarytree.select (k) << Endl; return 0;}


3. Running Results



Re-learn the data structure series--Balance tree sb tree (Size blanced tree)

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.