JSFiddle - React, Tailwind, and code Playground
by sumit_skr
JavaScript
class Node {
constructor(data){
this.data = data;
this.left = null;
this.right = null;
}
}
class BST{
constructor(){
this.root = null;
}
insert(data){
var newNode = new Node(data);
if(this.root===null){
this.root = newNode;
}
else {
insertNode(this.root, data);
}
}
insertNode(node, newNode) {
if(newNode.data < node.data) {
if(node.left ===null) {
node.left = newNode;
}
this.insertNode(node.left,newNode);
}
else {
if(node.right === null) {
node.right = newNode;
}
this.insertNode(node.right, newNode);
}
}
remove(data) {
this.root = this.removeNode(this.root, data);
}
removeNode(node, key) {
if(node===null){
return null;
}
else if(key< node.data){
node.left = this.removeNode(node.left, key);
return node;
}
else {
}
}
search(node, key) {
if(node===null)
return false;
else if(node.data === key){
return true;
}
else if(key< node.data){
return this.search(node.left, key)
}
else{
return this.search(node.right, key)
}
}
findMin(node) {
if(node === null);
return false;
while(node.left!==null) {
node = node.left;
}
return node.data;
}
}