Skip to content

[REFACTOR] [Hacker Rank] Interview Preparation Kit: Search: Swap Node… #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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