二叉树的遍历

二叉树的特性

二叉树的边的总数是所有结点的总数-1
二叉树度为2的结点n2与度为0的结点n0的关系为n2=n0-1
证明:所有的边=n0+n1+n2-1=n00+n11+n2*2

二叉树的遍历代码

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
class binnode { 
public:
	binnode() {
		leftchild = NULL;
		rightchild = NULL;
	}
	int val;
	binnode* leftchild;
	binnode* rightchild;
};
class bintree {
public:
	void preorder(const binnode*root);//前序遍历(非递归)
	void inorder(const binnode*root);//中序遍历(非递归)
	void postorder(const binnode*root);//后序遍历(非递归)
	void preorder_digui(const binnode*root);//前序遍历(递归)
	void inorder_digui(const binnode*root);//中序遍历(递归)
	void postorder_digui(const binnode*root);//后序遍历(递归)
	void levelorder(const binnode*root);//层序遍历
};

void bintree::preorder(const binnode*root) {
	if (!root)
		return;
	binnode*temp = new binnode;
	temp->leftchild = root->leftchild;
	temp->rightchild = root->rightchild;
	temp->val = root->val;
	stack<binnode*> s;
	while (temp || !s.empty()) {

		while (temp) {
			s.push(temp);
			cout << temp->val;
			temp = temp->leftchild;

		}
		if (!s.empty()) {
			binnode* curr = new binnode;
			curr = s.top();
			
			s.pop();
			temp = curr->rightchild;
		}
	}

}
void bintree::inorder(const binnode*root) {
	if (!root)
		return;
	binnode*temp = new binnode;
	temp->leftchild = root->leftchild;
	temp->rightchild = root->rightchild;
	temp->val = root->val;
	stack<binnode*> s;
	while (temp||!s.empty()) {
		
		while (temp) {
			s.push(temp);
			temp = temp->leftchild;
			
		}
		if (!s.empty()) {
			binnode* curr = new binnode;
			curr = s.top();
			cout << curr->val;
			s.pop();
			temp = curr->rightchild;
		}
	}
}
void bintree::postorder( const binnode*root) {
	if (root == NULL)
		return;

	stack<binnode*> s;
	binnode *cur=new binnode;                      //当前结点 
	cur->leftchild = root->leftchild;
	cur->rightchild = root->rightchild;
	cur->val = root->val;
	binnode *pre = NULL;                 //前一次访问的结点 
	s.push(cur);
	while (!s.empty()) {
		cur = s.top();//每次都选择栈顶元素
		if (cur->leftchild == NULL&&NULL == cur->rightchild || (pre != NULL && (pre == cur->leftchild || pre==cur->rightchild))) {//如果当前结点已经是叶子结点,那么就将其输出并弹出栈顶;如果是第二次访问该结点(其子节点已经在其前面访问),那么将其输出并弹出
			s.pop();
			cout << cur->val << ' ';
			pre = cur;
		}
		else{//先存放右子节点,再存放左子节点。这样优先处理左子节点的孩子结点
			if (cur->rightchild)
				s.push(cur->rightchild);
			if (cur->leftchild)
				s.push(cur->leftchild);
		}
	}

}
void bintree::preorder_digui(const binnode*root) {
	if (root) {
		cout << root->val << endl;
		preorder_digui(root->leftchild);
		preorder_digui(root->rightchild);
	}
}
void bintree::inorder_digui(const binnode*root) {
	if (root) {
		
		inorder_digui(root->leftchild);
		cout << root->val << endl;
		inorder_digui(root->rightchild);
	}
}
void inorder_digui(const binnode*root) {
	if (root) {

		inorder_digui(root->leftchild);
		cout << root->val << endl;
		inorder_digui(root->rightchild);
	}
}
void bintree::postorder_digui(const binnode*root) {
	if (root) {

		postorder_digui(root->leftchild);
		
		postorder_digui(root->rightchild);
		cout << root->val << endl;
	}
}
void bintree::levelorder(const binnode*root) {

	if (root == NULL)
		return;

	
	binnode *cur = new binnode;                      //当前结点 
	cur->leftchild = root->leftchild;
	cur->rightchild = root->rightchild;
	cur->val = root->val;
	queue<binnode*> s;
	s.push(cur);
	while (!s.empty()) {
		binnode*cur = s.front();
		s.pop();
		cout << cur->val << endl;
		if (cur->leftchild)
			s.push(cur->leftchild);
		if (cur->rightchild)
			s.push(cur->rightchild);
	}
}
int main() {
	
	bintree m;
	binnode A,B,C,D,E,F,G,H;
	A.val = 1;
	B.val = 2;
	C.val = 3;
	D.val = 4;
	E.val = 8;
	F.val = 5;
	G.val = 6;
	H.val = 7;


	A.leftchild =&B;
	A.rightchild = &C;
	B.leftchild = &D;
	B.rightchild = NULL;
	D.rightchild = &F;
	F.leftchild = &G;
	F.rightchild = &H;
	C.rightchild = &E;
	
	m.levelorder(&A);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值