📄 二叉树的生成与遍利.txt
字号:
二叉树生成与遍历
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
} BiTNode;
void CreateBiTree(BiTNode **T) /* 先序建立二叉树,空格结束子树/树*/
{
char ch;
printf("\n输入二叉树的结点数据域内容:");
ch=getch();
printf("%c",ch);
if(ch==' ')
{
*T=NULL;
}
else
{
*T=(BiTNode *)malloc(sizeof(BiTNode));
(*T)->data=ch;
CreateBiTree(&((*T)->lchild));
CreateBiTree(&((*T)->rchild));
}
return;
}
void DLR(BiTNode *T)/*根-左-右*/
{
if(T)
{
printf("%c ",T->data);
DLR(T->lchild);
DLR(T->rchild);
}
}
void LDR(BiTNode *T)/*左-根-右*/
{
if(T)
{
LDR(T->lchild);
printf("%c ",T->data);
LDR(T->rchild);
}
}
void LRD(BiTNode *T)/*左-右-根*/
{
if(T)
{
LRD(T->lchild);
LRD(T->rchild);
printf("%c ",T->data);
}
}
int main()
{
BiTNode *T;
CreateBiTree(&T);
printf("\n先根遍历次序:");
getch();
DLR(T);
printf("\n中根遍历次序:");
getch();
LDR(T);
printf("\n后根遍历次序:");
getch();
LRD(T);
system("pause");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -