Node
class to create a binary search tree.
data
, left
, and right
.insert
method for the Node
class.
insert
should accept an argument of data
, then insert a new Node
at the appropriate location in the tree.contains
method for the Node
class.
contains
should accept a data
argument and return the Node
in the tree with the same value.null
.Implementation
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
insert(data) {
if (data < this.data) {
if (this.left != null) {
this.left.insert(data);
} else {
this.left = new Node(data);
}
} else if (data > this.data) {
if (this.right != null) {
this.right.insert(data);
} else {
this.right = new Node(data);
}
}
}
contains(data) {
if (data == null) {
return;
}
if (data === this.data) {
return this;
}
return (this.left != null && this.left.contains(data))
|| (this.right != null && this.right.contains(data))
|| null;
}
}