Java二叉树,Node如何实现?
在树类中,我应该比较两个节点,因为您知道搜索和添加项目。我对如何使其具有可比性有一些问题。当向树添加数据(通用的,任何东西)时,我们调用 Tree 类,然后该类创建一个新的 Node 对象。如何在 Node 类中声明变量数据/元素,使其为 E 类型(任何类型)并且仍然可比较?说真的,我来回尝试过,但没有得出任何结论。
In the tree class I'm suppose to compare two node, for you know searching and adding items. I have some issues with how to make it comparable. When one adds data(generic, anything) to the tree one calls the Tree class which then makes a new Node object. How can I declare the variable data/element in the Node class so that it is of type E (anything) and still Comparable? Seriously, I've tried back and forth without concluding with anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并非一切都可以
比较
。你的要求是自相矛盾的。您可以通过声明通用参数来将E
限制为可比较的,如下所示:这样,该类的使用者就可以使用所有实现了
Comparable
接口的类。您将能够访问键入E
的内容的compareTo
方法。Not everything is
Comparable
. Your requirement is self-contradictory. You can constrainE
to be comparable by declaring the generic parameter like:This way, the consumer of the class can use all classes that implement
Comparable
interface with it. You'll be able to access thecompareTo
method on things typedE
.