C语言二叉树的各种功能的实现

//A(B(D,E(G,H)),C(F( ,I)))                           //二叉树的标准输入方法

#include<stdio.h>                                       

#include<stdlib.h>

 

#define MAX 20

typedef char ElementType;

 

struct TreeNode

{

ElementType value;

struct TreeNode *left;

struct TreeNode *right;

};

 

void init(struct TreeNode** root);                                           //根节点初始化

void create(struct TreeNode** root, char *string);                   // 创建结点

void previsit(struct TreeNode* root);                                       // 前序遍历

void midvisit(struct TreeNode* root);                                   // 中序遍历

void tailvisit(struct TreeNode* root);                                   //后序遍历

int treedepth(struct TreeNode* root);                               // 求二叉树深度

void printtree(struct TreeNode* root);                              //输出二叉树的标准表示方式

void update(struct TreeNode* root, ElementType old_value, ElementType  new_value);         //修改值

void levelvisit(struct TreeNode* root);                                                                         //按层遍历二叉树

void cleartree(struct TreeNode** root);                                                                      // 清空二叉树

 

int main()

{

struct TreeNode *root;

init(&root);

char *string = "A(B(D,E(G,H)),C(F( ,I)))";

create(&root, string);

previsit(root);

printf("\n");

midvisit(root);

printf("\n");

tailvisit(root);

printf("\n");

printf("depth = %d\n", treedepth(root));

printtree(root);

printf("\n");

update(root, 'A', 'Z');

printtree(root);

printf("\n");

levelvisit(root);

printf("\n");

cleartree(&root);

printtree(root);

printf("\n");

return 0;

}

 

void init(struct TreeNode** root)

{

*root = NULL;

}

//A(B(D,E(G,H)),C(F( ,I)))

void create(struct TreeNode** root, char *string)

{

struct TreeNode *p;//用来存放新创建的节点

int i = 0;//用来遍历字符串的位置参数

int flag = 1;// 1:左树 2:右树

struct TreeNode* s[MAX];//模拟栈

int top = -1;//层数

while(*(string + i) != '\0')

{

switch(*(string + i))

{

case ' ':

break;

case '(':

if(top == MAX - 1)

{

printf("tree is full\n");

exit(1);

}

top++;

s[top] = p;

flag = 1;

//左子树的出现

break;

case ')':

//右子树的结束

if (-1 == top)

{

printf("error in string\n");

exit;

}

top--;

break;

case ',':

flag = 2;

//左子树切换到右子树

break;

default:

//遇到其他字符,代表新的节点出现

p = (struct TreeNode*)malloc(sizeof(struct TreeNode));

if (NULL == p)

{

exit(1);

}

//对value部分赋值

p->value = *(string + i);

//左右子树的处理

p->left = NULL;

p->right = NULL;

//root节点的处理

if (NULL == *root)

{

//暂时还未有root节点

//新创建的节点将成为root

*root = p;

}

else

{

//已经存在root

if (1 == flag)

{

//新建的节点是左树

s[top]->left = p;

}

else

{

//新建的节点是右树

s[top]->right = p;

}

}

break;

}

i++;

}

}

 

void previsit(struct TreeNode* root)

{

if (root != NULL)

{

printf("%c\t", root->value);

previsit(root->left);

previsit(root->right);

}

}

 

void midvisit(struct TreeNode* root)

{

if (root != NULL)

{

midvisit(root->left);

printf("%c\t", root->value);

midvisit(root->right);

}

}

 

void tailvisit(struct TreeNode* root)

{

if (root != NULL)

{

tailvisit(root->left);

tailvisit(root->right);

printf("%c\t", root->value);

}

}

 

int treedepth(struct TreeNode* root)

{

if (NULL == root)

{

return 0;

}

else

{

int depthleft = treedepth(root->left);

int depthright = treedepth(root->right);

return depthleft > depthright ? ++depthleft : ++depthright;

}

}

 

void printtree(struct TreeNode* root)

{

if (root != NULL)

{

printf("%c", root->value);

if (NULL != root->left || NULL != root->right)

{

printf("(");

if (NULL == root->left)

{

printf(" ");

}

else

{

printtree(root->left);

}

if (NULL == root->right)

{

printf(", ");

}

else

{

printf(",");

printtree(root->right);

}

printf(")");

}

}

}

 

void update(struct TreeNode* root, ElementType old_value, ElementType new_value)

{

if (NULL != root)

{

update(root->left, old_value, new_value);

if (root->value == old_value)

{

root->value = new_value;

}

update(root->right, old_value, new_value);

}

}

 

void levelvisit(struct TreeNode* root)

{

struct TreeNode* p;

int front = 0;

int rear = 0;

struct TreeNode* array[MAX];

if (root != NULL)

{

//根元素入队

array[rear] = root;

rear = (rear + 1) % MAX;

}

while (front != rear)

{

p = array[front];

front = (front + 1) % MAX;

printf("%c\t", p->value);

if (NULL != p->left)

{

array[rear] = p->left;

rear = (rear + 1) % MAX;

}

if (NULL != p->right)

{

array[rear] = p->right;

rear = (rear + 1) % MAX;

}

}

}

 

void cleartree(struct TreeNode** root)

{

if (NULL != *root)

{

cleartree(&((*root)->left));

cleartree(&((*root)->right));

free(*root);

*root = NULL;

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值