Skip to content

Commit 25946bc

Browse files
authored
Merge pull request #443 from sir-gon/develop
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Search: Swap Node…
2 parents cb24bca + 91c5a3b commit 25946bc

File tree

2 files changed

+3
-39
lines changed

2 files changed

+3
-39
lines changed

src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.test.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import { describe, expect, it } from '@jest/globals';
22

3-
import { Node } from '../../lib/Node';
4-
import {
5-
build_tree,
6-
flat_tree,
7-
swap_branch,
8-
swapNodes,
9-
__INITIAL_LEVEL__
10-
} from './swap_nodes_algo';
3+
import { build_tree, flat_tree, swapNodes } from './swap_nodes_algo';
114
import TEST_CASES from './swap_nodes_algo.testcases.json';
125

136
describe('swap_nodes_algo', () => {
@@ -33,28 +26,6 @@ describe('swap_nodes_algo', () => {
3326
expect(t_result).toStrictEqual(expected);
3427
});
3528

36-
it('test_swap_branch empty', () => {
37-
expect.assertions(1);
38-
39-
const t_input: null = null;
40-
const t_result: Node<number> | null = swap_branch(t_input);
41-
const expected = null;
42-
43-
expect(t_result).toStrictEqual(expected);
44-
});
45-
46-
it('test_swap_branch', () => {
47-
expect.assertions(1);
48-
49-
const t_input: Node<number> = new Node<number>(__INITIAL_LEVEL__);
50-
t_input.left = new Node<number>(2);
51-
t_input.right = new Node<number>(3);
52-
const t_result: number[] = flat_tree(swap_branch(t_input));
53-
const expected: number[] = [3, 1, 2];
54-
55-
expect(t_result).toStrictEqual(expected);
56-
});
57-
5829
it('build tree and flattened tree test cases', () => {
5930
expect.assertions(4);
6031

src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,6 @@ export function flat_tree(root: Node<number> | null): number[] {
127127
return output;
128128
}
129129

130-
export function swap_branch(root: Node<number> | null): Node<number> | null {
131-
if (root) {
132-
[root.left, root.right] = [root.right, root.left];
133-
}
134-
135-
return root;
136-
}
137-
138130
export function swapNodes(indexes: number[][], queries: number[]): number[][] {
139131
const tree: Node<number> = build_tree(indexes);
140132
const output: number[][] = [];
@@ -161,7 +153,8 @@ export function swapNodes(indexes: number[][], queries: number[]): number[][] {
161153

162154
if (t_level % queries[query] == 0) {
163155
for (const node of node_list) {
164-
swap_branch(node);
156+
// swap branches
157+
[node.left, node.right] = [node.right, node.left];
165158
}
166159
}
167160
}

0 commit comments

Comments
 (0)