二叉树实现源代码
来源:互联网 发布:阿里云ces 编辑:程序博客网 时间:2024/05/17 09:03
二叉树实现源代码如下:
#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("/nPreOrder:");
PreOrderTraval(pRoot);
printf("/n");
printf("/nInOrder:");
InOrderTraval(pRoot);
printf("/n");
printf("/nPostOrder:");
PostOrderTraval(pRoot);
printf("/n");
printf("/nShowLeaves:");
ShowLeaves(pRoot);
printf("/n-----------------------/n");
printf("/n");
Display(pRoot,0);
printf("/n");
printf("/nDeleting Tree:/n");
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<Level-1;i++)
{
printf(" ");
}
if(Level>=1)
{
printf("--");
}
printf("%c/n",pTree->Data);
Display(pTree->rChild,Level+1);
}
status ShowLeaves(BiNode* pTree)
{
if(pTree)
{
if(ShowLeaves(pTree->lChild))
{
if(ShowLeaves(pTree->rChild))
{
if((pTree->lChild==NULL)&&(pTree->rChild==NULL))
{
if(!Visit(pTree->Data))
{
return ERROR;
}
}
return OK;
}
}
return ERROR;
}
else
{
return OK;
}
}
status DelTree(BiNode* pTree)
{
if(pTree)
{
if(DelTree(pTree->lChild))
{
if(DelTree(pTree->rChild))
{
printf("Deleting %c/n",pTree->Data);
free((void*)pTree);
return OK;
}
}
return ERROR;
}
else
{
return OK;
}
}
- 二叉树实现源代码
- 搜索二叉树基本功能实现(源代码)
- 二叉树源代码
- 二叉树的C++模板类头文件源代码实现
- 二叉树层次遍历源代码
- 不用栈实现二叉树非递归中序遍历源代码说明
- 如何在网络中 传输二叉树森林 (C++源代码实现)
- 单词吸血鬼源代码 二叉树操作
- 单词吸血鬼源代码 二叉树操作
- 单词吸血鬼源代码 二叉树操作
- 表达式转二叉树的C++源代码
- 二叉树的各种操作_源代码
- 二叉树的基本操作C源代码
- JAVA 实现二叉树
- 二叉树 java实现
- 二叉查找树实现
- 二叉树实现-java
- 二叉树的实现
- 深度探索C++对象模型(6)
- 深度探索C++对象模型(7)
- Unicode:宽字节字符集
- 有关遗传算法
- 计算机编程的24条法则
- 二叉树实现源代码
- 霍夫曼树编码的实现
- 排序算法比较程序
- 针对学校出现猪流感而做的一个简单的小软件(更新版)
- 字符串近似匹配算法
- 多边形扫描转换算法
- 大数的阶乘算法
- C++中的内存划分
- C++计算四则表达式的模板