I'm trying to create a binary tree using JS. I am not totally convinced that what I have done is good enough, but I have not found a better solution. Data structures like this one are somewhat new to me, which is why I would like to hear your comments on the code.

```
class Tree {
constructor(initial = null) {
this.root = initial === null ? null : this._createNode(initial);
}
_createNode(value, children = null) {
let left = null;
let right = null;
if (typeof children === "number") {
left = children;
} else if (Array.isArray(null)) {
(left, right) = children.sort((a, b) => a - b);
}
return {
value,
left,
right
};
}
_getNewPlace(value) {
let currentNode = this.root;
while (currentNode !== null) {
if (currentNode.value > value) {
if (currentNode.left === null) {
currentNode.left = this._createNode(value);
return;
} else {
currentNode = currentNode.left;
}
} else {
if (currentNode.right === null) {
currentNode.right = this._createNode(value);
return;
} else {
currentNode = currentNode.right;
}
}
}
}
getRoot() {
return this.root;
}
addNode(item) {
if (this.root === null) {
this.root = this._createNode(item);
} else {
this._getNewPlace(item);
}
return this;
}
}
module.exports = {
Tree
};
```

And tests:

```
const { Tree } = require("./index");
describe("Binary Tree tests", () => {
it("should create an empty tree", () => {
const t = new Tree();
expect(t.getRoot()).toBe(null);
});
it("should create a root node first", () => {
const t = new Tree();
t.addNode(1);
expect(t.getRoot()).toMatchObject({
value: 1,
left: null,
right: null
});
});
it("should create a root node from constuctor, when value is provided", () => {
const t = new Tree(1);
expect(t.getRoot()).toMatchObject({
value: 1,
left: null,
right: null
});
});
it("should create an additional node if there is a root", () => {
const t = new Tree(1);
t.addNode(2);
expect(t.getRoot()).toMatchObject({
value: 1,
left: null,
right: {
value: 2,
left: null,
right: null
}
});
});
});
```

And finally, the code on CodeSandbox: https://codesandbox.io/s/binary-tree-search-xq000

Thanks in advance!