Skip to content

Commit 4e8bef8

Browse files
author
Kohei Asai
authored
Refactor data structure (axross#131)
1 parent d55cb37 commit 4e8bef8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+258
-218
lines changed

Diff for: test_utilities/binary_tree.ts renamed to data_structures/binary_tree.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { BinaryTreeNode } from "../types/binary_tree.ts";
1+
export interface BinaryTreeNode<T> {
2+
val: T;
3+
left: BinaryTreeNode<T> | null;
4+
right: BinaryTreeNode<T> | null;
5+
}
26

37
export function createBinaryTreeNode<T>(
48
array: (T | null)[]

Diff for: data_structures/binary_tree_test.ts

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { test } from "https://deno.land/std/testing/mod.ts";
2+
import {
3+
assertEquals,
4+
assertStrictEq
5+
} from "https://deno.land/std/testing/asserts.ts";
6+
import { createBinaryTreeNode } from "./binary_tree.ts";
7+
8+
test("createBinaryTreeNode() returns a BinaryTreeNode", () => {
9+
assertEquals(createBinaryTreeNode([1, 2, 3]), {
10+
val: 1,
11+
left: { val: 2, left: null, right: null },
12+
right: { val: 3, left: null, right: null }
13+
});
14+
assertEquals(createBinaryTreeNode([1, null, 2, 3]), {
15+
val: 1,
16+
left: null,
17+
right: {
18+
val: 2,
19+
left: {
20+
val: 3,
21+
left: null,
22+
right: null
23+
},
24+
right: null
25+
}
26+
});
27+
assertEquals(createBinaryTreeNode([5, 4, 7, 3, null, 2, null, -1, null, 9]), {
28+
val: 5,
29+
left: {
30+
val: 4,
31+
left: {
32+
val: 3,
33+
left: {
34+
val: -1,
35+
left: null,
36+
right: null
37+
},
38+
right: null
39+
},
40+
right: null
41+
},
42+
right: {
43+
val: 7,
44+
left: {
45+
val: 2,
46+
left: {
47+
val: 9,
48+
left: null,
49+
right: null
50+
},
51+
right: null
52+
},
53+
right: null
54+
}
55+
});
56+
assertEquals(createBinaryTreeNode([1, 2, 3]), {
57+
val: 1,
58+
left: { val: 2, left: null, right: null },
59+
right: { val: 3, left: null, right: null }
60+
});
61+
assertEquals(createBinaryTreeNode([1, 2, 3]), {
62+
val: 1,
63+
left: { val: 2, left: null, right: null },
64+
right: { val: 3, left: null, right: null }
65+
});
66+
});
67+
68+
test("createBinaryTreeNode() returns null when the given array is empty", () => {
69+
assertStrictEq(createBinaryTreeNode([]), null);
70+
});

Diff for: test_utilities/linked_list.ts renamed to data_structures/linked_list.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { SinglyLinkedListNode } from "../types/linked_list.ts";
1+
export interface LinkedListNode<T> {
2+
val: T;
3+
next: LinkedListNode<T> | null;
4+
}
25

3-
export function createSinglyLinkedListNode<T>(
6+
export function createLinkedListNode<T>(
47
array: T[]
5-
): SinglyLinkedListNode<T> | null {
8+
): LinkedListNode<T> | null {
69
if (array.length === 0) return null;
710

8-
const head: SinglyLinkedListNode<T> = { val: array[0], next: null };
11+
const head: LinkedListNode<T> = { val: array[0], next: null };
912
let tail = head;
1013

1114
for (let i = 1; i < array.length; ++i) {
@@ -19,9 +22,9 @@ export function createSinglyLinkedListNode<T>(
1922
}
2023

2124
export function getNthNode<T>(
22-
node: SinglyLinkedListNode<T>,
25+
node: LinkedListNode<T>,
2326
n: number
24-
): SinglyLinkedListNode<T> {
27+
): LinkedListNode<T> {
2528
if (!Number.isSafeInteger(n)) {
2629
throw new TypeError("the given index is not an integer");
2730
}

Diff for: data_structures/linked_list_test.ts

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { test } from "https://deno.land/std/testing/mod.ts";
2+
import {
3+
assertEquals,
4+
assertStrictEq,
5+
assertThrows
6+
} from "https://deno.land/std/testing/asserts.ts";
7+
import { createLinkedListNode, getNthNode } from "./linked_list.ts";
8+
9+
test("createLinkedListNode() returns a LinkedListNode by the given array", () => {
10+
assertEquals(createLinkedListNode([1, 2, 3, 4, 5, 6, 7]), {
11+
val: 1,
12+
next: {
13+
val: 2,
14+
next: {
15+
val: 3,
16+
next: {
17+
val: 4,
18+
next: {
19+
val: 5,
20+
next: {
21+
val: 6,
22+
next: {
23+
val: 7,
24+
next: null
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
31+
});
32+
assertEquals(createLinkedListNode([1]), {
33+
val: 1,
34+
next: null
35+
});
36+
});
37+
38+
test("createLinkedListNode() returns null if the given array is empty", () => {
39+
assertStrictEq(createLinkedListNode([]), null);
40+
});
41+
42+
test("getNthNode(node, n) returns the n-th node", () => {
43+
assertStrictEq(
44+
getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, 0).val,
45+
0
46+
);
47+
assertStrictEq(
48+
getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, 2).val,
49+
2
50+
);
51+
assertStrictEq(
52+
getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, -1).val,
53+
5
54+
);
55+
assertStrictEq(
56+
getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, -3).val,
57+
3
58+
);
59+
});
60+
61+
test("getNthNode(node, n) doesn't mutates the given linked list", () => {
62+
const head = createLinkedListNode([0, 1, 2, 3, 4, 5])!;
63+
64+
getNthNode(head, 1);
65+
getNthNode(head, 2);
66+
getNthNode(head, 3);
67+
68+
assertStrictEq(getNthNode(head, 2).val, 2);
69+
});
70+
71+
test("getNthNode(node, n) throws an Error if the given index is out of bounds", () => {
72+
assertThrows(
73+
() => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, 7),
74+
Error
75+
);
76+
assertThrows(
77+
() => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, -7),
78+
Error
79+
);
80+
});
81+
82+
test("getNthNode(node, n) throws a TypeError if the given index is not an integer", () => {
83+
assertThrows(
84+
() => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, 1.1),
85+
TypeError
86+
);
87+
assertThrows(
88+
() => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, Infinity),
89+
TypeError
90+
);
91+
assertThrows(
92+
() =>
93+
getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, -Infinity),
94+
TypeError
95+
);
96+
assertThrows(
97+
() => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, NaN),
98+
TypeError
99+
);
100+
});

Diff for: solutions/binary_tree_inorder_traversal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BinaryTreeNode } from "../types/binary_tree.ts";
1+
import { BinaryTreeNode } from "../data_structures/binary_tree.ts";
22

33
// 94. Binary Tree Inorder Traversal
44
// https://leetcode.com/problems/binary-tree-inorder-traversal/

Diff for: solutions/binary_tree_inorder_traversal_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from "https://deno.land/std/testing/mod.ts";
22
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
3-
import { createBinaryTreeNode } from "../test_utilities/binary_tree.ts";
3+
import { createBinaryTreeNode } from "../data_structures/binary_tree.ts";
44
import inorderTraversal from "./binary_tree_inorder_traversal.ts";
55

66
test("94. Binary Tree Inorder Traversal", () => {

Diff for: solutions/binary_tree_level_order_traversal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BinaryTreeNode } from "../types/binary_tree.ts";
1+
import { BinaryTreeNode } from "../data_structures/binary_tree.ts";
22

33
// 102. Binary Tree Level Order Traversal
44
// https://leetcode.com/problems/binary-tree-level-order-traversal/

Diff for: solutions/binary_tree_level_order_traversal_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from "https://deno.land/std/testing/mod.ts";
22
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
3-
import { createBinaryTreeNode } from "../test_utilities/binary_tree.ts";
3+
import { createBinaryTreeNode } from "../data_structures/binary_tree.ts";
44
import levelorder from "./binary_tree_level_order_traversal.ts";
55

66
test("102. Binary Tree Level Order Traversal", () => {

Diff for: solutions/binary_tree_postorder_traversal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BinaryTreeNode } from "../types/binary_tree.ts";
1+
import { BinaryTreeNode } from "../data_structures/binary_tree.ts";
22

33
// 145. Binary Tree Postorder Traversal
44
// https://leetcode.com/problems/binary-tree-postorder-traversal/

Diff for: solutions/binary_tree_postorder_traversal_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from "https://deno.land/std/testing/mod.ts";
22
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
3-
import { createBinaryTreeNode } from "../test_utilities/binary_tree.ts";
3+
import { createBinaryTreeNode } from "../data_structures/binary_tree.ts";
44
import postorderTraversal from "./binary_tree_postorder_traversal.ts";
55

66
test("145. Binary Tree Postorder Traversal", () => {

Diff for: solutions/binary_tree_preorder_traversal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BinaryTreeNode } from "../types/binary_tree.ts";
1+
import { BinaryTreeNode } from "../data_structures/binary_tree.ts";
22

33
// 144. Binary Tree Preorder Traversal
44
// https://leetcode.com/problems/binary-tree-preorder-traversal/

Diff for: solutions/binary_tree_preorder_traversal_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from "https://deno.land/std/testing/mod.ts";
22
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
3-
import { createBinaryTreeNode } from "../test_utilities/binary_tree.ts";
3+
import { createBinaryTreeNode } from "../data_structures/binary_tree.ts";
44
import preorderTraversal from "./binary_tree_preorder_traversal.ts";
55

66
test("144. Binary Tree Preorder Traversal", () => {

Diff for: solutions/count_complete_tree_nodes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BinaryTreeNode } from "../types/binary_tree.ts";
1+
import { BinaryTreeNode } from "../data_structures/binary_tree.ts";
22

33
// 222. Count Complete Tree Nodes
44
// https://leetcode.com/problems/count-complete-tree-nodes/

Diff for: solutions/count_complete_tree_nodes_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from "https://deno.land/std/testing/mod.ts";
22
import { assertStrictEq } from "https://deno.land/std/testing/asserts.ts";
3-
import { createBinaryTreeNode } from "../test_utilities/binary_tree.ts";
3+
import { createBinaryTreeNode } from "../data_structures/binary_tree.ts";
44
import countNodes from "./count_complete_tree_nodes.ts";
55

66
test("222. Count Complete Tree Nodes", () => {

Diff for: solutions/count_univalue_subtrees.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BinaryTreeNode } from "../types/binary_tree.ts";
1+
import { BinaryTreeNode } from "../data_structures/binary_tree.ts";
22

33
// 250. Count Univalue Subtrees
44
// https://leetcode.com/problems/count-univalue-subtrees/

Diff for: solutions/count_univalue_subtrees_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from "https://deno.land/std/testing/mod.ts";
22
import { assertStrictEq } from "https://deno.land/std/testing/asserts.ts";
3-
import { createBinaryTreeNode } from "../test_utilities/binary_tree.ts";
3+
import { createBinaryTreeNode } from "../data_structures/binary_tree.ts";
44
import countUnivalSubtrees from "./count_univalue_subtrees.ts";
55

66
test("250. Count Univalue Subtrees", () => {

Diff for: solutions/diameter_of_binary_tree.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BinaryTreeNode } from "../types/binary_tree.ts";
1+
import { BinaryTreeNode } from "../data_structures/binary_tree.ts";
22

33
// 543. Diameter of Binary Tree
44
// https://leetcode.com/problems/diameter-of-binary-tree/

Diff for: solutions/diameter_of_binary_tree_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from "https://deno.land/std/testing/mod.ts";
22
import { assertStrictEq } from "https://deno.land/std/testing/asserts.ts";
3-
import { createBinaryTreeNode } from "../test_utilities/binary_tree.ts";
3+
import { createBinaryTreeNode } from "../data_structures/binary_tree.ts";
44
import diameterOfBinaryTree from "./diameter_of_binary_tree.ts";
55

66
test("543. Diameter of Binary Tree", () => {

Diff for: solutions/intersection_of_two_linked_lists.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { SinglyLinkedListNode } from "../types/linked_list.ts";
1+
import { LinkedListNode } from "../data_structures/linked_list.ts";
22

33
// 160. Intersection of Two Linked Lists
44
// https://leetcode.com/problems/intersection-of-two-linked-lists/
55
export default function getIntersectionNode<T>(
6-
headA: SinglyLinkedListNode<T> | null,
7-
headB: SinglyLinkedListNode<T> | null
8-
): SinglyLinkedListNode<T> | null {
6+
headA: LinkedListNode<T> | null,
7+
headB: LinkedListNode<T> | null
8+
): LinkedListNode<T> | null {
99
let a = headA;
1010
let b = headB;
1111

Diff for: solutions/intersection_of_two_linked_lists_test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import { test } from "https://deno.land/std/testing/mod.ts";
22
import { assertStrictEq } from "https://deno.land/std/testing/asserts.ts";
33
import {
4-
createSinglyLinkedListNode,
4+
createLinkedListNode,
55
getNthNode
6-
} from "../test_utilities/linked_list.ts";
6+
} from "../data_structures/linked_list.ts";
77
import getIntersectionNode from "./intersection_of_two_linked_lists.ts";
88

99
test("160. Intersection of Two Linked Lists", () => {
10-
const headA1 = createSinglyLinkedListNode([4, 1, 8, 4, 5]);
11-
const headB1 = createSinglyLinkedListNode([5, 0, 1, 8, 4, 5]);
10+
const headA1 = createLinkedListNode([4, 1, 8, 4, 5]);
11+
const headB1 = createLinkedListNode([5, 0, 1, 8, 4, 5]);
1212
const intersection1 = getNthNode(headA1!, 2);
1313

1414
getNthNode(headB1!, 2).next = intersection1;
1515

1616
assertStrictEq(getIntersectionNode(headA1, headB1), intersection1);
1717

18-
const headA2 = createSinglyLinkedListNode([0, 9, 1, 2, 4]);
19-
const headB2 = createSinglyLinkedListNode([3, 2, 4]);
18+
const headA2 = createLinkedListNode([0, 9, 1, 2, 4]);
19+
const headB2 = createLinkedListNode([3, 2, 4]);
2020
const intersection2 = getNthNode(headA2!, 3);
2121

2222
getNthNode(headB2!, 0).next = intersection2;
2323

2424
assertStrictEq(getIntersectionNode(headA1, headB1), intersection1);
2525

26-
const headA3 = createSinglyLinkedListNode([2, 6, 4]);
27-
const headB3 = createSinglyLinkedListNode([1, 5]);
26+
const headA3 = createLinkedListNode([2, 6, 4]);
27+
const headB3 = createLinkedListNode([1, 5]);
2828

2929
assertStrictEq(getIntersectionNode(headA3, headB3), null);
3030
});

Diff for: solutions/linked_list_cycle.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { SinglyLinkedListNode } from "../types/linked_list.ts";
1+
import { LinkedListNode } from "../data_structures/linked_list.ts";
22

33
// 141. Linked List Cycle
44
// https://leetcode.com/problems/linked-list-cycle/
5-
export default function hasCycle<T>(
6-
head: SinglyLinkedListNode<T> | null
7-
): boolean {
5+
export default function hasCycle<T>(head: LinkedListNode<T> | null): boolean {
86
let walker = head;
97
let runner = head ? head.next : null;
108

0 commit comments

Comments
 (0)