二叉树到JTree?
我正在尝试将由节点组成的 BinaryTree 转换为 GUI 视图的 JTree。我认为这是我需要的伪代码:
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
关于如何实际实现这个伪代码的任何想法?我知道需要进行一些递归才能使其影响所有子节点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要遍历树,而是实施
TreeModel
接口,以便根据JTree
的请求获取树的节点。示例可以在创建数据模型。附录:
FileSystemModel
是一个相关示例。Instead of traversing your tree, implement the
TreeModel
interface so that it fetches the tree's nodes as requested by theJTree
. Examples may be found in Creating a Data Model.Addendum:
FileSystemModel
is a related example.