-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
133 lines (129 loc) · 3.06 KB
/
index.test.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { makeIter } from "./index";
// 前提: https://ja.wikipedia.org/wiki/%E6%9C%A8%E6%A7%8B%E9%80%A0_(%E3%83%87%E3%83%BC%E3%82%BF%E6%A7%8B%E9%80%A0)#/media/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Sorted_binary_tree_preorder.svg
describe("#makeIter", () => {
it("最初はFのvalueを返すこと", () => {
const tree = {
value: 1
};
expect(makeIter(tree).next()).toStrictEqual({
done: false,
value: 1
});
});
describe("左のノードが存在する場合", () => {
it("F→Bの順で走破すること", () => {
const tree = {
value: 1,
left: { value: 2, left: { value: 1 } }
};
const iter = makeIter(tree);
expect([iter.next(), iter.next()]).toStrictEqual([
{
done: false,
value: 1
},
{
done: false,
value: 2
}
]);
});
it("F->B->Aの順で走破すること", () => {
const tree = {
value: 1,
left: { value: 2, left: { value: 3 } }
};
const iter = makeIter(tree);
expect([iter.next(), iter.next(), iter.next()]).toStrictEqual([
{
done: false,
value: 1
},
{
done: false,
value: 2
},
{
done: false,
value: 3
}
]);
});
it("F->B->A->Dの順で走破すること", () => {
const tree = {
value: 1,
left: { value: 2, left: { value: 3 }, right: { value: 4 } }
};
const iter = makeIter(tree);
expect([
iter.next(),
iter.next(),
iter.next(),
iter.next()
]).toStrictEqual([
{
done: false,
value: 1
},
{
done: false,
value: 2
},
{
done: false,
value: 3
},
{
done: false,
value: 4
}
]);
});
});
describe("左のノードが存在しないで右だけある場合", () => {
it("F→Gのノードの順で走破すること", () => {
const tree = {
value: 1,
right: { value: 3 }
};
const iter = makeIter(tree);
expect([iter.next(), iter.next()]).toStrictEqual([
{
done: false,
value: 1
},
{
done: false,
value: 3
}
]);
});
});
it("図の順番で走破できること", () => {
const tree = {
value: "F",
left: {
value: "B",
left: { value: "A" },
right: { value: "D", left: { value: "C" }, right: { value: "E" } }
},
right: {
value: "G",
right: { value: "I", left: { value: "H" } }
}
};
const iter = makeIter(tree);
let actual = [
iter.next().value,
iter.next().value,
iter.next().value,
iter.next().value,
iter.next().value,
iter.next().value,
iter.next().value,
iter.next().value,
iter.next().value
];
expect(actual).toStrictEqual(["F", "B", "A", "D", "C", "E", "G", "I", "H"]);
});
});