Trie树

Trie树简介

Trie 树,也叫字典树或者叫前缀树。顾名思义,它是一个树形结构。它是一种专门处理字符串匹配的树状结构,用来解决在一组字符串集合中快速查找某个字符串的问题。Trie 树的本质,就是利用字符串之间的公共前缀,将重复的前缀合并在一起。

下面展示了一个由“hello”、“her”、“hi”、“how”、“see”以及“so”组成的Trie树。

Trie-Tres.jpg

Trie特点

由上图中的Trie中可知

  • Trie的每个节点存储一个字符,根节点不保存字符
  • 从根节点到某一个节点,路径上经过的字符连接起来,为该节点对应的字符串
  • 每个节点的所有子节点包含的字符都不相同

实现Trie树

Trie树节点

由于每个节点只存储一个字符,以及指向它的子节点,那么对于每个节点有如下的数据结构

function TrieNode(key) {
    this.key = key;
    this.children = [];
}

其中key保存了这个节点的值,children中保存了该节点的所有子节点(对于小写英文字母而言,这个数组的长度为26)

Trie树的插入

构建一棵Trie树就是将字符串逐个插入的过程。由于Trie的特点,插入操作的过程中。要先判断Trie是否已经存在了与待插入字符串相同的前缀,找到所有的公共前缀后,将不同的字符插入Trie中。对于插入的过程,大致的代码如下:

// 采用递归插入字符串
function insertData(data, node){
    if (data === '') {
        return;
    }

    let children = node.children;
    let haveData = null;
    
   // 判断存储子节点的数组中,是否有与插入字符串的第一个值匹配的节点
    for (let i in children) {
        if (children[i].key == data[0]) {
            haveData = children[i];
        }
    }
    
    if(haveData) {
        // 待插入的第一个字符已经存在children数组中,则继续判断下一个字符
        this.insertData(data.substring(1), haveData);
    }else{
      // 未找到相应的子节点,分为两种情况
      // 已经是现有Trie的叶子节点
        if(children.length === 0) {
            let insertNode = new TrieNode(data[0]);
            children.push(insertNode);
            this.insertData(data.substring(1), insertNode); 
        }else{
          // 要在当前的children中找到合适的位置,插入字符
            let validPosition = 0;
            for (let j in children) {
                if (children[j].key < data[0]) {
                    validPosition++;
                }
            }
            let insertNode = new TrieNode(data[0]);
            children.splice(validPosition, 0, insertNode);
            this.insertData(data.substring(1), insertNode); 
        }
    }
}

在插入Trie树的过程中,需要遍历所有的字符串,时间复杂度是O(n),n表示所有字符串的长度和。

Trie树的查找

查找的过程与插入的过程类似,需要逐层遍历,寻找与待匹配字符串相同的字符。其大概代码如下:

function search (data) {
    if (data === '' || this.root.children.length === 0) {
        return false;
    }
  // 遍历children
    for (let i in this.root.children) {
        if (this.searchNext(this.root.children[i], data)) {
            return true;
        }
    }
    return false;
}
// 递归查找
function searchNext(node, data) {
    if(data[0] !== node.key) return false;
    let children = node.children;
  // 该节点已经是叶子节点,并且待查找字符串已查找完毕
    if(children.length === 0 && data.length === 1) {
        return true;
    } else if(children.length > 0 && data.length > 1) {
        for(let i in children) {
            // 继续判断下一个字符
            if(children[i].key === data[1]) {
                return searchNext(children[i], data.substring(1));
            }
        }
    } else {
        return false;
    }
}

在Trie树已经构建完成的情况下,查找一个字符串是否在Trie中效率是非常高的,其事件复杂度为O(K),k表示待查找字符串的长度。

Trie树的删除

先递归查找到字符串的叶子节点,然后从字符串的叶子节点逐级向根节点递归删除叶子节点,直到删除字符串。其大概的代码如下:

function deleteNode(data) {
    if (this.search(data)) { // 判断是否存在该单词(字符串)
        for (let i in this.root.children) {
            if (this.deleteNext(this.root, i, data, data)) {
                return;
            }
        }
    }
    return this;
}

/**
* @param parent 父节点
* @param index 子节点在父节点children数组中的索引位置
* @param stringData 递归遍历中的字符串
* @param delStr 调用delete方法时的原始字符串
*/
function deleteNext(parent, index, stringData, delStr) {
    let node = parent.children[index];
    // 若字符与节点key不相等,则不匹配
    if (stringData[0] != node.key) {
        return false;
    } else { // 若与key相等,继续判断
        let children = node.children;
        if (children.length == 0 && stringData.length == 1) { // 叶子节点,最后一个字符,则完全匹配
            // 删除叶子节点,利用父节点删除子节点原理
            parent.children.splice(index, 1);
            // 字符串从尾部移除一个字符后,继续遍历删除方法
            this.deleteNode(delStr.substring(0, delStr.length - 1));
        } else if (children.length > 0 && stringData.length > 1) { // 既不是叶子节点,也不是最后一个字符,则继续递归查找
            for (let i in children) {
                if (children[i].key == stringData[1]) {
                    return this.deleteNext(node, i, stringData.substring(1), delStr); // 记得return 递归函数,否则获取的返回值为undefined
                }
            }
        }
    }
}

Trie改进

