-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapNodes.js
More file actions
145 lines (132 loc) · 2.89 KB
/
Copy pathswapNodes.js
File metadata and controls
145 lines (132 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.trim().split('\n').map(str => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
class Node
{
constructor(value, depth) {
this.left = -1;
this.right = -1;
this.value = value;
this.depth = depth;
};
inorder () {
if (this.value == -1) {
return "";
}
if (this.left == -1 && this.right == -1) {
return new String(this.value).toString();
}
if (this.left == -1) {
var expr = this.right.inorder().toString();
return `${this.value} ${expr}`;
}
if (this.right == -1) {
var expr = this.left.inorder().toString();
return `${expr} ${this.value}`;
}
var l = this.left.inorder().toString();
var r = this.right.inorder().toString();
return `${l} ${this.value} ${r}`;
};
swap (k) {
if (this.depth % k == 0) {
var temp = this.left;
this.left = this.right;
this.right = temp;
}
if (this.left !== -1) {
this.left.swap(k);
}
if (this.right !== -1) {
this.right.swap(k);
}
};
addLeft (value) {
this.left = new Node(value, this.depth + 1);
return this.left;
};
addRight (value) {
this.right = new Node(value, this.depth + 1);
return this.right;
};
}
function swapNodes(indexes, queries) {
var result = [];
var queue = [];
var root = new Node (1,1);
queue.push(root);//root
for (let j=0; j< indexes.length; j++){
let i=indexes[j];
var currentNode = queue.shift();
//the order of pushing matters !!
if (i[0] != -1) {
var left = currentNode.addLeft(i[0]);
queue.push(left);
}
if (i[1] != -1) {
var right = currentNode.addRight(i[1]);
queue.push(right);
}
}
queries.forEach(element => {
root.swap(element);
result.push(root.inorder());
});
return result;
}
function main() {
let indexes = [];
let queries = [];
/*3
2 3
-1 -1
-1 -1
2
1
1
indexes = [[2,3], [-1,-1], [-1,-1]];
queries = [1,1];*/
/*5
2 3
-1 4
-1 5
-1 -1
-1 -1
1
2
indexes = [[2,3],[-1,4],[-1,5],[-1,-1],[-1,-1]];
queries = [2];*/
/*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*/
indexes = [[2,3],[4,-1],[5,-1],[6,-1],[7,8],[-1,9],[-1,-1],[10,11],[-1,-1],[-1,-1],[-1,-1]];
queries = [2,4];
let result = swapNodes(indexes, queries);
console.log(result.join("\n") + "\n");
}
main();