2014-11-20 54 views
0

我正在嘗試編寫一個Java程序,該程序將使用二進制搜索樹來創建一個數據庫,其中的鍵將由一輛汽車(例如Chevy)構成。該節點將包含一個鏈接列表,其中將包含有關汽車的更多詳細信息。包含鏈接列表的節點的Java二進制搜索樹

我的汽車被添加到名爲DBTreeNode的鏈接列表類中。

我可以修改BST實施here,使節點的數據成爲鏈接列表嗎?

回答

1

一個選擇是將您的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); 
    } 
    } 
+0

由於這是我在想什麼,我只是感到困惑在執行。我會給它一個鏡頭。 – 2014-11-21 15:28:17

+0

這有助於我開始。現在我唯一的問題是我在遍歷樹時遇到問題,看看細節是否正確添加。我錯過了一個簡單的toString方法,我可以嘗試嗎? – 2014-11-21 17:57:35

+0

其實沒關係我想通了。感謝您的幫助! – 2014-11-21 18:21:16