zoukankan      html  css  js  c++  java
  • 字典树(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": {}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    Kafka 再均衡监听器示例
    Spring boot中异步线程池
    【Java&Go并发编程系列】4.等待一组并发任务完成——CountDownLatch VS sync.WaitGroup
    Redis常用命令对应到Redisson对象操作
    js清空缓存,ajax
    phpexcel用法 转、
    composer 使用
    转:git操作
    手机微信内支付
    微信扫码支付
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/13234525.html
Copyright © 2011-2022 走看看