-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage_example.cpp
59 lines (48 loc) · 1.5 KB
/
usage_example.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cassert>
#include <iostream>
#include "prefix_tree.hpp"
int main()
{
auto tree = PrefixTree<int, int>{};
auto values1 = { 1, 2, 3 };
auto info1 = 10;
auto values2 = { 1, 2 };
auto info2 = 20;
auto values3 = { 3, 4 };
auto info3 = 30;
tree.Insert(values1, info1);
tree.Insert(values2, info2);
tree.Insert(values3, info3);
// After those operations, the Trie looks like this:
/*
┌──┐
│ │
└──┘
3 / \ 1
┌──┐ ┌──┐
│ │ │ │
└──┘ └──┘
4 / \ 2
┌──┐ ┌──┐
│30│ │20│
└──┘ └──┘
\ 3
┌──┐
│10│
└──┘
*/
// Where the terminal nodes have information associated and can be queried.
assert(tree.Size() == 3);
assert(tree.Get(values1) == info1);
assert(tree.Get(values2) == info2);
assert(tree.Get(values3) == info3);
// The intermediate notes can not be queried
// assert(tree.Get({1, 2})); // Runtime Error
assert(tree.Contains({ 1 }) == false);
tree.Erase(values2);
assert(tree.Contains(values2) == false);
assert(tree.Contains(values3)); // Other nodes are still ok, even if on the "path"
// Update a value
tree.Insert(values2, 50);
assert(tree.Get(values2) == 50);
}