【算法百题之九】二叉树的构造以及遍历

   【算法百题之九】二叉树的构造以及遍历

 

       大家好,我是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());
}

测试结果:

 

OK,今天的博客就到这里,谢谢大家!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lampard杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值