資源簡介
構建二叉樹、輸出二叉樹、求樹深、復制二叉樹
前序遍歷

代碼片段和文件信息
/*
構建二叉樹、輸出二叉樹、求樹深、復制二叉樹?7
*/
#include
#include
typedef?int?DataType;
typedef?struct?BinTreeNode?*PBinTreeNode;?
typedef?struct?BinTreeNode{
????DataType?info;
????PBinTreeNode?lchild;
????PBinTreeNode?rchild;
}*BinTree;
int?hdepth;
BinTree?Create();???
void?print(BinTree?t);
int?Depth(BinTree?tint?hint?depth);
BinTree?GetBinTreeNode(DataType?infostruct?BinTreeNode?lptrstruct?BinTreeNode?rptr);
BinTree?CopyTree(BinTree?t);
int?main()
{
????h?=?1;
????depth?=?1;
????BinTree?tcopyt;
????t?=?Create();
????print(t);
????printf(“\n“);
????depth?=?Depth(thdepth);
????copyt?=?CopyTree(t);
????print(copyt);
????printf(“\n%d\n“depth);
????system(“PAUSE“);
????return?0;
}
BinTree?Create()
{
????BinTree?t;
????DataType?data;
????if(scanf(“%c“&data)?&&?data?==?‘#‘)
????????t?=?NULL;
????else{
????????t?=?(BinTree)malloc(sizeof(struct?BinTreeNode));
????????t->info?=?data;
????????t->lchild?=?Create();
????????t->rchild?=?Create();????????
????}
????return?t;
}
void?print(BinTree?t)
{
?????if(t){
???????????printf(“%c“t->info);
???????????print(t->lchild);
???????????print(t->rchild);
?????}
?????
}
int?Depth(BinTree?tint?hint?depth)
{
????int?ab;
????if(t){
????????if(h?>?depth)depth?=?h;
????????a?=?Depth(t->lchildh?+?1depth);
????????b?=?Depth(t->rchildh?+?1depth);
????????depth?=?(a?>?b)??a:b;
????}
????return?depth;
????
}
BinTree?GetBinTreeNode(DataType?infoPBinTreeNode?lptrPBinTreeNode?rptr)
{
?????????????BinTree?newtnode;
?????????????newtnode?=?(BinTree)malloc(sizeof(struct?BinTreeNode));
?????????????newtnode->info?=?info;
?????????????newtnode->lchild?=?lptr;
?????????????newtnode->rchild?=?rptr;
?????????????return?newtnode;
}
BinTree?CopyTree(BinTree?t)
{
????????BinTree?newtnode;
????????BinTree?newlptrnewrptr;
????????newlptr?=?(BinTree)malloc(sizeof(struct?BinTreeNode));
????????newrptr?=?(BinTree)malloc(sizeof(struct?BinTreeNode));????????
????????if(!t)return?NULL;
????????else{
?????????????if(t->lchild)newlptr?=?CopyTree(t->lchild);
?????????????else?newlptr?=?NULL;
?????????????if(t->rchild)newrptr?=?CopyTree(t->rchild);
?????????????else?newrptr?=?NULL;
?????????????newtnode?=?GetBinTreeNode(t->infonewlptrnewrptr);
?????????????return?newtnode;
????????}
????????
}
/*
Inout
abdh###e##cf##g##
*/
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2427??2009-02-05?21:22??scanf?&?depth?(2).cpp
?????文件??????16801??2009-02-05?17:04??scanf?&?depth?(2).exe
-----------?---------??----------?-----??----
????????????????19228????????????????????2
評論
共有 條評論