Implementation Method of the Binary Search Tree for javascript data structure, javascript Data Structure

Source: Internet
Author: User

Implementation Method of the Binary Search Tree for javascript data structure, javascript Data Structure

This article describes how to implement the javascript Binary Search Tree. We will share this with you for your reference. The details are as follows:

Binary Search Tree: As the name suggests, each node on the tree can have at most two forks, and the value of the Left forks node is <the value of the right forks node.

Features: It is very convenient to insert nodes, find the Maximum/minimum nodes, and sort node values

Binary Search Tree-javascript implementation

<Script type = "text/javascript"> // <! [CDATA [// print output function println (msg) {document. write (msg + "");} // Node class var Node = function (v) {this. data = v; // node value this. left = null; // left node this. right = null; // right node} // Binary Search Tree var BinarySearchTree = function () {this. root = null; // when initializing, the root node is empty // insert node // parameter: v is the value of the node this. insert = function (v) {var newNode = new Node (v); if (this. root = null) {// when the tree is empty, the new node directly becomes the root node this. root = newNode; return;} v Ar currentNode = this. root; // working "Pointer" Node (start from the root to find down) var parentNode = null; while (true) {parentNode = currentNode; if (v <currentNode. data) {// value of the current node> value of the target node // insert to the left and move the work node to the left node currentNode = currentNode. left; if (currentNode = null) {// if no left node exists, the new node directly becomes the left node parentNode. left = newNode; return; // exit loop} else {// otherwise, insert to the right, and move the work node to the right node currentNode = currentNode. right; if (currentNode = null) {parentNode. ri Ght = newNode; return ;}}/// find the smallest node this. min = function () {var p = this. root; // work node while (p! = Null & p. left! = Null) {p = p. left;} return p;} // find the largest node this. max = function () {var p = this. root; // work node while (p! = Null & p. right! = Null) {p = p. right;} return p;} // this. inOrder = function (rootNode) {if (rootNode! = Null) {this. inOrder (rootNode. left); // first left node println (rootNode. data); // re-root node this. inOrder (rootNode. right); // then right node }}// first traverse this. preOrder = function (rootNode) {if (rootNode! = Null) {println (rootNode. data); // root this first. preOrder (rootNode. left); // click this on the left node. preOrder (rootNode. right); // then right node} // traverse this in descending order. postOrder = function (rootNode) {if (rootNode! = Null) {this. postOrder (rootNode. left); // The first left node this. postOrder (rootNode. right); // then the right node println (rootNode. data); // re-root node }}// The following is the test var bTree = new BinarySearchTree (); // Saudi Arabia. algorithm design skills and analysis book 3.9 tree bTree on the left. insert (6); bTree. insert (3); bTree. insert (8); bTree. insert (1); bTree. insert (4); bTree. insert (9); println ('ordinal traversal: ') bTree. inOrder (bTree. root); println ("<br/>"); println ("sequential traversal:"); bTree. preOrder (bTree. root ); Println ("<br/>"); println ("post-order traversal:"); bTree. postOrder (bTree. root); println ("<br/>"); var minNode = bTree. min (); println ("minimum node:" + (minNode = null? "Does not exist": minNode. data); println ("<br/>"); var maxNode = bTree. max (); println ("max node:" + (maxNode = null? "Does not exist": maxNode. data); //]> </script> <! -- Central order traversal: 1 3 4 6 8 9 <br> first order traversal: 6 3 1 4 8 9 <br> backward order traversal: 1 4 3 9 8 6 <br> Minimum node: 1 <br> maximum node: 9 -->

Output result:

In-order traversal: 1 3 4 6 8 9 first-order traversal: 6 3 1 4 8 9 after-order traversal: 1 4 3 9 8 6 Minimum node: 1 Maximum node: 9

I hope this article will help you design JavaScript programs.

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.