1

I have a following mysql table with a lot (3000+) of values:

[id] [parent_id] [string]

and want to print it out as a nested tree (eg. nested uls). Note that it has infinite depth.

I tried nesting functions as in this SO answer, but it was really slow. My other approaches failed, not obeying the infinite depth.

5
  • And your question is...? Commented Mar 17, 2012 at 15:17
  • 1
    3000 records are a pretty manageable amout and has definitely a finite depth. A recursive function may be slow here, but is a good way to travese your 'tree'. With only 3000 records it can't be really slow. There may be an issue in your implementation. -- How do you want to structure the same records as nested elements? Is parent_id the node? Unsure what you want here... -- What is the specific problem or question here? It's a little vague. Commented Mar 17, 2012 at 15:20
  • you want to print out a nested tree with infinite depth? :) did you mean "indefinite"? Commented Mar 17, 2012 at 15:32
  • And my question is: How should I do it? What I want to do is to create a nested array, which I could handle afterwards. Commented Mar 17, 2012 at 16:00
  • @Karoly: no. infinite is rather theoretical. It will probably be around 7 'layers' Commented Mar 17, 2012 at 16:01

1 Answer 1

3

Don't use the adjacency list model if you want infinite depth. You will keep posting questions here for every of the many headaches you will have.

Use the nested set model instead.

Sign up to request clarification or add additional context in comments.

5 Comments

"Don't use the adjacency list model if you want infinite depth." - not true. the problem here is that he needs to grab the whole tree and probably doesn't want to rebuild the tree outside of mysql.
The nested set model solves this problem. Printing the whole tree is much easier with it: you can easily get all the nodes in a print-friendly order. Try to do this with the adjacency list model.
that's just what I said.. in an indirect way.
I thought you were disagreeing with the solution, but now I understand you were actually disagreeing with what the problem is (weren't you?). Sorry I misunderstood. I think it still is part of the problem, as the last sentence of the question shows. But it also makes it easier to write display functions (which may use recursivity).
yep, that's what I thought. the solution is fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.