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": {}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    ES6 语法
    使用过滤器进行跨域
    java读取资源文件(Properties)
    跨域
    java提取(获取)博客信息(内容)
    SSM命名规范框架
    学校管理系统设计java(数据库、源码、演讲内容、ppt等)
    学校管理系统C#(数据库、源码、演讲内容、ppt等)
    vue快速使用
    故障排除:无法启动、访问或连接到 Azure 虚拟机上运行的应用程序
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/13234525.html
Copyright © 2011-2022 走看看