|
发表于 2014-9-6 10:56:27
|
显示全部楼层
- #include <stdio.h>
- #include <stdlib.h>
- int m, n;
- typedef struct tree //define
- {
- char zhi ;
- struct tree *Lchild;
- struct tree *Rchild;
- }ErTree,*PErTree;
- /************************************************************************/
- /*
- 初始化树根节点
- */
- /************************************************************************/
- void InitTree(PErTree* root)
- {
- *root = (PErTree)malloc(sizeof(struct tree));
- (*root)->zhi = 0;
- (*root)->Lchild = NULL;
- (*root)->Rchild = NULL;
- }
- /************************************************************************/
- /*
- build a Tree
- */
- /************************************************************************/
- void CreateTree(ErTree** Tree)
- {
- char a ;
- fflush(stdin);
- scanf("%c",&a);
- if(a == '#')
- {
- return ;
- }
- else
- {
- (*Tree)->Lchild = (ErTree *)malloc(sizeof (struct tree));
- (*Tree)->Lchild->zhi = a;
- (*Tree)->Lchild->Lchild = NULL;
- (*Tree)->Lchild->Rchild = NULL;
- CreateTree(&((*Tree)->Lchild));
-
- fflush(stdin);
- scanf("%c",&a);
- if( a == '#')
- {
- return ;
- }
- (*Tree)->Rchild = (ErTree *)malloc(sizeof (struct tree));
- (*Tree)->Rchild->zhi = a;
- (*Tree)->Rchild->Lchild = NULL;
- (*Tree)->Rchild->Rchild = NULL;
- CreateTree(&((*Tree)->Rchild));
- }
- }
-
-
- int Deep( ErTree*Tree) //求深度
- {
- int lChildDeep,rChildDeep;
- if (NULL == Tree)
- return 0 ;
- else
- {
- lChildDeep = Deep(Tree->Lchild);
- rChildDeep = Deep(Tree->Rchild);
- return (lChildDeep > rChildDeep)?(lChildDeep+1):(rChildDeep+1);
- }
- }
-
-
-
- int main()
- {
- int c ;
- ErTree* Tree;
- InitTree(&Tree);
- CreateTree(&Tree);
- c =Deep(Tree);
- printf("%d",c);
-
- return 0 ;
- }
复制代码
你看一下。 |
|