更换map的遍历顺序优先级_树的级别顺序遍历或宽度优先遍历

更换map的遍历顺序优先级

In this tutorial, we’ll be discussing what’s Level Order Traversal. Also, we’ll be printing each level of the tree. We’ll use the two major approaches – Recursive and Iterative and see what’s the difference in time complexities between them.

在本教程中,我们将讨论什么是水平订单遍历。 另外,我们将打印树的每个级别。 我们将使用两种主要方法-递归和迭代,看看它们之间的时间复杂度有何不同。

级别顺序遍历 (Level Order Traversal)

Level Order traversal is also known as Breadth-First Traversal since it traverses all the nodes at each level before going to the next level (depth).

级别顺序遍历也称为广度优先遍历,因为它在进入下一个级别(深度)之前遍历每个级别的所有节点。

Following illustration shows levels of a Binary Tree:

下图显示了二叉树的级别:

The last level of the tree is always equal to the height of the tree.

树的最后一层始终等于树的高度。

The last level of the tree should contain at least one Node.

树的最后一级应至少包含一个节点。

Following is the class that defines the Binary Tree Data Structure:

以下是定义二叉树数据结构的类:

public class BinaryTree {

	public TreeNode root;

	public static class TreeNode {

		public TreeNode left;
		public TreeNode right;
		public Object data;

		public TreeNode(Object data) {
			this.data = data;
			left = right = null;
		}
	}
}

Let’s write the Java Program for iterative and recursive approaches for breadth-first traversal.

让我们为广度优先遍历的迭代和递归方法编写Java程序。

广度优先遍历–递归 (Breadth First Traversal – Recursively)

Following Java program uses the recursive approach to print each level of the tree:

以下Java程序使用递归方法来打印树的每个级别:

package com.journaldev.tree.levelOrderTraversal;

import com.journaldev.tree.height.BinaryTree;
import com.journaldev.tree.height.BinaryTree.TreeNode;


public class PrintLevelsOfTree {

    public static void main(String[] args) {

        BinaryTree binaryTree = new BinaryTree();

        /**
         * Binary Tree in our example, height = 2
         * 		1		(Root)
         * 	  2	  3		(Level 1)
         *      4       5		(Level 2)
         */
        binaryTree.root = new BinaryTree.TreeNode(1);
        binaryTree.root.left = new BinaryTree.TreeNode(2);
        binaryTree.root.right = new BinaryTree.TreeNode(3);
        binaryTree.root.left.left = new BinaryTree.TreeNode(4);
        binaryTree.root.right.left = new BinaryTree.TreeNode(5);

        printLevelsRecursively(binaryTree.root);
    }

    private static void printLevelsRecursively(TreeNode root) {
        int height = heightOfTree(root);

        for (int i = 1; i <= height; i++) {
            System.out.print("Level " + i + " : ");
            printSingleLevelRecursively(root, i);
            System.out.println();

        }
    }

    private static int heightOfTree(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int leftHeight = heightOfTree(root.left);
        int rightHeight = heightOfTree(root.right);

        return Math.max(leftHeight, rightHeight) + 1;
    }

    private static void printSingleLevelRecursively(TreeNode root, int level) {
        if (root == null)
            return;

        if (level == 1) {
            System.out.print(root.data + " ");
        } else if (level > 1) {
            printSingleLevelRecursively(root.left, level - 1);
            printSingleLevelRecursively(root.right, level - 1);
        }
    }
}

Following is the output that gets printed:

以下是输出的输出:

Level Order Complexity for Recursive Approach 递归方法的层级顺序复杂度

Time Complexity – O(n^2)
Space Complextiy – O(1)

时间复杂度– O(n ^ 2)
空间复杂度– O(1)

Let’s optimize the above program by using the Iterative approach instead.

让我们通过使用迭代方法来优化上述程序。

public static void printLevelsIteratively(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();

        queue.add(root);

        while (!queue.isEmpty()) {


            TreeNode node = queue.peek();
            if (node != null) {
                System.out.print(node.data + " ");
                queue.remove();

                if (node.left != null)
                    queue.add(node.left);

                if (node.right != null)
                    queue.add(node.right);
            }

        }

    }

This prints the following:

打印以下内容:

In the above code, each Node is enqueued and dequeued once.

在上面的代码中,每个节点入队和出队一次。

Level Order Complexity for Iterative Approach 迭代方法的层级顺序复杂度

Time Complexity – O(n)
Space Complextiy – O(n)

时间复杂度– O(n)
空间复杂度– O(n)

The Queue size is equal to the number of the nodes.

队列大小等于节点数。

So in the above approach, we’ve traded with some space for optimizing the approach.

因此,在上述方法中,我们已经为优化该方法付出了一些空间。

That brings an end to this tutorial.

这样就结束了本教程。

GitHub Repository. GitHub存储库中检出完整的代码以及更多DS和算法示例。

翻译自: https://www.journaldev.com/23059/level-order-traversal-breadth-first-tree

更换map的遍历顺序优先级

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值