Tagged Questions
191
votes
13answers
23k views
What is the most efficient/elegant way to parse a flat table into a tree?
Assume you have a flat table that stores an ordered tree hierarchy:
Id Name ParentId Order
1 'Node 1' 0 10
2 'Node 1.1' 1 10
3 'Node 2' 0 ...
20
votes
2answers
4k views
Twig - How to render a tree
I would like to render a tree with an undetermined depth (children of children of children etc...). I need to loop through the array recursively, how can I do this in Twig?
Regards,
Ron
15
votes
5answers
16k views
PHP tree structure for categories and sub categories without looping a query
I'm trying to create a list of categories with any number of sub categories, where sub categories can also has their own sub categories.
I have selected all categories from the Mysql db, the cats are ...
12
votes
5answers
476 views
Using iterative style to clone an object in JavaScript
Is it possible to rewrite the following JavaScript recursive function to make it faster?
function clone_recursive(object) {
var result = {};
for (var key in object) {
var value = ...
12
votes
3answers
3k views
Hitting Maximum Recursion Depth Using Python's Pickle / cPickle
The background: I'm building a trie to represent a dictionary, using a minimal construction algorithm. The input list is 4.3M utf-8 strings, sorted lexicographically. The resulting graph is acyclic ...
11
votes
5answers
3k views
Traverse tree without recursion and stack in C
How to traverse each node of a tree efficiently without recursion in C (no C++)?
Suppose I have the following node structure of that tree:
struct Node
{
struct Node* next; /* sibling node ...
11
votes
7answers
1k views
What's a good way to rewrite this non-tail-recursive function?
For some reason, I am having trouble thinking of a good way to rewrite this function so it uses constant stack space. Most online discussions of tree recursion cheat by using the Fibonacci function ...
10
votes
13answers
17k views
Counting nodes in a tree in Java
First of all, I swear this is not homework, it's a question I was asked in an interview. I think I made a mess of it (though I did realise the solution requires recursion). Here is the question:
...
9
votes
9answers
7k views
Binary Tree: Longest path between 2 Nodes
Calculate the longest path between two nodes.
The path is in an arch.
Signature of method is:
public static int longestPath(Node n)
In the example binary tree below, it is 4 (going thru ...
9
votes
2answers
1k views
Converting Directed Acyclic Graph (DAG) to tree
I'm trying to implement algoritm to convert Directed Acyclic Graph to Tree (for fun, learining, kata, name it). So I come up with the data structure Node:
/// <summary>
/// Represeting a node ...
6
votes
3answers
523 views
N-Tree Traversal with Scala Causes Stack Overflow
I am attempting to return a list of widgets from an N-tree data structure. In my unit test, if i have roughly about 2000 widgets each with a single dependency, i'll encounter a stack overflow. What ...
6
votes
5answers
2k views
Binary Tree Depth Problem
I read a few other articles on here that looked similar, but didn't quite answer my problem. I've been given a question for an assignment to assign every node in a binary tree its respective depth. I ...
6
votes
2answers
412 views
How to convert recursion into iteration with LoadingCache?
I completely rewrote this question as the original one was unsolvable. In order to keep it simple I'm using Fibonacci numbers a toy example.
The trivial recursive cached computation ends with a very ...
6
votes
4answers
162 views
Find all paths through a tree (nested dicts) from top to bottom
EDIT: See below for a suggested answer and how it's not quite right yet.
There are many similar questions to this one on Stack Overflow, but none exactly like it in Python. I'm a programming novice, ...
5
votes
3answers
3k views
Python file parsing: Build tree from text file
I have an indented text file that will be used to build a tree. Each line represents a node, and indents represent depth as well as node the current node is a child of.
For example, a file might look ...