首页 > 编程 > Java > 正文

二叉树实现源代码

2019-09-06 23:33:34
字体:
来源:转载
供稿:网友

                    二叉树实现源代码如下:

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

#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==‘#‘)
   {
/t(*pTree)=NULL;
   }
   else
   {
/tif(!((*pTree)=(BiNode*)malloc(sizeof(BiNode))))
/t{
/t    exit(OVERFLOW);
/t}
/t(*pTree)->Data=ch;
/tCreateTree(&((*pTree)->lChild));
/tCreateTree(&((*pTree)->rChild));
   }
return OK;
}
status PreOrderTraval(BiNode* pTree)
{
   if(pTree)
   {
/tif(Visit(pTree->Data))
/t{
/t    if(PreOrderTraval(pTree->lChild))
/t    {
/t/tif(PreOrderTraval(pTree->rChild))
/t/t{
/t/t    return OK;
/t/t}
/t    }
/t}
/treturn ERROR;
   }
   else
   {
/treturn OK;
   }
}
status InOrderTraval(BiNode* pTree)
{
   if(pTree)
   {
/tif(InOrderTraval(pTree->lChild))
/t{
/t    if(Visit(pTree->Data))
/t    {
/t/tif(InOrderTraval(pTree->rChild))
/t/t{
/t/t    return OK;
/t/t}
/t    }
/t    return ERROR;
/t}
/treturn ERROR;
   }
   else
   {
/treturn OK;
   }
}
status PostOrderTraval(BiNode* pTree)
{
   if(pTree)
   {
/tif(PostOrderTraval(pTree->lChild))
/t{
/t    if(PostOrderTraval(pTree->rChild))
/t    {
/t/tif(Visit(pTree->Data))
/t/t{
/t/t    return OK;
/t/t}
/t/treturn ERROR;
/t    }
/t}
/treturn ERROR;
   }
   else
   {
/treturn 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<Level-1;i++)
   {
/tprintf(" ");
   }
   if(Level>=1)
   {
/tprintf("--");
   }
   printf("%c",pTree->Data);
   Display(pTree->rChild,Level+1);
}
status ShowLeaves(BiNode* pTree)
{
   if(pTree)
   {
/tif(ShowLeaves(pTree->lChild))
/t{
/t    if(ShowLeaves(pTree->rChild))
/t    {
/t/tif((pTree->lChild==NULL)&&(pTree->rChild==NULL))
/t/t{
/t/t    if(!Visit(pTree->Data))
/t/t    {
/t/t/treturn ERROR;
/t/t    }
/t/t}
/t/treturn OK;
/t    }
/t}
/treturn ERROR;
   }
   else
   {
/treturn OK;
   }
}
status DelTree(BiNode* pTree)
{
   if(pTree)
   {
/tif(DelTree(pTree->lChild))
/t{
/t    if(DelTree(pTree->rChild))
/t    {
/t/tprintf("Deleting %c",pTree->Data);
/t/tfree((void*)pTree);
/t/treturn OK;
/t    }
/t}
/treturn ERROR;
   }
   else
   {
/treturn OK;
   }
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选