C++根据广义表(括号表示法)构造二叉树,并分别进行前中后序层次遍历

前中后序遍历是递归形式,层次遍历是非递归形式。

代码:

#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <stack>
#include <queue>
using namespace std;

template<class T>
struct BTreeNode{
	T data;

	BTreeNode<T>* leftNode;
	BTreeNode<T>* rightNode;
};

typedef BTreeNode<int> BTINT;

template<class T>
class BTree{
	BTreeNode<T>* m_root = nullptr;
	BTreeNode<T>* getNewNode(T val){
		BTreeNode<T>* newNode = new (std::nothrow) BTreeNode<T>();
		newNode->data = val;
		if (newNode == nullptr){
			cerr << "申请新的内存失败!" << endl;
			this->destroyTree(m_root);
			exit(1);
		}
		return newNode;
	}
public:
	BTree():m_root(nullptr){}
	~BTree(){}
	BTreeNode<T>* const getRootNode(){
		return m_root;
	}
	void initBinaryTree(const string& desStr){
		//根据广义表字符串初始构造二叉树
		if (desStr == "")
			return;
		BTreeNode<T>* lastNode = nullptr;
		stack<BTreeNode<T>*> nodeStack;
		bool isLeft = true;
		int curIt = 0;
		char ch = desStr.at(curIt);
		while (ch != '\0'){
			switch (ch){
			case '(':
				nodeStack.push(lastNode);
				isLeft = true;
				break;
			case ')':
				isLeft = false;
				if (nodeStack.empty() != true)
					nodeStack.pop();
				break;
			case ',':
				isLeft = false;
				break;
			default:
				lastNode = this->getNewNode(ch);
				if ( m_root == nullptr){
					m_root = lastNode;
				}
				else{
					BTreeNode<T>* parentNode = nodeStack.top();
					if (isLeft == true){
						parentNode->leftNode = lastNode;
					}
					else{
						parentNode->rightNode = lastNode;
					}
				}
			}
			++curIt;
			if (curIt == desStr.length()){
				break;
			}
			ch = desStr.at(curIt);
		}//while end
	}

	void levelOrder(BTreeNode<T>* pCurNode) const {
		if (pCurNode == nullptr)
			return;
		BTreeNode<T>* curNode = pCurNode;
		queue < BTreeNode<T>*> que;
		que.push(curNode);
		while (que.empty() == false){
			curNode = que.front();
			que.pop();
			cout << curNode->data << endl;
			if (curNode->leftNode != nullptr)que.push(curNode->leftNode);
			if (curNode->rightNode != nullptr)que.push(curNode->rightNode);
		}
	}

	void preOrderPrint(BTreeNode<T>* pCurNode) const {
		//前序遍历
			if (pCurNode == nullptr)
				return;
			cout << pCurNode->data << endl;
			this->preOrderPrint(pCurNode->leftNode);
			this->preOrderPrint(pCurNode->rightNode);
	}

	void inOrderPrint(BTreeNode<T>* pCurNode) const{
		//中序遍历
			if (pCurNode == nullptr)
				return;
			this->inOrderPrint(pCurNode->leftNode);
			cout << pCurNode->data << endl;
			this->inOrderPrint(pCurNode->rightNode);
	}
	void postOrderPrint(BTreeNode<T>* pCurNode) const{
		//后序遍历
			if (pCurNode == nullptr)
				return;
			this->postOrderPrint(pCurNode->leftNode);
			this->postOrderPrint(pCurNode->rightNode);
			cout << pCurNode->data << endl;
	}

	void destroyTree(BTreeNode<T>* pCurNode){
		//后序遍历方式,销毁整颗树
			if (pCurNode == nullptr)
				return;
			this->destroyTree(pCurNode->leftNode);
			this->destroyTree(pCurNode->rightNode);
			delete pCurNode;
			pCurNode = nullptr;
	}

};

int main(){

	BTree<char>* binaryTree = new BTree<char>();
	binaryTree->initBinaryTree("1(2(3,4),5(6,7))");
	cout << "前序遍历" << endl;
	binaryTree->preOrderPrint(binaryTree->getRootNode());
	cout << "中序遍历" << endl;
	binaryTree->inOrderPrint(binaryTree->getRootNode());
	cout << "后序遍历" << endl;
	binaryTree->postOrderPrint(binaryTree->getRootNode());
	cout << "层次遍历" << endl;
	binaryTree->levelOrder(binaryTree->getRootNode());
	cout << endl;
	binaryTree->destroyTree(binaryTree->getRootNode());
	cout << endl;
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值