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

What is the traversal algorithm of js binary tree

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Js binary tree traversal algorithm is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

What is the traversal algorithm of binary tree?

Binary tree, there are three traversal algorithms, 1: medium order traversal, 2: first order traversal, 3: post order traversal. Before we look at the concrete implementation, we think about why we should do this. Binary tree is widely used in a large number of data search services, which can achieve more efficient search.

1: traversal in the middle order, that is, first find the left node, then find the root node, and finally find the right node. No matter the middle order, the first order, and the second order are all based on the root node. The following is the code

FunctioninOrder (node) {

If (! node===null&&nodeinstanceofBst)

InOrder (node.left)

Console.log (node.data)

InOrder (node.right)

}

2: preorder traversal:

FunctioninOrder (node) {

If (! node===null&&nodeinstanceofBst)

Console.log (node.data)

InOrder (node.left)

InOrder (node.right)

}

Post-order traversal is similar, I believe we can also find a certain rule.

The ergodic concept of binary tree

First of all, we need to know the concept of preorder traversal: mid-order traversal, post-order traversal:

Preorder traversal: start from the parent node, traverse the left tree, and then traverse the right tree

Traversal in the middle order: start with the left tree, then traverse the parents' nodes, and finally traverse the right tree

Post-order traversal: start with the left tree, then traverse the right tree, and finally traverse the parents' nodes

The core algorithm is simple: create an array and recurse the current node. The only difference is that the array is added to the node node in a different order:

/ / preorder algorithm

FunctionbeforeErgodic (node) {

If (!! node) {

Arr.push (node)

BeforeErgodic (node.firstElementChild)

BeforeErgodic (node.lastElementChild)

}

}

/ / medium order algorithm

FunctionmiddleErgodic (node) {

If (!! node) {

MiddleErgodic (node.firstElementChild)

Arr.push (node)

MiddleErgodic (node.lastElementChild)

}

}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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: 0

*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

Development

Wechat

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

12
Report