#include <stdio.h>
#include <stdlib.h>
#define MAX 20
typedef char ElementType;
typedef struct treenode
{
ElementType value;
struct treenode* left;
struct treenode* right;
}*TreeNode;
void init(TreeNode* t);//初始化树
void create1(TreeNode* t);//非递归的方式创建树
void create2(TreeNode *t);//使用递归创建树
void previsit(TreeNode t);//前序遍历树先访问根节点再访问左子树再访问右子树
void midvisit(TreeNode t);//中序遍历树
void tailvisit(TreeNode t);//后序遍历树
void levelvisit(TreeNode t);//按层遍历树
int depth(TreeNode t);//返回树的深度
void update(TreeNode t,ElementType old_value,ElementType new_value);//将树中所有的old_value值更新为new_value
void print(TreeNode t);//以广义表示法进行输出
void display(TreeNode t,int format);//以目录表示法进行输出
void clear(TreeNode* t);//清空树
char *string = "A(B(D,E(G,H)),C(F( ,I), ))";//广义表示法
char *str = "ABD##EA##H##CA#I###";//扩展二叉树
int main()
{
TreeNode tree;
init(&tree);
create1(&tree);
//create2(&tree);
previsit(tree);
printf("\n");
midvisit(tree);
printf("\n");
tailvisit(tree);
printf("\n");
levelvisit(tree);
printf("\n");
printf("the depth of tree is : %d\n",depth(tree));
//update(tree,'A','Z');
//previsit(tree);
//printf("\n");
print(tree);
printf("\n");
display(tree,0);
clear(&tree);
display(tree,0);
return 0;
}
void init(TreeNode* t)//初始化树
{
*t = NULL;
}
//char *string = "A(B(D,E(G,H)),C(F( ,I), ))";
void create1(TreeNode* t)//非递归的方式创建树
{
int i = 0;
int flag = 1;//建树的标志,1建左子树,2建右子树
TreeNode s[MAX];//数组模拟栈
int top = -1;//栈顶指针
TreeNode p;
while(*(string + i) != '\0')
{
switch(*(string + i))
{
case ' ':
break;
case '(':
if(top == MAX-1)
{
printf("the stcak is full\n");
exit(2);
}
top++;
s[top] = p;
flag = 1;
break;
case ',':
flag = 2;
break;
case ')':
if(-1 == top)
{
printf("the stack is empty\n");
exit(3);
}
top--;
break;
default:
p = (TreeNode)malloc(sizeof(struct treenode));
if(NULL == p)
{
exit(1);
}
p->value = *(string + i);
p->left = NULL;
p->right = NULL;
if(NULL == *t)
{
*t = p;
}
else
{
if(flag == 1)
{
s[top]->left = p;
}
else
{
s[top]->right = p;
}
}
break;
}
i++;
}
}
//char *str = "ABD##EG##H##CF#I###";
void create2(TreeNode* t)//使用递归创建树
{
static int i = 0;
char ch = *(str+i++);
if(ch == '#')
{
*t = NULL;
}
else
{
*t = (TreeNode)malloc(sizeof(struct treenode));
if(NULL == *t)
{
exit(1);
}
(*t)->value = ch;
create2(&((*t)->left));
create2(&((*t)->right));
}
}
void previsit(TreeNode t)//前序遍历树先访问根节点再访问左子树再访问右子树
{
if(t != NULL)
{
printf("%c ",t->value);
previsit(t->left);
previsit(t->right);
}
}
void midvisit(TreeNode t)//中序遍历树
{
if(t != NULL)
{
midvisit(t->left);
printf("%c ",t->value);
midvisit(t->right);
}
}
void tailvisit(TreeNode t)//后序遍历树
{
if(t != NULL)
{
tailvisit(t->left);
tailvisit(t->right);
printf("%c ",t->value);
}
}
void levelvisit(TreeNode t)//按层遍历树
{
TreeNode array[MAX] = {0};//模拟队列
int front = 0;
int rear = 0;
if(t != NULL)
{
array[rear] = t;
rear = (rear+1) % MAX;
}
while(front != rear)
{
printf("%c ",array[front]->value);
if(NULL != array[front]->left)
{
array[rear] = array[front]->left;
rear = (rear+1) % MAX;
}
if(NULL != array[front]->right)
{
array[rear] = array[front]->right;
rear = (rear+1) % MAX;
}
front = (front+1) % MAX;
}
}
int depth(TreeNode t)//返回树的深度
{
if(t != NULL)
{
int leftdepth = depth(t->left);
int rightdepth = depth(t->right);
return (leftdepth>rightdepth?leftdepth:rightdepth)+1;
}
else
{
return 0;
}
}
//将树中所有的old_value值更新为new_value
void update(TreeNode t,ElementType old_value,ElementType new_value)
{
if(NULL != t)
{
if(t->value == old_value)
{
t->value = new_value;
}
update(t->left,old_value,new_value);
update(t->right,old_value,new_value);
}
}
void print(TreeNode t)//以广义表示法进行输出
{
if(NULL != t)
{
printf("%c",t->value);
if(NULL != t->left || NULL != t->right)
{
printf("(");
if(NULL == t->left)
{
printf(" ");
}
else
{
print(t->left);
}
printf(",");
if(NULL == t->right)
{
printf(" ");
}
else
{
print(t->right);
}
printf(")");:êo
}
}
}
void display(TreeNode t,int format)//以°?目录表示法进行输出format为空格的个数
{
int i;
if(NULL != t)
{
for(i = 0;i < format;i++)
{
printf(" ");
}
printf("%c\n",t->value);
display(t->left,format+1);
display(t->right,format+1);
}
}
void clear(TreeNode* t)//清空树
{
if(NULL != *t)//必须以后序遍历的方式清除树
{
clear(&((*t)->left));
clear(&((*t)->right));
free(*t);
*t = NULL;
}
}