Saving nltk drawn parse tree to image file

Using the nltk.draw.tree.TreeView object to create the canvas frame automatically: >>> from nltk.tree import Tree >>> from nltk.draw.tree import TreeView >>> t = Tree.fromstring(‘(S (NP this tree) (VP (V is) (AdjP pretty)))’) >>> TreeView(t)._cframe.print_to_file(‘output.ps’) Then: >>> import os >>> os.system(‘convert output.ps output.png’) [output.png]:

The possible number of binary search trees that can be created with N keys is given by the Nth catalan number. Why?

Since there are four proofs in the wikipedia article you referenced, it seems you aren’t looking for a mathematical explanation for the correspondence between the Catalan numbers and the permutations of a binary tree. So instead, here are two ways to try and intuitively visualise how the Catalan sequence (1, 2, 5, 14, 42, …) … Read more

PHP tree structure for categories and sub categories without looping a query

This does the job: $items = array( (object) array(‘id’ => 42, ‘parent_id’ => 1), (object) array(‘id’ => 43, ‘parent_id’ => 42), (object) array(‘id’ => 1, ‘parent_id’ => 0), ); $childs = array(); foreach($items as $item) $childs[$item->parent_id][] = $item; foreach($items as $item) if (isset($childs[$item->id])) $item->childs = $childs[$item->id]; $tree = $childs[0]; print_r($tree); This works by first indexing … Read more

How to create a collapsing tree table in html/css/js?

SlickGrid has this functionality, see the tree demo. If you want to build your own, here is an example (jsFiddle demo): Build your table with a data-depth attribute to indicate the depth of the item in the tree (the levelX CSS classes are just for styling indentation):  <table id=”mytable”> <tr data-depth=”0″ class=”collapse level0″> <td><span class=”toggle … Read more

How to render a tree in Twig

I played around with domi27’s idea and came up with this. I made a nested array as my tree, [‘link’][‘sublinks’] is null or another array of more of the same. Templates The sub-template file to recurse with: <!–includes/menu-links.html–> {% for link in links %} <li> <a href=”https://stackoverflow.com/questions/8326482/{{ link.href }}”>{{ link.name }}</a> {% if link.sublinks %} … Read more

Definition of a Balanced Tree

The constraint is generally applied recursively to every subtree. That is, the tree is only balanced if: The left and right subtrees’ heights differ by at most one, AND The left subtree is balanced, AND The right subtree is balanced According to this, the next tree is balanced: A / \ B C / / … Read more

What is the left-child, right-sibling representation of a tree? Why would you use it?

The left-child, right-sibling representation (LCRS) is a way of encoding a multi-way tree (a tree structure in which each node can have any number of children) using a binary tree (a tree structure in which each node can have at most two children). Motivation To motivate how this representation works, let’s begin by considering a … Read more

Construct hierarchy tree from flat list with parent field? [duplicate]

function treeify(list, idAttr, parentAttr, childrenAttr) { if (!idAttr) idAttr=”id”; if (!parentAttr) parentAttr=”parent”; if (!childrenAttr) childrenAttr=”children”; var treeList = []; var lookup = {}; list.forEach(function(obj) { lookup[obj[idAttr]] = obj; obj[childrenAttr] = []; }); list.forEach(function(obj) { if (obj[parentAttr] != null) { if (lookup[obj[parentAttr]] !== undefined) { lookup[obj[parentAttr]][childrenAttr].push(obj); } else { //console.log(‘Missing Parent Data: ‘ + obj[parentAttr]); treeList.push(obj); … Read more