#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/0 is scanned/* *
while (A[i]!= '/0 ') {
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. Find binary Tree depth */
int btreedepth (struct btreenode *bt)
{
if (BT = NULL) {
Return 0; /* for empty trees, returns 0 ending recursion */
}else{
int dep1 = btreedepth (bt->left ); /* compute the depth of the left subtree */
int DEP2 = btreedepth (bt->right); /* compute the depth of the right subtree */
& Nbsp;if (Dep1 > Dep2) {
return dep1 + 1;
}else{
return DEP2 + 1;
&NBSP;&NBSP}
}
/* 5. Find a node with a value of x from a two fork tree, and return the element storage location if present, otherwise null */
Elemtype *findbtree (struct Btreenode *bt, elemtype x)
{
if (BT = null) {
return null;
}else{
if (bt->data = = x) {
Return & (Bt->data);
}else{ /* recursively find the left and right subtree */
Elemtype *p;
if (p = findbtree (Bt->left, x)) {
return p;
&NBSP;&NBSP;&NBSP}
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); /* subsequent traversal of the left subtree */
postorder (bt->right), /* after the right subtree to traverse the tree * *
printf ("%c", bt->data); /* Access root node */
}
return;
}
/* 11. Traversal by layer */
void Levelorder (struct btreenode *bt)
{
struct btreenode *p;
struct Btreenode *q[queue_max_size];
int front = 0, rear = 0;
/* The root pointer into team */
if (BT!= NULL) {
rear = (rear + 1)% Queue_max_size;
q[rear] = BT;
}
while (front!= rear) { /* queue non-null */
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 in the left child, the left child node pointer enters the team */
if (p->left!= NULL) {
Rear = (rear + 1)% Queue_max_size;
q[rear] = p->left;
}
/* If the node exists right child, the right child node pointer into 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 ("Lookup failed. /n ");
}
printf ("The depth of the binary tree is:");
printf ("%d/n", Btreedepth (BT));
Clearbtree (&BT);
return 0;
}
*/
#include < stdio.h >
#define QUEUE_MAX_SIZE 20
#define STACK_MAX_SIZE 10
typedef int ELEMTYPE;
#include "bt.c"
/* ********************************************************************** */
/* Here are 4 simple algorithms for binary search tree operations * *
/* ********************************************************************** */
* 1. Find/*
/* Recursive algorithm * *
Elemtype * FINDBSTREE1 (struct Btreenode * BST, Elemtype x)
{
/* The NULL is returned if the tree is empty * *
if (BST = NULL) {
return NULL;
} else {
if (x = = BST-> data) {
Return & (BST-> data);
} else {
if (x < BST-> data) {/* Find the Zoozi tree and return it directly * *
Return findBSTree1 (BST-> left, X);
else {//* Right subtree lookup and return directly * *
Return findBSTree1 (BST-> right, X);
}
}
}
}
/* Non-recursive algorithm * *
Elemtype * FINDBSTREE2 (struct Btreenode * BST, Elemtype x)
{
while (BST!= NULL) {
if (x = = BST-> data) {
Return & (BST-> data);
else if (x < BST-> data) {
BST = BST-> left;
} else {
BST = BST-> right;
}
}
return NULL;
}
/* 2. Insert */
/* Recursive algorithm * *
void InsertBSTree1 (struct btreenode * * BST, elemtype x)
{
/* Create a new root node * *
if (* BST = NULL) {
struct Btreenode * p = (struct Btreenode *) malloc (sizeof (struct btreenode));
P-> data = x;
P-> left = p-> right = NULL;
* BST = p;
return;
else if (x < (* BST)-> data) {/* to Zuozi complete insert operation * *
InsertBSTree1 (& (* BST)-> left), x);
else {/* Right subtree completes insert operation * *
InsertBSTree1 (& (* BST)-> right), x);
}
}
/* Non-recursive algorithm * *
void InsertBSTree2 (struct btreenode * * BST, elemtype x)
{
struct Btreenode * p;
struct Btreenode * t = * BST, * parent = NULL;
/* Find the insertion position for the element to be inserted/*
while (T!= NULL) {
parent = t;
if (x < T-> data) {
t = t-> left;
} else {
t = t-> right;
}
}
/* Establish a value of x, the left and right pointer field is empty new node * *
p = (struct Btreenode *) malloc (sizeof (struct btreenode));
P-> data = x;
P-> left = p-> right = NULL;
/* Link the new node to the location where the pointer is empty/*
if (parent = NULL) {
* BST = p; /* Insert as root node * *
else if (x < parent-> data) {/* Link to left pointer field * *
Parent-> left = p;
} else {
Parent-> right = p;
}
return;
}
* * 3. Set up * *
void Createbstree (struct btreenode * * BST, elemtype a[], int n)
{
int i;
* BST = NULL;
for (i = 0; i