Skip to content

Commit

Permalink
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Search: Swap Node…
Browse files Browse the repository at this point in the history
…s [Algo]. Swap variables without a function.
  • Loading branch information
Gonzalo Diaz committed Aug 21, 2024
1 parent cb24bca commit 91c5a3b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { Node } from '../../lib/Node';
import {
build_tree,
flat_tree,
swap_branch,
swapNodes,
__INITIAL_LEVEL__
} from './swap_nodes_algo';
import { build_tree, flat_tree, swapNodes } from './swap_nodes_algo';
import TEST_CASES from './swap_nodes_algo.testcases.json';

describe('swap_nodes_algo', () => {
Expand All @@ -33,28 +26,6 @@ describe('swap_nodes_algo', () => {
expect(t_result).toStrictEqual(expected);
});

it('test_swap_branch empty', () => {
expect.assertions(1);

const t_input: null = null;
const t_result: Node<number> | null = swap_branch(t_input);
const expected = null;

expect(t_result).toStrictEqual(expected);
});

it('test_swap_branch', () => {
expect.assertions(1);

const t_input: Node<number> = new Node<number>(__INITIAL_LEVEL__);
t_input.left = new Node<number>(2);
t_input.right = new Node<number>(3);
const t_result: number[] = flat_tree(swap_branch(t_input));
const expected: number[] = [3, 1, 2];

expect(t_result).toStrictEqual(expected);
});

it('build tree and flattened tree test cases', () => {
expect.assertions(4);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,6 @@ export function flat_tree(root: Node<number> | null): number[] {
return output;
}

export function swap_branch(root: Node<number> | null): Node<number> | null {
if (root) {
[root.left, root.right] = [root.right, root.left];
}

return root;
}

export function swapNodes(indexes: number[][], queries: number[]): number[][] {
const tree: Node<number> = build_tree(indexes);
const output: number[][] = [];
Expand All @@ -161,7 +153,8 @@ export function swapNodes(indexes: number[][], queries: number[]): number[][] {

if (t_level % queries[query] == 0) {
for (const node of node_list) {
swap_branch(node);
// swap branches
[node.left, node.right] = [node.right, node.left];
}
}
}
Expand Down

0 comments on commit 91c5a3b

Please sign in to comment.