Created
March 22, 2017 00:55
-
-
Save Millsky/9a1ae3c8133f22561c5a922a1d067be7 to your computer and use it in GitHub Desktop.
A binary search tree with insert - non self balancing
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 { | |
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; | |
} | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typescript Implementation of a binary search tree - insertion only