diff --git a/docs/hackerrank/interview_preparation_kit/search/swap-nodes-algo.md b/docs/hackerrank/interview_preparation_kit/search/swap-nodes-algo.md new file mode 100644 index 00000000..7b9d7449 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/search/swap-nodes-algo.md @@ -0,0 +1,223 @@ +# [Swap Nodes [Algo]](https://www.hackerrank.com/challenges/swap-nodes-algo) + +Given a tree and an integer, K, we have to swap the subtrees of +all the nodes which are at a depth h + +- Difficulty: `#medium` +- Category: `#ProblemSolvingAdvanced` `#search` + +A binary tree is a tree which is characterized by one of the following properties: + +- It can be empty (null). +- It contains a root node only. +- It contains a root node with a left subtree, a right subtree, + or both. These subtrees are also binary trees. + +In-order traversal is performed as + +1. Traverse the left subtree. +2. Visit root. +3. Traverse the right subtree. + +For this in-order traversal, start from the left child of the +root node and keep exploring the left subtree until you reach a leaf. +When you reach a leaf, back up to its parent, +check for a right child and visit it if there is one. +If there is not a child, you've explored its left and right subtrees fully. +If there is a right child, traverse its left subtree then +its right in the same manner. +Keep doing this until you have traversed the entire tree. +You will only store the values of a node as you visit +when one of the following is true: + +- it is the first node visited, the first time visited +- it is a leaf, should only be visited once +- all of its subtrees have been explored, + should only be visited once while this is true +- it is the root of the tree, the first time visited + +**Swapping**: Swapping subtrees of a node means that if initially node +has left subtree `L` and right subtree `R`, then after swapping, +the left subtree will be `R` and the right subtree, `L`. + +For example, in the following tree, we swap children of node `1`. + +```text + Depth + 1 1 [1] + / \ / \ + 2 3 -> 3 2 [2] + \ \ \ \ + 4 5 5 4 [3] +``` + +In-order traversal of left tree is 2 4 1 3 5 and of right tree is 3 5 1 2 4. + +**Swap operation**: + +We define depth of a node as follows: + +- The root node is at depth 1. +- If the depth of the parent node is d, then the depth of current node will be d+1. + +Given a tree and an integer, `k`, in one operation, we need to swap +the subtrees of all the nodes at each depth h, where h ∈ [k, 2k, 3k,...]. +In other words, if h is a multiple of k, swap the left and right +subtrees of that level. + +You are given a tree of n nodes where nodes are indexed from [1..n] +and it is rooted at 1. +You have to perform t swap operations on it, and after each swap +operation print the in-order traversal of the current state of the tree. + +## Function Description + +Complete the swapNodes function in the editor below. +It should return a two-dimensional array where each element +is an array of integers representing the node indices of an in-order +traversal after a swap operation. + +swapNodes has the following parameter(s): + +- indexes: an array of integers representing index values of each `node[i]`, + beginning with `node[1]`, the first element, as the root. +- queries: an array of integers, each representing a value. + +## Input Format + +The first line contains n, number of nodes in the tree. + +Each of the next n lines contains two integers, `a b`, +where a is the index of left child, and b is the index of right child of ith node. + +**Note**: -1 is used to represent a null node. + +The next line contains an integer, t, the size of . +Each of the next t lines contains an integer , each being a value . + +## Output Format + +For each k, perform the swap operation and store the indices of your +in-order traversal to your result array. +After all swap operations have been performed, return your result array for printing. + +## Constraints + +- $ 1 \leq n \leq 1024 $ +- $ 1 \leq t \leq 100 $ +- $ 1 \leq k \leq n $ +- Either a = -1 or 2 <= a <= n +- Either b = -1 or 2 <= b <= n +- The index of a non-null child will always be greater than that of its parent. + +## Sample Input 0 + +```text +3 +2 3 +-1 -1 +-1 -1 +2 +1 +1 +``` + +## Sample Output 0 + +```text +3 1 2 +2 1 3 +``` + +## Explanation 0 + +As nodes 2 and 3 have no children, swapping will not have any effect on them. +We only have to swap the child nodes of the root node. + +```text + 1 [s] 1 [s] 1 + / \ -> / \ -> / \ + 2 3 [s] 3 2 [s] 2 3 +``` + +Note: [s] indicates that a swap operation is done at this depth. + +## Sample Input 1 + +```text +5 +2 3 +-1 4 +-1 5 +-1 -1 +-1 -1 +1 +2 +``` + +## Sample Output 1 + +```text +4 2 1 5 3 +``` + +## Explanation 1 + +Swapping child nodes of node 2 and 3 we get + +```text + 1 1 + / \ / \ + 2 3 [s] -> 2 3 + \ \ / / + 4 5 4 5 +``` + +## Sample Input 2 + +```text +11 +2 3 +4 -1 +5 -1 +6 -1 +7 8 +-1 9 +-1 -1 +10 11 +-1 -1 +-1 -1 +-1 -1 +2 +2 +4 +``` + +## Sample Output 2 + +```text +2 9 6 4 1 3 7 5 11 8 10 +2 6 9 4 1 3 7 5 10 8 11 +``` + +## Explanation 2 + +Here we perform swap operations at the nodes whose depth is either +`2` or `4` for `K = 2` and then at nodes whose depth is 4 for `K = 4`. + +```text + + 1 1 1 + / \ / \ / \ + / \ / \ / \ + 2 3 [s] 2 3 2 3 + / / \ \ \ \ + / / \ \ \ \ + 4 5 -> 4 5 -> 4 5 + / / \ / / \ / / \ + / / \ / / \ / / \ +6 7 8 [s] 6 7 8 [s] 6 7 8 + \ / \ / / \ \ / \ + \ / \ / / \ \ / \ + 9 10 11 9 11 10 9 10 11 +``` diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.test.ts b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.test.ts new file mode 100644 index 00000000..e3685a12 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from '@jest/globals'; + +import { Node } from '../../lib/Node'; +import { swapNodes, build_tree, flat_tree } from './swap_nodes_algo'; +import BIG_TEST_CASES from './swap_nodes_algo.big.testcases.json'; + +describe('swap_nodes_algo', () => { + it('build_tree and plain test cases', () => { + expect.assertions(1); + + BIG_TEST_CASES.forEach((test) => { + const t_to_test: Node = build_tree(test.nodes); + const t_result: number[] = flat_tree(t_to_test); + + expect(t_result).toStrictEqual(test.flattened); + }); + }); + + it('swapNodes test cases', () => { + expect.assertions(1); + + BIG_TEST_CASES.forEach((test) => { + const t_result: number[][] = swapNodes(test.nodes, test.queries); + + expect(t_result).toStrictEqual(test.expected); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.testcases.json b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.testcases.json new file mode 100644 index 00000000..ca1cfea6 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.testcases.json @@ -0,0 +1,55 @@ +[ + { + "title": "Test case 4", + "nodes": [[-1, 2], [3, 4], [5, 6], [7, -1], [8, -1], [9, 10], [11, 12], [-1, 13], [14, 15], [-1, -1], [-1, 16], [17, -1], [18, 19], [20, 21], [22, 23], [-1, -1], [24, 25], [26, -1], [27, 28], [29, 30], [31, -1], [32, 33], [34, 35], [36, 37], [38, 39], [-1, 40], [-1, 41], [-1, 42], [-1, -1], [43, 44], [-1, -1], [45, 46], [47, 48], [-1, -1], [-1, 49], [-1, 50], [51, 52], [-1, 53], [54, 55], [-1, 56], [57, -1], [-1, 58], [59, -1], [60, 61], [-1, 62], [-1, 63], [-1, -1], [-1, 64], [65, -1], [66, -1], [-1, 67], [-1, -1], [-1, 68], [-1, 69], [70, -1], [71, -1], [72, 73], [74, 75], [-1, -1], [76, -1], [77, -1], [-1, -1], [78, -1], [-1, -1], [79, 80], [81, 82], [-1, 83], [84, -1], [85, -1], [86, -1], [-1, 87], [-1, -1], [-1, -1], [-1, 88], [-1, -1], [89, 90], [-1, -1], [91, -1], [-1, 92], [93, -1], [94, 95], [-1, -1], [96, 97], [98, -1], [-1, 99], [100, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1]], + "flattened": [1, 8, 26, 40, 71, 87, 56, 18, 13, 27, 72, 57, 73, 41, 19, 28, 42, 74, 88, 58, 75, 5, 3, 29, 20, 59, 43, 30, 89, 76, 90, 60, 44, 77, 61, 14, 31, 21, 9, 45, 62, 32, 46, 91, 78, 63, 22, 47, 33, 48, 64, 15, 34, 23, 35, 79, 92, 65, 93, 80, 49, 6, 10, 2, 11, 16, 7, 36, 94, 81, 95, 66, 82, 50, 24, 51, 67, 96, 83, 97, 37, 52, 17, 38, 53, 98, 84, 68, 25, 54, 85, 99, 69, 39, 100, 86, 70, 55, 12, 4], + "queries": [8, 6, 2, 3, 5, 7, 7, 5, 7, 1, 5, 6, 2, 8, 4, 6, 8, 12, 10, 6, 12, 11, 12, 8, 1, 7, 9, 3, 8, 7, 2, 7, 8, 3, 8, 9, 7, 9, 9, 8, 11, 10, 4, 12, 4], + "expected": [ + [1, 8, 40, 71, 87, 56, 26, 18, 13, 72, 57, 73, 41, 27, 19, 42, 74, 88, 58, 75, 28, 5, 3, 29, 20, 89, 76, 90, 60, 44, 77, 61, 30, 59, 43, 14, 31, 21, 9, 46, 91, 78, 63, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 79, 92, 65, 93, 80, 49, 35, 6, 10, 2, 11, 16, 7, 94, 81, 95, 66, 82, 50, 36, 24, 52, 37, 51, 67, 96, 83, 97, 17, 53, 98, 84, 68, 38, 25, 100, 86, 70, 55, 39, 54, 85, 99, 69, 12, 4], + [1, 8, 72, 57, 73, 41, 27, 19, 42, 74, 88, 58, 75, 28, 13, 40, 71, 87, 56, 26, 18, 5, 3, 31, 21, 14, 29, 20, 89, 76, 90, 60, 44, 77, 61, 30, 59, 43, 9, 34, 23, 79, 92, 65, 93, 80, 49, 35, 15, 46, 91, 78, 63, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 2, 11, 16, 7, 53, 98, 84, 68, 38, 25, 100, 86, 70, 55, 39, 54, 85, 99, 69, 17, 94, 81, 95, 66, 82, 50, 36, 24, 52, 37, 51, 67, 96, 83, 97, 12, 4], + [1, 36, 82, 66, 94, 81, 95, 50, 24, 51, 96, 83, 97, 67, 37, 52, 17, 38, 53, 68, 98, 84, 25, 54, 69, 85, 99, 39, 70, 100, 86, 55, 12, 7, 11, 16, 4, 2, 5, 8, 26, 40, 56, 71, 87, 18, 13, 27, 73, 57, 72, 41, 19, 28, 42, 75, 58, 74, 88, 3, 10, 6, 29, 20, 59, 43, 30, 60, 89, 76, 90, 44, 61, 77, 14, 31, 21, 9, 45, 62, 32, 46, 63, 91, 78, 22, 47, 33, 48, 64, 15, 34, 23, 35, 93, 80, 65, 79, 92, 49], + [1, 4, 38, 68, 98, 84, 53, 25, 69, 85, 99, 54, 39, 55, 70, 100, 86, 17, 36, 50, 82, 66, 94, 81, 95, 24, 96, 83, 97, 67, 51, 37, 52, 12, 7, 11, 16, 2, 10, 6, 31, 21, 14, 29, 20, 43, 59, 30, 61, 77, 44, 60, 89, 76, 90, 9, 34, 23, 35, 49, 93, 80, 65, 79, 92, 15, 62, 45, 32, 63, 91, 78, 46, 22, 47, 33, 64, 48, 3, 5, 8, 27, 41, 73, 57, 72, 19, 28, 75, 58, 74, 88, 42, 13, 26, 56, 71, 87, 40, 18], + [1, 4, 12, 38, 98, 84, 68, 53, 25, 85, 99, 69, 54, 39, 55, 100, 86, 70, 17, 36, 50, 94, 81, 95, 66, 82, 24, 67, 96, 83, 97, 51, 37, 52, 7, 16, 11, 2, 10, 6, 34, 23, 35, 49, 79, 92, 65, 93, 80, 15, 62, 45, 32, 91, 78, 63, 46, 22, 47, 33, 64, 48, 9, 31, 21, 14, 29, 20, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 3, 5, 27, 41, 72, 57, 73, 19, 28, 74, 88, 58, 75, 42, 13, 26, 71, 87, 56, 40, 18, 8], + [1, 4, 12, 85, 99, 69, 54, 39, 55, 100, 86, 70, 25, 38, 98, 84, 68, 53, 17, 67, 96, 83, 97, 51, 37, 52, 24, 36, 50, 94, 81, 95, 66, 82, 7, 16, 11, 2, 10, 6, 35, 49, 79, 92, 65, 93, 80, 23, 34, 15, 47, 33, 64, 48, 22, 62, 45, 32, 91, 78, 63, 46, 9, 21, 31, 14, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 20, 29, 3, 5, 28, 74, 88, 58, 75, 42, 19, 27, 41, 72, 57, 73, 13, 18, 26, 71, 87, 56, 40, 8], + [1, 4, 12, 38, 98, 84, 68, 53, 25, 85, 99, 69, 54, 39, 55, 100, 86, 70, 17, 36, 50, 94, 81, 95, 66, 82, 24, 67, 96, 83, 97, 51, 37, 52, 7, 16, 11, 2, 10, 6, 34, 23, 35, 49, 79, 92, 65, 93, 80, 15, 62, 45, 32, 91, 78, 63, 46, 22, 47, 33, 64, 48, 9, 31, 21, 14, 29, 20, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 3, 5, 27, 41, 72, 57, 73, 19, 28, 74, 88, 58, 75, 42, 13, 26, 71, 87, 56, 40, 18, 8], + [1, 4, 38, 68, 98, 84, 53, 25, 69, 85, 99, 54, 39, 55, 70, 100, 86, 17, 36, 50, 82, 66, 94, 81, 95, 24, 96, 83, 97, 67, 51, 37, 52, 12, 7, 11, 16, 2, 10, 6, 31, 21, 14, 29, 20, 43, 59, 30, 61, 77, 44, 60, 89, 76, 90, 9, 34, 23, 35, 49, 93, 80, 65, 79, 92, 15, 62, 45, 32, 63, 91, 78, 46, 22, 47, 33, 64, 48, 3, 5, 8, 27, 41, 73, 57, 72, 19, 28, 75, 58, 74, 88, 42, 13, 26, 56, 71, 87, 40, 18], + [1, 4, 69, 85, 99, 54, 39, 55, 70, 100, 86, 25, 38, 68, 98, 84, 53, 17, 96, 83, 97, 67, 51, 37, 52, 24, 36, 50, 82, 66, 94, 81, 95, 12, 7, 11, 16, 2, 10, 6, 21, 31, 14, 43, 59, 30, 61, 77, 44, 60, 89, 76, 90, 20, 29, 9, 35, 49, 93, 80, 65, 79, 92, 23, 34, 15, 47, 33, 64, 48, 22, 62, 45, 32, 63, 91, 78, 46, 3, 5, 8, 28, 75, 58, 74, 88, 42, 19, 27, 41, 73, 57, 72, 13, 18, 26, 56, 71, 87, 40], + [40, 87, 71, 56, 26, 18, 13, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 8, 5, 3, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 92, 79, 65, 80, 93, 49, 35, 9, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 14, 31, 21, 6, 10, 2, 16, 11, 7, 12, 95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 17, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 4, 1], + [8, 40, 56, 87, 71, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 5, 3, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 80, 93, 65, 92, 79, 49, 35, 6, 10, 2, 11, 16, 7, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 17, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 12, 4, 1], + [8, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 13, 40, 56, 87, 71, 26, 18, 5, 3, 31, 21, 14, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 9, 34, 23, 80, 93, 65, 92, 79, 49, 35, 15, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 2, 11, 16, 7, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 17, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 12, 4, 1], + [36, 95, 81, 94, 66, 82, 50, 24, 51, 67, 97, 83, 96, 37, 52, 17, 38, 53, 84, 98, 68, 25, 54, 99, 85, 69, 39, 86, 100, 70, 55, 12, 7, 11, 16, 4, 2, 5, 8, 26, 40, 87, 71, 56, 18, 13, 27, 72, 57, 73, 41, 19, 28, 42, 88, 74, 58, 75, 3, 10, 6, 29, 20, 59, 43, 30, 90, 76, 89, 60, 44, 77, 61, 14, 31, 21, 9, 45, 62, 32, 46, 78, 91, 63, 22, 47, 33, 48, 64, 15, 34, 23, 35, 92, 79, 65, 80, 93, 49, 1], + [95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 17, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 12, 7, 11, 16, 4, 2, 5, 8, 40, 87, 71, 56, 26, 18, 13, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 3, 10, 6, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 14, 31, 21, 9, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 92, 79, 65, 80, 93, 49, 35, 1], + [11, 16, 7, 36, 95, 81, 94, 66, 82, 50, 24, 51, 67, 97, 83, 96, 37, 52, 17, 38, 53, 84, 98, 68, 25, 54, 99, 85, 69, 39, 86, 100, 70, 55, 12, 4, 2, 8, 26, 40, 87, 71, 56, 18, 13, 27, 72, 57, 73, 41, 19, 28, 42, 88, 74, 58, 75, 5, 3, 29, 20, 59, 43, 30, 90, 76, 89, 60, 44, 77, 61, 14, 31, 21, 9, 45, 62, 32, 46, 78, 91, 63, 22, 47, 33, 48, 64, 15, 34, 23, 35, 92, 79, 65, 80, 93, 49, 6, 10, 1], + [11, 16, 7, 38, 53, 84, 98, 68, 25, 54, 99, 85, 69, 39, 86, 100, 70, 55, 17, 36, 95, 81, 94, 66, 82, 50, 24, 51, 67, 97, 83, 96, 37, 52, 12, 4, 2, 8, 27, 72, 57, 73, 41, 19, 28, 42, 88, 74, 58, 75, 13, 26, 40, 87, 71, 56, 18, 5, 3, 31, 21, 14, 29, 20, 59, 43, 30, 90, 76, 89, 60, 44, 77, 61, 9, 34, 23, 35, 92, 79, 65, 80, 93, 49, 15, 45, 62, 32, 46, 78, 91, 63, 22, 47, 33, 48, 64, 6, 10, 1], + [11, 16, 7, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 17, 95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 12, 4, 2, 8, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 13, 40, 87, 71, 56, 26, 18, 5, 3, 31, 21, 14, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 9, 34, 23, 92, 79, 65, 80, 93, 49, 35, 15, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 1], + [11, 16, 7, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 17, 95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 12, 4, 2, 8, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 13, 40, 87, 71, 56, 26, 18, 5, 3, 31, 21, 14, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 9, 34, 23, 92, 79, 65, 80, 93, 49, 35, 15, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 1], + [11, 16, 7, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 17, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 12, 4, 2, 8, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 13, 40, 56, 87, 71, 26, 18, 5, 3, 31, 21, 14, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 9, 34, 23, 80, 93, 65, 92, 79, 49, 35, 15, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 1], + [11, 16, 7, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 17, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 12, 4, 2, 8, 40, 56, 87, 71, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 5, 3, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 80, 93, 65, 92, 79, 49, 35, 6, 10, 1], + [11, 16, 7, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 17, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 12, 4, 2, 8, 40, 56, 87, 71, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 5, 3, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 80, 93, 65, 92, 79, 49, 35, 6, 10, 1], + [11, 16, 7, 82, 66, 94, 81, 95, 50, 36, 24, 52, 37, 51, 96, 83, 97, 67, 17, 53, 68, 98, 84, 38, 25, 70, 100, 86, 55, 39, 54, 69, 85, 99, 12, 4, 2, 8, 40, 56, 71, 87, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 74, 88, 28, 5, 3, 29, 20, 60, 89, 76, 90, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 91, 78, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 93, 80, 65, 79, 92, 49, 35, 6, 10, 1], + [11, 16, 7, 82, 66, 94, 81, 95, 50, 36, 24, 52, 37, 51, 96, 83, 97, 67, 17, 53, 68, 98, 84, 38, 25, 70, 100, 86, 55, 39, 54, 69, 85, 99, 12, 4, 2, 8, 40, 56, 71, 87, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 74, 88, 28, 5, 3, 29, 20, 60, 89, 76, 90, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 91, 78, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 93, 80, 65, 79, 92, 49, 35, 6, 10, 1], + [11, 16, 7, 36, 82, 66, 94, 81, 95, 50, 24, 51, 96, 83, 97, 67, 37, 52, 17, 38, 53, 68, 98, 84, 25, 54, 69, 85, 99, 39, 70, 100, 86, 55, 12, 4, 2, 8, 26, 40, 56, 71, 87, 18, 13, 27, 73, 57, 72, 41, 19, 28, 42, 75, 58, 74, 88, 5, 3, 29, 20, 59, 43, 30, 60, 89, 76, 90, 44, 61, 77, 14, 31, 21, 9, 45, 62, 32, 46, 63, 91, 78, 22, 47, 33, 48, 64, 15, 34, 23, 35, 93, 80, 65, 79, 92, 49, 6, 10, 1], + [1, 10, 6, 49, 92, 79, 65, 80, 93, 35, 23, 34, 15, 64, 48, 33, 47, 22, 78, 91, 63, 46, 32, 62, 45, 9, 21, 31, 14, 77, 61, 44, 90, 76, 89, 60, 30, 43, 59, 20, 29, 3, 5, 88, 74, 58, 75, 42, 28, 19, 41, 72, 57, 73, 27, 13, 18, 87, 71, 56, 40, 26, 8, 2, 4, 12, 55, 86, 100, 70, 39, 99, 85, 69, 54, 25, 84, 98, 68, 53, 38, 17, 52, 37, 67, 97, 83, 96, 51, 24, 50, 95, 81, 94, 66, 82, 36, 7, 16, 11], + [1, 10, 6, 34, 23, 49, 92, 79, 65, 80, 93, 35, 15, 78, 91, 63, 46, 32, 62, 45, 22, 64, 48, 33, 47, 9, 31, 21, 14, 29, 20, 77, 61, 44, 90, 76, 89, 60, 30, 43, 59, 3, 5, 41, 72, 57, 73, 27, 19, 88, 74, 58, 75, 42, 28, 13, 87, 71, 56, 40, 26, 18, 8, 2, 4, 12, 84, 98, 68, 53, 38, 25, 55, 86, 100, 70, 39, 99, 85, 69, 54, 17, 50, 95, 81, 94, 66, 82, 36, 24, 52, 37, 67, 97, 83, 96, 51, 7, 16, 11], + [1, 10, 6, 34, 23, 92, 79, 65, 80, 93, 49, 35, 15, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 9, 31, 21, 14, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 3, 5, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 13, 40, 87, 71, 56, 26, 18, 8, 2, 4, 12, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 17, 95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 7, 16, 11], + [1, 5, 87, 71, 56, 40, 26, 18, 13, 41, 72, 57, 73, 27, 19, 88, 74, 58, 75, 42, 28, 8, 3, 10, 6, 78, 91, 63, 46, 32, 62, 45, 22, 64, 48, 33, 47, 15, 34, 23, 49, 92, 79, 65, 80, 93, 35, 9, 29, 20, 77, 61, 44, 90, 76, 89, 60, 30, 43, 59, 14, 31, 21, 2, 12, 50, 95, 81, 94, 66, 82, 36, 24, 52, 37, 67, 97, 83, 96, 51, 17, 84, 98, 68, 53, 38, 25, 55, 86, 100, 70, 39, 99, 85, 69, 54, 7, 16, 11, 4], + [1, 5, 26, 87, 71, 56, 40, 18, 13, 27, 41, 72, 57, 73, 19, 28, 88, 74, 58, 75, 42, 8, 3, 10, 6, 62, 45, 32, 78, 91, 63, 46, 22, 47, 33, 64, 48, 15, 34, 23, 35, 49, 92, 79, 65, 80, 93, 9, 29, 20, 43, 59, 30, 77, 61, 44, 90, 76, 89, 60, 14, 31, 21, 2, 12, 36, 50, 95, 81, 94, 66, 82, 24, 67, 97, 83, 96, 51, 37, 52, 17, 38, 84, 98, 68, 53, 25, 99, 85, 69, 54, 39, 55, 86, 100, 70, 7, 16, 11, 4], + [1, 5, 18, 26, 87, 71, 56, 40, 13, 28, 88, 74, 58, 75, 42, 19, 27, 41, 72, 57, 73, 8, 3, 10, 6, 47, 33, 64, 48, 22, 62, 45, 32, 78, 91, 63, 46, 15, 35, 49, 92, 79, 65, 80, 93, 23, 34, 9, 43, 59, 30, 77, 61, 44, 90, 76, 89, 60, 20, 29, 14, 21, 31, 2, 12, 67, 97, 83, 96, 51, 37, 52, 24, 36, 50, 95, 81, 94, 66, 82, 17, 99, 85, 69, 54, 39, 55, 86, 100, 70, 25, 38, 84, 98, 68, 53, 7, 16, 11, 4], + [1, 16, 11, 7, 12, 55, 70, 86, 100, 39, 69, 99, 85, 54, 25, 68, 84, 98, 53, 38, 17, 52, 37, 97, 83, 96, 67, 51, 24, 50, 82, 66, 95, 81, 94, 36, 4, 2, 75, 58, 88, 74, 42, 28, 19, 41, 73, 57, 72, 27, 13, 18, 56, 87, 71, 40, 26, 8, 5, 3, 49, 80, 93, 65, 92, 79, 35, 23, 34, 15, 64, 48, 33, 47, 22, 63, 78, 91, 46, 32, 62, 45, 9, 21, 31, 14, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 20, 29, 6, 10], + [1, 16, 11, 7, 12, 68, 84, 98, 53, 38, 25, 55, 70, 86, 100, 39, 69, 99, 85, 54, 17, 50, 82, 66, 95, 81, 94, 36, 24, 52, 37, 97, 83, 96, 67, 51, 4, 2, 41, 73, 57, 72, 27, 19, 75, 58, 88, 74, 42, 28, 13, 56, 87, 71, 40, 26, 18, 8, 5, 3, 34, 23, 49, 80, 93, 65, 92, 79, 35, 15, 63, 78, 91, 46, 32, 62, 45, 22, 64, 48, 33, 47, 9, 31, 21, 14, 29, 20, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 6, 10], + [1, 16, 11, 7, 12, 38, 68, 84, 98, 53, 25, 69, 99, 85, 54, 39, 55, 70, 86, 100, 17, 36, 50, 82, 66, 95, 81, 94, 24, 97, 83, 96, 67, 51, 37, 52, 4, 2, 27, 41, 73, 57, 72, 19, 28, 75, 58, 88, 74, 42, 13, 26, 56, 87, 71, 40, 18, 8, 5, 3, 34, 23, 35, 49, 80, 93, 65, 92, 79, 15, 62, 45, 32, 63, 78, 91, 46, 22, 47, 33, 64, 48, 9, 31, 21, 14, 29, 20, 43, 59, 30, 61, 77, 44, 60, 90, 76, 89, 6, 10], + [1, 4, 16, 11, 7, 12, 36, 82, 66, 95, 81, 94, 50, 24, 51, 97, 83, 96, 67, 37, 52, 17, 38, 53, 68, 84, 98, 25, 54, 69, 99, 85, 39, 70, 86, 100, 55, 2, 45, 62, 32, 46, 63, 78, 91, 22, 47, 33, 48, 64, 15, 34, 23, 35, 80, 93, 65, 92, 79, 49, 9, 29, 20, 59, 43, 30, 60, 90, 76, 89, 44, 61, 77, 14, 31, 21, 6, 10, 3, 26, 40, 56, 87, 71, 18, 13, 27, 73, 57, 72, 41, 19, 28, 42, 75, 58, 88, 74, 8, 5], + [1, 4, 16, 11, 7, 12, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 17, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 2, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 80, 93, 65, 92, 79, 49, 35, 9, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 14, 31, 21, 6, 10, 3, 40, 56, 87, 71, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 8, 5], + [1, 4, 16, 11, 7, 12, 50, 82, 66, 95, 81, 94, 36, 24, 52, 37, 97, 83, 96, 67, 51, 17, 68, 84, 98, 53, 38, 25, 55, 70, 86, 100, 39, 69, 99, 85, 54, 2, 63, 78, 91, 46, 32, 62, 45, 22, 64, 48, 33, 47, 15, 34, 23, 49, 80, 93, 65, 92, 79, 35, 9, 29, 20, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 14, 31, 21, 6, 10, 3, 56, 87, 71, 40, 26, 18, 13, 41, 73, 57, 72, 27, 19, 75, 58, 88, 74, 42, 28, 8, 5], + [1, 4, 16, 11, 7, 12, 52, 37, 97, 83, 96, 67, 51, 24, 50, 82, 66, 95, 81, 94, 36, 17, 55, 70, 86, 100, 39, 69, 99, 85, 54, 25, 68, 84, 98, 53, 38, 2, 64, 48, 33, 47, 22, 63, 78, 91, 46, 32, 62, 45, 15, 49, 80, 93, 65, 92, 79, 35, 23, 34, 9, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 20, 29, 14, 21, 31, 6, 10, 3, 18, 56, 87, 71, 40, 26, 13, 75, 58, 88, 74, 42, 28, 19, 41, 73, 57, 72, 27, 8, 5], + [1, 4, 16, 11, 7, 12, 52, 37, 51, 97, 83, 96, 67, 24, 82, 66, 95, 81, 94, 50, 36, 17, 70, 86, 100, 55, 39, 54, 69, 99, 85, 25, 53, 68, 84, 98, 38, 2, 48, 64, 33, 47, 22, 46, 63, 78, 91, 32, 45, 62, 15, 80, 93, 65, 92, 79, 49, 35, 23, 34, 9, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 20, 29, 14, 21, 31, 6, 10, 3, 18, 40, 56, 87, 71, 26, 13, 42, 75, 58, 88, 74, 28, 19, 73, 57, 72, 41, 27, 8, 5], + [1, 4, 16, 11, 7, 12, 52, 37, 97, 83, 96, 67, 51, 24, 50, 82, 66, 95, 81, 94, 36, 17, 55, 70, 86, 100, 39, 69, 99, 85, 54, 25, 68, 84, 98, 53, 38, 2, 64, 48, 33, 47, 22, 63, 78, 91, 46, 32, 62, 45, 15, 49, 80, 93, 65, 92, 79, 35, 23, 34, 9, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 20, 29, 14, 21, 31, 6, 10, 3, 18, 56, 87, 71, 40, 26, 13, 75, 58, 88, 74, 42, 28, 19, 41, 73, 57, 72, 27, 8, 5], + [1, 4, 16, 11, 7, 12, 97, 83, 96, 67, 51, 37, 52, 24, 36, 50, 82, 66, 95, 81, 94, 17, 69, 99, 85, 54, 39, 55, 70, 86, 100, 25, 38, 68, 84, 98, 53, 2, 47, 33, 64, 48, 22, 62, 45, 32, 63, 78, 91, 46, 15, 35, 49, 80, 93, 65, 92, 79, 23, 34, 9, 43, 59, 30, 61, 77, 44, 60, 90, 76, 89, 20, 29, 14, 21, 31, 6, 10, 3, 18, 26, 56, 87, 71, 40, 13, 28, 75, 58, 88, 74, 42, 19, 27, 41, 73, 57, 72, 8, 5], + [1, 4, 16, 11, 7, 12, 96, 83, 97, 67, 51, 37, 52, 24, 36, 50, 82, 66, 94, 81, 95, 17, 69, 85, 99, 54, 39, 55, 70, 100, 86, 25, 38, 68, 98, 84, 53, 2, 47, 33, 64, 48, 22, 62, 45, 32, 63, 91, 78, 46, 15, 35, 49, 93, 80, 65, 79, 92, 23, 34, 9, 43, 59, 30, 61, 77, 44, 60, 89, 76, 90, 20, 29, 14, 21, 31, 6, 10, 3, 18, 26, 56, 71, 87, 40, 13, 28, 75, 58, 74, 88, 42, 19, 27, 41, 73, 57, 72, 8, 5], + [1, 4, 16, 11, 7, 12, 67, 96, 83, 97, 51, 37, 52, 24, 36, 50, 94, 81, 95, 66, 82, 17, 85, 99, 69, 54, 39, 55, 100, 86, 70, 25, 38, 98, 84, 68, 53, 2, 47, 33, 64, 48, 22, 62, 45, 32, 91, 78, 63, 46, 15, 35, 49, 79, 92, 65, 93, 80, 23, 34, 9, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 20, 29, 14, 21, 31, 6, 10, 3, 18, 26, 71, 87, 56, 40, 13, 28, 74, 88, 58, 75, 42, 19, 27, 41, 72, 57, 73, 8, 5], + [1, 4, 12, 52, 37, 67, 96, 83, 97, 51, 24, 50, 94, 81, 95, 66, 82, 36, 17, 55, 100, 86, 70, 39, 85, 99, 69, 54, 25, 98, 84, 68, 53, 38, 7, 16, 11, 2, 10, 6, 64, 48, 33, 47, 22, 91, 78, 63, 46, 32, 62, 45, 15, 49, 79, 92, 65, 93, 80, 35, 23, 34, 9, 77, 61, 44, 89, 76, 90, 60, 30, 43, 59, 20, 29, 14, 21, 31, 3, 5, 18, 71, 87, 56, 40, 26, 13, 74, 88, 58, 75, 42, 28, 19, 41, 72, 57, 73, 27, 8], + [1, 4, 12, 52, 37, 67, 96, 83, 97, 51, 24, 50, 94, 81, 95, 66, 82, 36, 17, 55, 100, 86, 70, 39, 85, 99, 69, 54, 25, 98, 84, 68, 53, 38, 7, 16, 11, 2, 10, 6, 64, 48, 33, 47, 22, 91, 78, 63, 46, 32, 62, 45, 15, 49, 79, 92, 65, 93, 80, 35, 23, 34, 9, 77, 61, 44, 89, 76, 90, 60, 30, 43, 59, 20, 29, 14, 21, 31, 3, 5, 18, 71, 87, 56, 40, 26, 13, 74, 88, 58, 75, 42, 28, 19, 41, 72, 57, 73, 27, 8], + [1, 4, 16, 11, 7, 12, 67, 96, 83, 97, 51, 37, 52, 24, 36, 50, 94, 81, 95, 66, 82, 17, 85, 99, 69, 54, 39, 55, 100, 86, 70, 25, 38, 98, 84, 68, 53, 2, 47, 33, 64, 48, 22, 62, 45, 32, 91, 78, 63, 46, 15, 35, 49, 79, 92, 65, 93, 80, 23, 34, 9, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 20, 29, 14, 21, 31, 6, 10, 3, 18, 26, 71, 87, 56, 40, 13, 28, 74, 88, 58, 75, 42, 19, 27, 41, 72, 57, 73, 8, 5] + ] + } +] 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 new file mode 100644 index 00000000..34f5c5b2 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.test.ts @@ -0,0 +1,78 @@ +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 TEST_CASES from './swap_nodes_algo.testcases.json'; + +describe('swap_nodes_algo', () => { + it('test_build_tree_empty', () => { + expect.assertions(1); + + const t_input: number[][] = []; + const t_to_test = build_tree(t_input); + const t_result = flat_tree(t_to_test); + const expected = [1]; + + expect(t_result).toStrictEqual(expected); + }); + + it('test_build_malformed_tree', () => { + expect.assertions(1); + + const t_input: number[][] = [[], []]; + const t_to_test = build_tree(t_input); + const t_result = flat_tree(t_to_test); + const expected = [1]; + + 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); + + TEST_CASES.forEach((test) => { + const t_to_test = build_tree(test.nodes); + const t_result = flat_tree(t_to_test); + + expect(t_result).toStrictEqual(test.flattened); + }); + }); + + it('swapNodes test cases', () => { + expect.assertions(4); + + TEST_CASES.forEach((test) => { + const t_result: number[][] = swapNodes(test.nodes, test.queries); + + expect(t_result).toStrictEqual(test.expected); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.testcases.json b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.testcases.json new file mode 100644 index 00000000..dc1c8751 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.testcases.json @@ -0,0 +1,35 @@ +[ + { + "title": "Sample 0", + "nodes": [[2, 3], [-1, -1], [-1, -1]], + "flattened": [2, 1, 3], + "queries": [1, 1], + "expected": [[3, 1, 2], [2, 1, 3]] + }, + { + "title": "Sample 1", + "nodes": [[2, 3], [-1, 4], [-1, 5], [-1, -1], [-1, -1]], + "flattened": [2, 4, 1, 3, 5], + "queries": [2], + "expected": [[4, 2, 1, 5, 3]] + }, + { + "title": "Sample 2", + "nodes": [[2, 3], [4, -1], [5, -1], [6, -1], [7, 8], [-1, 9], + [-1, -1], [10, 11], [-1, -1], [-1, -1], [-1, -1]], + "flattened": [6, 9, 4, 2, 1, 7, 5, 10, 8, 11, 3], + "queries": [2, 4], + "expected": [[2, 9, 6, 4, 1, 3, 7, 5, 11, 8, 10], + [2, 6, 9, 4, 1, 3, 7, 5, 10, 8, 11]] + }, + { + "title": "Sample Test Case 1", + "nodes": [[2, 3], [4, 5], [6, -1], [-1, 7], [8, 9], [10, 11], [12, 13], + [-1, 14], [-1, -1], [15, -1], [16, 17], [-1, -1], [-1, -1], + [-1, -1], [-1, -1], [-1, -1], [-1, -1]], + "flattened": [4, 12, 7, 13, 2, 8, 14, 5, 9, 1, 15, 10, 6, 16, 11, 17, 3], + "queries": [2, 3], + "expected": [[14, 8, 5, 9, 2, 4, 13, 7, 12, 1, 3, 10, 15, 6, 17, 11, 16], + [9, 5, 14, 8, 2, 13, 7, 12, 4, 1, 3, 17, 11, 16, 6, 10, 15]] + } +] diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.ts b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.ts new file mode 100644 index 00000000..9eec5c3a --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.ts @@ -0,0 +1,177 @@ +/** + * @link Problem export functioninition [[docs/hackerrank/interview_preparation_kit/search/swap-nodes-algo.md]] + */ + +import { logger as console } from '../../../logger'; +import { Node } from '../../lib/Node'; + +// CONSTANTS +export const __INITIAL_LEVEL__: number = 1; +export const __ROOT_VALUE__: number = 1; +export const __LEAF_VALUE__: number = -1; + +export function callback_collect_nodes( + root: Node | null | undefined, + collect: Record[]>, + level: number +): void { + if (root) { + if (collect?.[level] === undefined) { + collect[level] = [root]; + } else { + collect[level].push(root); + } + } +} + +export function callback_collect_flat( + root: Node | null | undefined, + collect: Record[]>, + level: number +): void { + const _level: number = 0 * level; // set a unique key to use dict as a list + if (root) { + if (collect?.[_level] === undefined) { + collect[_level] = [root]; + } else { + collect[_level].push(root); + } + } +} + +export function traverse_in_order_collector( + root: Node | null | undefined, + collect: Record[]>, + level: number, + callbackFn: ( + root: Node | null | undefined, + collect: Record[]>, + level: number + ) => void +): Record[]> { + if (root?.left !== null) { + traverse_in_order_collector(root?.left, collect, level + 1, callbackFn); + } + + callbackFn(root, collect, level); + + if (root?.right !== null) { + traverse_in_order_collector(root?.right, collect, level + 1, callbackFn); + } + + return collect; +} + +export function build_tree(indexes: number[][]): Node { + const indexesCopy: number[][] = [...indexes]; + const root: Node = new Node(__ROOT_VALUE__); + let node_collector: Record[]> = {}; + + while (indexesCopy.length > 0) { + node_collector = {}; + + traverse_in_order_collector( + root, + node_collector, + __INITIAL_LEVEL__, + callback_collect_nodes + ); + + const last_level: number = parseInt( + Object.keys(node_collector) + .sort((a, b) => parseInt(b) - parseInt(a)) + .shift() as string + ); + + const level_size = Math.min( + indexesCopy.length, + node_collector[last_level]?.length + ); + for (let i = 0; i < level_size; i++) { + const current_node: Node = node_collector[last_level][i]; + const new_element: number[] = indexesCopy.shift() as Array; + + if ((new_element?.[0] ?? __LEAF_VALUE__) != __LEAF_VALUE__) { + current_node.left = new Node(new_element[0]); + } + if ((new_element?.[1] ?? __LEAF_VALUE__) != __LEAF_VALUE__) { + current_node.right = new Node(new_element[1]); + } + } + } + + return root; +} + +export function flat_tree(root: Node | null): number[] { + let node_collector: Record[]> = {}; + + node_collector = traverse_in_order_collector( + root, + node_collector, + __INITIAL_LEVEL__, + callback_collect_flat + ); + + const last_level: number = parseInt( + Object.keys(node_collector) + .sort((a, b) => parseInt(b) - parseInt(a)) + .shift() as string + ); + + const output: number[] = []; + node_collector[last_level].forEach((node: Node) => { + output.push(node.data); + }); + + 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[][] = []; + let node_collector: Record[]> = {}; + + traverse_in_order_collector( + tree, + node_collector, + __INITIAL_LEVEL__, + callback_collect_nodes + ); + + node_collector = Object.fromEntries( + Object.entries(node_collector).sort(([a], [b]) => parseInt(a) - parseInt(b)) + ); + + let flattened_tree: number[] = flat_tree(tree); + + console.debug(`Plain tree: ${flattened_tree}`); + + for (const query in queries) { + for (const [level, node_list] of Object.entries(node_collector)) { + const t_level: number = parseInt(level); + + if (t_level % queries[query] == 0) { + for (const node of node_list) { + swap_branch(node); + } + } + } + + flattened_tree = flat_tree(tree); + output.push(flattened_tree); + } + + return output; +} + +export default { swapNodes, __INITIAL_LEVEL__ }; diff --git a/src/hackerrank/lib/Node.ts b/src/hackerrank/lib/Node.ts new file mode 100644 index 00000000..b8a84e33 --- /dev/null +++ b/src/hackerrank/lib/Node.ts @@ -0,0 +1,15 @@ +/* istanbul ignore file */ + +export class Node { + left: Node | null; + right: Node | null; + data: T; + + constructor(data: T) { + this.left = null; + this.right = null; + this.data = data; + } +} + +export default { Node };