Binary Tree lookup JavaScript

Source: Internet
Author: User

Now talk about finding it in a binary tree. For a two-fork lookup tree, there are generally three types of lookups: maximum, minimum, and given value.

Finding the minimum value is traversing the left subtree until the last node is found, because the smaller value in the binary lookup tree is always on the left child node.

The code is as follows:

function Getmin () {//Find minimum var current=this.root;//point to root node while (current.left!=null) {current=current.left;} return current.data;}

Similarly, the code to find the maximum value is as follows:

function Getmax () {//Find maximum var current=this.root;while (current.right!=null) {////If the right node is not found, always look for current=current.right;} return current.data;}
It is also not difficult to find the specified value in the binary lookup tree, which is the size of the lookup value of the node value in turn, if the node value is small, continue to the right to find, if the node value is large, then continue to find the left, the code is as follows:

function find (data) {//Find a value var current=this.root;while (current!=null) {if (current.data==data) {return current;} else if (current.data>data) {//If the node value is larger than the search value, then go to the left current=current.left;} else{//If the node value is smaller than the search value, go to the right Current=current.right;}} Returns Nullreturn null if not found;}





Binary Tree lookup JavaScript

Related Article

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.