summaryrefslogtreecommitdiff
path: root/tree.js
blob: 8229caee793d525b24dbd93d833e1f4a1be3c555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
}