Skip to content

Instantly share code, notes, and snippets.

@amosgwa
Forked from anonymous/BOGO.js
Created September 17, 2016 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amosgwa/b23545b66f4a56765278c4c9452b3abc to your computer and use it in GitHub Desktop.
Save amosgwa/b23545b66f4a56765278c4c9452b3abc to your computer and use it in GitHub Desktop.
https://repl.it/DD97/41 created by amosgwa
function Node(data) {
this.data = data;
this.parent = null;
this._left = null;
this._right = null;
}
function biTree(data) {
var node = new Node(data);
this._root = node;
}
biTree.prototype.push = function(v) {
var node = new Node(v);
if(!this._root) {
this._root = node;
return;
}
var currNode = this._root;
while(currNode){
if(v > currNode.data) {
if(!currNode._right){
currNode._right = node;
break;
}
currNode = currNode._right;
} else {
if(!currNode._left){
currNode._left = node;
break;
}
currNode = currNode._left;
}
}
};
biTree.prototype.inOrder = function(b){
(function recurse(currNode, b){
if(currNode._left) recurse(currNode._left,b);
b.push(currNode.data)
console.log(currNode.data);
if(currNode._right) recurse(currNode._right,b);
})(this._root,b);
};
biTree.prototype.postOrder = function() {
(function recurse(currNode) {
if(currNode._left) recurse(currNode._left);
if(currNode._right) recurse(currNode._right);
console.log(currNode.data);
})(this._root);
};
biTree.prototype.preOrder = function() {
(function recurse(currNode) {
console.log(currNode.data);
if(currNode._left) recurse(currNode._left);
if(currNode._right) recurse(currNode._right);
})(this._root);
};
biTree.prototype.dfs = function() {
this.preOrder();
};
biTree.prototype.bfs = function(){
(function recurse(currNode){
var queue = [];
queue.push(currNode);
while(queue.length !== 0){
currNode = queue.shift();
console.log(currNode.data);
if(currNode._left){
queue.push(currNode._left);
}
if(currNode._right){
queue.push(currNode._right);
}
}
})(this._root);
};
biTree.prototype.height = function() {
console.log((function recurse(currNode) {
if(!currNode){
return -1;
}
return Math.max(recurse(currNode._left), recurse(currNode._right)) + 1;
})(this._root));
};
biTree.prototype.min = function() {
console.log((function recurse(currNode){
if(!currNode) return;
if(currNode._left) return recurse(currNode._left);
return currNode.data;
})(this._root));
};
biTree.prototype.max = function() {
console.log((function recurse(currNode){
if(!currNode) return;
if(currNode._right) return recurse(currNode._right);
return currNode.data;
})(this._root));
};
function isBST(tree) {
var b = [];
tree.inOrder(b);
for(var i=1; i < b.length; b++){
if(b[i] <= b[i-1]) return false;
}
return true;
}
var newtree = new biTree(5);
newtree.push(2);
newtree.push(20);
newtree.push(10);
newtree.push(25);
newtree.push(1);
console.log("\nIn Order");
var b = [];
newtree.inOrder(b);
console.log(b);
console.log("\nPost Order");
newtree.postOrder();
console.log("\nPre Order");
newtree.preOrder();
console.log("\nHeight");
newtree.height();
console.log("\nMinimum");
newtree.min();
console.log("\nMaximum");
newtree.max();
console.log("\nBFS");
newtree.bfs();
console.log("\nDFS");
newtree.dfs();
console.log("\nisBST", isBST(newtree));
Native Browser JavaScript
>>>
In Order
1
2
5
10
20
25
[ 1, 2, 5, 10, 20, 25 ]
Post Order
1
2
10
25
20
5
Pre Order
5
2
1
20
10
25
Height
2
Minimum
1
Maximum
25
BFS
5
2
20
1
10
25
DFS
5
2
1
20
10
25
1
2
5
10
20
25
isBST false
=> undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment