-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic_Programming.js
More file actions
228 lines (182 loc) · 4.77 KB
/
Dynamic_Programming.js
File metadata and controls
228 lines (182 loc) · 4.77 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Declare and initialize an array
let arr = [1, 2, 3, 4, 5];
// Access the 2nd element (indexed from 0)
console.log(arr[1]); // Outputs: 2
// Modify the 3rd element
arr[2] = 8;
// Insert a new element at the end
arr.push(6);
// Remove the last element
arr.pop();
// Find the index of an element
let index = arr.indexOf(4); // index will be 3
function linearSearch(arr, x) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === x) return i; // return the index of the found element
}
return -1; // element not found
}
let index = linearSearch([2, 4, 0, 1, 9], 1);
console.log(index); // 3
function binarySearch(arr, x) {
let l = 0, r = arr.length - 1;
while (l <= r) {
let m = Math.floor((l + r) / 2);
if (arr[m] === x) return m; // element found
if (arr[m] < x) l = m + 1; // search the right half
else r = m - 1; // search the left half
}
return -1; // element not found
}
let sortedArr = [0, 1, 2, 4, 9];
let index = binarySearch(sortedArr, 4);
console.log(index); // 3
let numbers = [10, 20, 30, 40, 50];
// map: returns a new array with the results of calling a function for every array element
let doubled = numbers.map(num => num * 2);
// filter: creates a new array with elements that pass the test of the provided function
let largeNumbers = numbers.filter(num => num > 25);
// reduce: reduces the array to a single value (from left-to-right)
let sum = numbers.reduce((total, num) => total + num, 0);
console.log(doubled); // [20, 40, 60, 80, 100]
console.log(largeNumbers); // [30, 40, 50]
console.log(sum); // 150
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.isEmpty())
return "Underflow";
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length == 0;
}
}
let stack = new Stack();
stack.push(10);
stack.push(20);
console.log(stack.peek()); // 20
stack.pop();
console.log(stack.peek()); // 10
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
insert(data) {
const newNode = new Node(data);
if (!this.root) {
this.root = newNode;
} else {
this._insertNode(this.root, newNode);
}
}
_insertNode(node, newNode) {
if (newNode.data < node.data) {
if (!node.left) {
node.left = newNode;
} else {
this._insertNode(node.left, newNode);
}
} else {
if (!node.right) {
node.right = newNode;
} else {
this._insertNode(node.right, newNode);
}
}
}
// In-order traversal (left, root, right)
inorder() {
this._inorder(this.root);
}
_inorder(node) {
if (node) {
this._inorder(node.left);
console.log(node.data);
this._inorder(node.right);
}
}
}
// Using the BinaryTree
const tree = new BinaryTree();
tree.insert(15);
tree.insert(10);
tree.insert(25);
tree.insert(7);
tree.insert(12);
tree.insert(20);
tree.insert(30);
// This will print the numbers in ascending order: 7, 10, 12, 15, 20, 25, 30
tree.inorder();
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
insert(data) {
const newNode = new Node(data);
if (!this.root) {
this.root = newNode;
} else {
this._insertNode(this.root, newNode);
}
}
_insertNode(node, newNode) {
if (newNode.data < node.data) {
if (!node.left) {
node.left = newNode;
} else {
this._insertNode(node.left, newNode);
}
} else {
if (!node.right) {
node.right = newNode;
} else {
this._insertNode(node.right, newNode);
}
}
}
// In-order traversal (left, root, right)
inorder() {
this._inorder(this.root);
}
_inorder(node) {
if (node) {
this._inorder(node.left);
console.log(node.data);
this._inorder(node.right);
}
}
}
// Using the BinaryTree
const tree = new BinaryTree();
tree.insert(15);
tree.insert(10);
tree.insert(25);
tree.insert(7);
tree.insert(12);
tree.insert(20);
tree.insert(30);
// This will print the numbers in ascending order: 7, 10, 12, 15, 20, 25, 30
tree.inorder();