-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.js
97 lines (81 loc) · 2.03 KB
/
Trie.js
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
90
91
92
93
94
95
96
97
class TrieNode {
constructor() {
this.children = Array(10).fill(null);
this.parent = null;
}
}
class ContactNode {
constructor(name, number, parent) {
this.name = name;
this.number = number;
this.parent = parent;
}
}
class Trie {
constructor() {
this.root = new TrieNode();
this.current = this.root;
let init = [
[ 'Jaswanth', '1234567890' ],
[ 'Sriram', '1235467120' ],
[ 'Ram', '1236541962' ],
[ 'Sid', '1234651954' ],
[ 'Siddhu', '1239805102' ]
];
for (let i = 0; i < init.length; i++) {
this.add(init[i][1], init[i][0], 0);
}
}
add(number, name, pos = 0, node = this.root) {
if (pos === number.length - 1) {
node.children[number[pos] - '0'] = new ContactNode(name, number, node);
return;
}
if (node.children[number[pos] - '0'] === null) {
let newnode = new TrieNode();
node.children[number[pos] - '0'] = newnode;
newnode.parent = node;
}
this.add(number, name, pos + 1, node.children[number[pos] - '0']);
}
findAll(node) {
if (node === null) return;
if (node instanceof ContactNode) {
this.res.push(node);
this.contacts.push(node);
return;
}
for (let i = 0; i < 10; i++) {
this.findAll(node.children[i]);
}
}
findNext(step) {
if (step === -1) {
this.current = this.current.parent;
} else if (step !== -2) {
if (this.current.children[step - '0'] === null) {
let newnode = new TrieNode();
this.current.children[step - '0'] = newnode;
newnode.parent = this.current;
}
this.current = this.current.children[step - '0'];
}
this.res = [];
this.contacts = [];
this.findAll(this.current);
return { res: this.res, contacts: this.contacts };
}
del(number, pos = 0, node = this.root) {
if (pos === number.length - 1) {
node.children[number[pos] - '0'] = null;
return;
}
if (node.children[number[pos] - '0'] === null) {
let newnode = new TrieNode();
node.children[number[pos] - '0'] = newnode;
newnode.parent = node;
}
this.del(number, pos + 1, node.children[number[pos] - '0']);
}
}
export default Trie;