二叉树的非递归遍历总结

二叉树的非递归遍历总结
/**
 *
 *
 *非递归遍历
 *
 *
 */
 
public class BinaryTree{
	//先序遍历,且为根左右
	public void preOrder(Node root){
		if(root == null)
			return;
		Stack<Node> stack = new Stack<>();
		stack.push(root);
		while(!stack.isEmpty()){
			Node cur = stack.pop();
			//先处理根节点
			System.out.println(cur.Value);
			//后处理左右子节点,由于是根左右的顺序,因此先处理左节点,要先入栈右节点
			if(cur.rightChild != null)
				stack.push(cur.rightChild);
			if(cur.leftChild != null)
				stack.push(cur.leftChild);
		}
	}
	//中序遍历,且为左根右
	public void midOrder(Node root){
		if(root == null)
			return;
		Stack<Node> stack = new Stack<>();
		Node myRoot = root;
		while(!stack.isEmpty() || myRoot != null){
			if(myRoot != null){
				stack.push(myRoot);
				myRoot = myRoot.leftChild;
			}else{
				myRoot = stack.pop();
				System.out.println(myRoot.value);
				myRoot = myRoot.rightChild;
			}
		}
	}
	//后续遍历,且为左右根
	//两个栈实现
	//栈2保存最后可以直接按顺序输出的值
	//栈1作为中间容器,实现栈2按顺序保存树的值
	public void posOrder(Node root){
		if(root == null)
			return;
		Stack<Node> stackOne = new Stack<>();
		Stack<Node> stackTwo = new Stack<>();
		stackOne.push(root);
		while(!stackOne.isEmpty()){
			Node cur = stackOne.pop();
			stackTwo.push(cur);
			if(cur.leftChild != null)
				stackOne.push(cur.leftChild);
			if(cur.rightChild != null)
				stackOne.push(cur.rightChild);
		}
		while(!stackTwo.isEmpty()){
			Node cur = stackTwo.pop();
			System.out.println(cur.value);
		}
	}
	//一个栈实现后续遍历
	//栈中保存一个子树的左子节点和根节点,这样可以先处理左子节点然后通过根节点处理右子节点,最后处理根节点
	public void posOrder2(Node root){
		if(root == null)
			return;
		Stack<Node> stack = new Stack<>();
		Node curNode = root;
		Node lastNode = null;
		while(curNode != null){
			stack.push(curNode);
			curNode = curNode.leftChild;
		}
		while(!stack.isEmpty()){
			curNode = stack.pop();
			//有右子树并且右子树不是上一次刚处理过,则不处理当前的根节点,当前根节点需要入栈,要先处理右子树才可以处理当前根节点
			//否则可以处理当前根节点
			if(curNode.rightChild != null && curNode.rightChild != lastNode){
				stack.push(curNode);
				curNode = curNode.rightChild;
				//因为是处理右子树,故仍需要将该右子树的左节点入栈
				while(curNode != null){
					stack.push(curNode);
					curNode = curNode.leftChild;
				}
			}else{
				//处理当前根节点
				System.out.println(curNode.value);
				lastNode = curNode;
			}
		}
	}
}
class Node{
    public int value;
    public Node leftChild;
    public Node rightChild;

    public Node(int value){
        this(value, null, null);
    }
    public Node(int value, Node leftChild, Node rightChild){
        this.value = value;
        this.leftChild = leftChild;
        this.rightChild = rightChild;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java二叉树非递归遍历可以通过使用栈数据结构来实现。首先,我们创建一个空的栈,将根节点入栈。然后,我们进入一个循环,直到栈为空为止。在每一次循环中,我们弹出栈顶元素,并将其访问。接下来,如果该节点有右子节点,则将右子节点入栈。如果该节点有左子节点,则将左子节点入栈。由于栈是先进后出的数据结构,所以我们先入栈右子节点,再入栈左子节点,以确保在遍历过程中先访问左子树节点。这样就能够实现二叉树非递归遍历。 以下是一个示例代码实现二叉树非递归中序遍历: ```java public void inorderTraversal(Node root) { if (root == null) { return; } Stack<Node> stack = new Stack<>(); Node current = root; while (current != null || !stack.isEmpty()) { while (current != null) { stack.push(current); current = current.leftChild; } current = stack.pop(); System.out.print(current.data + " "); // 访问节点 current = current.rightChild; } } ``` 在这个示例代码中,我们首先判断当前节点是否为空或者栈是否为空,如果不满足则进入循环。在循环内部,我们首先将当前节点及其所有左子节点入栈,直到当前节点为空。然后,我们弹出栈顶节点并访问该节点。最后,将当前节点更新为其右子节点,并继续下一次循环。 通过这种方式,我们可以实现二叉树非递归中序遍历。你可以根据需要修改代码实现其他类型的非递归遍历,比如前序遍历和后序遍历。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [用Python实现二叉树二叉树非递归遍历及绘制的例子](https://download.csdn.net/download/weixin_38618784/14869891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java实现二叉树树的非递归遍历](https://blog.csdn.net/weixin_41826973/article/details/105555647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值