Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Find Closest</title>
</head>
<body>
Enter elements delimited by comma and press enter:
<br><br>
<input type="text" id="input">
<br><br>
Enter the number:
<br><br>
<input type="text" id="number">
<br><br>
<input type="button" id="submit" value="Submit" onclick="processInput()">
<input type="button" id="print" value="Print" onclick="printBst()">
<p id="output"></p>
<script>
var input,number,output;
var bst;
function processInput() {
bst = new BinarySearchTree();
input = document.getElementById('input').value;
number = document.getElementById('number').value;
output = document.getElementById('output');
if(number === "") {
alert("Enter a valid number");
return;
}
var array = input.split(",");
// bst.root = new Node(array[0]);
for (var i = 0; i < array.length; i++) {
array[i] = array[i].trim();
bst.insert(bst.root, array[i]);
}
document.getElementById('output').innerHTML = mode(array, number);
}
function printBst() {
bst.printTree(bst.root);
}
function mode(array, number) {
// console.log("mode > " + bst.root.data);
// console.log("mode > " + bst.root.right.data);
// console.log("mode > " + bst.root.data);
// console.log("mode > " + bst.root.data);
// console.log("mode > " + bst.root.data);
// console.log("mode > " + bst.root.data);
// console.log("mode > " + bst.root.data);
var node = bst.searchTree(bst.root, number);
return node.data;
}
class BinarySearchTree {
constructor() {
this.root = null;
}
insert(node, data) {
// console.log(node);
var newnode = new Node(data);
if(node === null) {
this.root = newnode;
return;
}
// console.log(newnode);
if(parseInt(newnode.data) < parseInt(node.data)) {
if(node.left === null) {
node.left = newnode;
} else {
this.insert(node.left, data);
}
} else if(parseInt(newnode.data) > parseInt(node.data)) {
if(node.right === null) {
node.right = newnode;
} else {
this.insert(node.right, data);
}
}
}
searchTree(nodeOne, data) {
// console.log(nodeOne);
// console.log(data);
// var ans;
if(nodeOne === null || parseInt(nodeOne.data) === parseInt(data)) {
return nodeOne;
} else if(parseInt(data) < parseInt(nodeOne.data)){
if(nodeOne.left === null) {
return nodeOne;
}
console.log(parseInt(nodeOne.data));
return this.searchTree(nodeOne.left, data);
} else if(parseInt(data) > parseInt(nodeOne.data)){
if(nodeOne.right === null) {
return nodeOne;
}
console.log(parseInt(nodeOne.data));
return this.searchTree(nodeOne.right, data);
}
// return ans;
}
printTree(node) {
if(node === null) {
return;
}
this.printTree(node.left);
console.log(node)
this.printTree(node.right);
}
}
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.