Derek
Derek

Reputation: 9943

BinaryTree to JTree?

I am trying to convert a BinaryTree which is made of Nodes into a JTree for a GUI view. I figure this is the pseudo-code I need:

if root == null
        set data  as root
 if data < root
        if leftNode == null
            add data to left node
        if data < leftNode
            add data to left node
        if data > leftNode
            add data to right node
 if data > root
        if rightNode == null
            add data to right node
        if data < right node
            add data to left node
        if data > right node
            add data to right node

Any ideas on how to actually implement this pseudo code? I know there needs to be some recursion to get this to affect all the child nodes.

Upvotes: 2

Views: 779

Answers (1)

trashgod
trashgod

Reputation: 205865

Instead of traversing your tree, implement the TreeModel interface so that it fetches the tree's nodes as requested by the JTree. Examples may be found in Creating a Data Model.

Addendum: FileSystemModel is a related example.

Upvotes: 4

Related Questions