树的遍历_(前序(递归和非递归)、中序(递归和非递归)、后续(递归和非递归)、层次、深度(递归和非递归) )

17 篇文章 0 订阅
14 篇文章 0 订阅
//树中节点的定义
typedef  struct _BTNode
{
int data;
struct _BTNode *lchild;
struct _BTNode *rchild;
}BiTNode ,*BiNTree;
//树的层次遍历-- 先进先出 需要借助队列来实现
void BinaryTreeLevelOrder(BiNTree root)
{
	queue<BiTNode*> q;
	if(root==NULL)
		return;
    q.push(root);
  
    while(!q.empty())
    {
		BiTNode *p = q.front();
        cout<<p->data<<endl;
     
        if(p->lchild!=NULL)
        {
            q.push(p->lchild);
			
        }
        
        if(p->rchild!=NULL)
        {
           q.push(p->rchild);
        //   TravelByLayer(T->rchild);
        }
		   q.pop();
    }
    
}

 

//二叉树的深度优先遍历--非递归形式 由于先进后出,因而要借助栈
void  BinaryTreeDepthFirstOrder(BiNTree root)
{
	stack<BiTNode*> s;
	if(root==NULL)
		return;
	//q.push(T);
	s.push(root);
	while(!s.empty())
	{
		BiTNode *p = s.top();
		cout<< p->data<<endl;
		s.pop();
		if(p->rchild!=NULL)
		{
		
		 s.push(p->rchild);
		}

		if(p->lchild!=NULL)
		{
		 s.push(p->lchild);
			
		}
		
	}

}
//二叉树的深度遍历--借助于递归实现
void TravelByDeptWithtRecursion(BiNTree root)
{
	if(root==NULL)
		return;
		cout<<root->data<<endl;
	TravelByDeptWithtRecursion(root->lchild);
	TravelByDeptWithtRecursion(root->rchild);

}

 

//二叉树的前序遍历--采用递归形式实现
void PreOrderWithRecursion(BiNTree root)
{
	if (root==NULL)
	{
		return ;
	}
	cout<<root->data<<endl;
	PreOrderWithRecursion(root->lchild);
	PreOrderWithRecursion(root->rchild);
}
//二叉树的前序遍历 --非递归形式实现
void PreOrderWithoutRecursion(BiNTree root)
{
	if (root==NULL)
	{
		return ;
	}
	stack<BiTNode*> s;
	s.push(root);
	while(!s.empty())
	{
		cout<<s.top()->data<<endl;
		BiTNode *p = s.top();
		s.pop();
		if (p->rchild!=NULL)
		{
			s.push(p->rchild);
		}
		if (p->lchild!=NULL)
		{
			s.push(p->lchild);
		}
		
	}
}
//二叉树的中序遍历--采用递归形式实现
void BinaryTreeInOderWithRecursion(BiNTree root)
{
	
	if (root==NULL)
		return;
	BinaryTreeInOderWithRecursion(root->lchild);
	cout<<root->data<<endl;
	BinaryTreeInOderWithRecursion(root->rchild);
}
//中序遍历的非递归实现
void BinaryTreeInOderWithoutRecursion(BiNTree root)
{
	if (root==NULL)
	{
		return ;
	}
		stack<BiTNode*> s;
	//s.push(T);
	BiTNode* p = root;
	while(!s.empty()||p)
	{
		while(p!=NULL)
		{
			s.push(p);
			p = p->lchild;
		}//先走到最左分支 
		if (!s.empty())//之后出栈
		{
			p = s.top();
			s.pop();
			cout<<p->data<<endl;
			p = p ->rchild;//遍历右子树 进行新的一轮左子树的遍历
		}
	}
}
//后序遍历的递归实现
void BinaryTreePostOrderWithRecursion(BiNTree root)
{
	if (root==NULL)
		return ;
	BinaryTreePostOrderWithRecursion(root->lchild);
	BinaryTreePostOrderWithRecursion(root->rchild);
	cout<<root->data<<endl;
	
}
void BinaryTreePostOrderWithoutRecursion(BiNTree root)
{
	if (root==NULL)
		return ;
	stack<BiTNode*> s;
	BiTNode *pCur,*pLastVisit;//记录当前访问的节点和标记上次访问的节点
	pCur =root;
	pLastVisit ==NULL;

	while(pCur!=NULL)
	{
		s.push(pCur);
		pCur = pCur->lchild;
	}
	while(!s.empty())
	{
		//走到这里,说明已经遍历到了子树的最左边
		pCur = s.top();
		s.pop();
		//一个根节点被访问的前提是:无右子树或者右子树已经被访问过了
		if (pCur->rchild==NULL||pCur->rchild==pLastVisit)
		{
			cout<< pCur->data<<endl;
			pLastVisit =pCur;
		}
		else
		{
			//根节点入栈
			s.push(pCur);
			//进入右子树,并且可以肯定右子树必定不为空
			pCur = pCur->rchild;
			while(pCur)
			{
				s.push(pCur);
				pCur = pCur->lchild;

			}
		}

	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值