summaryrefslogtreecommitdiff
path: root/tree.js
diff options
context:
space:
mode:
authorJulie Lala <jules@okfoc.us>2014-04-14 09:22:07 -0400
committerJulie Lala <jules@okfoc.us>2014-04-14 09:22:07 -0400
commit2fdd2f4ee268b8d159e57a91bb0dbf977f6fecef (patch)
tree597d43b1c42252ea6bb6e6897a3338967bf6aec2 /tree.js
parentad3c9f89e76b1221fb6ee096f0ad7c0c395f000c (diff)
naive tree implementation
Diffstat (limited to 'tree.js')
-rw-r--r--tree.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/tree.js b/tree.js
new file mode 100644
index 0000000..8229cae
--- /dev/null
+++ b/tree.js
@@ -0,0 +1,24 @@
+var tree = function(n, data){
+ this.lo = null
+ this.hi = null
+ this.value = n
+ this.data = data
+}
+tree.prototype.find = function(n){
+ if (n == this.value) return this
+ if (n < this.value) return this.lo ? this.lo.find(n) : this
+ if (n > this.value) return this.hi ? this.hi.find(n) : this
+}
+tree.prototype.add = function(n, data){
+ var closest = this.find(n)
+ if (n == closest.value) return
+ if (n < closest.value) closest.lo = new tree(n, data)
+ if (n > closest.value) closest.hi = new tree(n, data)
+}
+tree.prototype.toString = function(){
+ var s = "";
+ if (this.lo) s += this.lo.toString()
+ s += this.value + ","
+ if (this.hi) s += this.hi.toString()
+ return s
+}