二叉树
树是一种多对一的树状结构。
1.二叉树的建立:
#include<stdio.h>
#include<malloc.h>
struct tree
{
char data;
struct tree *lchild,*rchild;//左孩子指针和右孩子指针
} node;
这是树的大致结构。左右指针域指向左右两个孩子的值。
void createtree(struct tree **P)//传递的指针要在此过程中发生改变
{
char ch;
scanf("%c",&ch);
if(ch=='#')
(*P)=NULL;
else
{
(*P)=(struct tree*)malloc(sizeof(node));
(*P)->data=ch;
createtree(&(*P)->lchild);//先序建立,先坐后右
createtree(&(*P)->rchild);
}
}
变量传递如果要更改那个变量的话要将它的指针传递到下一个函数,对于指针也是一样的,所以上面代码的传递就有了**P这样的存在。
2.二叉树的遍历