在晴天的簡單愛
2018-08-20 11:06
function BinaryTree(){
var Node = function(){
this.key = key;
this.left = null;
this.right = null;
}
var root = null;
var insertNode = function(node, newNode){
if(newNode.key < node.key){
if(node.left === null){
node.left = newNode;
}else{
insertNode(node.left, newNode);
}
}else{
if(node.rigth === null){
node.rigth = newNode;
}else{
insertNode(node.rigth, newNode);
}
}
}
this.insert = function(key){
var newNode = new Node(key);
if(root === null){
root = node;
}else{
insertNode(root, newNode);
}
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key);
});
第二行构造函数要有形参key
Javascript实现二叉树算法
46934 学习 · 97 问题
相似问题