summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/tree.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-06-03 17:51:34 -0400
committerJules Laplace <jules@okfoc.us>2014-06-03 17:51:34 -0400
commit90142bd07f926ef8a7f3ea86a563ec0ca648ca5d (patch)
tree2851e990b4b71e215ad2fff4dbcaf80229953c35 /public/assets/javascripts/rectangles/models/tree.js
parent607f69c67a5b4dc72d2754192e3cdf67d0ad11d0 (diff)
pulling in more stuff from posthang
Diffstat (limited to 'public/assets/javascripts/rectangles/models/tree.js')
-rw-r--r--public/assets/javascripts/rectangles/models/tree.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/models/tree.js b/public/assets/javascripts/rectangles/models/tree.js
new file mode 100644
index 0000000..8193988
--- /dev/null
+++ b/public/assets/javascripts/rectangles/models/tree.js
@@ -0,0 +1,37 @@
+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 closest
+ if (n < closest.value) return closest.lo = new Tree(n, data)
+ if (n > closest.value) return closest.hi = new Tree(n, data)
+}
+Tree.prototype.toArray = function(){
+ var a = []
+ if (this.lo) a = a.concat(this.lo.toArray())
+ a.push(this.data)
+ if (this.hi) a = a.concat(this.hi.toArray())
+ return a
+}
+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
+}
+Tree.prototype.depth = function(){
+ if (this.lo && this.hi) return 1 + max(this.lo.depth(), this.hi.depth())
+ else if (this.lo) return 1 + this.lo.depth()
+ else if (this.hi) return 1 + this.hi.depth()
+ else return 0
+}