Created
October 8, 2019 06:23
-
-
Save kingsleytan/5e14b9259248f1c90e33e28ac0e31d4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(data, left = null, right = null) { | |
this.data = data; | |
this.left = left; | |
this.right = right; | |
} | |
} | |
class BST { | |
constructor() { | |
this.root = null; | |
} | |
add(data) { | |
const node = this.root; | |
if (node === null) { | |
this.root = new Node(data); | |
return; | |
} else { | |
const searchTree = function (node) { | |
if (data < node.data) { | |
if (node.left === null) { | |
node.left = new Node(data); | |
return; | |
} else if (node.left !== null) { | |
return searchTree(node.left); | |
} | |
} else if (data > node.data) { | |
if (node.right === null) { | |
node.right = new Node(data); | |
return; | |
} else if (node.right !== null) { | |
return searchTree(node.right); | |
} | |
} else { | |
return null; | |
} | |
}; | |
return searchTree(node); | |
} | |
} | |
findMin() { | |
let current = this.root; | |
while (current.left !== null) { | |
current = current.left; | |
} | |
return current.data; | |
} | |
findMax() { | |
let current = this.root; | |
while (current.right !== null) { | |
current = current.right; | |
} | |
return current.data; | |
} | |
find(data) { | |
let current = this.root; | |
while (current.data !== data) { | |
if (data < current.data) { | |
current = current.left | |
} else { | |
current = current.right; | |
} | |
if (current === null) { | |
return null; | |
} | |
} | |
return current; | |
} | |
isPresent(data) { | |
let current = this.root; | |
while (current) { | |
if (data === current.data) { | |
return true; | |
} | |
if (data < current.data) { | |
current = current.left; | |
} else { | |
current = current.right; | |
} | |
} | |
return false; | |
} | |
remove(data) { | |
const removeNode = function (node, data) { | |
if (node == null) { | |
return null; | |
} | |
if (data == node.data) { | |
// no child node | |
if (node.left == null && node.right == null) { | |
return null; | |
} | |
// no left node | |
if (node.left == null) { | |
return node.right; | |
} | |
// no right node | |
if (node.right == null) { | |
return node.left; | |
} | |
// has 2 child nodes | |
var tempNode = node.right; | |
while (tempNode.left !== null) { | |
tempNode = tempNode.left; | |
} | |
node.data = tempNode.data; | |
node.right = removeNode(node.right, tempNode.data); | |
return node; | |
} else if (data < node.data) { | |
node.left = removeNode(node.left, data); | |
return node; | |
} else { | |
node.right = removeNode(node.right, data); | |
return node; | |
} | |
} | |
this.root = removeNode(this.root, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment