龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

二叉树实现源代码

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
二叉树实现源代码如下: #include #include #include #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OVERFLOW -2 typedef int status; typedef strUCt BiNode { char Data; struct BiNode* lChild; struct BiNode* rChild

  二叉树实现源代码如下:

  

  

#include

  

#include

  

#include

  

  

#define OK 1

  

#define ERROR 0

  

#define TRUE 1

  

#define FALSE 0

  

#define OVERFLOW -2

  

typedef int status;

  

  

typedef strUCt BiNode

  

{

  

char Data;

  

struct BiNode* lChild;

  

struct BiNode* rChild;

  

}BiNode,*pBiNode;

  

  

status CreateTree(BiNode** pTree);

  

status PreOrderTraval(BiNode* pTree);

  

status Visit(char Data);

  

status Display(BiNode* pTree,int Level);

  

status Clear(BiNode* pTree);

  

  

BiNode *pRoot=NULL;

  

  

main()

  

{

  

clrscr();

  

CreateTree(&pRoot);

  

  

printf("

PreOrder:");

  

PreOrderTraval(pRoot);

  

printf("

");

  

  

printf("

InOrder:");

  

InOrderTraval(pRoot);

  

printf("

");

  

  

printf("

PostOrder:");

  

PostOrderTraval(pRoot);

  

printf("

");

  

  

printf("

ShowLeaves:");

  

ShowLeaves(pRoot);

  

printf("

-----------------------

");

  

printf("

");

  

  

Display(pRoot,0);

  

  

printf("

");

  

printf("

Deleting Tree:

");

  

DelTree(pRoot);

  

printf("BiTree Deleted.");

  

  

getch();

  

}

  

status CreateTree(BiNode** pTree) /*Input Example: abd##e##cf##g##*/

  

{

  

char ch;

  

scanf("%c",&ch);

  

if(ch==‘#‘)

  

{

  

(*pTree)=NULL;

  

}

  

else

  

{

  

if(!((*pTree)=(BiNode*)malloc(sizeof(BiNode))))

  

{

  

exit(OVERFLOW);

  

}

  

  

(*pTree)->Data=ch;

  

CreateTree(&((*pTree)->lChild));

  

CreateTree(&((*pTree)->rChild));

  

}

  

return OK;

  

}

  

status PreOrderTraval(BiNode* pTree)

  

{

  

if(pTree)

  

{

  

if(Visit(pTree->Data))

  

{

  

if(PreOrderTraval(pTree->lChild))

  

{

  

if(PreOrderTraval(pTree->rChild))

  

{

  

return OK;

  

}

  

}

  

}

  

return ERROR;

  

}

  

else

  

{

  

return OK;

  

}

  

}

  

status InOrderTraval(BiNode* pTree)

  

{

  

if(pTree)

  

{

  

if(InOrderTraval(pTree->lChild))

  

{

  

if(Visit(pTree->Data))

  

{

  

if(InOrderTraval(pTree->rChild))

  

{

  

return OK;

  

}

  

}

  

return ERROR;

  

}

  

return ERROR;

  

  

}

  

else

  

{

  

return OK;

  

}

  

}

  

status PostOrderTraval(BiNode* pTree)

  

{

  

if(pTree)

  

{

  

if(PostOrderTraval(pTree->lChild))

  

{

  

if(PostOrderTraval(pTree->rChild))

  

{

  

if(Visit(pTree->Data))

  

{

  

return OK;

  

}

  

return ERROR;

  

}

  

}

  

return ERROR;

  

}

  

else

  

{

  

return OK;

  

}

  

}

  

status Visit(char Data)

  

{

  

printf("%c",Data);

  

return OK;

  

}

  

status Display(BiNode* pTree,int Level)

  

{

  

int i;

  

if(pTree==NULL) return;

  

Display(pTree->lChild,Level+1);

  

for(i=0;i

  

{

  

printf(" ");

  

精彩图集

赞助商链接