-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTries.cpp
More file actions
89 lines (75 loc) · 1.71 KB
/
Tries.cpp
File metadata and controls
89 lines (75 loc) · 1.71 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
using namespace std;
class Node{
public:
char word;
Node *left;
Node *middle;
Node *right;
long long int value;
};
Node * makeNode(char w){
Node *n = new Node();
n->word = w;
n->left = n->middle = n->right = NULL;
n->value = 0;
return n;
}
Node *root = NULL;
Node * putItem(Node *, string, long long int, int);
Node * getItem(Node *, string, int);
Node * put(string key, long long int value){
root = putItem(root, key, value, 0);
return root;
}
Node * putItem(Node *root, string key, long long int value, int index){
char c = key[index];
if(root==NULL){
root = makeNode(c);
}
if(c < root->word){
root->left = putItem(root->left, key, value, index);
}
else if(c > root->word){
root->right = putItem(root->right, key, value, index);
}
else if(index < key.length() - 1){
root->middle = putItem(root->middle, key, value, index + 1);
}
else{
root->value = value;
}
return root;
}
long long int get(string key){
Node *n = getItem(root, key, 0);
if(n==NULL){
return -1;
}
return n->value;
}
Node * getItem(Node *root, string key, int index){
char c = key[index];
if(root==NULL){
return root;
}
else if(c < root->word){
return getItem(root->left, key, index);
}
else if(c > root->word){
return getItem(root->right, key, index);
}
else if(index < key.length() - 1){
return getItem(root->middle, key, index + 1);
}
else{
return root;
}
}
int main(){
root = put("car", 200);
root = put("hack", 100);
root = put("road", 100);
cout<<get("car")<<endl;
return 0;
}