二叉树C++版

#include <iostream>
#include <stack>
using namespace std;

class BinNode{
public:
	char data;
	BinNode* lChild;
	BinNode* rChild;
public:
	BinNode(char data, BinNode* lChild=nullptr, BinNode* rChild=nullptr){
		this->data = data;
		this->lChild = lChild;
		this->rChild = rChild;
	}
};

class BinTree{
public:
	BinNode* root;
public:
	//构造空树
	BinTree(){
		root = nullptr;
	}
	//构造只有根结点的树 
	BinTree(char data){
		root = new BinNode(data);
	}
	//析构函数 
	~BinTree(){
		delete root;
	}
	//以扩展二叉树前序遍历序列创建二叉树,空树时输入为# 
	void createTree(BinNode*& root){
		char data;
		cin >> data;
		if(data != '#'){
			root = new BinNode(data);
			createTree(root->lChild);
			createTree(root->rChild);
		}
		else{
			root = nullptr;
		}
	} 
	//递归前序遍历 
	void preOrder(BinNode*& root){
		BinNode* cur = root;
		if(cur){
			cout << cur->data << endl;
			preOrder(cur->lChild);
			preOrder(cur->rChild);
		}
	}
	//非递归前序遍历
	void preOrder_2(BinNode*& root){
		BinNode* cur = root;
		stack<BinNode*> stack;
		while(cur || !stack.empty()){
			while(cur){
				cout << cur->data << endl;
				cur = cur->lChild;
				stack.push(cur->rChild);//沿路将右子树记录下来表示未曾访问 
			}
			//遇到空树则回溯处理右子树
			cur = stack.top();
			stack.pop();
		}
	}
	//递归中序遍历
	void inOrder(BinNode*& root){
		BinNode* cur = root;
		if(cur){
			inOrder(cur->lChild);
			cout << cur->data << endl;
			inOrder(cur->rChild);
		}
	}
	//非递归中序遍历
	void inOrder_2(BinNode*& root){
		BinNode* cur = root;
		stack<BinNode*> stack;
		while(cur || !stack.empty()){
			while(cur){
				stack.push(cur);
				cur = cur->lChild;
			} 
			cur = stack.top();
			stack.pop();
			cout << cur->data << endl;
			cur = cur->rChild;
		}
	} 
	//递归后序遍历
	void postOrder(BinNode*& root){
		BinNode* cur = root;
		if(cur){
			postOrder(cur->lChild);
			postOrder(cur->rChild);
			cout << cur->data << endl;
		}
	} 
	//非递归后序遍历
	void postOrder_2(BinNode*& root){
		BinNode* cur = root;
		stack<BinNode*> stack;
		while(cur || !stack.empty()){
			while(cur){
				stack.push(cur);
				//能左就左,否则向右 
				if(cur->lChild)
					cur = cur->lChild;
				else
					cur = cur->rChild;
			}
			cur = stack.top();
			stack.pop();
			cout << cur->data << endl;
			if(!stack.empty() && cur==stack.top()->lChild)
				//stack非空,因为stack.top()用到了stack,如果是空的,则引发错误 
				//如果被访问的结点是其父的左结点,直接转到其右兄弟结点继续 
				cur = stack.top()->rChild;
			else
				//如果被访问的结点是其父的右结点,设cur为空,强制访问更上一层结点 
				cur = nullptr;
		}
	} 
	int size(BinNode*& root){
		if(root){
			int sl = size(root->lChild);
			int sr = size(root->rChild);
			return sl+sr+1;
		}
		else{
			return 0;
		}
	}
	int height(BinNode*& root){
		if(root){
			int hl = height(root->lChild);
			int hr = height(root->rChild);
			return hl>hr?hl+1:hr+1;
		}
		else{
			return 0;
		}
	}
};


int main(){
	BinTree tree;
	tree.createTree(tree.root);
	tree.postOrder(tree.root);
	tree.postOrder_2(tree.root);
	return 0;
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值