【发布时间】:2014-11-20 20:15:29
【问题描述】:
我正在尝试编写一个 Java 程序,该程序将使用二叉搜索树创建一个数据库,其键将是汽车的制造商(例如雪佛兰)。该节点将包含一个链接列表,其中将包含有关汽车的更多详细信息。
我的汽车被添加到一个名为 DBTreeNode 的链接列表类中。
能否修改BST实现here,使Node的数据成为链表?
【问题讨论】:
标签: java linked-list binary-search-tree
我正在尝试编写一个 Java 程序,该程序将使用二叉搜索树创建一个数据库,其键将是汽车的制造商(例如雪佛兰)。该节点将包含一个链接列表,其中将包含有关汽车的更多详细信息。
我的汽车被添加到一个名为 DBTreeNode 的链接列表类中。
能否修改BST实现here,使Node的数据成为链表?
【问题讨论】:
标签: java linked-list binary-search-tree
一种选择是将您的 DBTreeNode 列表添加为 BST 节点的成员以及其他字段,如 Left、Right 等...然后为 DBTreeNode 添加访问器(getter、setter)。希望这可以帮助。祝你好运!
这是一个例子:
public class BST<Key extends Comparable<Key>, Value> {
private Node root; // root of BST
private class Node {
private Key key; // sorted by key
private Value val; // associated data
private Node left, right; // left and right subtrees
private int N; // number of nodes in subtree
private DBTreeNode VehicleDetails; // your list
public Node(Key key, Value val, int N) {
this.key = key;
this.val = val;
this.N = N;
this.VehicleDetails = new DBTreeNode(); // initialize your list
}
public DBTreeNode getDetails(){
return this.VehicleDetails;
}
public void addDetails(DBTreeNode details){
for(DBTreeNodeElement detail : details) this.VehicleDetails.add(detail);
}
}
【讨论】: