From 91c5a3b3ce5cdc2888bf902db1cce77eca583b57 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 21 Aug 2024 10:31:51 -0400 Subject: [PATCH] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Search: Swap Nodes [Algo]. Swap variables without a function. --- .../search/swap_nodes_algo.test.ts | 31 +------------------ .../search/swap_nodes_algo.ts | 11 ++----- 2 files changed, 3 insertions(+), 39 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.test.ts b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.test.ts index 34f5c5b2..6e0c0914 100644 --- a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.test.ts +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.test.ts @@ -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', () => { @@ -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 | null = swap_branch(t_input); - const expected = null; - - expect(t_result).toStrictEqual(expected); - }); - - it('test_swap_branch', () => { - expect.assertions(1); - - const t_input: Node = new Node(__INITIAL_LEVEL__); - t_input.left = new Node(2); - t_input.right = new Node(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); diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.ts b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.ts index a619a0ff..3ade8fae 100644 --- a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.ts +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.ts @@ -127,14 +127,6 @@ export function flat_tree(root: Node | null): number[] { return output; } -export function swap_branch(root: Node | null): Node | null { - if (root) { - [root.left, root.right] = [root.right, root.left]; - } - - return root; -} - export function swapNodes(indexes: number[][], queries: number[]): number[][] { const tree: Node = build_tree(indexes); const output: number[][] = []; @@ -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]; } } }