#include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 30
#define QUEUE_MAX_SIZE 30
#ifndef Elemtype
typedef char ELEMTYPE;
#endif
/************************************************************************/
/* Here are 11 simple algorithms for binary tree operations.
/************************************************************************/
struct btreenode{
Elemtype data;
struct Btreenode *left;
struct Btreenode *right;
};
/* 1. Initialize the binary tree * *
void Initbtree (struct btreenode* *bt)
{
*BT = NULL;
Return
}
* 2. Establish two fork tree (based on a two-fork tree Generalized table string)
void Createbtree (struct btreenode* *bt, char *a)
{
struct Btreenode *p;
struct Btreenode *s[stack_max_size];/* defines s-array for storing root-node pointers for stack use * *
int top =-1; /* Defines top as the stack head of the s stack, the initial value is 1, which means the empty stack * *
int k; /* using K as the Saozi right subtree for processing nodes, K = 1 processing left subtree, k = 2 processing right subtree/
int i = 0; /* Use I scan array A in the two-fork tree Generalized table string, initial value is 0 * *
*BT = NULL; /* Set the root pointer to empty, that is, starting from the empty tree two fork tree
/* Process one character at a time until the string terminator is scanned.
while (A[i]!= ' ") {
Switch (A[i]) {
Case ':
Break /* No handling of spaces * *
Case ' (':
if (top = = stack_max_size-1) {
printf ("Stack space is too small!") \ n ");
Exit (1);
}
top++;
S[top] = p;
K = 1;
Break
Case ') ':
if (top = = 1) {
printf ("Binary Tree Generalized table string error!\n");
Exit (1);
}
top--;
Break
Case ', ':
K = 2;
Break
Default
p = malloc (sizeof (struct btreenode));
P->data = A[i];
P->left = P->right = NULL;
if (*BT = = NULL) {
*BT = p;
}else{
if (k = = 1) {
S[top]->left = p;
}else{
S[top]->right = p;
}
}
}
i++; /* Modify the I value for scanning the next character * *
}
Return
}
* 3. Check that the two fork tree is empty, return 1 for NULL, otherwise return 0 * *
int Emptybtree (struct btreenode *bt)
{
if (BT = NULL) {
return 1;
}else{
return 0;
}
}
/* 4. To find the depth of the two fork tree * *
int btreedepth (struct btreenode *bt)
{
if (BT = NULL) {
return 0; /* For empty tree, return 0 End recursive * *
}else{
int dep1 = btreedepth (bt->left); /* Calculate the depth of the left subtree * *
int DEP2 = btreedepth (bt->right); /* Calculate the depth of the right subtree * *
if (Dep1 > Dep2) {
return DEP1 + 1;
}else{
return DEP2 + 1;
}
}
}
/* 5. Find the node with x from the two fork tree, and return the element storage location if present, otherwise return null value * *
Elemtype *findbtree (struct Btreenode *bt, Elemtype x)
{
if (BT = NULL) {
return NULL;
}else{
if (Bt->data = = x) {
Return & (Bt->data);
}else{/* To search the left and right sub-trees recursively
Elemtype *p;
if (p = findbtree (Bt->left, x)) {
return p;
}
if (p = findbtree (Bt->right, x)) {
return p;
}
return NULL;
}
}
}
/* 6. Output binary tree (pre-sequence traversal) * *
void Printbtree (struct btreenode *bt)
{
/* tree is NULL to end recursion, otherwise do the following: *
if (BT!= NULL) {
printf ("%c", bt->data); /* Output root node value * *
if (bt->left!= null | | | bt->right!= NULL) {
printf ("(");
Printbtree (Bt->left);
if (bt->right!= NULL) {
printf (",");
}
Printbtree (Bt->right);
printf (")");
}
}
Return
}
* 7. Clear the binary tree, make it into an empty tree
void Clearbtree (struct btreenode* *bt)
{
if (*BT!= NULL) {
Clearbtree (& ((*BT)->left));
Clearbtree (& ((*BT)->right));
Free (*BT);
*BT = NULL;
}
Return
}
/* 8. Pre-sequence Traversal * *
void preorder (struct btreenode *bt)
{
if (BT!= NULL) {
printf ("%c", bt->data); /* Access root node * *
Preorder (Bt->left); /* pre-order traversal left subtree * *
Preorder (bt->right); /* Pre-order traversal right subtree * *
}
Return
}
/* 9. Pre-sequence Traversal * *
void Inorder (struct btreenode *bt)
{
if (BT!= NULL) {
Inorder (Bt->left); /* In-sequence traversal left subtree * *
printf ("%c", bt->data); /* Access root node * *
Inorder (Bt->right); /* In-order traversal right subtree *
}
Return
}
* 10. Subsequent traversal * *
void Postorder (struct btreenode *bt)
{
if (BT!= NULL) {
Postorder (Bt->left); * The subsequent traversal of the left subtree * *
Postorder (Bt->right); * * After the right subtree traversal tree *
printf ("%c", bt->data); /* Access root node * *
}
Return
}
* 11. Traverse by Layer * *
void Levelorder (struct btreenode *bt)
{
struct Btreenode *p;
struct Btreenode *q[queue_max_size];
int front = 0, rear = 0;
/* Bring the root pointer into the team/*
if (BT!= NULL) {
Rear = (rear + 1)% Queue_max_size;
Q[rear] = BT;
}
while (front!= rear) {/* Queue not empty */
Front = (front + 1)% Queue_max_size; /* Make the team head pointer to the team first element * *
p = Q[front];
printf ("%c", p->data);
* If the node exists left child, then the left child node pointer into the team.
if (p->left!= NULL) {
Rear = (rear + 1)% Queue_max_size;
Q[rear] = p->left;
}
* If the node exists right child, then the right child node pointer into the team.
if (p->right!= NULL) {
Rear = (rear + 1)% Queue_max_size;
Q[rear] = p->right;
}
}
Return
}
/************************************************************************/
/*
int main (int argc, char *argv[])
{
struct Btreenode *bt; /* Pointer to two-fork root node
Char *b; /* The string used to deposit the two-fork tree generalized table.
Elemtype x, *px;
Initbtree (&BT);
printf ("Input string of a generalized table of binary trees: \ n");
/* SCANF ("%s", b); */
b = "A (b (c), D (E (f, G), H (, i))";
Createbtree (&BT, b);
if (BT!= NULL)
printf ("%c", bt->data);
printf ("Output in the form of a generalized table: \ n");
Printbtree (BT); /* To output a binary tree in the form of a generalized table * *
printf ("\ n");
printf ("Pre-preface:"); /* Pre-order traversal * *
Preorder (BT);
printf ("\ n");
printf ("In order:"); /* in-sequence traversal * *
Inorder (BT);
printf ("\ n");
printf ("After:"); /* Subsequent traversal * *
Postorder (BT);
printf ("\ n");
printf ("by layer:"); /* by layer traverse * *
Levelorder (BT);
printf ("\ n");
/* Find an element node from the two fork tree.
printf ("Enter a character to find: \ n");
scanf ("%c", &x); /* The space in the format string skips the white-space character * *
px = Findbtree (BT, x);
if (px) {
printf ("Find success:%c\n", *px);
}else{
printf ("Find failed!") \ n ");
}
printf ("The depth of the binary tree is:");
printf ("%d\n", Btreedepth (BT));
Clearbtree (&BT);
return 0;
}
*/