Binary sort tree, also called binary search tree. It is either an empty tree or a two-fork tree with the following properties.
- If its left subtree is not empty, then the value of all nodes on the left subtree is less than the value of its root node;
- If its right subtree is not empty, then the value of all nodes on the right subtree is greater than the value of its root node;
- Its left and right sub-trees are also two-fork sorting trees.
The above is a binary sort tree, and when we are traversing it in sequence, we can get an ordered sequence {35,37,47,51,58,62,73,88,93,99}.
Constructs a binary sort tree, not for sorting, but to improve the speed of finding and inserting deleted keywords.
Binary sorting tree operations mainly include:
- Lookup: Recursive lookup for existence of key;
- Insert: Key not present in the original tree, insert key to return True, otherwise return false;
- Construction: Loop insert operation;
- Delete:
– Leaf nodes: Delete directly without affecting the original tree;
– Only the nodes of the left or right subtree: After the deletion of the node, the entire left subtree or right subtree is moved to the location of the deleted node, and the child is in his father's footsteps;
– Both left and right subtree nodes: Find the direct precursor of node p that needs to be removed or direct successor S, replace node p with S, and then delete node S.
Additional Source code:
#include "stdio.h"#include "stdlib.h"#include "io.h"#include "math.h"#include "time.h"#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 100/* Storage space Initial allocation * /typedef intStatus;/ * Status is the type of function, whose value is the function result status code, such as OK, etc. * / / * Two fork Tree two-fork list node structure definition * /typedef structBitnode/ * Node structure * /{intData/ * Node data * / structBitnode *lchild, *rchild;/ * Left and right child hands * /} Bitnode, *bitree;/ * Recursive lookup for the existence of a key in the binary sort tree T, * // * Pointer f points to the parents of T, whose initial call value is NULL * // * If the lookup succeeds, the pointer p points to the data element node and returns True * // * Otherwise the pointer p points to the last node accessed on the lookup path and returns false * /Status Searchbst (Bitree T,intKey, Bitree F, Bitree *p) {if(! T/ * Find unsuccessful * /{*p = f;returnFALSE; }Else if(Key==t->data)/ * Find success * /{*p = T;returnTRUE; }Else if(Key<t->data)returnSearchbst (T->lchild, Key, T, p);/ * Continue to find in record * / Else returnSearchbst (T->rchild, Key, T, p);/ * Continue to find in right subtree * /}/ * When there is no data element with the keyword equal to key in the binary sort tree T, * // * Insert key and return True, otherwise false * /Status Insertbst (Bitree *t,intKey) {Bitree p,s;if(! Searchbst (*t, Key, NULL, &p))/ * Find unsuccessful * /{s = (bitree)malloc(sizeof(Bitnode)); S->data = key; S->lchild = S->rchild = NULL;if(!p) *t = s;/ * Insert s for new root node * / Else if(key<p->data) P->lchild = s;/ * Insert s for left child * / ElseP->rchild = s;/ * Insert s for right child * / returnTRUE; }Else returnFALSE;/ * nodes with the same keyword in the tree are no longer inserted * /}/* Remove the node p from the two-fork sort tree and re-connect its left or right subtree. */Status Delete (Bitree *p) {Bitree q,s;if((*p)->rchild==null)/* Right subtree empty just need to re-connect its Zuozi (to delete the node is the leaf also walk this branch) */{q=*p; *p= (*p)->lchild; Free(q); }Else if((*p)->lchild==null)/ * Simply re-connect its right subtree * /{q=*p; *p= (*p)->rchild; Free(q); }Else / * The subtree is not empty * /{q=*p; s= (*p)->lchild; while(S->rchild)/* Turn left and then right to the end (find the precursor to the deleted node) */{q=s; s=s->rchild; } (*P)->data=s->data;/* s direct precursor to the deleted node (replaces the value of the deleted node with the value of the deleted point precursor) */ if(q!=*p) q->rchild=s->lchild;/ * Re-connect the right sub-tree of Q * / Elseq->lchild=s->lchild;/ * Re-connect the left sub-tree of Q * / Free(s); }returnTRUE;}/ * If there is a data element in the binary sort tree T that has the keyword equal to key, the data element node is deleted, * // * and returns True, otherwise false. */Status Deletebst (Bitree *t,intKey) {if(!*t)/ * There is no data element with the keyword equal to key * / returnFALSE;Else{if(key== (*t)->data)/ * Find the data element with the keyword equal to key * / returnDelete (T);Else if(key< (*t)->data)returnDeletebst (& (*t)->lchild,key);Else returnDeletebst (& (*t)->rchild,key); }}intMainvoid){intIinta[Ten]={ +, the, -, -, *, the,Wuyi, About,Panax Notoginseng, the}; Bitree T=null; for(i=0;i<Ten; i++) {Insertbst (&t, a[i]); } deletebst (&t, the); Deletebst (&t, -);printf("This sample suggests breakpoint tracking to see binary sort tree structure");return 0;}
Big talk data structure-two fork sort tree