Binary sort Tree

Source: Internet
Author: User

Two fork sorting treedefinition

The binary sort tree is either an empty tree or a two-fork tree with the following properties:

(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less than or equal to the value of its root node;

(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than or equal to the value of its root node;

(3) The left and right sub-trees are also two-fork sorting trees respectively;

FindThe keyword value of the Joghen node is equal to the search keyword, which succeeds. Otherwise, if the key value of the root node is small, the left subtree is recursively checked. Jordahugen the key value of the node and recursively checks the right subtree. If the subtree is empty, the lookup is unsuccessful.

Insert

First, the search algorithm is performed to find the Father node of the inserted node.

The left and right son of his father's knot is judged by the insertion point. Insert nodes as leaf nodes.

If the binary tree is empty. First, the root node is generated separately.

Note: The newly inserted node is always the leaf knot point.

Delete

In the binary sort tree Delete a node, divided into three kinds of situation discussion:

1. If the *p node is a leaf node, that is, pl (left subtree) and PR (right subtree) are empty trees. Since the deletion of leaf nodes does not destroy the structure of the whole tree, the sub-node can be deleted directly.

2. If the *P node only left dial hand tree pl or right subtree PR, at this time as long as the PL or PR directly become the parent node *f Zuozi (when *p is Zuozi) or right subtree (when *p is the right subtree) can, make this modification does not destroy the characteristics of binary sorting tree.

3. If the Saozi right subtree of the *P node is not empty. After deleting *p, in order to keep the relative position of other elements unchanged, it can be adjusted in order by the middle order traversal, there are two ways to do it:

One is to make the left *p of the *f left/right (according to *p is *f Zuozi or right subtree) subtree, *s for *p Zuozi the most right node, and *p right sub-tree *s right subtree;

The second is to make the direct precursor (or direct successor) of *p replace the *p, then delete its direct precursor (or direct successor) from the two-fork sorting tree-that is, let *f's Zuozi (if any) become the leftmost lower node of *p Zuozi (if any), then let *f be the parent node of the *p's left and right nodes.

The diagram for deleting a node on a binary sort tree is as follows:

All code:

#include"iostream"using namespaceStd;typedefintelement;structtree{element data; Tree*left,*Right ;};//InsertvoidInsert (tree* &t,element data) {    if(!t) {T=NewTree; T->data =data; T->left =NULL; T->right =NULL; }    Else if(Data < t->data) {Insert (T-left,data); }    Else if(Data > T->data) {Insert (T-right,data); }}//Look fortree* Find (tree* &t,element Key) {    if(!t | | t->data = =key) {        returnT; }    if(Key < T->data) {        returnFind (t->Left,key); }    returnFind (t->Right,key); }//PrintvoidShow (Tree* &t) {    if(t) {Show (T-Left ); cout<<t->data<<ends; Show (t-Right ); }}//CreatevoidCreate (tree* &t,element *a,intN) {     for(inti =0; I < n;i++) {Insert (t,a[i]); }}//DeleteintDeleted (Tree* &t,element Key) {Tree*f = Null,*p = t;//f parent node and p Delete node//Find Delete point P     while(p) {if(P->data = =key) {             Break; } f=p; if(Key < P->data) {P= p->Left ; }        Else{p= p->Right ; }    }    if(!p) {//not found        return 0; } Tree*q =p; //if P has two children    if(P->right && p->Left ) {Tree*s = q->Left ;  while(s->Right ) {Q=s; S= s->Right ; } P->data = s->data; //Remove S        if(q = =p) {P->left = s->Left ; }        Else{Q->right = s->Left ; }        Deletes; return 1; }    //No right child .    if(!p->Right ) {Q= p->Left ; }    //no left child .    Else if(!p->Left ) {Q= p->Right ; }    //the point to delete is the root    if(!f) {T=Q; }    Else{        //at the parent node, left.        if(p = = f->Left ) {f->left =Q; }        //at the parent node, right.        Else{f->right =Q; }    }    Deletep; return 1;}intMain () {Const intN = One; Tree*t =NULL; intA[n] = { $, A, -,3,Panax Notoginseng, -, -, A, -, -,7};    Create (T,a,n);    Show (t); cout<<Endl; Deleted (T,3);    Show (t); //Cout<<find (t,100)->data<<endl;     return 0;}

Binary sort 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.