Skip to content

Instantly share code, notes, and snippets.

@Millsky
Created March 22, 2017 00:55
Show Gist options
  • Save Millsky/9a1ae3c8133f22561c5a922a1d067be7 to your computer and use it in GitHub Desktop.
Save Millsky/9a1ae3c8133f22561c5a922a1d067be7 to your computer and use it in GitHub Desktop.
A binary search tree with insert - non self balancing
class node {
value: any
left: any
right: any
height: number
balanceFactor(){
var bf = (this.right.height) - (this.left.height);
}
constructor(v,h) {
this.value = v;
this.height = h;
}
}
class bst {
root: any
addValue(v) {
if (!this.root) {
this.root = new node(v,0);
return;
}
var currentNode = this.root;
var height = 0;
while (currentNode) {
height++;
if (v < currentNode.value) {
if (!currentNode.left) {
currentNode.left = new node(v,height);
break;
} else {
currentNode = currentNode.left;
}
} else {
if (!currentNode.right) {
currentNode.right = new node(v,height);
break;
} else {
currentNode = currentNode.right;
}
}
}
}
}
@Millsky
Copy link
Author

Millsky commented Mar 22, 2017

Typescript Implementation of a binary search tree - insertion only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment