题目:

思路:

这题是比较典型的树的遍历问题,思路就是将中序遍历作为位置的判断依据,假设有个节点A和它的父亲Afa,那么如果A和Afa的顺序在中序遍历中是先A后Afa,则A是Afa的左儿子,否则是右儿子。

用for遍历一遍所有的节点,让每一个节点都连接到它的父亲,最后从根节点开始访问即可。

代码:

//
// main.cpp
// Tree
//
// Created by wasdns on 16/12/19.
// Copyright ? 2016年 wasdns. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct Node
{
int num; Node *lnext, *rnext;
}; int fa[10005]; //父亲 Node* node[100005]; //节点 int midorder[100005]; //中序 int preorder[100005]; //先序 int aftorder[100005]; //后序 int tot = 0; /*
Ininode函数:初始化各个节点
*/
void Ininode(int n)
{
for (int i = 1; i <= n; i++)
{
Node *p = new Node; p -> num = i;
p -> lnext = NULL;
p -> rnext = NULL; node[i] = p;
}
} /*
isleft函数:判断儿子是左儿子还是右儿子
*/
bool isleft(int n, int num, int f)
{
bool ans = true; for (int i = 1; i <= n; i++)
{
if (midorder[i] == num || midorder[i] == f)
{
if (midorder[i] == f) {
ans = false;
} break;
}
} return ans;
} /*
CreatTree:建树
*/
Node* CreatTree(int n)
{
int i; int fanum; for (i = 2; i <= n; i++)
{
fanum = fa[i]; if (isleft(n, i, fanum)) {
node[fanum] -> lnext = node[i];
} else {
node[fanum] -> rnext = node[i];
}
} return node[1];
} /*
CalPreorder函数:计算先序
*/
void CalPreorder(Node *p)
{
if (p -> lnext == NULL && p -> rnext == NULL) { preorder[tot++] = p -> num; return ;
} preorder[tot++] = p -> num; if (p -> lnext != NULL) CalPreorder(p -> lnext); if (p -> rnext != NULL) CalPreorder(p -> rnext);
} /*
CalAftorder函数:计算后序
*/
void CalAftorder(Node *p)
{
if (p -> lnext == NULL && p -> rnext == NULL) { aftorder[tot++] = p -> num; return ;
} if (p -> lnext != NULL) CalAftorder(p -> lnext); if (p -> rnext != NULL) CalAftorder(p -> rnext); aftorder[tot++] = p -> num;
} /*
PrintTree函数:中序遍历(queue思想)输出树
*/
void PrintTree(Node *head)
{
queue<Node*> q; q.push(head); Node *p; while (!q.empty())
{
p = q.front(); q.pop(); cout << p -> num << " "; if (p -> lnext != NULL) {
q.push(p -> lnext);
} if (p -> rnext != NULL) {
q.push(p -> rnext);
}
} cout << endl;
} /*
Print函数:输出结果
*/
void Print(int n)
{
int i; for (i = 0; i < n; i++) {
cout << preorder[i] << " ";
} cout << endl; for (i = 0; i < n; i++) {
cout << aftorder[i] << " ";
} cout << endl;
} int main()
{
int n; cin >> n; int i; for (i = 1; i <= n; i++) {
cin >> fa[i];
} for (i = 1; i <= n; i++) {
cin >> midorder[i];
} Ininode(n); Node* head; head = new Node; head = CreatTree(n); //PrintTree(head); CalPreorder(head); tot = 0; CalAftorder(head); Print(n); return 0;
}

2016/12/19

DS实验题 Order 已知父节点和中序遍历求前、后序的更多相关文章

  1. DS实验题 order

    算法与数据结构 实验题 6.4 order ★实验任务 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后序遍历. ★数据输入 输入第一行为一个正整数n表示二叉树的节点数目,节点编号从 ...

  2. DS实验题 融合软泥怪-2 Heap实现

    题目和STL实现:DS实验题 融合软泥怪-1 用堆实现优先队列 引言和堆的介绍摘自:Priority Queue(Heaps)--优先队列(堆) 引言: 优先队列是一个至少能够提供插入(Insert) ...

  3. DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储

    题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...

  4. HDU1710---树(知前序遍历与中序遍历 求后序遍历)

    知前序遍历与中序遍历 求后序遍历 #include<iostream> #include<cstring> #include<queue> #include< ...

  5. 数据结构实验之求二叉树后序遍历和层次遍历(SDUT 2137)

    Problem Description 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历. Input 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据. ...

  6. DS实验题 Dijkstra算法

    参考:Dijkstra算法 数据结构来到了图论这一章节,网络中的路由算法基本都和图论相关.于是在拿到DS的实验题的时候,决定看下久负盛名的Dijkstra算法. Dijkstra的经典应用是开放最短路 ...

  7. DS实验题 sights

    算法与数据结构实验题 6.3 sights ★实验任务 美丽的小风姑娘打算去旅游散心,她走进了一座山,发现这座山有 n 个景点, 由于山路难修,所以施工队只修了最少条的路,来保证 n 个景点联通,娇弱 ...

  8. DS实验题 PlayGame Kruskal(UnionFindSet)

    题目: 思路: 有两种做法,一种是Prim算法,另外一种则是我所使用的Kruskal算法,Kruskal的算法实现可以参考:最小生成树-Prim算法和Kruskal算法,讲的已经是十分清楚了. 具体算 ...

  9. PAT A1020——已知后序中序遍历求层序遍历

    1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...

随机推荐

  1. CSS3-样式继承,层叠管理,文本格式化

  2. 实现点击不同的按钮加载不同的css

    这段时间做一个小网站发现有时候特别需要点击不同的按钮去加载不同的css,这样可以确定点击的是哪个,由于每个按钮都是从后端数据库加载过来的,不仅是简简单单的用id或者是类名,用过this也不行: 前端加 ...

  3. http://blog.csdn.net/chenleixing/article/details/43740759

    http://blog.csdn.net/chenleixing/article/details/43740759

  4. MySql 查询数据库中所有表名

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='csdb' and table_type= ...

  5. HowTo:使用数据流读写消息

      本文主要演示使用TPL 数据流库从数据流块(dataflow block)读写消息. 提供了同步方法和异步方法. 主要使用BufferBlock,其既能作为message source,有能作为m ...

  6. CSS-布局【1】-图片在div中垂直居中

    方法一:通过增加100%高度行内块居中对齐 <!DOCTYPE html> <html> <head> <meta name="viewport&q ...

  7. 多级下拉菜单zz

    MultiLevelMultiSelectCombo (Silverlight) Tharindu Nishad Patikirikorala, 14 Aug 2013 CPOL 5.00 (1 vo ...

  8. 使用GDB 修改MySQL参数不重启

    link:http://blog.chinaunix.net/uid-20785090-id-4016315.html mysql很多参数都需要重启才能生效,有时候条件不允许,可以使用gdb作为最后的 ...

  9. 使用linux命令行配置无线网链接

    1. 需安装wpa_supplicant https://wiki.archlinux.org/index.php/WPA_Supplicant_%28%E7%AE%80%E4%BD%93%E4%B8 ...

  10. [转]基于gulp和webpack的前端工程化

    本文样例代码 :https://github.com/demohi/learning-gulp 本文主要简单介绍一下基于gulp和webpack的前端工程化. 技术栈 React.js reFlux ...