This may seem like a duplicate question but I haven't been able to find the answer for my question anywhere on SOF or elsewhere.
I'd like to build an Incomplete Binary Tree from an Array and a not null root node, in JavaScript with null value represents the null subtree, not a subtree with value = null.
Expected result:
TreeNode {
val: 1,
left:
TreeNode {
val: 2,
left: TreeNode { val: 4, left: null, right: null },
right: null },
right:
TreeNode {
val: 3,
left: null,
right: TreeNode { val: 5, left: null, right: null } } }
The expected output above could be done manually like so:
const myTree = new TreeNode(1);
myTree.left = new TreeNode(2);
myTree.left.left = new TreeNode(4);
myTree.right = new TreeNode(3);
myTree.right.right = new TreeNode(5);
The tree should look like this:
1
/\
2 3
/ \
4 5
Here's my code:
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
insert(values, i = 0) {
if (i >= values.length) return;
const queue = [this];
while (queue.length > 0) {
let current = queue.shift();
if (current.left === null) {
current.left = new TreeNode(values[i]);
break;
}
queue.push(current.left);
if (current.right === null) {
current.right = new TreeNode(values[i]);
break;
}
queue.push(current.right);
}
this.insert(values, i + 1);
return this;
}
}
(function() {
// This builds a tree with null nodes with null values and null children.
// The goal is to skip updating any null child
const myTree = new TreeNode(1);
myTree.insert2([2,3,4,null,null,5]);
console.log(myTree);
}());
The issue here is I get a null child as a TreeNode with value = null like so:
TreeNode {
value: 1,
left:
TreeNode {
value: 2,
left: TreeNode { value: 4, left: null, right: null },
right: TreeNode { value: null, left: null, right: null } },
right:
TreeNode {
value: 3,
left: TreeNode { value: null, left: null, right: null },
right: TreeNode { value: 5, left: null, right: null } } }
I can handle the children of a null node by adding:
constructor(value) {
this.value = value;
if (value) this.left = null;
if (value) this.right = null;
}
but I haven't been able to add only null value instead of a full TreeNode.
I've tried:
if (current.left === null) {
current.left = values[i] !== null ? new BinaryTree(values[i]) : null;
break;
}
Or:
if (current.left === null && values[i]) {}
Or:
if (!values[i]) return;
But none returns the expected result.
I've also looked at some solutions using Java but the answers use built-in LinkedList in Java so I'm not sure that's the right way to do in JS.
Answer to Scott Sauyet's question:
The expected result for
const myTree = new TreeNode (1);
myTree .insert ([2,null, 4, null, 6, 7])
is:
TreeNode {
val: 1,
left: TreeNode {
val: 2,
left: TreeNode {
val: 4,
left: TreeNode { val: 6, left: null, right: null },
right: TreeNode { val: 7, left: null, right: null }
},
right: null
},
right: null
}
const myTree = new TreeNode (1); myTree .insert ([2,null, 4, null, 6, 7])? That is, what happens if you don't create the node (3) that's supposed to be the parent of other nodes (6and7)?