到現在爲止,我一直在寫一個節點類作爲Java:我如何實現一個通用的二叉搜索樹?
class Node {
private value;
private Node left;
private Node right;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
和二叉搜索樹爲
public class BinarySearchTree {
private Node root;
public BinarySearchTree(int value) {
root = new Node(value);
}
public void insert(int value) {
Node node = new Node(value);
// insert logic goes here to search and insert
}
}
現在我想支持BinarySearchTree有任何類型的字符串一樣,人們的插入節點
我怎樣才能使它通用持有任何類型?
你嘗試過什麼?你研究過java泛型,你知道關於語法嗎? –