Trie树是非常消耗内存的数据结构,用的是一种空间换时间的思路。Trie树的问题就在于存储子节点的children数组中。如果字符串中只包含从a到z的26个字符,那么children的长度就为26。注意,这里说的是每一个Trie树的节点,都需要申请一个长度为26的数组,即使这个节点只有一个子节点!

那么如果字符串包含了大小写,或者是数字特殊字符。Trie所需的空间就更大了。尤其是在重复的前缀不多的情况下,Trie树不但不能节省内存,而且还有可能浪费更多的内存空间。可以想到的方法就是,将children修改为其他的数据结构,比如有序数组、跳表等。

对只有一个子节点的节点,而且此节点不是一个串的结束节点可以将此节点与子节点合并,这就是缩点优化

Trie-up.jpg

Trie树完整代码

function TrieNode(key) {
    this.key = key;
    this.children = [];
}

function Trie() {
    this.root = new TrieNode('/'); // 添加根节点
    this.insert = insert; // 插入
    this.insertData = insertData;
    this.search = search; // 查找
    this.searchNext = searchNext;
    this.deleteNode = deleteNode; // 删除
    this.deleteNext = deleteNext;

    this.nodeNumber = 0; // trie所有节点个数
    this.print = print; // 打印Trie树
    this.printHelper = printHelper;
}

function insert(data) {
    this.insertData(data, this.root);
}

function insertData(data, node){
    if (data === '') {
        return;
    }

    let children = node.children;
    let haveData = null;
    
    for (let i in children) {
        if (children[i].key == data[0]) {
            haveData = children[i];
        }
    }
    
    if(haveData) {
        this.insertData(data.substring(1), haveData);
    }else{
        if(children.length === 0) {
            let insertNode = new TrieNode(data[0]);
            children.push(insertNode);
            this.insertData(data.substring(1), insertNode); 
        }else{
            let validPosition = 0;
            for (let j in children) {
                if (children[j].key < data[0]) {
                    validPosition++;
                }
            }
            let insertNode = new TrieNode(data[0]);
            children.splice(validPosition, 0, insertNode);
            this.insertData(data.substring(1), insertNode); 
        }
    }
}

function search (data) {
    if (data === '' || this.root.children.length === 0) {
        return false;
    }
    for (let i in this.root.children) {
        if (this.searchNext(this.root.children[i], data)) {
            return true;
        }
    }
    return false;
}

function searchNext(node, data) {
    if(data[0] !== node.key) return false;
    let children = node.children;
    if(children.length === 0 && data.length === 1) {
        return true;
    } else if(children.length > 0 && data.length > 1) {
        for(let i in children) {
            if(children[i].key === data[1]) {
                return searchNext(children[i], data.substring(1));
            }
        }
    } else {
        return false;
    }
}

function deleteNode(data) {
    if (this.search(data)) { 
        for (let i in this.root.children) {
            if (this.deleteNext(this.root, i, data, data)) {
                return;
            }
        }
    }
    return this;
}

function deleteNext(parent, index, stringData, delStr) {
    let node = parent.children[index];
    if (stringData[0] != node.key) {
        return false;
    } else {
        let children = node.children;
        if (children.length == 0 && stringData.length == 1) { 
            parent.children.splice(index, 1);
            this.deleteNode(delStr.substring(0, delStr.length - 1));
        } else if (children.length > 0 && stringData.length > 1) {
            for (let i in children) {
                if (children[i].key == stringData[1]) {
                    return this.deleteNext(node, i, stringData.substring(1), delStr);
                }
            }
        }
    }
}

function print () {
    for (let i in this.root.children) {
        this.nodeNumber++;
        this.printHelper(this.root.children[i], [this.root.children[i].key]);
    }
}

function printHelper (node, data) {
    if (node.children.length === 0) {
        console.log('>', data.join(''));
        return;
    }
    for (let i in node.children) {
        this.nodeNumber++;
        data.push(node.children[i].key);
        this.printHelper(node.children[i], data);
        data.pop();
    }
}

let trie = new Trie();

trie.insert('apple');
trie.insert('appcd');
trie.insert('banana');

trie.print();
console.log(trie.search("app"));
trie.deleteNode('appcd')
trie.print();

console.log(trie.nodeNumber);

// 输出结果
> appcd
> apple
> banana
false
> apple
> banana
11

学习心得

第一次看Trie树的概念,完全没明白到底是什么样的数据结构。直到看了Trie树的图示例,一下就明白了,Trie树的概念非常好理解,就是把相同的前缀放到一起构成了一个数据结构。在对比字符串的时候,就像查字典一层层的比较。

对于每个节点的存储结构确实比较绕,每次遍历children数组就相当于去往下一层比较了,反正每次涉及到递归函数总是不好理解。

参考资料

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容

  • 什么是“Trie树”? Trie树,又称前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二...
    尼桑麻阅读 826评论 0 2
  • 数据结构在各大开发语言中应用广泛,尤其是后端开发对数据的处理,而大部分前端开发很少应用到,或是应用场景不允许等因素...
    烟伤肺阅读 3,442评论 1 3
  • 字典树(Trie)笔记 特别声明 本文只是一篇笔记类的文章,所以不存在什么抄袭之类的。 以下为我研究时参考过的链接...
    Harlan1994阅读 20,127评论 2 21
  • 1、 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个...
    Shadowsocks2阅读 670评论 1 2
  • 声明:摘自github:https://github.com/ZtesoftCS/go-ethereum-code...
    蓝Renly阅读 736评论 0 0