From d6e459e2808ea16a4a0715e294182bafe74d310c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 15 Apr 2014 16:08:40 -0400 Subject: rearranging things --- assets/javascripts/rectangles/tree.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 assets/javascripts/rectangles/tree.js (limited to 'assets/javascripts/rectangles/tree.js') diff --git a/assets/javascripts/rectangles/tree.js b/assets/javascripts/rectangles/tree.js new file mode 100644 index 0000000..f5eb117 --- /dev/null +++ b/assets/javascripts/rectangles/tree.js @@ -0,0 +1,30 @@ +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.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 +} -- cgit v1.2.3-70-g09d2