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

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

代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;

template
struct BTreeNode{
	T data;

	BTreeNode* leftNode;
	BTreeNode* rightNode;
};

typedef BTreeNode BTINT;

template
class BTree{
	BTreeNode* m_root = nullptr;
	BTreeNode* getNewNode(T val){
		BTreeNode* newNode = new (std::nothrow) BTreeNode();
		newNode->data = val;
		if (newNode == nullptr){
			cerr << "申请新的内存失败!" << endl;
			this->destroyTree(m_root);
			exit(1);
		}
		return newNode;
	}
public:
	BTree():m_root(nullptr){}
	~BTree(){}
	BTreeNode* const getRootNode(){
		return m_root;
	}
	void initBinaryTree(const string& desStr){
		//根据广义表字符串初始构造二叉树
		if (desStr == "")
			return;
		BTreeNode* lastNode = nullptr;
		stack*> 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* 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* pCurNode) const {
		if (pCurNode == nullptr)
			return;
		BTreeNode* curNode = pCurNode;
		queue < BTreeNode*> 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* pCurNode) const {
		//前序遍历
			if (pCurNode == nullptr)
				return;
			cout << pCurNode->data << endl;
			this->preOrderPrint(pCurNode->leftNode);
			this->preOrderPrint(pCurNode->rightNode);
	}

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

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

};

int main(){

	BTree* binaryTree = new BTree();
	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;
}

你可能感兴趣的:(C/C++,数据结构/算法)