Day 53 - The Binary Search Tree
The questions from here onwards will be focusing on complex data structures and hence will be little more open ended than before, try to do them yourself.
Ques) Implement a Binary Search Tree, and the following methods
insert()
- to insert a new value to the BSTfind()
- to find a value in the BST
Binary Search Tree
Binary Search Tree is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a binary search tree.
Read more about Binary Search Trees… (Geeks4Geeks)
Solution
JavaScript Implementation
Solution
/* ==== A very simple implementation of a BST ==== */
class Node {
constructor (value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BST {
constructor () {
this.rootnode = null;
}
}
See full solution here