数组二叉树的遍历

一棵结点总数为N的二又树用[val,left,right]表示,结点编号从1-N,其中val=1代表整棵树的根结点,l代表va的左子树根结点编号,r代表val的右子树根结点编号,若编号为0,代表NULL。例如:

3
123
200
300

上述数据表示该二又树由3个结点构成,1为根结点,结点1的左子树结点为2,右子树结点为3,要求分别对该二叉树进行前序、中序、后序遍历,并分别输出结果。
二叉树结点类型定义
typedef struct node {
int val
int left
int right
}Node;

Code:

using namespace std;
#include <algorithm>
#include<cstring>
#include <iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<iomanip>
#include<sstream>
#include<vector>;
#include<stack>
void show(const vector<vector<int> >& v)
{
cout << "----show----" << endl;
for (vector<int>a : v)
{
for (int b : a)cout << b << " ";
cout << endl;
}
cout << endl;
}
void p_travel(const vector<vector<int> >& v)
{
stack<int>st;
st.push(v[1][0]);
while (!st.empty())
{
int t = st.top(); st.pop();
cout << t << " ";
if(v[t][2])st.push(v[t][2]); if(v[t][1])st.push(v[t][1]);
}
}
void m_travel(const vector<vector<int> >& v,int t)//中序遍历 左根右
{
if (v[t][1])m_travel(v, v[t][1]);
cout << t << " ";
if (v[t][2])m_travel(v, v[t][2]);
}
void l_travel(const vector<vector<int> >& v,int t)//后续遍历 左右根
{
if (v[t][1])l_travel(v, v[t][1]);
if (v[t][2])l_travel(v, v[t][2]);
cout << t << " ";
}
int main()
{
int n, a, b, c; cin >> n;
vector<vector<int> >v(n + 1, vector<int>(3, 0));
cout << "n = " << n << endl;
for (int i = 1; i < n+1; i++)
{
cin >> a >> b >> c;
v[i][0] = a; v[i][1] = b; v[i][2] = c;
}
cout << "--------停止输入-----------" << endl;
//show(v);
p_travel(v);
cout << endl << "---------" << endl;
m_travel(v,1);
cout << endl << "---------" << endl;
l_travel(v,1);
cout << endl << "---------" << endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
二叉树是一种常见的数据结构遍历二叉树有多种方法,包括先序遍历、中序遍历和后序遍历。以下是在C语言中用数组表示二叉树并进行遍历的示例代码: ```c #include <stdio.h> // 定义二叉树节点结构 struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; }; // 构建二叉树 struct TreeNode nodes[10]; // 使用数组表示二叉树 void buildTree() { for (int i = 0; i < 10; i++) { nodes[i].data = i; nodes[i].left = NULL; nodes[i].right = NULL; } nodes[0].left = &nodes[1]; nodes[0].right = &nodes[2]; nodes[1].left = &nodes[3]; nodes[1].right = &nodes[4]; nodes[2].left = &nodes[5]; nodes[2].right = &nodes[6]; } // 先序遍历 void preOrder(struct TreeNode *root) { if (root != NULL) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } // 中序遍历 void inOrder(struct TreeNode *root) { if (root != NULL) { inOrder(root->left); printf("%d ", root->data); inOrder(root->right); } } // 后序遍历 void postOrder(struct TreeNode *root) { if (root != NULL) { postOrder(root->left); postOrder(root->right); printf("%d ", root->data); } } int main() { buildTree(); printf("先序遍历结果:"); preOrder(&nodes[0]); printf("\n中序遍历结果:"); inOrder(&nodes[0]); printf("\n后序遍历结果:"); postOrder(&nodes[0]); return 0; } ``` 以上代码定义了一个二叉树的结构,使用数组表示节点,并实现了先序、中序和后序遍历的方法。在主函数中构建了一个二叉树,并进行了遍历操作,打印出遍历的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微__凉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值