Skip to content

Commit c7e834b

Browse files
authored
Merge pull request #441 from sir-gon/feature/swap_nodes_algo
Feature/swap nodes algo
2 parents 0f42047 + 6972415 commit c7e834b

File tree

7 files changed

+611
-0
lines changed

7 files changed

+611
-0
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
# [Swap Nodes [Algo]](https://www.hackerrank.com/challenges/swap-nodes-algo)
2+
3+
Given a tree and an integer, K, we have to swap the subtrees of
4+
all the nodes which are at a depth h
5+
6+
- Difficulty: `#medium`
7+
- Category: `#ProblemSolvingAdvanced` `#search`
8+
9+
A binary tree is a tree which is characterized by one of the following properties:
10+
11+
- It can be empty (null).
12+
- It contains a root node only.
13+
- It contains a root node with a left subtree, a right subtree,
14+
or both. These subtrees are also binary trees.
15+
16+
In-order traversal is performed as
17+
18+
1. Traverse the left subtree.
19+
2. Visit root.
20+
3. Traverse the right subtree.
21+
22+
For this in-order traversal, start from the left child of the
23+
root node and keep exploring the left subtree until you reach a leaf.
24+
When you reach a leaf, back up to its parent,
25+
check for a right child and visit it if there is one.
26+
If there is not a child, you've explored its left and right subtrees fully.
27+
If there is a right child, traverse its left subtree then
28+
its right in the same manner.
29+
Keep doing this until you have traversed the entire tree.
30+
You will only store the values of a node as you visit
31+
when one of the following is true:
32+
33+
- it is the first node visited, the first time visited
34+
- it is a leaf, should only be visited once
35+
- all of its subtrees have been explored,
36+
should only be visited once while this is true
37+
- it is the root of the tree, the first time visited
38+
39+
**Swapping**: Swapping subtrees of a node means that if initially node
40+
has left subtree `L` and right subtree `R`, then after swapping,
41+
the left subtree will be `R` and the right subtree, `L`.
42+
43+
For example, in the following tree, we swap children of node `1`.
44+
45+
```text
46+
Depth
47+
1 1 [1]
48+
/ \ / \
49+
2 3 -> 3 2 [2]
50+
\ \ \ \
51+
4 5 5 4 [3]
52+
```
53+
54+
In-order traversal of left tree is 2 4 1 3 5 and of right tree is 3 5 1 2 4.
55+
56+
**Swap operation**:
57+
58+
We define depth of a node as follows:
59+
60+
- The root node is at depth 1.
61+
- If the depth of the parent node is d, then the depth of current node will be d+1.
62+
63+
Given a tree and an integer, `k`, in one operation, we need to swap
64+
the subtrees of all the nodes at each depth h, where h ∈ [k, 2k, 3k,...].
65+
In other words, if h is a multiple of k, swap the left and right
66+
subtrees of that level.
67+
68+
You are given a tree of n nodes where nodes are indexed from [1..n]
69+
and it is rooted at 1.
70+
You have to perform t swap operations on it, and after each swap
71+
operation print the in-order traversal of the current state of the tree.
72+
73+
## Function Description
74+
75+
Complete the swapNodes function in the editor below.
76+
It should return a two-dimensional array where each element
77+
is an array of integers representing the node indices of an in-order
78+
traversal after a swap operation.
79+
80+
swapNodes has the following parameter(s):
81+
82+
- indexes: an array of integers representing index values of each `node[i]`,
83+
beginning with `node[1]`, the first element, as the root.
84+
- queries: an array of integers, each representing a value.
85+
86+
## Input Format
87+
88+
The first line contains n, number of nodes in the tree.
89+
90+
Each of the next n lines contains two integers, `a b`,
91+
where a is the index of left child, and b is the index of right child of ith node.
92+
93+
**Note**: -1 is used to represent a null node.
94+
95+
The next line contains an integer, t, the size of .
96+
Each of the next t lines contains an integer , each being a value .
97+
98+
## Output Format
99+
100+
For each k, perform the swap operation and store the indices of your
101+
in-order traversal to your result array.
102+
After all swap operations have been performed, return your result array for printing.
103+
104+
## Constraints
105+
106+
- $ 1 \leq n \leq 1024 $
107+
- $ 1 \leq t \leq 100 $
108+
- $ 1 \leq k \leq n $
109+
- Either a = -1 or 2 <= a <= n
110+
- Either b = -1 or 2 <= b <= n
111+
- The index of a non-null child will always be greater than that of its parent.
112+
113+
## Sample Input 0
114+
115+
```text
116+
3
117+
2 3
118+
-1 -1
119+
-1 -1
120+
2
121+
1
122+
1
123+
```
124+
125+
## Sample Output 0
126+
127+
```text
128+
3 1 2
129+
2 1 3
130+
```
131+
132+
## Explanation 0
133+
134+
As nodes 2 and 3 have no children, swapping will not have any effect on them.
135+
We only have to swap the child nodes of the root node.
136+
137+
```text
138+
1 [s] 1 [s] 1
139+
/ \ -> / \ -> / \
140+
2 3 [s] 3 2 [s] 2 3
141+
```
142+
143+
Note: [s] indicates that a swap operation is done at this depth.
144+
145+
## Sample Input 1
146+
147+
```text
148+
5
149+
2 3
150+
-1 4
151+
-1 5
152+
-1 -1
153+
-1 -1
154+
1
155+
2
156+
```
157+
158+
## Sample Output 1
159+
160+
```text
161+
4 2 1 5 3
162+
```
163+
164+
## Explanation 1
165+
166+
Swapping child nodes of node 2 and 3 we get
167+
168+
```text
169+
1 1
170+
/ \ / \
171+
2 3 [s] -> 2 3
172+
\ \ / /
173+
4 5 4 5
174+
```
175+
176+
## Sample Input 2
177+
178+
```text
179+
11
180+
2 3
181+
4 -1
182+
5 -1
183+
6 -1
184+
7 8
185+
-1 9
186+
-1 -1
187+
10 11
188+
-1 -1
189+
-1 -1
190+
-1 -1
191+
2
192+
2
193+
4
194+
```
195+
196+
## Sample Output 2
197+
198+
```text
199+
2 9 6 4 1 3 7 5 11 8 10
200+
2 6 9 4 1 3 7 5 10 8 11
201+
```
202+
203+
## Explanation 2
204+
205+
Here we perform swap operations at the nodes whose depth is either
206+
`2` or `4` for `K = 2` and then at nodes whose depth is 4 for `K = 4`.
207+
208+
```text
209+
210+
1 1 1
211+
/ \ / \ / \
212+
/ \ / \ / \
213+
2 3 [s] 2 3 2 3
214+
/ / \ \ \ \
215+
/ / \ \ \ \
216+
4 5 -> 4 5 -> 4 5
217+
/ / \ / / \ / / \
218+
/ / \ / / \ / / \
219+
6 7 8 [s] 6 7 8 [s] 6 7 8
220+
\ / \ / / \ \ / \
221+
\ / \ / / \ \ / \
222+
9 10 11 9 11 10 9 10 11
223+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
3+
import { Node } from '../../lib/Node';
4+
import { swapNodes, build_tree, flat_tree } from './swap_nodes_algo';
5+
import BIG_TEST_CASES from './swap_nodes_algo.big.testcases.json';
6+
7+
describe('swap_nodes_algo', () => {
8+
it('build_tree and plain test cases', () => {
9+
expect.assertions(1);
10+
11+
BIG_TEST_CASES.forEach((test) => {
12+
const t_to_test: Node<number> = build_tree(test.nodes);
13+
const t_result: number[] = flat_tree(t_to_test);
14+
15+
expect(t_result).toStrictEqual(test.flattened);
16+
});
17+
});
18+
19+
it('swapNodes test cases', () => {
20+
expect.assertions(1);
21+
22+
BIG_TEST_CASES.forEach((test) => {
23+
const t_result: number[][] = swapNodes(test.nodes, test.queries);
24+
25+
expect(t_result).toStrictEqual(test.expected);
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)