首先我已经搜索了关于java中泛型类型的用法,但是我发现的答案太简单或复杂了。所以这是我确切的问题。在Java中使用泛型类型的树实现
我有三个类分别PerfectTreeControl,Tree和Entry。
树有
public class Tree<K> { public Entry <K> root;
条目已
public class Entry<K> {
public K element;
public Entry<K> parent, left_child, right_child;
public Entry(K element) {
this.element = element;
}
public Entry(K element, Entry<K> left, Entry<K> right) {
left_child = left;
right_child = right;
this.element = element;
}
我想了解什么是进入母体和Entry <ķ>父母之间有什么区别?我知道K element
可以用作整数,字符串或任何我想要的,但是对于对象来说是否也是这样?我试图使用不带参数的Entry变量,它只是说Entry是一个原始类型,应该参数化并且它仍然没有错误地工作。
我的第二个问题是关于检查树是否完美。下面是一些代码到目前为止,我已经试过:
public class PerfectTreeControl {
public static boolean isPerfect(Tree<String> tree) {
Tree t1 = new Tree();
if(t1.isFull(tree.root)) {
int depth = t1.height(tree.root);
return t1.everyLeafHasSameDepth(tree.root, depth);
}
else
return false;
}
}
public class Tree<K> {
public Entry <K> root;
public boolean isLeaf(Entry e) {
return e.left_child == null &&
e.right_child == null;
}
public int height(Entry e) {
if(e == null ||
e.left_child == null &&
e.right_child == null)
return 0;
int left = height(e.left_child);
int right = height(e.right_child);
return 1 + Math.max(left, right);
}
public boolean isFull(Entry base) {
if(isLeaf(base))
return true;
else
if(base.left_child != null && base.right_child != null) {
return isFull(base.left_child) &&
isFull(base.right_child);
} else {
return false;
}
}
public int depth(Entry e) {
if(e == root) {
return 0;
} else {
return 1 + depth(e.parent);
}
}
public boolean everyLeafHasSameDepth(Entry base, int depth) {
if(base == null)
return false;
else if(isLeaf(base))
return depth(base) == depth;
else {
return
everyLeafHasSameDepth(base.left_child, depth) &&
everyLeafHasSameDepth(base.right_child, depth);
}
}
- 入门级(我写在页面的顶部)如你所见,在PerfectTreeControl类isPerfect方法使用树-String-树作为参数,我不知道它是什么。在Tree类中,我尝试了Entry,并且再次没有区别。代码将无法正常工作,我完全困惑。
你不应该担心原始类型。它们只是为了向后兼容,并且不应该使用通用类型的原始版本。 (即在你的代码中,无处不在使用'Entry'。) –
millimoose
定义通用对象有什么意义?更清楚的是,将对象定义为泛型而不是普通对象有什么优势?对象已经可以将你在类中定义的任何东西当作一个变量,字符串,int等等。我错过了什么? – nihirus
@nihirus:如果您在任何地方使用普通对象,则每次需要将值转换为所需类型时,都需要执行某些操作。 – Tudor