由于树的操作离不开指针,所以树创建和遍历比线性表复杂很多,文本用C++实现了树的创建和创建完之后的六种遍历方法,现在已知某个八个结点的二叉树的前序遍历序列是:1,2,4,7,3,5,6,8;中序遍历序列是:4,7,2,1,5,3,8,6;根据前中后续的遍历规则,可以得到1是整棵树的根节点,2,4,7是左子树的前序遍历序列,4,7,2是左子树的中序遍历序列,3,5,6,8是右子树的前序遍历序列,5,3,8,6是右子树的中序遍历序列。以此类推可以将二叉树重构出来,下面是程序实现。(至于其它的操作,以后再补充)
首先是包含的头文件
#include<iostream>
#include<stdlib.h>
#include<stack>
using namespace std;//使用的命名空间
二叉树结点的数据抽象:
struct BinaryTreeNode//二叉树的定义
{
int value;//根的值
BinaryTreeNode* m_pLeft;//指向左子节点的指针
BinaryTreeNode* m_pRight;//指向右子节点的指针
};
根据前中序遍历序列重构二叉树:
BinaryTreeNode *ConstructCore(int *preorderStrat, int *preorderEnd, int *inorderStart, int *inorderEnd)
{
int rootValue = preorderStrat[0];
BinaryTreeNode *root = new BinaryTreeNode();
root->value = rootValue;
root->m_pLeft = nullptr;
root->m_pRight = nullptr;
if (preorderStrat == preorderEnd)
{
if (inorderStart == inorderEnd && *preorderStrat == *inorderStart)
{
return root;
}
else
throw std::exception("invalid input");
}
int *rootinorder = inorderStart;
while (rootinorder <= inorderEnd && *rootinorder != rootValue)
++rootinorder;
if (rootinorder == inorderEnd &&*rootinorder != rootValue)
throw std::exception("invalid input");
int leftLength = rootinorder - inorderStart;
int *leftPreorderEnd = preorderStrat + leftLength;
if (leftLength > 0)
{
root->m_pLeft = ConstructCore(preorderStrat+1,leftPreorderEnd,inorderStart,rootinorder-1);
}
if (leftLength < inorderEnd - inorderStart)
{
root->m_pRight = ConstructCore(leftPreorderEnd+1,preorderEnd,rootinorder+1,inorderEnd);
}
return root;
}
BinaryTreeNode *Construct(int *preorder, int *inorder,int length)
{
if (preorder == nullptr || inorder == nullptr || length <= 0)
return nullptr;
return ConstructCore(preorder,preorder+length-1,inorder,inorder+length-1);
}
根据重建的二叉树,用六种遍历方式进行遍历:
//*************二叉树前中后序的六种遍历方法(递归与非递归)***********//
void preorderRecursively(BinaryTreeNode *root)//*****前序递归
{
if (root)
{
cout << root->value << " ";
preorderRecursively(root->m_pLeft);
preorderRecursively(root->m_pRight);
}
}
void preorderNonRecursion(BinaryTreeNode *root)//*********前序非递归遍历
{
if (root == nullptr)
cout << "the tree is empty!" << endl;
stack<BinaryTreeNode*> treeNode;
while (root || !treeNode.empty())
{
while (root)
{
cout << root->value << " ";
treeNode.push(root);
root = root->m_pLeft;//检查完根节点后立马检查该根节点的左节点
}
root = treeNode.top();//检查没有左节点的根节点有没有有节点
treeNode.pop();
root = root->m_pRight;
}
cout << endl;
}
void inorderRecursively(BinaryTreeNode *root)//中序递归遍历,与前序相比就是输出根节点的位置发生变化
{
if (root)
{
inorderRecursively(root->m_pLeft);
cout << root->value << " ";
inorderRecursively(root->m_pRight);
}
}
void inorderNonRecursion(BinaryTreeNode *root)//中序非递归遍历,与前序相比也仅仅是输入位置的改变
{
if (nullptr == root)
cout << "the tree is empty!" << endl;
stack<BinaryTreeNode *> treeNode;
while (root || !treeNode.empty())
{
while (root)
{
treeNode.push(root);
root = root->m_pLeft;
}
root = treeNode.top();
cout << root->value << " ";
treeNode.pop();
root = root->m_pRight;
}
cout << endl;
}
void postorderRecursively(BinaryTreeNode *root)//后续递归遍历
{
if (root)
{
postorderRecursively(root->m_pLeft);
postorderRecursively(root->m_pRight);
cout << root->value << " ";
}
}
void postNonRecursion(BinaryTreeNode *root)//后续非递归遍历
{
if (nullptr == root)
cout << "the tree is empty!" << endl;
stack<BinaryTreeNode *> treeNodeAuxiliary;//辅助栈,用于遍历整个数的节点
stack<BinaryTreeNode *> treeNodeResult;//根据辅助栈的遍历情况,将节点压入栈中,
//反序输出就是最终的结果
while (root || !treeNodeAuxiliary.empty())
{
while (root)
{
treeNodeAuxiliary.push(root);
treeNodeResult.push(root);
root = root->m_pRight;
}
root = treeNodeAuxiliary.top();
treeNodeAuxiliary.pop();
root = root->m_pLeft;
}
//栈中反向输出
while (!treeNodeResult.empty())
{
root = treeNodeResult.top();
cout << root->value << " ";
treeNodeResult.pop();
}
cout << endl;
}
//********************************************************************//
主函数:
int main()
{
BinaryTreeNode *tree;
int preorder[8] = { 1, 2, 4, 7, 3, 5, 6, 8 };
int inorder[8] = { 4, 7, 2, 1, 5, 3, 8, 6 };
tree = Construct(preorder, inorder, 8);
cout << "前序递归遍历: ";
preorderRecursively(tree);
cout << endl;
cout << "前序非递归遍历: ";
preorderNonRecursion(tree);
cout << "中序递归遍历: ";
inorderRecursively(tree);
cout << endl;
cout << "中序非递归遍历: ";
inorderNonRecursion(tree);
cout << "后续递归遍历: ";
postorderRecursively(tree);
cout << endl;
cout << "后续非递归遍历: ";
postNonRecursion(tree);
system("pause");
return 0;
}
运行效果: