字典樹(js實現)

function TreeNode(val) {
    this.key = val;
    this.cnt = 1;//字符串佔用個數
    this.isEnd = false;
    this.value = null;
    this.children = new Map();
}
function Tree() {
    this.root = new TreeNode(null);
}
/* 方法 */
Tree.prototype = {
    /* 插入操作 */
    insert(str, value) {
        let node = this.root, len = str.length;
        /* 遍歷字符串 */
        for (let i = 0; i < len; i++) {
            key = str[i];
            /* 查找是否有該子節點 */
            if (node.children[key] === undefined) {
                node.children[key] = new TreeNode(key);
            } else {
                node.children[key].cnt++;//記錄經過字符串個數
            }
            /* 下一層 */
            node = node.children[key];
        }
        //尾部標誌
        node.value = value;
        node.isEnd = true;
    },
    /* 查詢操作 */
    query(str) {
        let node = this.root, len = str.length;
        for (let i = 0; i < len; i++) {
            let key = str[i];
            if (node.children[key] === undefined) {
                return false;
            } else {
                node = node.children[key];
            }
        }
        return node.isEnd ? node.value : false;
    },
    /* 刪除操作   */
    delete(str) {
        if (this.query(str) === false) {
            console.log('不存在要刪除的字符串');
            return;
        }
        let node = this.root, len = str.length;
        for (let i = 0; i < len; i++) {
            let key = str[i];
            if (node.children[key] === undefined) {
                return;
            } else {
                node.children[key].cnt--;
                /* 數量爲0則刪除該子樹 */
                if (!node.children[key].cnt) {
                    delete node.children[key];
                    return;
                }
            }
            node = node.children[key];
        }
    }
}
/* test */
let tree = new Tree();
tree.insert('111');
tree.insert('121');
tree.insert('112');
tree.delete("112");
let fs = require('fs');
fs.writeFile('./test.json', JSON.stringify(tree), err => {
});
/* ---------------------test.json--------------------- */
{
    "root": {
        "key": null,
        "cnt": 1,
        "isEnd": false,
        "value": null,
        "children": {
            "1": {
                "key": "1",
                "cnt": 2,
                "isEnd": false,
                "value": null,
                "children": {
                    "1": {
                        "key": "1",
                        "cnt": 2,
                        "isEnd": false,
                        "value": null,
                        "children": {
                            "1": {
                                "key": "1",
                                "cnt": 1,
                                "isEnd": true,
                                "children": {}
                            },
                            "2": {
                                "key": "2",
                                "cnt": 1,
                                "isEnd": true,
                                "children": {}
                            }
                        }
                    }
                }
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章