前缀树的实现(C++/Go)

前缀树的实现,对于节点来说记录着pass 有多少个string 经过这个节点,end是有多少个string 以这个节点为结尾,map为记录着边信息,key 是字符信息,value是对应的这个key的信息。

go实现代码

package main

import "fmt"

type Tree struct {
	pass  int
	end   int
	edges map[byte]*Tree
}

var root *Tree

func (p *Tree) InsertString(str string) {
	cur := p
	cur.pass++
	for _, ch := range str {
		if value, ok := cur.edges[byte(ch)]; ok {
			cur = value
			cur.pass++
		} else {
			cur.edges[byte(ch)] = &Tree{1, 0, make(map[byte]*Tree)}
			cur = cur.edges[byte(ch)]
		}
	}
	cur.end++
}
func (p *Tree) SearchString(str string) bool {
	cur := p
	for _, ch := range str {
		if value, ok := cur.edges[byte(ch)]; ok {
			cur = value
		} else {

			return false
		}
	}
	return true
}
func Display(root *Tree) {
	for k, value := range root.edges {
		fmt.Println(string(k), value.pass, value.end)
		Display(value)
	}
}
func main() {
	strs := make([]string, 0, 10)
	strs = append(strs, "abc", "ab", "abcd", "abuy")
	root = &Tree{0, 0, make(map[byte]*Tree)}
	for _, str := range strs {
		root.InsertString(str)
	}
	Display(root)
}

c++代码

#include <iostream>
#include <unordered_map>
struct Node{
      int pass;// 有几个经过该Node
      int end;// 是否以这个为结尾
      std::unordered_map<char,Node*>edges;// 边表示字符
      Node(int pass,int end):pass(pass),end(end){} 
};
class Tree{
private:
    Node* root;
    void PrintTree(Node* root){
        for(auto it = root->edges.begin(); it != root->edges.end(); ++it){
            std::cout<<it->first<<' '<<it->second->pass<<' '<<it->second->end<<std::endl;
            PrintTree(it->second);
        }
    }
public:
    Tree():root(new Node(0,0)){}
    void InsertString(std::string str){
         Node* cur = root;
         ++cur->pass;
         for(int i = 0; i < str.size(); ++i){
            if(cur->edges.find(str[i]) != cur->edges.end()){
                  cur = cur->edges[str[i]];
                  ++cur->pass;
            } else{
                  cur->edges[str[i]] = new Node(1,0);
                  cur = cur->edges[str[i]];
            }
         }
         ++cur->end;
         cur = nullptr;
    } 
    bool SearchString(std::string str){
         Node * cur = root;
         for(char ch : str){
            if(cur->edges.find(ch) != cur->edges.end()){
                  cur = cur->edges[ch];
            } else{
                  return false;
            }
         }
         return true;
    }

    void DisplayTree(){
         this->PrintTree(root);
    }

};
int main(){
      std::string strs[] = {"abc","abcd","ab","adryu"};
      Tree test;
      for(auto str : strs){
            test.InsertString(str);
      }
      test.DisplayTree();
      std::getchar();
      return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坏牧羊人.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值