0

Best way to form a Tree

asked 2009-01-16 01:54:12 +0800

evpole gravatar image evpole
481 2

updated 2009-01-16 01:59:16 +0800

we offen have the database like this structure if the date is in tree structure.



ID……Parent_ID………Name………Layer………active
-------------------------------------------------------------------
1…………0…………node1 …………010000 …………0
2…………1…………node1.1 …………010100 …………0
4…………2…………node1.1.1 …………010101 …………0
5…………2…………node1.1.2 …………010102 …………0
3…………1…………node1.2 …………010200 …………0
6…………1…………node1.3 …………010300 …………0
7…………0…………node2 …………020000 …………0
8…………7…………node2.1 …………020100 …………0



we get it like this: select * from categorytable order by layer.
(the layer is used to see which level the node is in)

but how could we form a tree from it ?
i've seen the small talk about SimpleTreeMode. in that sample,it seems have to form treenode from the lowest node to the highest node.
is there any way to form a tree in my order above ?

delete flag offensive retag edit

8 Replies

Sort by » oldest newest

answered 2009-01-16 04:38:32 +0800

nathan gravatar image nathan
57

it should be something as below:
==================================================

MyObject o1_1 = new MyObject("2", "node1.1", "010100", false);

List<SimpleTreeNode> node1_1_children = new ArrayList<SimpleTreeNode>();
node1_1_children.add(stn1_1_1); // skip
node1_1_children.add(stn1_1_2); // skip
SimpleTreeNode stn1_1= new SimpleTreeNode(o1_1, node1_1_children);

MyObject root = new MyObject("1", "node1", "010000", false);

List<SimpleTreeNode> rootChildrenList = new ArrayList<SimpleTreeNode>();
rootChildrenList.add(stn1_1);
rootChildrenList.add(stn1_2); // skip
rootChildrenList.add(stn1_3); // skip
SimpleTreeNode stn1= new SimpleTreeNode(root, rootChildrenList);
simpleTreeModel = new SimpleTreeModel(stn1);

Cheers,
Nathan

link publish delete flag offensive edit

answered 2009-01-16 07:49:35 +0800

evpole gravatar image evpole
481 2

updated 2009-01-16 07:55:26 +0800

Thank you ,nathan.
but your code also fill the lowest chidren-node first,the root node last.
usually we have the data store in above structure in database. so it's easy to get it by "select * from tablename orderby layer".and if we read the Resultset from index 0 to last,it's easy to get the text to form a tree in text appearance(show like above).
but i want to use the tree appearance.Is it possible to fill a tree in this order ?not from lowest chidren-node to root node, but in the order i listed above.that is:
first fill:node 1
then fill: node 1.1
then: node 1.1.1
then:node 1.l.2
then:node 1.2
…………

why need this ?beacause we read it from ResultSet in this order,it's convienence to fill a tree in the same loop as read ResultSet.And if in this order, it's not neccessary to choose out children-nodes in every level.
in another word,if fill a tree like SimpleTreeNode does,it neccessary to choose out the lowest children-node first.then chooose out every level's children-node.and last the root node.(e.g. in the code above , we have to choose out node 1.1.1, node 1.1.2 first)

thanks.

link publish delete flag offensive edit

answered 2009-01-16 12:46:36 +0800

nathan gravatar image nathan
57

updated 2009-01-16 14:08:39 +0800

i guess this can be done by a recursive function, isn't it??

link publish delete flag offensive edit

answered 2009-01-17 02:43:48 +0800

evpole gravatar image evpole
481 2

updated 2009-01-17 02:44:11 +0800

Thanks for advise,i'll have a try.

link publish delete flag offensive edit

answered 2009-01-19 01:38:56 +0800

evpole gravatar image evpole
481 2

updated 2009-01-19 01:39:29 +0800

I've got it work!
our zk is so strong that we even havn't to consider the tree order.
(i could del the layer now for its unuseness.)

Thanks very much, nathan!

link publish delete flag offensive edit

answered 2009-01-19 02:57:36 +0800

nathan gravatar image nathan
57

no worries mate.

Recursive function is always the best way to solve this kind of problem.
Only a few lines of code will do :)

link publish delete flag offensive edit

answered 2009-01-20 03:06:27 +0800

evpole gravatar image evpole
481 2

yes, that's right!
Thanks very much! :)

link publish delete flag offensive edit

answered 2015-08-07 20:40:08 +0800

jasf90 gravatar image jasf90
1

friend how are you I am in a case like yours and I wonder if I could share the recursive function q've done'm in a dilemma and I'm very confused ... I know this topic this since 2009 but I'm new ZK .. help me Please ... my email is [email protected] I certainly do not speak much English because I'm Latin, well I appreciate any help you much

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-01-16 01:54:12 +0800

Seen: 236 times

Last updated: Aug 07 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More