二叉树的遍历

二叉树的遍历:广度遍历和深度遍历

广度遍历

广度遍历:即层次遍历:一层一层遍历
算法思想:
在这里插入图片描述

public class LevelTraversal {
    public static void level(BTNode node) {
        ArrayDeque<BTNode> queue = new ArrayDeque<>();   //创建队列
        //首先将根节点加入栈中
        queue.add(node);
        //遍历二叉树
        while (!queue.isEmpty()) {
            BTNode tempNode = queue.poll();				//取出队列元素
            System.out.print(tempNode.data + " ");		//打印

            if(tempNode.leftChild != null){				//左孩子放进队列
                queue.add(tempNode.leftChild);
            }
            if(tempNode.rightChild != null){			//右孩子放进队列
                queue.add(tempNode.rightChild);
            }
        }
    }
}
class BTNode{
    String data;
    BTNode leftChild;
    BTNode rightChild;
}

深度遍历

先序:双亲节点->左子树->右子树
中序:左子树->双亲节点->右子树
后序:左子树->右子树->双亲节点

在这里插入图片描述

结果

先序:1 2 4 6 7 8 3 5
中序:4 7 6 8 2 1 3 5
后序:7 8 6 4 2 5 3 1

实现源码

import java.util.Stack;

/**
 * @Author shall潇
 * @Date 2021/5/8
 * @Description 树的遍历:广度遍历,深度遍历
 *
 * 广度遍历:即层次遍历:一层一层遍历
 *
 * 深度遍历:
 * 先序:双亲节点->左子树->右子树
 * 中序:左子树->双亲节点->右子树
 * 后序:左子树->右子树->双亲节点
 */

class TreeNode{
    String date;
    TreeNode leftchlid;  //左孩子
    TreeNode rightchild; //右孩子
}

public class TraverableTree {
    /*-------------------------递归--------------------------*/
    //先序遍历
    public static void recursionPreorderTraversal(TreeNode root){
        if(root!=null){
            System.out.println(root.date);
            recursionPreorderTraversal(root.leftchlid);
            recursionPreorderTraversal(root.rightchild);
        }
    }
    //中序遍历
    public static void recursionMiddleorderTraversal(TreeNode root){
        if(root!=null){
            recursionMiddleorderTraversal(root.leftchlid);
            System.out.println(root.date);
            recursionMiddleorderTraversal(root.rightchild);
        }
    }
    //后序遍历
    public static void recursionPostorderTraversal(TreeNode root){
        if(root!=null){
            recursionPostorderTraversal(root.leftchlid);
            recursionPostorderTraversal(root.rightchild);
            System.out.println(root.date);
        }
    }
    /*-------------------------非递归--------------------------*/
    //先序
    public static void rePreorderTravseral(TreeNode root){
        Stack<TreeNode> treeNodesStack = new Stack<>();  //用栈来暂存当前节点
        TreeNode node = root;                            //保存原来数据
        while (node!=null || !treeNodesStack.isEmpty()){ //节点非空或者栈非空的话
            while (node!=null){
                System.out.println(node.date);           //首先打印节点数据
                treeNodesStack.push(node);               //把这个节点入栈
                node = node.leftchlid;                   //取出它的左子树
            }
            if(!treeNodesStack.isEmpty()){               //没有左子树了,该遍历右子树
                node = treeNodesStack.pop();             //先弹出栈
                node= node.rightchild;                   //遍历右子树
            }
        }
    }
    //中序
    public static void reMiddleorderTravseral(TreeNode root){
        Stack<TreeNode> treeNodeStack = new Stack<>();
        TreeNode node = root;
        while (node !=null || !treeNodeStack.isEmpty()){
            while (node!=null) {
                treeNodeStack.push(node);
                node = node.leftchlid;
            }
            if(!treeNodeStack.isEmpty()){
                node = treeNodeStack.pop();
                System.out.println(node.date);
                node = node.rightchild;
            }
        }
    }
    //后序
    public static void rePostorderTravseral(TreeNode root){
        Stack<TreeNode> treeNodeStack = new Stack<>();
        TreeNode node = root;
        TreeNode lastVisit = root;
        while (node!=null || !treeNodeStack.isEmpty()){
            while (node !=null){
                treeNodeStack.push(node);
                node = node.leftchlid;
            }
            node = treeNodeStack.peek();  	    //查看栈顶元素
            if(node.rightchild == null || node.rightchild == lastVisit){
                System.out.println(node.date); //如果右子树为空 或 右子树已经被访问过,直接输出
                treeNodeStack.pop();
                lastVisit = node;
                node = null;
            }else {                      	  //如果右子树没被访问或不为空,则遍历它
                node = node.rightchild;
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值