二叉树重建及遍历
2015-09-17 03:06
507 查看
树是一种在实际编程中经常遇到的数据结构。它的逻辑很简单:除了根结点之外每个结点只有一个父结点,根节点没有父结点;除了叶结点之外所有结点都有一个或多个子结点,叶结点没有子结点。
二叉树的遍历方式:
前序遍历:先访问根结点,再访问左子结点,最后访问右子结点。
中序遍历:先访问左子结点,在访问根结点,最后访问右子结点。
后序遍历:先访问左子结点,再访问右子结点,最后访问根结点。
题目:输入某二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
// 重建二叉树.cpp : 定义控制台应用程序的入口点。
//输入某二叉树的前序遍历和中序遍历的结果,重建出该二叉树,
//假设输入的前序遍历和中序遍历的结果中不含重复的数字
#include "stdafx.h"
#include<iostream>
using namespace std;
typedef int ElemType;
struct BinaryTreeNode
{
ElemType m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode* ConstructCore(int* startpreorder, int* endpreorder, int* startinorder, int* endinorder)
{
int rootValue = startpreorder[0];
BinaryTreeNode* root = new BinaryTreeNode();
root->m_nValue = rootValue;
root->m_pLeft = NULL;
root->m_pRight = NULL;
if (startpreorder == endpreorder)
{
if (startinorder == endinorder && *startpreorder == *startinorder)
{
return root;
}
else
{
throw exception("Invalid input.");
}
}
int* rooInorder = startinorder;
while (rooInorder <= endinorder&&*rooInorder != rootValue)
{
++rooInorder;
}
if (rooInorder == endinorder && *rooInorder != rootValue)
{
throw exception("Invalid input.");
}
int leftLength = rooInorder - startinorder;
int* leftPreorderEnd = startpreorder + leftLength;
if (leftLength > 0)
{
//构建左子树
root->m_pLeft = ConstructCore(startpreorder + 1, leftPreorderEnd, startinorder, rooInorder - 1);
}
if (leftLength < endpreorder - startpreorder)
{
//构建右子树
root->m_pRight = ConstructCore(leftPreorderEnd + 1, endpreorder, rooInorder + 1, endinorder);
}
return root;
}
BinaryTreeNode* Construct(int* preorder, int* inorder, int length)
{
if (preorder == NULL || inorder == NULL || length <= 0)
{
return NULL;
}
return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
}
//前序遍历
void PreTraverseBinaryTree(BinaryTreeNode* root)
{
if (root == NULL)
return;
cout << root->m_nValue << "\t";
PreTraverseBinaryTree(root->m_pLeft);
PreTraverseBinaryTree(root->m_pRight);
}
//中序遍历
void InTraverseBinaryTree(BinaryTreeNode* root)
{
if (root == NULL)
{
return;
}
InTraverseBinaryTree(root->m_pLeft);
cout << root->m_nValue << "\t";
InTraverseBinaryTree(root->m_pRight);
}
int _tmain(int argc, _TCHAR* argv[])
{
int PreOrder[8] = { 1, 2, 4, 7, 3, 5, 6, 8 };
int InOrder[8] = { 4, 7, 2, 1, 5, 3, 8, 6 };
BinaryTreeNode* Root;
Root = Construct(PreOrder, InOrder, 8);
PreTraverseBinaryTree(Root);
cout << endl;
InTraverseBinaryTree(Root);
cout << endl;
return 0;
}
运行结果:
二叉树的遍历方式:
前序遍历:先访问根结点,再访问左子结点,最后访问右子结点。
中序遍历:先访问左子结点,在访问根结点,最后访问右子结点。
后序遍历:先访问左子结点,再访问右子结点,最后访问根结点。
题目:输入某二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
// 重建二叉树.cpp : 定义控制台应用程序的入口点。
//输入某二叉树的前序遍历和中序遍历的结果,重建出该二叉树,
//假设输入的前序遍历和中序遍历的结果中不含重复的数字
#include "stdafx.h"
#include<iostream>
using namespace std;
typedef int ElemType;
struct BinaryTreeNode
{
ElemType m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode* ConstructCore(int* startpreorder, int* endpreorder, int* startinorder, int* endinorder)
{
int rootValue = startpreorder[0];
BinaryTreeNode* root = new BinaryTreeNode();
root->m_nValue = rootValue;
root->m_pLeft = NULL;
root->m_pRight = NULL;
if (startpreorder == endpreorder)
{
if (startinorder == endinorder && *startpreorder == *startinorder)
{
return root;
}
else
{
throw exception("Invalid input.");
}
}
int* rooInorder = startinorder;
while (rooInorder <= endinorder&&*rooInorder != rootValue)
{
++rooInorder;
}
if (rooInorder == endinorder && *rooInorder != rootValue)
{
throw exception("Invalid input.");
}
int leftLength = rooInorder - startinorder;
int* leftPreorderEnd = startpreorder + leftLength;
if (leftLength > 0)
{
//构建左子树
root->m_pLeft = ConstructCore(startpreorder + 1, leftPreorderEnd, startinorder, rooInorder - 1);
}
if (leftLength < endpreorder - startpreorder)
{
//构建右子树
root->m_pRight = ConstructCore(leftPreorderEnd + 1, endpreorder, rooInorder + 1, endinorder);
}
return root;
}
BinaryTreeNode* Construct(int* preorder, int* inorder, int length)
{
if (preorder == NULL || inorder == NULL || length <= 0)
{
return NULL;
}
return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
}
//前序遍历
void PreTraverseBinaryTree(BinaryTreeNode* root)
{
if (root == NULL)
return;
cout << root->m_nValue << "\t";
PreTraverseBinaryTree(root->m_pLeft);
PreTraverseBinaryTree(root->m_pRight);
}
//中序遍历
void InTraverseBinaryTree(BinaryTreeNode* root)
{
if (root == NULL)
{
return;
}
InTraverseBinaryTree(root->m_pLeft);
cout << root->m_nValue << "\t";
InTraverseBinaryTree(root->m_pRight);
}
int _tmain(int argc, _TCHAR* argv[])
{
int PreOrder[8] = { 1, 2, 4, 7, 3, 5, 6, 8 };
int InOrder[8] = { 4, 7, 2, 1, 5, 3, 8, 6 };
BinaryTreeNode* Root;
Root = Construct(PreOrder, InOrder, 8);
PreTraverseBinaryTree(Root);
cout << endl;
InTraverseBinaryTree(Root);
cout << endl;
return 0;
}
运行结果:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- AVL树-自平衡二叉查找树(Java实现)
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(七):数据结构详解
- Lua教程(二):C++和Lua相互传递数据示例
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例