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