By Hemanta Sundaray on 2021-07-29
A tree is a node based data structure.
A binary tree is a tree in which each node has zero, one, or two children.
A binary search tree is a tree-based data structure that confirms to the following rules:
Here’s an example of a binary search tree, in which the values are numbers:
Note that each node has one child with a lesser value than itself, which is depicted using a left arrow, and one child with a greater value than itself, which is depicted using a right arrow.
Additionally, notice that all of the 50’s left descendants are less than it. At the same time, all of the 50’s right descendants are greater than it. The same pattern goes for each and every node.
Implement the Node class to create a binary search tree. The constructor should initialize values: data, left & right.
class Node {
constructor(data) {
this.data = data
this.left = null
this.right = null
}
}
const node = new Node(50)
console.log(node)
// Node { data: 50, left: null, right: null }
Implement the insert method for the Node class. The insert method should accept an argument called data, then create and insert a new node at the appropriate location in the tree.
We will implement the insert method using recursion.
class Node {
constructor(data) {
this.data = data
this.left = null
this.right = null
}
insert(data) {
if (data < this.data && this.left) {
this.left.insert(data)
} else if (data < this.data) {
this.left = new Node(data)
} else if (data > this.data && this.right) {
this.right.insert(data)
} else {
this.right = new Node(data)
}
}
}
const node = new Node(50)
node.insert(20)
console.log(node)
// Node {
// data: 50,
// left: Node { data: 20, left: null, right: null },
// right: null
// }