Java语言中的二叉树 [英] binary tree in java language

查看:63
本文介绍了Java语言中的二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中有二叉树来建立家庭成员树.

在添加功能中,我想确定将要有新孩子的父母,然后,用户输入新孩子的名字.

我面临的问题是家谱无法比较,例如该树具有整数.

有没有办法做到这一点?

这是我代码中的主要内容

I have binary tree in java to build family members tree.

In add function, I want to determine the parent that will have new child, after that, the user enter the name of new child.

The problem I face is the family tree cannot compare such as the tree has integer numbers.

Is there a method to do this?

This is a main in my code

static public void main(String args[])
  {
      System.out.println(">>> creating empty tree");
      StringBinaryTree t = new StringBinaryTree();
      System.out.println(">>> adding elements to the tree");
      t.add("lion","");
      t.add("shark","r");
      t.add("elephant","l");
      t.add("zebra","rr");
  }


感谢您的帮助.


Thanks for the help

推荐答案

您可以实现一个比较器,该比较器传递给构造函数,该构造函数将用于确定需要在何处添加子项.您可以自己在比较器中指定如何完成此操作.
http://www.javadeveloper.co.in/java-example/java-comparator- example.html [ ^ ]

祝你好运!
You could implement an comparator that you pass to the constructor that will be used to determine where the child needs to be added. You can specify how this should be done in the comparator yourself.
http://www.javadeveloper.co.in/java-example/java-comparator-example.html[^]

Good luck!


这篇关于Java语言中的二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