-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbktree.h
More file actions
73 lines (53 loc) · 1.56 KB
/
bktree.h
File metadata and controls
73 lines (53 loc) · 1.56 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef BKTREE_H
#define BKTREE_H
#include<string>
#include <unordered_map>
#include <vector>
#include <list>
#include <algorithm>
#include <memory>
class InvalidKeyException{
private:
int key;
public:
InvalidKeyException(int key_) : key(key_){}
int operator()(){
return key;
}
};
class notInTreeException{
private:
const std::string word;
public:
notInTreeException(const std::string& word_) : word(word_){}
std::string operator()(){
return word;
}
};
class Node{
private:
const std::string word;
std::unordered_map<int, std::shared_ptr<Node>> children;
public:
//ctor
Node(const std::string& word_) : word(word_){}
const std::string& getWord() const;
void addChild(int key, const std::string& word);
std::vector<int> keys() const;
bool containsKey(int key) const;
std::shared_ptr<Node> operator[](int i) const;
};
class BKTree{
private:
std::shared_ptr<Node> root;
void recursiveSearch(std::shared_ptr<const Node> current_node, std::list<std::string>& results,
const std::string& query, int tolerence) const;
int distence(const std::string& a, const std::string& b) const;
public:
//ctor
BKTree() : root(nullptr){}
void add(const std::string& word);
std::list<std::string> search(const std::string& query, int tolerence) const;
std::shared_ptr<const Node> get(const std::string& word) const;
};
#endif