Javascript code for binary tree traversal, javascript Binary Tree

Source: Internet
Author: User

Javascript code for binary tree traversal, javascript Binary Tree

Preface:

Next, let's talk about the javascript Implementation of the binary tree in the previous article.

The following binary tree is used as an example to traverse this serious nonsense:

The following Code introduces the binary tree implementation:

function Node(data, left, right) {  this.data = data;  this.left = left;  this.right = right;  this.show = show;}function show() {  return this.data;}function BST() {  this.root = null;  this.insert = insert;}function insert(data) {  var n = new Node(data, null, null);  if (this.root == null) {   this.root = n;  }  else {   var current = this.root;   var parent;   while (true) {     parent = current;     if (data < current.data) {      current = current.left;      if (current == null) {        parent.left = n;        break;      }     }     else {      current = current.right;      if (current == null) {        parent.right = n;        break;      }     }   }  }}

Binary tree traversal Classification

Binary tree traversal is divided into sequential, central, and backward traversal. The first, middle, and last orders mentioned here are relative to the parent node. The value of the parent node is output in the first order, and the output in the middle is the middle order, and the final output is the back order. As for the parent node, each node can be a parent node except the leaf node (the bottom node.

First-order traversal

First, print the parent node, then the left child node (left child tree), and then the right child node (Child tree ).

Function preOrder (node) {if (! (Node = null) {console. log (node. show () + ""); preOrder (node. left); preOrder (node. right) ;}}// Add the member method of sequential traversal to the BST class function BST () {this. root = null; this. insert = insert; this. preOrder = preOrder ;}

The preOrder function is implemented recursively. It should be said that binary tree traversal is implemented recursively. Some may print the parent node first, then the left child node (left child tree), and then the right child node (Child tree) "and into a wrong idea, what is this idea, please refer:

Note that in the red box, the parent node is 10, the left child node is 3, and the right child node is 18, it may mistakenly think that the order of printing is 10 → 3 → 18. However, this is not the case. The actual order is: Print 10 first, and then print the left subtree, after all the nodes in the left subtree are printed, the right subtree with 10 parent nodes is printed:

At this time, you should think like this in your mind:

Then I thought like this:

Print the left subtree with 10 as the parent node, and then print the right subtree with 10 as the parent node in this way, we can better understand the traversal of Binary Trees Based on this concept of splitting.

Then, the order of first traversing and modifying a binary tree is as follows:

The output order is as follows:10 -> 3 -> 2 -> 4 -> 9 -> 8 -> 9 -> 18 -> 13 -> 21

Finally, let's take a look at it and traverse it in sequence:

var bst = new BST();var nums = [10, 3, 18, 2, 4, 13, 21, 9, 8, 9];for(var i = 0; i < nums.length; i++) {  bst.insert(nums[i]);}bst.preOrder(bst.root);

Here we emphasize that the output sequence is related to the insertion sequence, because the binary tree generated by different insertion sequence is also different. If you have any questions, you can go to the javascript Implementation of the binary tree to take a closer look. If you have a clear description of the binary tree, you can also perform an experiment:

Sequential Traversal

After reading the first-order traversal, you can now draw a lot of knowledge points related to the middle-order and post-order traversal. The features of sequential traversal are: print the left subtree (left subtree) first, then print the parent node, and finally print the right subtree (right subtree ).

Function inOrder (node) {if (! (Node = null) {inOrder (node. left); console. log (node. show () + ""); inOrder (node. right) ;}} // Add this member Method to the BST class function BST () {this. root = null; this. insert = insert; this. preOrder = preOrder; this. inOrder = inOrder ;}

Print order of sequential traversal:

The output order is as follows:2 -> 3 -> 4 -> 8 -> 9 -> 9 -> 10 -> 13 -> 18 -> 21

Next, let's take a look at the ordinal traversal:

Post-order traversal

Function postOrder (node) {if (! (Node = null) {postOrder (node. left); postOrder (node. right); console. log (node. show () + "") ;}/// add this member Method to the BST class function BST () {this. root = null; this. insert = insert; this. preOrder = preOrder; this. inOrder = inOrder; this. postOrder = postOrder ;}

Print order of post-order traversal

The output order is as follows:2 -> 8 -> 9 -> 9 -> 4 -> 3 -> 13 -> 21 -> 18  -> 10

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.