Created
February 16, 2013 16:42
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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