#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 int STATUS;/* 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 struct BITNODE/* Node structure */
{
int data;/* node Data */
struct Bitnode *lchild, *rchild;/* Children's hands */
} Bitnode, *bitree;
/* Recursive lookup binary sort tree T exists key, */
/* Pointer F 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, int key, Bitree F, bitree *p)
{
if (! T)/* Find unsuccessful */
{
*p = f;
return FALSE;
}
Else if (key==t->data)/* Lookup succeeded */
{
*p = T;
return TRUE;
}
Else if (key<t->data)
return Searchbst (T->lchild, Key, T, p);/* continue to find in record */
Else
return Se Archbst (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, int key)
{ Br>bitree P,s;
if (! Searchbst (*t, Key, NULL, &p))/* Lookup 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 */
Else
P-> ; rchild = s; /* Insert S for right child */
return TRUE;
}
Else
return FALSE;/* The node with the same keyword in the tree is 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 then only 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/* 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 delete node) */
{
Q=s;
s=s->rchild;
}
(*p)->data=s->data;/* s direct precursor to the deleted node (replaces the value of the deleted node precursor with the deleted point value) */
if (q!=*p)
Q->rchild=s-> Lchild; /* Re-connect the right subtree of Q */
Else
q->lchild=s->lchild;/* Re-connect the left subtree of Q */
Free (s);
}
return TRUE;
}
/* 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,int key)
{
if (!*t)/* There is no data element with the keyword equal to key */
return FALSE;
Else
{
if (key== (*t)->data)/* Find the data element with the keyword equal to key */
Return Delete (T);
else if (key< (*t)->data)
Return Deletebst (& (*t)->lchild,key);
Else
Return Deletebst (& (*t)->rchild,key);
}
}
int main (void)
{
int i;
int a[10]={62,88,58,47,35,73,51,99,37,93};
Bitree T=null;
for (i=0;i<10;i++)
{
Insertbst (&t, a[i]);
}
Deletebst (&t,93);
Deletebst (&t,47);
printf ("This sample suggests breakpoint tracking to see binary sort tree structure");
return 0;
}
Binary sort Tree