简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM