国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

二叉樹實(shí)現(xiàn)源代碼

2019-09-06 23:33:34
字體:
供稿:網(wǎng)友

                    二叉樹實(shí)現(xiàn)源代碼如下:

#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;
   }
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 平顶山市| 鸡西市| 禄丰县| 呼伦贝尔市| 额济纳旗| 娱乐| 精河县| 湘阴县| 中宁县| 纳雍县| 枣强县| 襄城县| 安化县| 凌云县| 南靖县| 广饶县| 灵宝市| 务川| 阿瓦提县| 大厂| 湟中县| 平果县| 富川| 利川市| 满洲里市| 鄯善县| 温宿县| 镇宁| 乌兰县| 澎湖县| 永嘉县| 中山市| 敦化市| 大名县| 开鲁县| 家居| 类乌齐县| 乐山市| 巴马| 遂川县| 墨玉县|