• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copy subtree in jtree

 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine, if you will, a method that makes a copy of a subtree from an existing swing.jTree filled with string objects. Having this subtree copied and saved in a DefaultMutableTreeNode variable, would allow you to paste said subtree anywhere you would like in the tree. Below is my last fruitless attempt to do the infuriating. Can anyone help me to understand why the path information is being lost? All this does is collect the children, subchildren, subsubchildren etc… and paste them all as one level. It loses all directory structure. Any help leading me to a solution that you can provide would be greatly appreciated.


 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have expected to see a recursive method to clone a tree. And I would have expected the method to take a parameter containing the root of the tree to be cloned. But I don't see either of those things. Perhaps you could explain in detail how the method decides which tree is supposed to be cloned and how you access all the descendants of the root of that tree, because I don't see any code like that in your post.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, ok. The jTree1.getSelectionPath().getLastPathComponent() is grabbing and cloning a node (which is selected when this method is called) for use as a root for the new subtree.
I then use the current selected location (saved in "theSource") to guide me down the existing path of the original jtree. I use a for loop to allow the correct number of loops for the number of children. inside the loop I use the *.getNextNode(); which uses the inorder traversal of a tree, starting at "theSource" which is the subtree I wish to copy. I have the ‘if’ statement to check to make sure that after the *.getNextNode is called that it is not null. If it isn't, I clone the child and add it to the subtree root named "theClone".

Now that that is nice and explained, let’s just say that I am aware that this code is garbage and should be set on fire at my first opportunity. I get that I am cloning nodes and not saving path information in this method. I guess, I assumed that there was a pre-existing method that would do an inorder traversal of the tree from a selected location inorder to allow the copying of subtrees. I am beginning to believe that no such thing exists and I will be forced to write my own recursive traversal. It seems a shame as I see that it has been coded in many locations, like the enumerations for example. I feel like I am re-inventing the wheel.

Now that my book of moaning and groaning has been completed, perhaps I could ask for some idea on how to properly implement the subtree copy.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are looking at all of the descendants of the original root, in some order, and adding them all as immediate children to the new root.

Here's pseudo-code for what a tree-cloning method should look like:
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oddly enough, I figured it out on my own. Looking at your pseudocode makes me think I may have made it more complecated than it should have been. Perhaps you can look at this code and tell me if you think it is acceptablly written (i.e. sound reasoning).
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham:
You have been more help in a few hours than countless people have been in days. Thank you.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one improvement: you're iterating over the children twice. You can combine those two loops:
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boss! AKA nice! I love to see my code optimized. I sometimes forget in the excitement of getting it to work, that efficiency is the next step before you are really done. While two loops that are non-nested moved to one does not change Big-O it does change your little 'o'. Sometimes that is the difference between selling your code and not selling your code. Thank you.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jake benn wrote:I love to see my code optimized. I sometimes forget in the excitement of getting it to work, that efficiency is the next step before you are really done.

Well, at least you got the main principle of programming right: get it to work first, worry about performance afterwards.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic