Skip to content

Instantly share code, notes, and snippets.

@0xjjpa
Created February 16, 2013 16:42
Show Gist options
  • Save 0xjjpa/4967612 to your computer and use it in GitHub Desktop.
Save 0xjjpa/4967612 to your computer and use it in GitHub Desktop.
Simple binary tree structure in a C-like syntax (no class, just node wrapper)
var Node = function(v, l, r) {
this.value = v; this.left = l || {}; this.right = r || {};
}
Node.prototype.addChildren = function(number) {
if(number) {
if(this.value) {
Node.prototype.parent = this;
if(this.value > number) {
Node.prototype.direction = "left";
Node.prototype.addChildren.call(this.left, number);
} else {
Node.prototype.direction = "right";
Node.prototype.addChildren.call(this.right, number);
}
} else {
Node.prototype.direction === "left" ?
Node.prototype.parent.left = new Node(number) :
Node.prototype.parent.right = new Node(number);
return Node.prototype.parent;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment