#include "string.h"
#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. */
/* For constructing two-fork tree ********************************** */
int index=1;
typedef char STRING[24]; /* Unit No. 0 The length of the string */
String str;
Status strassign (String t,char *chars)
{
int i;
if (strlen (chars) >maxsize)
return ERROR;
Else
{
T[0]=strlen (chars);
for (i=1;i<=t[0];i++)
t[i]=* (chars+i-1);
return OK;
}
}
/* ************************************************ */
typedef char Telemtype;
Telemtype nil= '; /* Character type is empty by space */
Status visit (Telemtype e)
{
printf ("%c", e);
return OK;
}
typedef struct BITNODE/* node structure */
{
Telemtype data;/* Node Data */
struct Bitnode *lchild,*rchild; /* Left and right child hands */
}bitnode,*bitree;
/* Construct empty binary tree T */
Status Initbitree (Bitree *t)
{
*t=null;
return OK;
}
/* Initial condition: two fork tree T exists. Operation Result: Destroy binary tree T */
void Destroybitree (Bitree *t)
{
if (*t)
{
if ((*t)->lchild)/* have left child */
Destroybitree (& (*t)->lchild); /* Destroy left child subtree */
if ((*t)->rchild)/* have right child */
Destroybitree (& (*t)->rchild); /* Destroy right child subtree */
Free (*t); /* Release root node */
*t=null; /* NULL pointer assignment 0 */
}
}
/* Enter the value of the node in the binary tree (one character) by the preamble */
/* #表示空树, construct a two-fork list representing a binary tree T. */
void Createbitree (Bitree *t)
{
Telemtype ch;
/* SCANF ("%c", &ch); */
Ch=str[index++];
if (ch== ' # ')
*t=null;
Else
{
*t= (bitree) malloc (sizeof (Bitnode));
if (!*t)
Exit (OVERFLOW);
(*t)->data=ch; /* Generate root node */
Createbitree (& (*t)->lchild); /* Construct left sub-tree */
Createbitree (& (*t)->rchild); /* Construct Right Sub-tree */
}
}
/* Initial condition: two fork tree T exists */
/* Operation Result: Returns True if T is an empty binary tree, otherwise false */
Status bitreeempty (Bitree T)
{
if (T)
return FALSE;
Else
return TRUE;
}
#define Clearbitree Destroybitree
/* Initial condition: two fork tree T exists. Operation Result: Returns the depth of T */
int bitreedepth (Bitree T)
{
int i,j;
if (! T
return 0;
if (t->lchild)
I=bitreedepth (T->lchild);
Else
i=0;
if (t->rchild)
J=bitreedepth (T->rchild);
Else
j=0;
Return i>j?i+1:j+1;
}
/* Initial condition: two fork tree T exists. Operation Result: Returns the root of T */
Telemtype Root (Bitree T)
{
if (Bitreeempty (T))
return Nil;
Else
Return t->data;
}
/* Initial condition: two fork tree T exists, p points to a node in T */
/* Operation Result: Returns the value of the node indicated by P */
Telemtype Value (Bitree p)
{
Return p->data;
}
/* Assign value to p node = *
void Assign (Bitree p,telemtype value)
{
p->data=value;
}
/* Initial condition: two fork tree T exists */
/* Operation Result: pre-order recursive traversal T */
void Preordertraverse (Bitree T)
{
if (t==null)
Return
printf ("%c", t->data);/* Display node data, can be changed to other node operations */
Preordertraverse (T->lchild); /* Re-sequence the left sub-tree first */
Preordertraverse (T->rchild); /* Last sequential traversal of right subtree */
}
/* Initial condition: two fork tree T exists */
/* Operation Result: middle order recursive traversal T */
void Inordertraverse (Bitree T)
{
if (t==null)
Return
Inordertraverse (T->lchild); /* Middle sequence traversal left subtree */
printf ("%c", t->data);/* Display node data, can be changed to other node operations */
Inordertraverse (T->rchild); /* Last middle order traverse right subtree */
}
/* Initial condition: two fork tree T exists */
/* Operation result: Sequential recursive traversal T */
void Postordertraverse (Bitree T)
{
if (t==null)
Return
Postordertraverse (T->lchild); /* Sequential traversal of left sub-tree */
Postordertraverse (T->rchild); /* Go through the right sub-tree again * *
printf ("%c", t->data);/* Display node data, can be changed to other node operations */
}
int main ()
{
int i;
Bitree T;
Telemtype E1;
Initbitree (&t);
Strassign (str, "abdh#k## #E # #CFI # # #G #j##");
Createbitree (&t);
printf ("Constructs empty binary tree, tree empty no?") %d (1: Yes 0: NO) tree depth =%d\n ", Bitreeempty (t), bitreedepth (t));
E1=root (T);
printf ("The root of the binary tree is:%c\n", E1);
printf ("\ n the pre-order traversal binary tree:");
Preordertraverse (T);
printf ("\ n sequence traversal binary tree:");
Inordertraverse (T);
printf ("\ n post-traverse binary tree:");
Postordertraverse (T);
Clearbitree (&t);
printf ("\ n Clear the binary tree, the tree is empty?") %d (1: Yes 0: NO) tree depth =%d\n ", Bitreeempty (t), bitreedepth (t));
I=root (T);
if (!i)
printf ("Tree empty, no root \ n");
return 0;
}
Binary tree chain structure realizes C language