二叉树的建立及遍历(C++实现)

   包含了二叉树的递归建立、前中后序遍历的递归和非递归实现以及层序遍历的非递归实现。

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

typedef struct TreeNode{
	char data;
	TreeNode *lchild,*rchild;
}TreeNode;

//输入二叉树的前序遍历序列来构建二叉树,#号代表结点为空 
TreeNode* createBinaryTree(TreeNode* root){
	char ch;
	cin>>ch;
	if(ch=='#')
		root=NULL;
	else{
		root=new TreeNode;
		root->data=ch;
		root->lchild=createBinaryTree(root->lchild);
		root->rchild=createBinaryTree(root->rchild);
	}
	return root;
}

//二叉树前序遍历的递归实现 
void preOrder(TreeNode* root){
	if(!root)
		return;
	else{
		cout<<root->data<<" ";
		preOrder(root->lchild);
		preOrder(root->rchild);
	}
}

//二叉树中序遍历的递归实现 
void inOrder(TreeNode* root){
	if(!root)
		return;
	else{
		inOrder(root->lchild);
		cout<<root->data<<" ";
		inOrder(root->rchild);
	}
}

//二叉树后序遍历的递归实现 
void postOrder(TreeNode* root){
	if(!root)
		return;
	else{
		postOrder(root->lchild);
		postOrder(root->rchild);
		cout<<root->data<<" ";
	}
}

//二叉树层序遍历的非递归实现(采用队列) 
void levelOrder(TreeNode* root){
	if(!root)
		return;
	queue<TreeNode*> que;
	que.push(root);
	while(!que.empty()){
		TreeNode *cur=que.front();
		que.pop();
		cout<<cur->data<<" ";
		if(cur->lchild)
			que.push(cur->lchild);
		if(cur->rchild)
			que.push(cur->rchild);
	}
} 

//二叉树前序遍历的非递归实现。(采用函数重载的机制,增加一个bool型参数作为区分)
//中序遍历和后序遍历同前序遍历 
void preOrder(TreeNode* root,bool isRecur){
	if(!root)
		return;
	stack<TreeNode*> s;
	TreeNode* cur=root;
	while(cur||!s.empty()){
		//迭代访问结点的左孩子,并入栈
		while(cur){
			cout<<cur->data<<" ";
			s.push(cur);
			cur=cur->lchild;
		}
		if(!s.empty()){
			cur=s.top();
			s.pop();
			cur=cur->rchild;
		} 
	}
} 

void inOrder(TreeNode* root,bool isRecur){
	if(!root)
		return;
	TreeNode* cur=root;
	stack<TreeNode*> s;
	while(cur||!s.empty()){
		while(cur){
			s.push(cur);
			cur=cur->lchild;
		}
		//中序遍历在左结点为空时输出根节点 
		if(!s.empty()){
			cur=s.top();
			s.pop();
			cout<<cur->data<<" ";
			cur=cur->rchild;
		}
	}
} 

//后序遍历:遍历顺序仍旧是先左后右。因为是后序遍历,如果当前栈顶元素有右节点,则不pop,cur=top->right,
//然后把栈顶元素的右指针置为nullptr 
void postOrder(TreeNode* root,bool isRecur){
	if(!root)
		return;
	TreeNode *cur=root;
	stack<TreeNode*> s;
	while(cur||!s.empty()){
		if(cur){
			s.push(cur);
			cur=cur->lchild;
		}
		else{
			TreeNode* top=s.top();
			if(!top->rchild){
				s.pop();
				cout<<top->data<<" ";
				continue;
			}
			cur=top->rchild;
			top->rchild=nullptr;
		}
	}
} 
int main()
{	TreeNode *root=NULL; 
	root=createBinaryTree(root);
	cout<<"The binary tree has been created successfully!"<<endl;
	cout<<"PreOrder is:";
	preOrder(root);
	cout<<endl;
	cout<<"InOrder is:";
	inOrder(root);
	cout<<endl;
	cout<<"PostOrder is:";
	postOrder(root);
	cout<<endl;
	cout<<"LevelOrder is:";
	levelOrder(root);
	cout<<endl;
	
	cout<<"No-Recur PreOrder is:";
	preOrder(root,0);
	cout<<endl;
	cout<<"No-Recur InOrder is:";
	inOrder(root,0);
	cout<<endl;
	cout<<"No-Recur PostOrder is:";
	postOrder(root,0);
	cout<<endl;
	return 0;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值