#include <iostream>
#include <queue>
using namespace std;
const int N = 10010;
const int INF = -1;
struct Node {
int w;
int p;
int l;
int r;
};
Node node[N];
void input(int n)
{
cin >> node[n].w;
if (node[n].w == INF)
{
return;
}
node[n].p = n / 2;
node[n].l = n * 2;
node[n].r = n * 2 + 1;
input(n * 2);
input(n * 2 + 1);
}
void preOrderParse(int n)
{
if (node[n].w == INF)
{
return;
}
cout << node[n].w << " ";
preOrderParse(node[n].l);
preOrderParse(node[n].r);
}
void inOrderParse(int n)
{
if (node[n].w == INF)
{
return;
}
inOrderParse(n * 2);
cout << node[n].w << " ";
inOrderParse(n * 2 + 1);
}
void postOrderParse(int n)
{
if (node[n].w == INF)
{
return;
}
postOrderParse(n * 2);
postOrderParse(n * 2 + 1);
cout << node[n].w << " ";
}
void sequenceParse()
{
queue<int> que;
int n = 1;
que.push(1);
while (!que.empty())
{
n = que.front();
que.pop();
if (node[n].w != INF)
{
cout << node[n].w << " ";
que.push(node[n].l);
que.push(node[n].r);
}
}
}
int main()
{
cout << "请以前序遍历的顺序输入二叉树,空节点输入 -1 :" << endl;
input(1); // 从下标为 1 开始前序输入二叉树
cout << "前序遍历:" << endl;
preOrderParse(1);
cout << endl << "中序遍历:" << endl;
inOrderParse(1);
cout << endl << "后序遍历:" << endl;
postOrderParse(1);
cout << endl << "层序遍历:" << endl;
sequenceParse();
return 0;
}
二叉树遍历C++
最新推荐文章于 2023-09-02 16:48:31 发布