声明一个二叉树,它采用包含通用类型键值的泛型类型的节点? [英] Declare a Binary Tree which takes a Generic type of Node which contains a Generic type of key value?
问题描述
class Node< p>如果我有一个节点类可以接受通用类型的键值, K扩展了Comparable< K>>实现可比< Node< K> {
...
}
class KeyValueNode< K extends Comparable< K> ;, V>扩展节点< K> {
...
}
是否可以声明通用二进制树类接受一个通用类型的节点,它可以包含一个通用类型的键值?我认为它会看起来像这样....
class BinaryTree< N< K>> {
N< K>根;
BinaryTree< N< K>>左右;
...
}
对于任何明显的误解,仍然试图在Java中获得泛型和语法的悬挂,将非常感谢任何帮助或见解。
谢谢!
对根节点的引用。因此,它应该具有与其节点相同的类型参数:
class BinaryTree< K extends Comparable< K>> {
节点< K>根;
}
或者对于键值设计:
class KeyValueBinaryTree< K extends Comparable< K>,V> {
KeyValueNode< K,V>根;
$ / code>
请注意,是否需要封装树类是值得商榷的,因为它是节点指向彼此。
If I have a node class(es) that can accept a generic type for it's key value:
class Node<K extends Comparable<K>> implements Comparable<Node<K> {
...
}
class KeyValueNode<K extends Comparable<K>, V> extends Node<K> {
...
}
Is it possible to declare a generic binary tree class that accepts a generic type of node, which can contain a generic type of key value? I thought it would look something like this....
class BinaryTree<N<K>> {
N<K> root;
BinaryTree<N<K>> left, right;
...
}
Apologies for any glaring misunderstandings, I'm still trying to get the hang of generics and the syntax in Java, would greatly appreciate any help or insights.
Thanks!
A binary tree structure will essentially just hold a reference to the root node. So it should have the same type parameters as its nodes:
class BinaryTree<K extends Comparable<K>> {
Node<K> root;
}
Or for the key-value design:
class KeyValueBinaryTree<K extends Comparable<K>, V> {
KeyValueNode<K, V> root;
}
Note that it's debatable whether an enclosing tree class is necessary, since it's the nodes that are pointing to each other.
这篇关于声明一个二叉树,它采用包含通用类型键值的泛型类型的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!