Javascript search Binary Tree
Function Tree () {this. root = null;} Tree. prototype = {constructor: Tree, addItem: function (value) {var Node = {data: value, left: null, right: null}; if (this. root = null) {this. root = Node;} else {var current = this. root; var parent = current; while (current! = Null) {parent = current; if (value <current. data) {current = current. left; continue; // This is easy to ignore, And the next sentence if is missing to judge current. data will report an error.} if (value = current. data) {return false;} if (value> current. data) {current = current. right; continue ;}}if (value <parent. data) {parent. left = Node;} if (value> parent. data) {parent. right = Node ;}},/* First traverse */firstlist: function (root) {if (root! = Null) {console. log (root. data); this. firstlist (root. left); this. firstlist (root. right) ;}},/* sequential traversal */lastlist: function (root) {if (root! = Null) {this. lastlist (root. left); this. lastlist (root. right); console. log (root. data) ;}},/* sequential traversal */inlist: function (root) {if (root! = Null) {this. inlist (root. left); console. log (root. data); this. inlist (root. right) ;}}; var Tree = new Tree (); Tree. addItem (5); Tree. addItem (1); Tree. addItem (6); Tree. addItem (8); Tree. addItem (7 );