// new.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <MEMORY.H>
#include <STRING.H>
#include <STDLIB.H>
#include <WINDOWS.H>
/*
先序遍历 DLR
中序遍历 LDR
后序遍历 LRD
*/
typedef struct Node{
char data;
struct Node* lChild,*rChild;
}Tree,*PTree;
void CreateNode(struct Node** pNode)
{
//以空格作为树的结束
char ch;
printf("请输入节点:");
scanf("%c",&ch);
getchar();
if(ch == ' ')
{
pNode = NULL;
return;
}
else
{
*pNode = (PTree)malloc(sizeof(struct Node));
memset(*pNode,0,sizeof(struct Node));
(*pNode)->data = ch;
CreateNode(&((*pNode)->lChild));
CreateNode(&((*pNode)->rChild));
}
}
//先序遍历
void prevSearch(PTree* pNo)
{
PTree PNode = *pNo;
if(PNode)
{
printf("%c\t",PNode->data);
prevSearch(&(PNode->lChild));
prevSearch(&(PNode->rChild));
}
}
//中序遍历
void CenterSearch(PTree* pNo)
{
PTree PNode = *pNo;
if(PNode)
{
CenterSearch(&(PNode->lChild));
printf("%c\t",PNode->data);
CenterSearch(&(PNode->rChild));
}
}
// 后序遍历
void postSeaech(PTree* pNo)
{
PTree pNode = *pNo;
if(pNode)
{
postSeaech(&(pNode->lChild));
postSeaech(&(pNode->rChild));
printf("%c\t",pNode->data);
}
}
void Destory(PTree* pNo)
{
PTree pNode = *pNo;
if(pNode)
{
Destory(&(pNode->lChild));
Destory(&(pNode->rChild));
free(pNode);
}
}
bool Menu(PTree* root)
{
char Option;
printf("A . 创造树\n");
printf("B . 先序遍历\n");
printf("C . 中序遍历\n");
printf("D . 后续遍历\n");
printf("E . 退出\n");
printf("请输入你的选项:");
scanf("%c",&Option);
getchar();
switch(Option)
{
case 'A': CreateNode(root);
return true;
case 'B':
if(!root){printf("该树是空的,请先创建树"); break;}
prevSearch(root);
return true;
case 'C':
if(!root){printf("该树是空的,请先创建树"); break;}
CenterSearch(root);
return true;
case 'D':
if(!root){printf("该树是空的,请先创建树"); break;}
postSeaech(root);
return true;
case 'E':
printf("程序正在结束!");
Destory(root);
printf("程序已经结束!");
return false;
}
return true;
}
int main(int argc, char* argv[])
{
PTree root = NULL;
bool flag = true;
while(flag)
{
flag=Menu(&root);
}
return 0;
}
//
#include "stdafx.h"
#include <MEMORY.H>
#include <STRING.H>
#include <STDLIB.H>
#include <WINDOWS.H>
/*
先序遍历 DLR
中序遍历 LDR
后序遍历 LRD
*/
typedef struct Node{
char data;
struct Node* lChild,*rChild;
}Tree,*PTree;
void CreateNode(struct Node** pNode)
{
//以空格作为树的结束
char ch;
printf("请输入节点:");
scanf("%c",&ch);
getchar();
if(ch == ' ')
{
pNode = NULL;
return;
}
else
{
*pNode = (PTree)malloc(sizeof(struct Node));
memset(*pNode,0,sizeof(struct Node));
(*pNode)->data = ch;
CreateNode(&((*pNode)->lChild));
CreateNode(&((*pNode)->rChild));
}
}
//先序遍历
void prevSearch(PTree* pNo)
{
PTree PNode = *pNo;
if(PNode)
{
printf("%c\t",PNode->data);
prevSearch(&(PNode->lChild));
prevSearch(&(PNode->rChild));
}
}
//中序遍历
void CenterSearch(PTree* pNo)
{
PTree PNode = *pNo;
if(PNode)
{
CenterSearch(&(PNode->lChild));
printf("%c\t",PNode->data);
CenterSearch(&(PNode->rChild));
}
}
// 后序遍历
void postSeaech(PTree* pNo)
{
PTree pNode = *pNo;
if(pNode)
{
postSeaech(&(pNode->lChild));
postSeaech(&(pNode->rChild));
printf("%c\t",pNode->data);
}
}
void Destory(PTree* pNo)
{
PTree pNode = *pNo;
if(pNode)
{
Destory(&(pNode->lChild));
Destory(&(pNode->rChild));
free(pNode);
}
}
bool Menu(PTree* root)
{
char Option;
printf("A . 创造树\n");
printf("B . 先序遍历\n");
printf("C . 中序遍历\n");
printf("D . 后续遍历\n");
printf("E . 退出\n");
printf("请输入你的选项:");
scanf("%c",&Option);
getchar();
switch(Option)
{
case 'A': CreateNode(root);
return true;
case 'B':
if(!root){printf("该树是空的,请先创建树"); break;}
prevSearch(root);
return true;
case 'C':
if(!root){printf("该树是空的,请先创建树"); break;}
CenterSearch(root);
return true;
case 'D':
if(!root){printf("该树是空的,请先创建树"); break;}
postSeaech(root);
return true;
case 'E':
printf("程序正在结束!");
Destory(root);
printf("程序已经结束!");
return false;
}
return true;
}
int main(int argc, char* argv[])
{
PTree root = NULL;
bool flag = true;
while(flag)
{
flag=Menu(&root);
}
return 0;
}