Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Breadth-first traversal is similar to what traversal of binary tree

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

What this article shares with you is that the breadth-first traversal is similar to the traversal of the binary tree. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Breadth-first traversal is similar to hierarchical traversal of binary trees. The breadth-first search traverses along the width of the tree starting from the root node, that is, traversing according to the hierarchy; each layer is accessed in turn from top to bottom, and in each layer, the node is accessed from left to right (or from right to left). After visiting one layer, go to the next layer until no node can be accessed.

Breadth-first search (Breadth First Search) (actually a hierarchical traversal of a binary tree), also known as width-first search or horizontal-first search, traverses along the width of the tree starting from the root node.

Access each layer in turn from top to bottom, in each layer, access the node from left to right (or from right to left), and then enter the next layer until no node can be accessed.

The traversal order of the above binary tree is: ABCDEFG. You can use queues to achieve breadth-first search.

Breadth first search algorithm:

Keep all the nodes, occupy a large space; no backtracking operation (that is, no stack operation, out of the stack operation), the running speed is fast.

Breadth-first search algorithm generally needs to store all the generated nodes, which takes up much more storage space than depth-first search. Therefore, the problems of overflow and saving memory space must be considered in programming. However, the breadth-first search method generally has no backtracking operation, that is, into and out of the stack, so it runs faster than depth-first search.

Example:

In terms of process verification, each layer node is visited in turn, after visiting one layer to the next layer, and each node can only be visited once. For the above example, the result of breadth-first traversal is: a-Magi, B, C, D, D, E, and H (assuming that each layer node is accessed from left to right).

Breadth first traverses each node and needs to use the data structure of Queue. Queue is characterized by first-in, first-out. In fact, double-ended queues can also be used. The difference is that the first bit of double-ended queues can be inserted and popped up. The whole traversal process is as follows:

First insert Node An into the queue, queue (A)

The A node is popped up, while the child node B of An is inserted into the queue, where B is at the head of the queue, C is at the end of the queue, and queue (BMague C)

The B node is popped up, while the child node D _ ther E of B is inserted into the queue, where C is at the head of the queue, E is at the end of the queue, and queue (C ~ D _ ~ E)

The C node is popped up, and the child node of C, FMagna GMagna H, is inserted into the queue, where D is at the head of the queue, H is at the end of the queue, and queue (DMageEMagMagGMagh)

Pop up the D node, D has no child nodes, then E is at the head of the queue, H is at the end of the queue, queue (Ereco FMagi GMagh)

... Go down in turn, and finally traverse it.

The Java code is roughly as follows:

Public class TreeNode {int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode (int val) {this.val = val;}} public class Solution {public ArrayList wide (TreeNode root) {ArrayList lists=new ArrayList (); if (root==null) return lists; Queue queue=new LinkedList (); queue.offer (root) While (! queue.isEmpty ()) {TreeNode node = queue.poll (); if (node.leftpassport null) {queue.offer (node.left);} if (node.rightlighting null) {queue.offer (node.right);} lists.add (node.val) } return lists;}} above is that breadth-first traversal is similar to what traversal of binary tree. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 224

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report