二叉树的非递归遍历实现

import java.util.Stack;


public class Iterator1
{
	//先序遍历
	 public void preIterator(BiTree root)
	 {
		 if(root == null)
			 return;
		 Stack<BiTree> stack = new Stack<BiTree>();
		 BiTree p = root;
		 while(!stack .empty() || p != null  )
		 {
			 
			while(p != null)
			{
				System.out.print(p.val + "-->");//若节点不为空先访问再压栈(每个节点都可以看成根节点)
				stack.push(p);
				p = p.left;//将当前节点置为p的左孩子,若不为空继续访问并压栈
			}
			//当p为空时,说明根节点和左孩子打印遍历完毕了,接下来出栈遍历右孩子
			 if(!stack.empty())
			 {
				 p = stack.pop();
				 p = p.right;
			 }
		 }
	 }
	 //中序遍历
	 public void inIterator(BiTree root)
	 {
		 if(root == null)
			 return;
		 Stack<BiTree> stack = new Stack<BiTree>();
		 BiTree p = root;//让p指向根节点
		 while(!stack .empty() || p != null  )
		 {
			 //一直遍历到左子树最下边,边遍历边保存根节点到栈中(每个节点都可以看成一个新的子树的根节点)
			 while(p != null)  
			 {
				 stack.push(p);//若节点的左孩子不为空,将左孩子压栈,因为需要借助遍历过的节点进入右子树
				 p = p.left;
			 }
			 //当p为空时,说明已经到达左子树最下边,这时需要出栈了
			 if(stack != null)
			 {
				 p = stack.pop();
				 System.out.print(p.val + "-->");//访问根节点
				 p = p.right;//进入右子树,此时p是右子树的根节点(开始新一轮的遍历)
			 }
		 }
		 
	 }
	 //后序遍历
	 public  void postIterator(BiTree root)
	 {
		 if(root == null)
			 return;
		 Stack<BiTree> stack = new Stack<BiTree>();
		 BiTree pre = null;//当前节点的之前访问的节点
		 BiTree current;
		 stack.push(root);
		 while(!stack.empty())
		 {
			 current = stack.peek();
			 if((current.left == null && current.right == null) ||  //当前节点是叶子节点,可以直接访问该节点
					 (pre != null &&(pre == current.left|| pre == current.right)))
				 //当前一个节点不为空并且是当前节点的左孩子或者右孩子,当是左孩子时说明当前节点右孩子为空,当是右孩子时,说明左右孩子都访问过了,且都不为空
			 {
				System.out.print(current.val + "-->"); 
				stack.pop();
				pre = current;
			 }
			 else  //当前节点为栈顶元素  如果当前节点不是叶子节点,在当前节点之前访问的那个节点不是当前节点的孩子,则进行压栈
			 {
				 if(current.right != null) //先压栈右节点再压栈左节点 这样出栈时是先左后右
				 {
					 stack.push(current.right);
				 }
				 if(current.left != null)
				 {	 
					 stack.push(current.left);
				 }
			 }
		 }
	 }
	 
	 public static void main(String[] args)
	{
		BiTree root = BiTree.buildTree();
		Iterator1 iterator = new Iterator1();
		iterator.preIterator(root);
		System.out.println();
		iterator.inIterator(root);
		System.out.println();
		iterator.postIterator(root);
	}
 
    1. http://robinsoncrusoe.iteye.com/blog/808526 
    2. /** 非递归实现后序遍历 单栈法*/  
    3.     protected static void iterativePostorder3(Node p) {  
    4.         Stack<Node> stack = new Stack<Node>();  
    5.         Node node = p, prev = p;  
    6.         while (node != null || stack.size() > 0) {  
    7.             while (node != null) {  
    8.                 stack.push(node);  
    9.                 node = node.getLeft();  
    10.             }  
    11.             if (stack.size() > 0) {  
    12.                 Node temp = stack.peek().getRight();  
    13.                 if (temp == null || temp == prev) {  
    14.                     node = stack.pop();  
    15.                     visit(node);  
    16.                     prev = node;  
    17.                     node = null;  
    18.                 } else {  
    19.                     node = temp;  
    20.                 }  
    21.             }  
    22.   
    23.         }  
    24.     }  



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树非递归遍历可以使用栈来实现。以下是二叉树非递归前序、中序和后序遍历的完整代码实现: ```cpp #include <iostream> #include <stack> using namespace std; // 二叉树节点的定义 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 非递归前序遍历 void preorderTraversal(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> st; st.push(root); while (!st.empty()) { TreeNode* node = st.top(); st.pop(); cout << node->val << " "; if (node->right) st.push(node->right); if (node->left) st.push(node->left); } } // 非递归中序遍历 void inorderTraversal(TreeNode* root) { stack<TreeNode*> st; TreeNode* node = root; while (node != NULL || !st.empty()) { while (node != NULL) { st.push(node); node = node->left; } node = st.top(); st.pop(); cout << node->val << " "; node = node->right; } } // 非递归后序遍历 void postorderTraversal(TreeNode* root) { stack<TreeNode*> st; TreeNode* node = root; TreeNode* lastVisited = NULL; while (node != NULL || !st.empty()) { while (node != NULL) { st.push(node); node = node->left; } TreeNode* topNode = st.top(); if (topNode->right == NULL || topNode->right == lastVisited) { cout << topNode->val << " "; st.pop(); lastVisited = topNode; } else { node = topNode->right; } } } int main() { // 构造二叉树 TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); // 非递归前序遍历 cout << "Preorder traversal: "; preorderTraversal(root); cout << endl; // 非递归中序遍历 cout << "Inorder traversal: "; inorderTraversal(root); cout << endl; // 非递归后序遍历 cout << "Postorder traversal: "; postorderTraversal(root); cout << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值