Cannot read property 'key' of null
function Bin() {
var Node = function (key) {
this.key = key;
this.left = null;
this.right = null;
};
var root = null;
var insertNode = function (node, newNode) {
if (newNode.key < node.key) {
if (node.key === null) {
node.left = newNode;
} else {
insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
insertNode(node.right, newNode);
}
}
};
this.insert = function (key) {
var newNode = new Node(key);
if (root === null) {
root = newNode;
} else {
insertNode(root, newNode);
}
};
}
var nodes = [8, 3, 10, 1, 14, 7, 13];
var bin = new Bin();
nodes.forEach(function (key) {
bin.insert(key);
});