首先我已經搜索了關於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