二叉树的遍历

二叉树

一个结点的子节点不超过两个的树叫做二叉树

二叉树

在这里插入图片描述

二叉树的遍历,前序遍历,中序遍历,后序遍历,层次遍历

前三种遍历,分别是根据子节点的位置,来进行命名的,假设子节点在前,也就是谦虚遍历,子节点在中间这就是中序遍历,当子节点在后,就是后续遍历。然后就是层次遍历

前三种遍历都比较容易,我们来遍历以前三种方式来分别遍历一下这个二叉树

前序遍历

递归式遍历

public static void BfS(BinaryTree root){
        // 如果该节点存在输出该节点
        if (root!=null) {
            System.out.print(root.val+" ");
        }
        // 子结点处理完成,处理左节点,若左节点存在,处理左节点
        if (root.left!=null){
            BfS(root.left);
        }
        // 若右节点存在处理右节点
        if (root.right!=null){
            BfS(root.right);
        }
    }

非递归式遍历

public static void BFS(BinaryTree root){
    // 判断边界条件
    if (root == null){
        return;
    }
    // 初始化一个栈,用来存放数据
    Stack<BinaryTree> stack = new Stack<>();
    // 将子节点放进栈中
    stack.push(root);
    //从栈中一个个往外拿,处理完成一个再处理下一个,直到栈式空的为止
    while (!stack.isEmpty()){
        // 处理栈中的个结点,一个个往外拿
        root = stack.pop();
        // 输出这个结点的值
        System.out.print(root.val+" ");
        // 判断该结点是否存在子节点
        // 若是存在子结点中,若是存到,放到栈中存着,等待处理
        if (root.right!=null){
            stack.push(root.right);
        }if (root.left!=null){
            stack.push(root.left);
        }
    }
}

输出是这个样子的 1 2 4 5 3 7 6 ,这个是前序遍历的结果

后续遍历

递归式遍历

public static void BfS(BinaryTree root){
    // 如果该节点存在输出该节点
    if (root!=null) {
        System.out.print(root.val+" ");
    }
    //子结点处理完成
    // 若右节点存在处理右节点
    if (root.right!=null){
        BfS(root.right);
    }
    // 处理左节点,若左节点存在,处理左节点
    if (root.left!=null){
        BfS(root.left);
    }
}

非递归式遍历

public static void BFS(BinaryTree root){
    // 判断边界条件
    if (root == null){
        return;
    }
    // 初始化一个栈,用来存放数据
    Stack<BinaryTree> stack = new Stack<>();
    // 将子节点放进栈中
    stack.add(root);
    //从栈中一个个往外拿,处理完成一个再处理下一个,直到栈式空的为止
    while (!stack.isEmpty()){
        // 处理栈中的个结点,一个个往外拿
        root = stack.pop();
        // 输出这个结点的值
        System.out.print(root.val+" ");
        // 判断该结点是否存在子节点
        // 若是存在子结点中,若是存到,放到栈中存着,等待处理
        if (root.left!=null){
            stack.add(root.left);
        }
        if (root.right!=null){
            stack.add(root.right);
        }
    }
}

中序遍历

递归

public static void BfS(BinaryTree root){
        // 处理左节点,若左节点存在,处理左节点
        if (root.left!=null){
            BfS(root.left);
        }
        // 如果该节点存在输出该节点
        if (root!=null) {
            System.out.print(root.val+" ");
        }
        //处理右节点
        if (root.right!=null){
            BfS(root.right);
        }
    }

非递归

public static void BFS(BinaryTree root){
    // 判断边界条件
    if (root == null){
        return;
    }
    // 初始化一个栈,用来协调数据存储处理
    Stack<BinaryTree> stack = new Stack<>();
    // 当该节点是空的不是空的和栈中有元素的时候,对数据进行处理
    while (root!=null||!stack.isEmpty()){
        // 若该节点不是空的,则先将该结点推入栈中,等待稍后处理
        while (root!=null){
            stack.push(root);
            // 将该节点切换为该节点的左节点
            root = root.left;
            //一直循环直到找到树最深的最左边的结点,这样,就将上面的结点都
        }
        // 开始处理栈中结点
        root = stack.pop();
        System.out.print(root.val+" ");
        // 处理完该节点,开始向右处理节点
        root=root.right;
    }
}

4 2 5 1 7 3 6 这个是中序遍历的结果

我们可以看到,只要在访问的时候,改变输出顺序就可以采用前中后三种不同的方式对二叉树进行遍历。

层次遍历

层次遍历要采用队列,因为队列的特性是先进先出

public static void BFS(BinaryTree root){
    if (root == null){
        return;
    }
    Queue<BinaryTree> queue = new ArrayDeque<>();
    //因为是层次遍历,先处理根节点,所以先将根节点放进队列中,这样先处理的也是根节点
    queue.add(root);
    while (!queue.isEmpty()){
        //将队列中的元素取出来
        BinaryTree binaryTree = queue.remove();
        // 先输出这个结点的元素值
        System.out.print(binaryTree.val+" ");
        //然后将该结点的左右儿子,放进队列中
        if (binaryTree.left!=null){
            queue.add(binaryTree.left);
        }
        if (binaryTree.right!=null){
            queue.add(binaryTree.right);
        }
        // 这样不断在队列中往外取,也同时往队列里存放
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值