Re-learning Data structure Series--the balance tree of sb trees (Size blanced) __ Data structure

Source: Internet
Author: User
Source of study: GarlicBalance Tree
1. Define
for each node,The absolute value of the height difference of the left and right two subtrees is no 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 will degenerate into an ordered list, and the efficiency is greatly reduced.


2. The concept

All the balance trees are composed of the following three features: 1. Self-equilibrium conditions
2. Rotation operation
3. Rotation trigger

By setting reasonable self equilibrium conditions, the equilibrium tree so that the performance of searching, inserting and so on of binary sort tree is not degraded to O (n) o (n), and when the binary sort tree is searched and inserted, the corresponding rotation operation is made if the trigger condition of one of the rotations is met.


Left-handed and right-handed: (My understanding)

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

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

Spin: 1. Rotate the left and then the right 2. Rotate the right to the left.


SB Tree
This is not very familiar, the program is the garlic, probably understand, do some notes, but the comments are their own understanding of the
strong>1. Define
He is also a kind of balance tree. The number of nodes in the subtree of each node is not less than the number of nodes in the subtree of the two children of the sibling. And Sbtree can trigger adjustments only when inserting.

It is said that he has a special feature that can look up the small element K, why is this characteristic? First, the concept: the size of the tree: the number of nodes contained, and then SB tree is also part of the data structure of the node of the sort trees sb. Tree with size stored in the current node as the root of the trees, then its father is ranked in the size+1 bit, specifically to see the code better understand the
2. Implementation
functions that are not annotated below see this (binary sort tree): http://blog.csdn.net/u012763794/article/details/50967631

#include <iostream> using namespace std; Class Sbtnode {public://size: The size of the subtree (the number of nodes of the tree with the root of the current node)//data: Weight, which is the value of the node stored on the tree//value: should be a temporary storage value of int data, size, VA
    Lue
	Sbtnode * lchild, * rchild, * father;
    Constructors, parameters are weights, the size of the tree at the root of the current node, the Father node Sbtnode (int init_data, int init_size = 0, Sbtnode * init_father = NULL);
	~sbtnode ();
    The following is//two fork sorting tree insertion, search, find the predecessor, find the successor, remove a certain degree of 0 or 1 nodes, remove a point of weight, find the K-large element 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 sequence is//two fork tree Insert Lookup Delete node find the K-large tree, all of which are void inserts (int value) based on the function of the node class above;
    BOOL Find (int value);
    BOOL Remove (int value);
