当前位置: 首页 > 图文教程 > 开发语言 > VC++ > 根据前序和中序序列生成二叉树

VC++
宏的妙用
泛型编程与设计新思维
C++中的虚函数(一)
C++模板元编程
C++多态技术
通用结构复制函数
<C++实践系列>C++中的虚函数(virtual function)
<C++实践系列>C++中的引用(reference)
<C++实践系列>C++中的异常(exception)
<C++实践系列>C++中的模板(template)
构造函数中的this指针
串行化(Serialization)
二进制浏览、编辑的实现
介绍一个模板动态数组
VC++界面一揽子解决方案(第三版) 介绍
VC++通用GIS功能开发解决方案 2.0v 介绍
确定有穷自动机分析内核
委托、信号和消息反馈的模板实现技术
按照类型名称动态创建对象
Boost中应用的泛型编程技术

VC++ 中的 根据前序和中序序列生成二叉树


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 47 ::
收藏到网摘: n/a

根据前序和中序序列生成二叉树
作者:宋科
作者主页:kesongemini.diy.163.com

下载本文示例代码

一、前言:
我的一个同事拿来她老公的远程教育的考试题,叫大家帮着做,我写了这道,源码通过VC6编译链接,执行成功,呵呵;)题目就是给出了一个二叉树的前序序列(ABDCEGF)和中序序列(DBAEGCF),要求根据这两个输入完成一个二叉树,但没有指定用什么方式,所以我用了最笨拙的顺序存储二叉树,不过用了map来保存二叉树。题目还要求形成二叉树后可以遍历这个二叉树,由于三种遍历只是顺序不同,所以我只实现了后序遍历,何况输入就是前序和中序。;)本来应该用template做的,不过同事要求不要用复杂的C++语法,以免姐夫难以应对老师的提问,所以我只用了string来保存数据。本人是数学系毕业的,数据结构当时开课只学了几章,所以请朋友们不要笑话我。:> 我想这个程序唯一能够对您有帮助的地方就是提醒您不要将递归的函数做成inline的,希望大家干活儿的时候多注意这点儿。非常渴望朋友们和我联系email,批评我,让我进步,谢谢。:>

二、代码实现:
// BTree3.cpp : Defines the entry point for the console application.//// AUTHOR: Song Ke// EMAIL: [email protected]// HOMEPAGE: http://kesongemini.diy.163.com// QQ: 123588179// COMPANY: www.etonglian.com// AIM: programmed by SongKe 2002/12/16 night at home //	for Sister An Ning''s her husband''s exam-3://	known as preorder(ABDCEGF) and inorder(DBAEGCF) sequence//	to request for the btree and postorder sequence// STATUS: finished!// METHOD: the binary tree SongKe_BinaryTree with ordinal saving// TEST: succeeded by compile and link// PLATFORM: VC++6.0 + sp5 //	MS-STL NOT SGI-STL!//	at Windows2000 + sp3 // NOTE: DON''T WRITE THE RECURSION FUNCTION AS INLINE FUNCTION!#include "stdafx.h"#include <vector>#include <string>#include <iostream>#include <utility>#include <map>#include <algorithm>using namespace std;class SongKe_BinaryTree{public:	SongKe_BinaryTree() : _i_generate(0) {}	~SongKe_BinaryTree() {}	void pre_insert(const string& s) { _pre.push_back(s); }	void in_insert(const string& s) { _in.push_back(s); }	void pre_out()	{ _out("PREORDER : ", _pre); }	void in_out()	{ _out("INORDER : ", _in); }	void post_out();	// generate the binary tree	void generate();private:	// get left or right subtree for iteration	void _get_subtree(int iNode, vector< pair<string, int> >& v);	void _get_subtree(bool bLeft, int iNode, vector< pair<string, int> >& v);	void _postorder_iterate(const vector< pair<string, int> >& v);// postorder iterate	void _inorder_iterate(const vector< pair<string, int> >& v);	// using postorder iterate	void _preorder_iterate(const vector< pair<string, int> >& v);// using postorder iterate	int _i_generate; // point to current element of preorder	// mark the elements in inorders, and recurse the func in.	// bLeft == true or false is right	void _kesongemini(bool bLeft, int iNode, const vector<string>& v);	void _kesongemini(const string& node, int jj, bool bLeft, int iNode, const vector<string>& v);	// print out	void _out(const string& explain, const vector<string>& vec);	vector<string> _pre; // preorder sequence as known	vector<string> _in; // inorder sequence as known	vector<string> _post; // postorder sequence as request	vector< pair<string, int> > _s; // string, position as ordinal saving	map<int, string> _sm; // assistant for making subtree when postorder iterate	vector<int> _si;	// assistant for making subtree when postorder iterate};void SongKe_BinaryTree::_out(const string& explain, const vector<string>& vec){	cout << explain << "\t";	for(vector<string>::const_iterator i = vec.begin(); i != vec.end(); ++i)	{	cout << *i << " ";	}	cout << endl;}void SongKe_BinaryTr