【算法百题之九】二叉树的构造以及遍历
大家好,我是Lampard~~
很高兴又能和大家见面了,接下来准备系列更新的是算法题,一日一练,早日升仙!
今天的问题是:
构造一个二叉树,并且实现前中后序遍历
我的思路:
首先定义一个树结点的类,再定义一个树的类,并且把它们关联起来。
之后在二叉树这个类中封装(递归)前中后序遍历的方法就可以了。
树结点类的声明(.h文件):
树结点类的定义(.cpp文件):
#include "pch.h"
#include <iostream>
#include "BinaryTreeNode.h"
// 构造函数的声明
BinaryTreeNode::BinaryTreeNode()
{
LeftChild = NULL;
RightChild = NULL;
data = 0;
}
BinaryTreeNode::BinaryTreeNode(char theData)
{
LeftChild = NULL;
RightChild = NULL;
data = theData;
}
BinaryTreeNode::BinaryTreeNode(char theData, BinaryTreeNode* left, BinaryTreeNode* right)
{
LeftChild = left;
RightChild = right;
data = theData;
}
// 单独设置三个属性的值
void BinaryTreeNode::setData(char theData)
{
data = theData;
}
void BinaryTreeNode::setLeftChild(BinaryTreeNode* left)
{
LeftChild = left;
}
void BinaryTreeNode::setRightChild(BinaryTreeNode* right)
{
RightChild = right;
}
// 得到三个属性的值
char BinaryTreeNode::getData()
{
return data;
}
BinaryTreeNode* BinaryTreeNode::getLeftChild()
{
return LeftChild;
}
BinaryTreeNode* BinaryTreeNode::getRightChild()
{
return RightChild;
}
bool BinaryTreeNode::isLeaf()
{
if (LeftChild == NULL && RightChild == NULL)
return true;
else
return false;
}
二叉树类的声明(.h文件):
二叉类的定义(.cpp文件):
#include "pch.h"
#include <iostream>
#include "BinaryTree.h"
#include "BinaryTreeNode.h"
using namespace std;
BinaryTree::BinaryTree()
{
}
BinaryTreeNode* BinaryTree::createBinaryTree()
{
BinaryTreeNode* newNode = new BinaryTreeNode();
char theData;
cout << "请按前序遍历顺序输入二叉树数据" << endl;
cout << "输入#号则代表没有该结点:" << endl;
cin >> theData;
if (theData == '#')
newNode = NULL;
else
{
newNode->setData(theData);
newNode->setLeftChild(createBinaryTree());
newNode->setRightChild(createBinaryTree());
}
return newNode;
}
void BinaryTree::setRoot(BinaryTreeNode* theRoot)
{
root = theRoot;
}
bool BinaryTree::isEmpty()
{
if (root == NULL)
return true;
else
return false;
}
BinaryTreeNode* BinaryTree::getRoot()
{
return root;
}
void BinaryTree::preOrder(BinaryTreeNode* node)
{
if (node == NULL)
return;
else
{
cout << node->getData() << " ";
preOrder(node->getLeftChild());
preOrder(node->getRightChild());
}
}
void BinaryTree::inOrder(BinaryTreeNode* node)
{
if (node == NULL)
return;
else
{
inOrder(node->getLeftChild());
cout << node->getData() << " ";
inOrder(node->getRightChild());
}
}
void BinaryTree::postOrder(BinaryTreeNode* node)
{
if (node == NULL)
return;
else
{
postOrder(node->getLeftChild());
postOrder(node->getRightChild());
cout << node->getData() << " ";
}
}
前中后序递归实现我就不多说了,大家可以看看我的代码注释。
值得一提的是一开始如何为二叉树插入数据。
我们是从控制台按前序遍历的顺序传入字符,当输入为#时,证明该结点为空,否则则填入该结点的值。
例如我们要实现这样的一个树;
那么我们的输入顺序则是:1,2,3,#,#,4,#,#,5,6,#,#,#
这是我们的测试main方法:
#include "pch.h"
#include "BinaryTreeNode.h"
#include "BinaryTree.h"
#include <iostream>
using namespace std;
int main()
{
BinaryTree* myTree = new BinaryTree();
myTree->setRoot(myTree->createBinaryTree());
cout << "这是前序遍历的结果:" << endl;
myTree->preOrder(myTree->getRoot());
cout << endl;
cout << "这是中序遍历的结果:" << endl;
myTree->inOrder(myTree->getRoot());
cout << endl;
cout << "这是后序遍历的结果:" << endl;
myTree->postOrder(myTree->getRoot());
}
测试结果: