Skip to content
Permalink
Browse files

SYOKURIN KATSUDO

  • Loading branch information...
himanoa committed Jul 20, 2019
1 parent f0ca7bd commit a165b76b773edc699fb5d2a53d947e9afae3a9bc
Showing with 163 additions and 1 deletion.
  1. 0 .prettierrc
  2. +133 −0 index.test.ts
  3. +22 −0 index.ts
  4. +1 −0 package.json
  5. +2 −1 tsconfig.json
  6. +5 −0 yarn.lock
No changes.
@@ -0,0 +1,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"]);
});
});
@@ -0,0 +1,22 @@
export type Tree<T> = {
value: T;
left?: Tree<T>;
right?: Tree<T>;
};

export const makeIter = function* traverse<T>(
tree: Tree<T>
): IterableIterator<T> {
yield tree.value;
if (tree.left) {
const result = yield* traverse(tree.left);
if (tree.right) {
const r = yield* traverse(tree.right);
if (r !== undefined) {
yield r;
}
}
} else if (tree.right) {
yield yield* traverse(tree.right);
}
};
@@ -7,6 +7,7 @@
"devDependencies": {
"@types/jest": "^24.0.15",
"jest": "^24.8.0",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"typescript": "^3.5.3"
}
@@ -2,9 +2,10 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"downlevelIteration": true,
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
"lib": ["es6", "dom"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
@@ -2695,6 +2695,11 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=

prettier@^1.18.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==

pretty-format@^24.8.0:
version "24.8.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.8.0.tgz#8dae7044f58db7cb8be245383b565a963e3c27f2"

0 comments on commit a165b76

Please sign in to comment.
You can’t perform that action at this time.