Skip to content

Instantly share code, notes, and snippets.

@jesslilly
Created February 5, 2014 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesslilly/8827192 to your computer and use it in GitHub Desktop.
Save jesslilly/8827192 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var a = require( "./tree" );
/*
* 1A
* 2A 2B 2C
* 3A 3B 3C
* 4A 4B 4C
* 5A
*/
console.log("=============== organic tree ================");
var tree1 = new a.Tree("1A");
var _2a = tree1.add("2A");
_2a.add("3A");
_2a.add("3B");
tree1.add("2B");
var _3c = tree1.add("2C").add("3C");
_3c.add("4A");
_3c.add("4B");
_3c.add("4C").add("5A");
tree1.print();
console.log("Num Nodes:" + tree1.countNodes());
console.log("Depth:" + tree1.getDepth());
console.log("=============== small tree ================");
var tree2 = new a.Tree("SMALLTREE");
tree2.print();
console.log("Num Nodes:" + tree2.countNodes());
console.log("Depth:" + tree2.getDepth());
/*
* A
* B C
* D E F G
* H I
*/
console.log("=============== binary tree ================");
var tree3 = new a.Tree("A");
var b = tree3.add("B");
var c = tree3.add("C");
var d = b.add("D");
b.add("E");
d.add("H");
d.add("I");
c.add("F");
c.add("G");
tree3.print();
console.log("Num Nodes:" + tree3.countNodes());
console.log("Depth:" + tree3.getDepth());
/*
* @description Node module for a Tree object.
* Constructor example:
* var tree = new Tree("A");
* Add example:
* tree.add("B").add("C");
* tree.add("D");
* This will create a tree like this:
* A
* B D
* C
*/
/*
* Constructor
* @param val, an object start the tree.
*/
var Tree = function(val) {
var value = val;
var leaves = [];
this.getValue = function() {
return value;
};
this.getLeaves = function() {
return leaves;
};
this.hasLeaves = function() {
return leaves.length > 0;
};
this.numLeaves = function() {
return leaves.length;
};
};
/*
* Add a value to a node in the tree.
* You can add as many nodes at any level. (It's not a binary tree)
* Returns the new Tree that was added for call chaining like:
* var tree = new Tree("A");
* tree.add("B").add("C");
* @param value/object to add to the tree.
* @return the new Tree that was added.
*/
Tree.prototype.add = function(value) {
var leaves = this.getLeaves();
var leaf = new Tree(value);
leaves.push(leaf);
// For chaining.
return leaf;
};
/*
* Print out the values in the tree in a simple parent, children pairing.
*/
Tree.prototype.print = function() {
console.log("Parent: " + this.getValue());
var levelString = "";
this.getLeaves().forEach(function(leaf, index) {
levelString += leaf.getValue() + " ";
});
console.log("Leaves: " + levelString);
this.getLeaves().forEach(function(node, index) {
if (node.hasLeaves()) {
node.print();
}
});
};
/*
* @return the number of nodes in the tree.
*/
Tree.prototype.countNodes = function() {
var count = 0;
var countNodesWorker = function(node) {
if (node.hasLeaves()) {
count = node.numLeaves();
//console.log("Node " + node.getValue() + " has " + node.numLeaves());
node.getLeaves().forEach(function(node, index) {
count += countNodesWorker(node);
});
return count;
}
return 0;
};
// Add one for the parent node.
return countNodesWorker(this) + 1;
};
/*
* @return the depth of the tree.
*/
Tree.prototype.getDepth = function() {
var maxLevel = 0;
var getDepthWorker = function(node, curLevel) {
curLevel++;
//console.log("Node " + node.getValue() + " level " + curLevel);
if (curLevel > maxLevel) {
maxLevel = curLevel;
}
node.getLeaves().forEach(function(node, index) {
getDepthWorker(node, curLevel);
});
};
getDepthWorker(this, 0);
return maxLevel;
};
exports.Tree = Tree;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment