六、树和二叉树--(3)已知先序遍历和中序遍历求后序遍历
2016-05-02 22:23
477 查看
摘自计蒜客:http://www.jisuanke.com/course/35/1397
算法过程如下:在先序遍历中知道根结点的编号,在中序遍历中找到根结点所在位置,那么位置前面的结点就是根结点的左
子树上的结点,位置后面的结点就是右子树上的结点。按照以上方法递归建立起一个二叉树,最后调用二叉树的后序遍历函
数,输出后序遍历。
#include<iostream>
#include<string>
using namespace std;
class Node {
public:
int data;
Node *lchild, *rchild;
Node(int _data) {
data = _data;
lchild = NULL;
rchild = NULL;
}
~Node() {
if (lchild != NULL) {
delete lchild;
}
if (rchild != NULL) {
delete rchild;
}
}
void postorder() {
if (lchild != NULL) {
lchild->postorder();
}
if (rchild != NULL) {
rchild->postorder();
}
cout << data << " ";
}
Node *build(const string &pre_str, const string &in_str, int len) {
Node *p = new Node(pre_str[0]-'0');
int pos = in_str.find(pre_str[0]);
if (pos > 0) {
p->lchild = build(pre_str.substr(1, pos), in_str.substr(0, pos), pos);
}
if (len-pos-1 > 0) {
p->rchild = build(pre_str.substr(pos+1), in_str.substr(pos+1), len-pos-1);
}
return p;
}
};
class BinaryTree {
private:
Node *root;
public:
BinaryTree() {
root = NULL;
}
~BinaryTree() {
if (root != NULL) {
delete root;
}
}
BinaryTree(const string &pre_str, const string &in_str, int len) {
root = root->build(pre_str, in_str, len);
}
void postorder() {
root->postorder();
}
};
int main() {
string pre_str = "136945827";
string in_str = "963548127";
BinaryTree binarytree(pre_str, in_str, in_str.length());
binarytree.postorder();
cout << endl;
return 0;
}
算法过程如下:在先序遍历中知道根结点的编号,在中序遍历中找到根结点所在位置,那么位置前面的结点就是根结点的左
子树上的结点,位置后面的结点就是右子树上的结点。按照以上方法递归建立起一个二叉树,最后调用二叉树的后序遍历函
数,输出后序遍历。
#include<iostream>
#include<string>
using namespace std;
class Node {
public:
int data;
Node *lchild, *rchild;
Node(int _data) {
data = _data;
lchild = NULL;
rchild = NULL;
}
~Node() {
if (lchild != NULL) {
delete lchild;
}
if (rchild != NULL) {
delete rchild;
}
}
void postorder() {
if (lchild != NULL) {
lchild->postorder();
}
if (rchild != NULL) {
rchild->postorder();
}
cout << data << " ";
}
Node *build(const string &pre_str, const string &in_str, int len) {
Node *p = new Node(pre_str[0]-'0');
int pos = in_str.find(pre_str[0]);
if (pos > 0) {
p->lchild = build(pre_str.substr(1, pos), in_str.substr(0, pos), pos);
}
if (len-pos-1 > 0) {
p->rchild = build(pre_str.substr(pos+1), in_str.substr(pos+1), len-pos-1);
}
return p;
}
};
class BinaryTree {
private:
Node *root;
public:
BinaryTree() {
root = NULL;
}
~BinaryTree() {
if (root != NULL) {
delete root;
}
}
BinaryTree(const string &pre_str, const string &in_str, int len) {
root = root->build(pre_str, in_str, len);
}
void postorder() {
root->postorder();
}
};
int main() {
string pre_str = "136945827";
string in_str = "963548127";
BinaryTree binarytree(pre_str, in_str, in_str.length());
binarytree.postorder();
cout << endl;
return 0;
}
相关文章推荐
- 简述Bootstrap栅格布局方式
- 【Python】装饰器理解
- 引用传递和值传递
- AdaBoost
- jdbc的使用
- 搞定immutable.js详细说明
- Docker的安装和镜像创建
- STM32控制IO基本配置的讲解
- 将输入的若干整数逆序建立一单链表
- java--IO 2
- Android之Activity
- BestCoder Round #82 (div.2) A B
- 5-33 地下迷宫探索 (30分)
- Hadoop MapReduce概念学习系列之在MapReduce编程时,三大接口抉择(十六)
- arm linux start_kernel之前页表初始化
- Collection和Collections的区别
- Linux之DRBA搭建与部署
- Java笔记---Hadoop 2.7.1下WordCount程序详解
- springMVC(10)------springMVC重定向
- Mongoose(三):数据类型