int select (int k);

};
Here we have a node with a weighted value of 0 to avoid the null pointer (NULL) in case of boundary conditions, so that all of the original points to the null pointer to a ZPTR, and its size set to 0, thus reducing the complexity of the code.
Sbtnode ZERO (0); Sbtnode * ZPTR = &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; }//L: Turn the right child into a "root node" (the root node of the current subtree), o the right child's left child becomes the original root node of the right child//The following note: node (original root node) of the right child with "root node" for Sbtnode * left_rotate (Sbtnode *
	node) {//Save "root node" with temp = sbtnode * temp = node->rchild;
	The left child of the "root knot" becomes node (the original root node) of the right child Node->rchild = temp->lchild;
	Update "root node" the original left child's father is node (original root node) Temp->lchild->father = node;
	Node (the original root node) becomes the "root node" of the left child temp->lchild = node;
	The father of the "root node" is updated to node (the original root node) of the Father Temp->father = node->father;
	The father of node (the original root node) is updated to "root node" node->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 (original root node) is updated to the size of its left child and right child again on itself 1 node->size = Node->lchilD->size + node->rchild->size + 1;
Returns the root node after the left rotation return temp; //Right: Turn the left child into a "root node" (the root node of the current subtree), o the right child of the left child becomes the original root node of the left child//The following note: node (original root node) of the left child with "root node" to say//anyway here to keep up with almost the opposite Sbtnode * right_
	Rotate (Sbtnode * node) {//Save "root node" with temp Sbtnode * temp = node->lchild;
	The "root knot" of the right child becomes node (the original root node) of the left child node->lchild = temp->rchild;
	Update "root node" the original right child's father is node (original root node) Temp->rchild->father = node;
	Node (the original root node) becomes the "root node" of the right child temp->rchild = node;
	The father of the "root node" is updated to node (the original root node) of the Father Temp->father = node->father;
	The father of node (the original root node) is updated to "root node" node->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 (original root node) is updated to the size of its left child and right child again on itself 1 node->size = node->lchild->size + node->rchild->size + 1;
Returns the root node after the right rotation return temp;  ///using the left and right rotation to adjust the function//flag is false: processing the higher case of the left subtree, otherwise processing a higher//node of the tree: the root node of the subtree to be adjusted Sbtnode * Maintain (SBTNODE * node, BOOL flag) {///Supi right subtree height (or deep depth) if (flag = = False) {//ll: The number of elements in the left subtree of the Zuozi is greater than the number of elements in the right subtree, and should be right-handed if (Node->lchild->lchild->size > Node->rchild->size) {//Right-spin
        and updating the root node node = right_rotate (node) of the subtree; //LR Type: 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, then the LL type, and then right can be else if (Node->lchild->rchild->size > Node-&gt
			Rchild->size) {//L and updating the root node of the left subtree Node->lchild = left_rotate (node->lchild);
        Right-rotate and update root node = right_rotate (node);
        else {//description balanced, return node for root; //Right subtree is more than Gow (or deep)} else {//rr: the right subtree of the right subtree is larger than the number of elements in the left subtree, and should be left-spin if (Node->rchild->rchild->size & Gt
        Node->lchild->size) {//L and Update root node = left_rotate (node); //RL: The left subtree of the right subtree is more than the number of elements in the left subtree//Then the right subtree is turned to the right, then the RR type, the left soon can else if (Node->rchild->lchild->size > Node-&gt
			Lchild->size) {//right-rotate and update the root node of the left subtree Node->rchild = right_rotate (node->rchild); L and Update root node = left_rotate (nODE);
        else {//description balanced, return node for root; }//The following is recursive invocation, because sometimes the above adjustment, the Saozi right subtree of a node or unbalanced//recursive call, processing possible left subtree Zuozi height of the situation//false that the left subtree higher node->lchild = Maintai
	N (node->lchild, false);
	The right subtree of the right subtree is higher in height Node->rchild = Maintain (Node->rchild, true);
    Finally, we adjust node = Maintain (node, false) to the left and right subtree of the tree root node.
	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 (v
            Alue, 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-&Gt;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 to temp;
    } Sbtnode * Sbtnode::successor () {Sbtnode * temp = rchild;
    while (temp!= zptr && temp->lchild!= zptr) {temp = temp->lchild;
return to 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;
	the int sbtnode::select (int k) {//rank represents the row int rank = lchild->size + 1 of the current node in the subtree;
	If rank is equal to K small, the description is to find the value, the direct return weight can be if (rank = = k) {returns data;
	}else if (K < rank) {//Less than rank, indicates that you want to find a smaller than the current node, find return Lchild->select (k) on the left; }else{//greater than on the right here why look at the K-rank, because we have ruled out the former rank,//the equivalent of we are in the right subtree (see him as a new tree to find), so the rank of course to subtract rank return Rchild->sel
	ECT (K-rank);

} binarytree::binarytree () {root = NULL;}
    Binarytree::~binarytree () {if (root!= NULL) {delete root;  } void Binarytree::insert (int value) {if (root = = NULL) {//Initialize only 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 a small element of K:" <<endl;
    int k;
	Cin >> K;
    cout<<endl<< "<<k<<" small elements are: "<<endl;
    cout << Binarytree.select (k) << Endl;
return 0; }


3. Operating results



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.