亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java中分層數據表示的最佳數據結構是什么?

java中分層數據表示的最佳數據結構是什么?

一只甜甜圈 2023-09-27 17:15:16
我需要在java中創建一個可以表示數據層次結構的數據結構。示例用例如下圖所示。組織層次結構在我的例子中,只有葉子級別才會有數據,內部節點應該像索引一樣工作。我應該能夠使用多個鍵(復合鍵)從數據結構中獲取數據。是否可以使用嵌套映射,或者我應該為此用例實現一個 m 路樹(B 樹/B+ 樹)。
查看完整描述

3 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

如果嵌套數據的結構是不變的,您可以使用帶有屬性的普通類。


如果結構是動態的,我會使用Maps,接口并忽略實現。


關于使用自定義樹結構,如果可以使用類,那就更好了。如果您使用Maps,我會從 a 開始,HashMap如果您發現這是一個問題,您可以Map稍后將其替換為其他內容。


查看完整回答
反對 回復 2023-09-27
?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

您可以為此用例實現 Trie。迭代復合鍵并返回數據(如果找到)。


類定義:


public class TrieNode {

    private HashMap<String, TrieNode> children;

    private Data data;

    private boolean isLeaf;


   // ...

}

查找查詢將如下所示:


public Data find(List<String> compositeKey) {

    TrieNode current = root;

    for (String key: compositeKey) {

        TrieNode node = current.getChildren().get(key);

        if (node == null) {

            return null;

        }

        current = node;

    }

    if(current.isLeaf()) {

       return current.getData();

    } else {

       return null;

    }         

}

插入將如下所示:


public void insert(List<String> compositeKey, Data data) {

    TrieNode current = root;


    for (String key: compositeKey) {

        current = current.getChildren()

          .computeIfAbsent(key, c -> new TrieNode());

    }

    current.setLeaf(true);

    current.setData(data);

}


查看完整回答
反對 回復 2023-09-27
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

顯然你必須使用類似樹的數據結構。這是它的示例代碼。


高級代碼思想


class Entity{  

    // declare you attributes and below two properties


    List<Entity> children;


    boolean isleafNode;// for parent node its 'false' and for leaf node it will 'true'


    }


查看完整回答
反對 回復 2023-09-27
  • 3 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號