包含链接列表的节点的Java二进制搜索树
问题描述:
我正在尝试编写一个Java程序,该程序将使用二进制搜索树来创建一个数据库,其中的键将由一辆汽车(例如Chevy)构成。该节点将包含一个链接列表,其中将包含有关汽车的更多详细信息。包含链接列表的节点的Java二进制搜索树
我的汽车被添加到名为DBTreeNode的链接列表类中。
我可以修改BST实施here,使节点的数据成为链接列表吗?
答
一个选择是将您的DBTreeNode列表添加为BST节点的成员以及其他字段,如左,右等等......然后为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);
}
}
由于这是我在想什么,我只是感到困惑在执行。我会给它一个镜头。 – 2014-11-21 15:28:17
这有助于我开始。现在我唯一的问题是我在遍历树时遇到问题,看看细节是否正确添加。我错过了一个简单的toString方法,我可以尝试吗? – 2014-11-21 17:57:35
其实没关系我想通了。感谢您的帮助! – 2014-11-21 18:21:16