Last active
April 21, 2020 06:31
-
-
Save RaufR/bb7cf43d5f7121d339d0b1f4e86be45f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function Node(data, left, right) { | |
this.data = data; | |
this.left = left; | |
this.right = right; | |
this.show = show; | |
} | |
function show() { | |
return this.data; | |
} | |
function BinarySearchTree() { | |
this.root = null; | |
this.insert = insert; | |
this.inOrder = inOrder; | |
this.preOrder = preOrder; | |
this.postOrder = postOrder; | |
this.getMin = getMin; | |
this.getMax = getMax; | |
this.find = find; | |
this.remove = remove; | |
} | |
// insert | |
function insert(data) { | |
var n = new Node(data, null, null); | |
if (this.root == null) { | |
this.root = n; | |
} else { | |
var current = this.root; | |
var parent; | |
while (true) { | |
parent = current; | |
if (data < current.data) { | |
current = current.left; | |
if (current == null) { | |
parent.left = n; | |
break; | |
} | |
} else { | |
current = current.right; | |
if (current == null) { | |
parent.right = n; | |
break; | |
} | |
} | |
} | |
} | |
} | |
// traverse | |
function inOrder(node) { | |
if (!(node == null)) { | |
inOrder(node.left); | |
node.show(); | |
inOrder(node.right); | |
} | |
} | |
function preOrder(node) { | |
if (!(node == null)) { | |
node.show(); | |
preOrder(node.left); | |
preOrder(node.right); | |
} | |
} | |
function postOrder(node) { | |
if (!(node == null)) { | |
postOrder(node.left); | |
postOrder(node.right); | |
node.show(); | |
} | |
} | |
// Search | |
function getMin() { | |
var current = this.root; | |
while (!(current.left == null)) { | |
current = current.left; | |
} | |
return current.data; | |
} | |
function getMax() { | |
var current = this.root; | |
while (!(current.right == null)) { | |
current = current.right; | |
} | |
return current.data; | |
} | |
function find(data) { | |
var 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; | |
} | |
// remove | |
// Receive the value to be remove. | |
function remove(data) { | |
root = removeNode(this.root, data); | |
} | |
// Main function | |
function removeNode(node, data) { | |
if (node == null) { | |
return null; | |
} | |
if (data == node.data) { | |
// node has no children | |
if (node.left == null && node.right == null) { | |
return null; | |
} | |
// node has no left child | |
if (node.left == null) { | |
return node.right; | |
} | |
// node has no right child | |
if (node.right == null) { | |
return node.left; | |
} | |
// node has two children | |
var tempNode = getSmallest(node.right); | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment