// 二叉树 function BinaryTree() { this.root = null; this.node = function (val) {return {val: val, left: null, right: null}}; this.add = function (val) { if (this.root === null) { this.root = this.node(val); } else { var current = this.root; var parent; while (true) { parent = current; if (val < current.val) { current = current.left; if (current === null) { parent.left = this.node(val); break; } } else { current = current.right; if (current === null) { parent.right = this.node(val); break; } } } } }; }
时间: 2024-10-25 12:24:08