Cho đến bây giờ, tôi đã viết một lớp Node nhưJava: Làm thế nào để thực hiện một cây tìm kiếm nhị phân chung?
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;
}
}
và Binary Search Tree như
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
}
}
Bây giờ tôi muốn hỗ trợ BinarySearchTree có chèn nút của bất kỳ loại như dây đàn, người
Làm cách nào để tôi có thể giữ nguyên kiểu này?
Bạn đã thử gì? Bạn đã nghiên cứu Generics java và bạn có biết về cú pháp? –