-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.h
37 lines (30 loc) · 916 Bytes
/
trie.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef TRIE_H
#define TRIE_H
#include <iostream>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
struct Data{}; // just to show that we can keep some data in tree
struct Node;
using Children = std::unordered_map<std::string, Node>;
using ChildrenPtr = std::shared_ptr<Children>;
using Result = std::pair<bool, const Data*>;
struct Node {
bool marker{false};
const Data* data{nullptr};
ChildrenPtr children = std::make_shared<Children>();
Node& operator[](const std::string& key);
};
struct Trie : protected Node {
public:
/**
* Inserts element with key and data
* If key already exists, data will be replace by new one.
*/
void Insert(std::string_view key, const Data* data = nullptr);
void Remove(std::string_view key);
Result Find(std::string_view key) const;
};
std::ostream& operator<<(std::ostream& out, const Trie& tree);
#endif // TRIE_H