Skip to content

Instantly share code, notes, and snippets.

@seungjin
Created August 17, 2009 21:13
Show Gist options
  • Save seungjin/169384 to your computer and use it in GitHub Desktop.
Save seungjin/169384 to your computer and use it in GitHub Desktop.
/**
* from Flare demo
* Demos/uutils/GraphUtils.as
*/
/**
* Create a diamond tree, with a given branching factor at
* each level, and depth levels for the two main branches.
* @param b the number of children of each branch node
* @param d1 the length of the first (left) branch
* @param d2 the length of the second (right) branch
* @return the generated Tree
*/
public static function diamondTree(b:int, d1:int, d2:int) : Tree
{
var tree:Tree = new Tree();
var n:NodeSprite = tree.addRoot();
var l:NodeSprite = tree.addChild(n);
var r:NodeSprite = tree.addChild(n);
deepHelper(tree, l, b, d1-2, true);
deepHelper(tree, r, b, d1-2, false);
while (l.firstChildNode != null)
l = l.firstChildNode;
while (r.lastChildNode != null)
r = r.lastChildNode;
deepHelper(tree, l, b, d2-1, false);
deepHelper(tree, r, b, d2-1, true);
return tree;
}
private static function deepHelper(t:Tree, n:NodeSprite,
breadth:int, depth:int, left:Boolean) : void
{
var c:NodeSprite = t.addChild(n);
if (left && depth > 0)
deepHelper(t, c, breadth, depth-1, left);
for (var i:uint = 1; i<breadth; ++i) {
c = t.addChild(n);
}
if (!left && depth > 0)
deepHelper(t, c, breadth, depth-1, left);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment