1 | import { DOWN, UP, SAME } from "./characters";
|
2 | export const serializeTrie = (root) => {
|
3 | let serialized = "";
|
4 | const queue = Array.from(root.children.values());
|
5 | let current;
|
6 | while ((current = queue.shift()) !== undefined) {
|
7 | if (current === UP) {
|
8 | serialized += UP;
|
9 | continue;
|
10 | }
|
11 | serialized += current.label;
|
12 | if (current.children.size === 0) {
|
13 | if (queue.length > 0 && queue[0] !== UP) {
|
14 | serialized += SAME;
|
15 | }
|
16 | continue;
|
17 | }
|
18 | serialized += DOWN;
|
19 | const newItems = Array.from(current.children.values());
|
20 | if (queue.length > 0) {
|
21 | newItems.push(UP);
|
22 | }
|
23 | queue.unshift(...newItems);
|
24 | }
|
25 | return serialized;
|
26 | };
|
27 |
|
\ | No newline at end of file |