文章标题
来源:互联网 发布:阿里云 域名 cname 编辑:程序博客网 时间:2024/06/04 18:07
二叉树的相关知识点
1.四种遍历方法
2.更新树中的值
掌握重点:
1.对树要理解,以及其中的每个部分的意义
2.此学习代码是以二叉树为基础
3.理解前,中,后序遍历以及按层遍历,重点就是分清左右遍历与根节点遍历的先后顺序。
4.理解字符串(树的表示)中每个字符与树对应关系,这样才能用switch语句建立联系
5.遍历中使用了递归算法,要充分理解
6.知识点和链表有联系,注意两者间的联系
7.4和5代码红最重要的部分
include
include
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 free(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”);
free(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);
}
}
void free(struct TreeNode* root)
{
if (root != NULL)
{
tailvisit(root->left);
tailvisit(root->right);
free(root);
}
}
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; }}
}
这里写代码片
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 新的旅程!
- Lucen支持中文分词的分析器使用
- 1031: 判断点在第几象限
- 即时通讯框架T-io之WebSocket协议再之HelloWorld
- ODPS技术架构及应用实践
- 文章标题
- 内存缓冲区管理
- Linux 常用命令
- C++上机的微薄建议
- mac mysql-server 安装以及一些基本操作
- IP Header Checksum计算c实现
- Ubuntu 设置共享文件夹
- 使用PHP制作等比例缩放图片
- 【数据结构】线性表(Linear List)