数据结构——18 二叉树(非递归)

二叉树——非递归法


二叉树——使用非递归方法,创建树、前中后序及层级遍历树。
建立树时,按照左子树小于树根,右子树大于树根,这样中序遍历就是有序表

#include<iostream>
#include<queue>       //层级遍历时,用到了queue模板类
#include<stack>       //前中后遍历时,用到了stack模板类
using namespace std;
class node            //树节点定义
{
public:
	int data;         //元素值
	node *parent;     //父节点
	node *left;       //左子节点
	node *right;      //右子节点
	int tag;        //这个在后序遍历中用到,作为标志位
public:
//	node():data(-1),parent(NULL),left(NULL),right(NULL){};
	node(int num):data(num),parent(NULL),left(NULL),right(NULL),tag(0){};        //构造函数,初始化类成员变量

};

class tree            //树定义
{
public:
	tree(int num[],int len);           //构造函数


	void insertNode(int data);         //安左低右高插入树的节点
	void preOrderTree();               //前序
	void inOrderTree();                //中序             
	void postOrderTree();              //后序
	void levelOrderTree();             //层级遍历


private:
	node *root;                        //树的根节点
};

tree::tree(int num[],int len)          //构造函数,插入树节点
{
	root=new node(num[0]);
	for(int i=1;i<len;i++)
		insertNode(num[i]);
}
void tree::insertNode(int data)
{
	node *p,*par;
	node *newnode=new node(data);
	p=par=root;


	while(p)
	{
		par=p;
		if(data > p->data)
			p=p->right;
		else if(data < p->data)
			p=p->left;
		else if(data == p->data)
		{
			delete newnode;
			return;
		}
	}


	newnode->parent=par;
	if(par->data > newnode->data)
		par->left=newnode;
	else
		par->right=newnode;
}

void tree::preOrderTree()             //先序遍历
{
	stack<node *> s;
	node *p=root;                     //从根节点开始
	
	while(p|| !s.empty())             //树不为空或栈不为空
	{
		while(p)                      //遍历左子树,压入栈,入栈前,输出节点
		{
			cout<<p->data<<"  ";
			s.push(p);
			p=p->left;
			
		}
		if(!s.empty())
		{
			p=s.top();
			s.pop();
			p=p->right;
		}
	}
}

void tree::inOrderTree()            //中序遍历
{
	stack<node *> s;
	node *p=root;                   //从根节点开始
	 
	while(p|| !s.empty())           //树不为空或栈不为空
	{
		while(p)                    //遍历左子树,压入栈
		{
			s.push(p);
			p=p->left;
		}
		if(!s.empty())
		{
			p=s.top();              //p指向栈顶
			s.pop();                //出栈
			cout<<p->data<<"  ";
			p=p->right;             //指向这个节点的右子树,遍历右子树做准备
		}
	}
}

void tree::postOrderTree()           //后序遍历
{
	stack<node *> s;
	node *p=root;
	
	while(p|| !s.empty())
	{
		while(p)
		{
			s.push(p);
			p=p->left;
			
		}
		if(!s.empty())
		{
			p=s.top();
			if(p->tag)       //tag为0表示遍历左子树前的保护现场,tag为1表示遍历右子树前的现场保护
			{
			cout<<p->data<<"  ";
			s.pop();
			p=NULL;
			}
			else
			{
				p->tag=1;
				p=p->right;
			
			}
			
		}
	}
}

void tree::levelOrderTree()    //层级遍历
{
	queue<node *> q;           //定义一个队列
	node *ptr=NULL;


	q.push(root);
	while(!q.empty())
	{
		ptr=q.front();         //得到队列的头节点
		q.pop();               //出队列
		cout<<ptr->data<<"  ";


		if(ptr->left!=NULL)    //当前节点存在左节点,左节点入队
			q.push(ptr->left);
		if(ptr->right!=NULL)   //当前节点存在右节点,右节点入队
			q.push(ptr->right);	
	}
}


int main()
{
	int num[8]={5,3,7,2,4,6,8,1};
	tree t(num,8);


	cout<<"前序遍历:  ";
	t.preOrderTree();
	cout<<endl<<endl;


	cout<<"中序遍历:  ";
	t.inOrderTree();
	cout<<endl<<endl;


	cout<<"后序遍历:  ";
	t.postOrderTree();
	cout<<endl<<endl;


	cout<<"层级遍历:  ";
	t.levelOrderTree();
	cout<<endl;
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值