#include <iostream> |
using namespace std; |
typedef char TElemType; |
typedef struct TNode |
{ |
TElemType data; |
TNode*parent,*fc,*ns; |
}*tree; |
void treelists(tree T); //树的广义表形式输出 |
int degereeTree(tree T); //树的深度 |
void CreateTree(tree &T, char s[], int &i); |
void CreateTree(tree &T, char s[]); |
int main() |
{ |
tree T; |
char s[]= "AB#C###" ; |
CreateTree(T,s); |
cout<< "该树的广义表形式输出为:" <<endl; |
treelists(T); |
cout<<endl<< "该树的深度为" <<degereeTree(T)<<endl; |
return 0; |
} |
void treelists(tree T) |
{ |
tree p; |
if (!T){cout<< '#' ; return ;} |
cout<<T->data; |
p=T->fc; if (!p) return ; |
cout<< '(' ; |
while (p) |
{ |
treelists(p);p=p->ns; |
if (p) cout<< ',' ; |
} |
cout<< ')' ; |
} |
void CreateTree(tree &T, char s[], int &i) |
{ |
i++; |
if (s[i]== '#' ){T=NULL; return ;} |
T= new TNode; T->data=s[i]; |
CreateTree(T->fc,s,i); |
CreateTree(T->ns,s,i); |
} |
void CreateTree(tree &T, char s[]) |
{ |
int i=-1; |
CreateTree(T,s,i); |
} |
int degereeTree(tree T) |
{ |
if (!T) return 0; |
int x,y; |
x=degereeTree(T->fc)+1; |
y=degereeTree(T->ns); |
return x>y?x:y; |
} |