- ********************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- /********************************************************/
- #define OK 1
- #define ERROR 0
- #define TRUE 1
- #define FALSE 0
- typedef char ElemType; //数据类型
- typedef int Status; //返回值类型
- /********************************************************/
- typedef struct BiTNode{
- ElemType data; //数据域
- struct BiTNode *lChild, *rChlid; //左右子树域
- }BiTNode, *BiTree;
- /********************************************************/
- //先序创建二叉树
- Status CreateBiTree(BiTree *T)
- {
- ElemType ch;
- ElemType temp;
- scanf("%c", &ch);
- temp = getchar();
- if (' ' == ch)
- *T = NULL;
- else
- {
- *T = (BiTree)malloc(sizeof(BiTNode));
- if (!(*T))
- exit(-1);
- (*T)->data = ch;
- printf("输入%c的左子节点:", ch);
- CreateBiTree(&(*T)->lChild);
- printf("输入%c的右子节点:", ch);
- CreateBiTree(&(*T)->rChlid);
- }
- return OK;
- }
- /********************************************************/
- //先序遍历二叉树
- void TraverseBiTree(BiTree T)
- {
- if (NULL == T)
- return ;
- printf("%c ", T->data);
- TraverseBiTree(T->lChild);
- TraverseBiTree(T->rChlid);
- }
- /********************************************************/
- //二叉树的输出 嵌套括号表示法
- void DispBiTree(BiTree T)
- {
- if (NULL != T)
- {
- printf("%c", T->data);
- if (T->lChild!=NULL || T->rChlid!=NULL)
- {
- printf("(");
- DispBiTree(T->lChild);
- if (NULL != T->rChlid)
- printf(",");
- DispBiTree(T->rChlid);
- printf(")");
- }
- }
- }
- /*********************************************************/
- //二叉树的输出 凹入表表示法 输出无法分辨左右子数
- //写的不好 第一次调用时必须给一个参数i 用来控制空格的数目
- void DispBiTree_into(BiTree T, int i)
- {
- int k = i;
- if (NULL != T)
- {
- putchar('\n');
- while (k)
- {
- putchar(' ');
- --k;
- }
- printf(" %c", T->data);
- DispBiTree_into(T->lChild, i+1);
- DispBiTree_into(T->rChlid, i+1);
- }
- }
- /********************************************************/
- //查找结点 如果找到就返回指向该结点的指针 否则返回NULL
- BiTree locate(BiTree T, ElemType e)
- {
- BiTree p;
- if (NULL == T)
- return NULL;
- else
- {
- if (e == T->data)
- return T;
- else
- p = locate(T->lChild, e);
- if (p)
- return p;
- else
- return locate(T->rChlid, e);
- }
- }
- /********************************************************/
- //统计树中结点的个数
- int numofnode(BiTree T)
- {
- if (NULL == T)
- return 0;
- else
- return (numofnode(T->lChild) + numofnode(T->rChlid) + 1);
- }
- /********************************************************/
- //判断二叉树是否等价
- Status isequal(BiTree T1, BiTree T2)
- {
- int t = 0;
- if (NULL==T1 && NULL==T2)
- t = 1;
- else
- {
- if (T1!=NULL && T2!=NULL) //如果两棵树都不为空
- if (T1->data == T2->data) //根节点相同
- if ( isequal(T1->lChild, T2->lChild) ) //如果左子树相同 就继续比较右子树
- t = isequal(T1->rChlid, T2->rChlid);
- }
- return t;
- }
- /********************************************************/
- //求二叉树的高度
- int depth(BiTree T)
- {
- int h, lh, rh;
- if (NULL == T)
- h = 0;
- else
- {
- lh = depth(T->lChild);
- rh = depth(T->rChlid);
- if (lh >= rh)
- h = lh+1;
- else
- h = rh+1;
- }
- return h;
- }
- /********************************************************/
- int main(void)
- {
- BiTree T;
- BiTree p;
- int i=0;
- CreateBiTree(&T);
- TraverseBiTree(T);
- DispBiTree(T);
- DispBiTree_into(T, i);
- p = locate(T, 'c');
- printf("\n\n%c\n", p->data);
- i = numofnode(T);
- printf("%d", i);
- return 0;
- }