//混用了一下c和c++(仅输入输出混用了一下,其他没有)
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
#define MaxSize 50
//树
typedef char DataType;
typedef struct tnode {
DataType data;
struct tnode* lchild, * rchild;
}BT, * BTT;
// 函数声明
BT* Create_(); // 先序创建二叉树
void cen_bianli_(BT* T); //层次遍历二叉树
void visit_(BT* p); //输出函数
void visit2(BT* T); //中序遍历二叉树
//main函数
int main()
{
BT* T;
T = Create_();
//Create_(T);
printf("中序遍历:");
visit2(T);
printf("层次遍历:");
cen_bianli_(T);
return 0;
}
//中序遍历
void visit2(BT* T)
{
//printf("1");
if (T != NULL)
{
visit2(T->lchild);
printf("%c ", T->data);//cout << T->data << " ";
visit2(T->rchild);
}
}
//先序创建二叉树
BT* Create_()
{
BT* t;
char ch;
cin >> ch;//scanf("%c", &ch);
getchar();
if (ch == '#')
{
t = NULL;
}
else
{
t = (BT*)malloc(s
c语言实现层次遍历二叉树完整代码
于 2022-07-09 09:39:15 首次发布