Related operations of binary search tree

Source: Internet
Author: User

Operations include the creation of a two-fork search tree, inserting, searching, searching for precursor successors, deleting, rotating left and right, inserting elements as root nodes, and merging of two binary trees.

Binary tree creation is simple, only need to set the value, left child, right child can be.

Inserted recursively into the appropriate position in the tree, by comparing the value of the inserted element with the value of the root node element, or recursively inserting into the left subtree if it is less, or recursively inserting into the right sub-tree.

When the search is similar to inserting, compare the value to search and the value of the root node element, if less than the recursive to the left subtree to find, otherwise recursive to the right subtree to find.

The way to find a precursor is to find it recursively in the right node of the left subtree, has been found right node of the right sub-node is empty, then the right node is the precursor, looking for the successor is in the right subtree of the left node in the recursive search, has found the left node of the left Dial hand node is empty, then the left node is the successor.

The deletion of the node is somewhat complex, if the deletion of the leaf node, the deletion of the node, or the value in the precursor of the node will be replaced by the value in the junction, and then recursively delete the precursor, if there is no precursor, then use the successor of the node instead of the value in the node, and then recursively delete the successor, Prove that the node is a leaf node, directly deleted.

The left-hand way is to make the right node of the root node into a new root node, and then let the new root node point to the original root node, and then the original root node point to the right node point to the original left node of the new root node, so that after the operation as if the original root node rotated to the left.

Right-handed operation is symmetrical to the left and the left node of the root node becomes the new root node, then the right node of the new root node points to the original root node, and then the left node of the original root node points to the original right node of the new root node, so the operation is as if the root node is right-handed.

The original insert operation is inserted directly into the leaf node, and inserting an element to make it into a root node will need to rotate the operation to achieve, if the insertion element is greater than the root node value, then recursively inserted into the right subtree to make it the root node of the right subtree, and then the tree left immediately achievable, if the insertion element small root node value, Then recursively inserted into the left subtree to make it the root node in the left subtree, and then the tree to the right, recursive to the last inevitable to the leaf node, at this time with the value of the inserted element to create a new node, then the return rotation can be realized.

To merge two binary trees, the basic idea is to traverse one of the trees, then recursively inserted into another tree, the optimization method is to insert one of the root node of tree A into another tree B, at this time the root node of the two trees is the same, then the Zuozi of tree A is inserted into tree B must also be in the left subtree, the right subtree is the same. So it is possible to recursively insert the left node of a into the left subtree of B, recursively insert the right node of a into the right subtree of B, and note that because this method relies on the size of the tree and the root node, it can only be used in the pre-order recursion, otherwise it will break the order of the elements in the tree.

See the code for details:

Bst.cpp#include <malloc.h> #include <iostream> #include "binary_tree.h"//defined BST struct#include " Print_binary_tree.h "//the node of a tree, included in Binary_tree.h#include <stdlib.h> #include <time.h> Using namespace Std;//create node with value, the left child and the right child struct bst_node* create_bst_node (int value, Struc T bst_node* l, struct bst_node* r) {struct bst_node* t = (struct bst_node*) malloc (sizeof *t);//should call free () when    Finished t->value = value;    T->l = l;    T->r = R; return t;} BST insertstruct bst_node* bst_insert (struct bst_node* root, int value) {if (root = NULL) return create_bst_n Ode (value,null,null);//tree is empty//if root was not NULL if (value < root->value) Root->l = Bst_ins ERT (Root->l,value);//insert into left child else if (Value > Root->value) root->r = Bst_insert (root- >r,value);//return root;} BST search, the keyword "struct" can be omittedstrUCT bst_node* bst_search (struct bst_node* bst_root, int value) {if (bst_root = = null) return null;    if (value < Bst_root->value) {return Bst_search (bst_root->l, value);    }else if (value > Bst_root->value) {return Bst_search (bst_root->r, value); } return Bst_root;//neither greater nor less, must be Equal}//predecessor bst_node* predecessor (bst_node* t) {t = t    >l;    while (t->r! = NULL) {t = t->r; } return t;}    Successor bst_node* successor (Bst_node* t) {t = t->r;    while (t->l! = NULL) {t = t->l; } return t;}    BST removebst_node* Bst_remove (bst_node* t, int value) {if (t = = NULL) return null; if (value < T->value) {t->l = Bst_remove (t->l,value);//return a new left subtree which have removed the V Alue}else if (value > T->value) {t->r = Bst_remove (t->r,value);//return a new right subtree which ha  s removed the value}else{//have found the value      if (t->l! = NULL) {//get the predecessor first, then remove predecessor in left subtree T->value = pr            Edecessor (t)->value;        T->l = Bst_remove (T->l,t->value); }else if (t->r! = NULL) {//get The successor first, then remove successor in right subtree T->value = Succe            Ssor (t)->value;        T->r = Bst_remove (T->r,t->value);            }else{//now T is a leaf node,and remove it directly free (t);        t = NULL; }} return t;} BST Rotate//bst rotate Right, return left childbst_node* Bst_rot_r (bst_node* t) {if (t = = NULL | | t->l ==null) retu    RN T;        else{bst_node* tl = t->l;        T->l = tl->r;        Tl->r = t;    return TL;    }}//bst rotate left, return right childbst_node* bst_rot_l (bst_node* t) {if (t = = NULL | | t->r ==null) return t;        else{bst_node* tr = t->r;        T->r = tr->l;        Tr->l = t; return TR; }}//insert node to rootbst_node* bst_insert_to_root (bst_node* t, int value) {if (t = = NULL)//insert to Leaf Retu    RN Create_bst_node (value,null,null);        if (value > T->value) {t->r = Bst_insert_to_root (T->r,value);    return bst_rot_l (t);        }else if (value < T->value) {t->l = Bst_insert_to_root (T->l,value);    return Bst_rot_r (t);    }else{return t;    }}//merge-treesbst_node* Bst_merge_mine (bst_node* A, bst_node* b) {if (b==null) return A;        if (a==null) return B; Get the value from a, and inserts the value into B//I am inserting directly from the root node//My algorithm pre-order recursion and post-ordering recursion are all possible//the middle order recursion will generate a sloping tree, which should be for the middle sequence traversal node    The value is incremented sequentially, the last element is the largest, and it becomes the root node, so the synthetic tree is the B = bst_merge_mine (a->l,b) tilted to the right.    b = Bst_merge_mine (a->r,b); b = Bst_insert_to_root (B,a->value);//let root of a tobe the root of merged tree return b;} Merge two trees//The basic idea is to traverse the elements in a, recursively inserted into B//But this is a little better than my algorithm is to set the root node of a and B to the same after//each insertion can not start from the root node of B, because at this time a Zo Shu insert mustOn the left side of the merged tree, the right sub-tree of the//a is also bound to the right of the merged tree, so the left subtree of a can be inserted directly into the left Dial hand node of B,//The right node of a can be inserted into the right sub-tree of B to bst_node* Bst_merge (bst_node* A, BST _node* b) {//The entire recursive process is a pre-order recursion: Root->left child->right Child//From the outset it is not difficult to think of these two conditions,//But at the time of recursion these two conditions play a significant role//These two conditions are combined The left and right sub-tree assignment statement of the polygon b->l= ... b->r= ...    Played a wonderful role if (a==null) return b;//if (b==null) return a;//This condition is very subtle, in the recursive time performance is very clever//attention, because here the use of the relationship between the left and right Schich, so can only be pre-order recursion The sequence recursion and post-order recursion will destroy the elements in the tree. B = Bst_insert_to_root (b,a->value);//set A to root b->l = Bst_merge (A->l, B-&gt , l);//The Left node of a is taken out and inserted into the left subtree of b to B->r = Bst_merge (A->r, b->r);//The right node of a is taken out and inserted in the right subtree of B to free (a);//The Subtree of A is released after each merge to A = N    ULL; return b;}    int main () {//create_bst_node ();    Srand ((unsigned) time (NULL));//Use System timing to initialize the random seed int value = rand ()%123;    bst_node* bst_root = create_bst_node (value, NULL, NULL);        for (int k=0, k<39; k++) {for (int k=0; k<9; k++) {value = rand ()%123;    Bst_insert (bst_root, value);    } print_binary_tree (Bst_root); Value = RAnd ()%123;    bst_node* Bst_root2 = create_bst_node (value, NULL, NULL);        for (int k=0, k<39; k++) {for (int k=0; k<9; k++) {value = rand ()%123;    Bst_insert (Bst_root2, value);    } print_binary_tree (Bst_root2);    bst_node* m = Bst_merge (Bst_root,bst_root2);    Print_binary_tree (m); return 0;}

Related operations of binary search 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.