Javascript search Binary Tree, javascript 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 );
Javascript can write Binary Trees
Think about it yourself. It's not good to use software. Check whether there is any problem with traversal.
The binary search tree is a Complete Binary Tree.
Binary Search Tree, or an empty Tree, or a Binary Tree of the following nature: if its left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of its root node. If its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node. Its left and right subtree are also binary sorting trees.
So not necessarily