Two fork sorting tree (binary sort trees) Also known as binary search tree, also known as binary tree.
It 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 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 the value of its root node;
(3) The left and right sub-trees are also two-fork sorting trees respectively;
On-Machine code:
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <cmath > using namespace std; #define KEYTYPE int #define EQ (a) = = (b) #define LT (A, B) ((a) < (a)) #define LQ (A, a) ((a) &L T;= (b)) #define FALSE 0#define TRUE 1 #define OK 1//#define OVERFLOW 0#define ERROR 0#define stack_init_size 100#define STA Ckincrement 10typedef struct {KeyType key; Key field} Elemtype; typedef struct BITNODE//define binary Tree two fork list {elemtype data;struct bitnode *lchild, *rchild;} Bitnode,*bitree,*selemtype;typedef Struct{selemtype *base; Selemtype *top;int stacksize;} Sqstack;int Destroybitree (Bitree &t)//Destroy Tree {if (t!=null) free (T); return 0;} int Clearbitree (Bitree &t)//Empty tree {if (t!=null) {t->lchild=null; t->rchild=null; T=null;} return 0;} int Searchbst (bitree t,keytype key,bitree f,bitree &p)//Find keyword, pointer p returns {if (! T) {p=f; return FALSE;} else if EQ (key,t->data.key) {P=t;return TRUE;} else if LT (Key,t->data.key) return Searchbst (t->lchild,key,t,p), Else return Searchbst (t->rchild,key,t,p);} int Insertbst (Bitree &t,elemtype e)//Insert node element {Bitree s,p;if (! Searchbst (t,e.key,null,p)) {s= (bitree) malloc (sizeof (Bitnode)); S->data=e;s->lchild=s->rchild=null;if (!p ) T=s;else if LT (e.key,p->data.key) p->lchild=s;else P->rchild=s;return TRUE;} else return FALSE;} int Showbst (bitree t,int nlayer)//display tree binary sort tree {int i;if (t==null) return FALSE; Showbst (t->rchild,nlayer+1); for (i=0;i<nlayer;i++) printf ("");p rintf ("%d\n", t->data); Showbst (t->lchild,nlayer+1); return OK;} int Visit (elemtype e)//visit function {printf ("%d", E.key); return OK;} int Initstack (Sqstack &s)//construct empty stack {s.base= (selemtype*) malloc (stack_init_size *sizeof (Selemtype)); s.base) exit (OVERFLOW); S.top=s.base; S.stacksize=stack_init_size;return OK;} Initstackint Push (Sqstack &s, selemtype e)//INSERT element E for new stack top {if (s.top-s.base≫=s.stacksize) {s.base= (selemtype*) realloc (S.base, (s.stacksize+stackincrement) *sizeof (Selemtype)); s.base) exit (OVERFLOW); S.top=s.base+s.stacksize; S.stacksize+=stackincrement;} *s.top++=e;return OK;} Pushint Pop (sqstack &s,selemtype &e)//delete stack top, apply E to return its value {if (s.top==s.base) return Error;e=*--s.top;return OK;} Popint stackempty (Sqstack S)//Determine if the empty stack {if (s.base==s.top) return True;return FALSE;} int Preordertraverse (Bitree t,int (*visit) (Elemtype e))//ordinal traversal, using stack {sqstack S; Bitree P;initstack (S);p =t;while (p| |! Stackempty (S)) {if (p) {Push (s,p); Visit (P->data)) return ERROR; P=p->lchild;} Else{pop (S, p);p =p->rchild;}} return OK;} int Inordertraverse (bitree T, int (*visit) (Elemtype e))//middle sequence traversal, using stack {sqstack S; Bitree P;initstack (S);p =t;while (p| |! Stackempty (S)) {if (p) {Push (s,p);p =p->lchild;} Else{pop (s,p); if (! Visit (P->data)) return error;p=p->rchild;}} return OK;} int Postordertraverse (Bitree t,int (*visit) (Elemtype e))//post-operation traversal, using stack {sqstack s,ss; Bitree P;initstack (S); IniTstack (SS);p =t;while (p| |! Stackempty (S)) {if (p) {Push (s,p); Push (ss,p);p =p->rchild;} Else{if (! Stackempty (s)) {Pop (S, p);p =p->lchild;}}} while (! Stackempty (ss)) {POP (SS, p); Visit (P->data)) return ERROR; }return OK;} int delete (Bitree &p)//Three delete node operation implementation {Bitree q,s;if (!p->rchild)//Right subtree is empty {q=p;p=p->lchild;free (q);} else if (!p->lchild)//left dial hand tree is empty {q=p;p=p->rchild;free (q);} Else{q=p;s=p->lchild;while (s->rchild) {q=s;s=s->rchild;} P->data=s->data;if (q!=p) q->rchild=s->lchild;elseq->lchild=s->lchild;free (s);} return TRUE;} int Deletebst (Bitree &t,keytype key)//implement two-fork sort Tree Delete operation {if (! T) return False;else{if (EQ (key,t->data.key))//t->data.key equals Keyreturn Delete (t); else if (LT (Key,t->data.key ))//t->data.key is less than Keyreturn deletebst (T->lchild,key); else return Deletebst (T->rchild,key);} return 0;} int main () {int i,nlayer; Elemtype k,d; Bitree bt,p; bt=null;p=null;nlayer=1;printf ("Enter the value of the inserted two-fork tree node (enter the number 0 to end the node assignment): \ n"); scanf ("%d", &k.key); for (i=0;k.key!=null;i++) {if (! Searchbst (BT,K.KEY,NULL,P))//Find keyword {insertbst (bt,k); Binary tree node value inserted scanf ("%d", &k.key);} else{printf ("Input data Repeat! \ n "); return 0;}} printf ("Binary sort tree-tree output: \ n"); Showbst (Bt,nlayer); Tree display binary sort tree printf ("Please enter deleted data:"); scanf ("%d", &d.key);D Eletebst (Bt,d.key); Delete keyword Showbst (bt,nlayer);p rintf ("First order traversal is:"); First order traversal, middle sequence traversal, post-order traversal Preordertraverse (bt,visit);p rintf ("\ n sequence Traversal:"), Inordertraverse (BT, Visit);p rintf ("\ n post-order traversal as:"); Postordertraverse (Bt,visit); printf ("\ n Empty the two-fork sort tree. \ n"); Empty the binary Tree Clearbitree (BT); Showbst (Bt,nlayer);p rintf ("\ n Destroy the two-fork sort tree. \ n"); Destroy binary tree Clearbitree (BT); return 0;}
Data structure-implementation of a two-fork sorting tree