Binary sort Tree

Source: Internet
Author: User

Use C implementation.

Binary sort tree: Suzi node, right subtree greater than root node

Because the characteristics of the binary sorting is not difficult to see the result after the sequence traversal is ordered (from small to large)
If the search is greater than the root node to traverse the right subtree, the small root node will traverse the left subtree, otherwise it will find success.

Summarize:

two forks The sorting tree is chained to the storage, keeping the link storage structure in the execution of INSERT and delete operation is not the advantage of moving elements, as long as the appropriate insertion and deletion location, only need to modify the link pointer,
Insert Delete time performance is better
the number of times to be searched is The path from the root node to the node to be found, which is equal to the number of layers of the given node in the two-fork tree.
The minimum number of lookups is O (1), and long does not exceed the depth of the tree
so the performance of the lookup depends on the shape of the two-fork sort tree.

//Binary sort Tree#include <stdio.h>#include <stdlib.h>#define TRUE 1#define FALSE 0typedef int STATUS;//The structure of a binary tree defines a typedef struct bitnode{int data; struct Bitnode *lchild, *rchild;//Left and right child hands}bitnode, *bitree;//Binary sort Tree Lookup implementation/* Recursive implementation, find the binary sort tree T in existence key pointer F is the parents of T, the initial value isNULLFind success, point to the node with pointer p to the data, returntrueLookup failed with pointer p pointing to the last visited node on the lookup path, returningfalseNumber of num lookups */status searchbst (bitree T, int key, Bitree F, bitree *p, int *num) {(*num) + +;if(! T) {//Find failed *p = f;returnFALSE; }Else if(T->data = = key) {//Find success *p = T;returnTRUE; }Else if(T->data > key) {Searchbst (T->lchild, Key, T, p, num);//Find in the left subtree}Else{Searchbst (T->rchild, Key, T, p, num);//Look in the right subtree}}//Insert/* Key not in tree, insert */insertbst (bitree *t, int key) {Bitree p, s; int num =0;if(!searchbst (*t, Key, NULL, &p, &num)) {//Find unsuccessful S = (bitree) malloc (sizeof (Bitnode));        S->data = key; S->lchild = S->rchild = NULL;if(!p) {*t = s;//Root node}Else if(P->data > key) {//The new node is inserted into record p->lchild = s; }Else{P->rchild = s; }returnTRUE; }returnFALSE;}//Middle sequence traversal binary sort treevoidInordertraverse (Bitree T) {if(T = = NULL) {return;    } inordertraverse (T->lchild); printf"%d", T->data); Inordertraverse (t->rchild);}//Pre-sequence traversal binary sort treevoidPreordertraverse (Bitree T) {if(T = = NULL) {return; } printf ("%d", T->data);    Preordertraverse (T->lchild); Preordertraverse (t->rchild);}//Sequential traversal binary sorting treevoidPostordertraverse (Bitree T) {if(T = = NULL) {return;    } postordertraverse (T->lchild);    Postordertraverse (T->rchild); printf"%d", t->data);}//Delete of binary sort tree reconstruct left and right subtree statusDelete(Bitree *p) {Bitree Q, S;if ((*p)->rchild = = null){//Right subtree is empty simply refactor left subtree q = *p; *P = (*p)Lchild;    Free (q); }Else if ((*p)->lchild = = null){//Left dial hand tree is empty just refactor right subtree q = *p; *P = (*p)Rchild;    Free (q); }Else{//The left and right subtrees are notNULLQ = *p; S = (*p)Lchild; while(S->rchild) {//Find the maximum value of Zuozi q = s;         s = s->rchild; } (*p)data = s->data;//Overwrite the value of s directly with the node to be deletedif(q! = *p) {Q->rchild = s->lchild;//Refactor Q's Right subtree}Else{Q->rchild = s->lchild;//Refactor Q's Left subtree} free (s); }returnTRUE;}//The deletion of binary sort tree Find node/* Two fork in the sort tree where the keyword is key, the Delete element node returnstrueotherwise returnfalse*/status Deletebst (bitree *t, int key) {if(!*t) {returnFALSE;//There is no data element for the keyword key}Else{if((*t)->data = = key){return Delete(T); }Else if((*t)->data > key){return Deletebst(& ((*t)->lchild), key); }Else{return Deletebst(& ((*t)->rchild), key); }}}//Traversalvoid Travese(Bitree T){printf("pre-order traversal is:");Preordertraverse(T);printf("\ n");printf("Middle sequence traversal is:");Inordertraverse(T);printf("\ n");printf("post-posttraversal is:");Postordertraverse(T);printf("\ n");}Status Main(void){Bitree T=NULL,P;int arr[10] = {1,34,3,234,2,6,343,43,4,32};int I; for(i = 0; i <  ; i + +){Insertbst(&t, arr[i]); }int option= 1,Key= 0,Res= 0,Num= 0;printf("1. Find data \ n 2. Traverse data \ n 3. Delete data \ n 0. Exit \ n "); while(option){scanf("%d", &option);Switch(option){ Case1:Num= 0;printf("Please enter the data you want to find \ n");scanf("%d", &key);Res=Searchbst(T, Key, NULL, &p, &num);if(res = = TRUE){printf("The number of search successful lookups is:%d\n", num); }Else{printf("The number of lookup failed lookups is:%d\n", num); } Break; Case2:Travese(T); Break; Case3:printf("Please enter the data to be deleted \ n");scanf("%d", &key);Res=Deletebst(&t, key);if(res = = TRUE){printf("Delete succeeded \ n");Travese(T); }Else{printf("Delete failed to delete data does not exist \ n"); } Break; Case0:return TRUE; Break;default:printf("1. Find data \ n 2. Traverse data \ n 0. Exit \ n "); Break; }    }return TRUE;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.