-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_17.js
276 lines (217 loc) · 5.55 KB
/
day_17.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Activity 1: Linked List
// Task 1 Implement a Node class to represent an element in a linked list with properties value and next.
class Node {
val;
next;
constructor(val) {
this.val = val;
this.next = null;
}
}
const firstNode = new Node(2);
const secondNode = new Node(10);
firstNode.next = secondNode;
console.log(firstNode);
console.log(firstNode.next);
// Task 2 Implement a LinkedList class with methods to add a node to the end, remove the node from the end and display all nodes.
class LinkedList {
constructor() {
this.head = null;
}
add(val) {
let newNode = new Node(val);
if(this.head == null) {
this.head = newNode;
}
else {
let temp = this.head;
while(temp.next != null) temp = temp.next;
temp.next = newNode;
}
}
remove() {
if(!this.head) {
return null;
}
else if(!this.head.next) {
return this.head = null;
}
let temp = this.head;
while(temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
show() {
let temp = this.head;
while(temp != null) {
process.stdout.write(temp.val + " -> ");
temp = temp.next;
}
console.log("null");
}
}
const ll = new LinkedList;
ll.add(3);
ll.add(5);
ll.add(7);
ll.add(2);
ll.add(19);
ll.show();
ll.remove();
ll.show();
// Activity 2: Stack
// Task 3 Implement a Stack class wtih methods push, pop and peek.
class Stack {
constructor() {
this.arr = [];
}
push(val) {
this.arr.push(val);
}
pop() {
if(this.isEmpty()) {
throw new Error("Stack is Empty!!");
}
return this.arr.pop();
}
peak() {
if(this.isEmpty()) {
throw new Error("Stack is Empty!!");
}
return this.arr[this.arr.length-1];
}
isEmpty() {
return this.arr.length == 0;
}
}
let st = new Stack;
st.push(3);
st.push(6);
st.push(1);
st.push(19);
console.log(st.peak());
st.pop();
console.log(st.peak());
// Task 4 Use the satck class to reverse the string by puhsing all characters onto the stack and then popping them off.
let str = "Prince Singh";
let revStr = new Stack;
for(let i = 0; i<str.length; i++) {
revStr.push(str.charAt(i));
}
let reversed = "";
while(!revStr.isEmpty()) {
reversed += revStr.pop();
}
console.log(reversed);
// Activity 3: Queue
// Task 5 Implement a queue class with methods enqueue (add element), dequeue(remvoe element), and front(view first element).
class Queue {
constructor() {
this.arr = [];
}
enqueue(val) {
this.arr.push(val);
}
dequeue() {
if(this.isEmpty()) {
throw new Error("Queue is Empty!!");
}
return this.arr.shift();
}
front() {
if(this.isEmpty()) {
throw new Error("Queue is Empty!!");
}
return this.arr[0];
}
isEmpty() {
return this.arr.length === 0;
}
}
let q = new Queue;
q.enqueue(12);
q.enqueue(24);
q.enqueue(36);
q.enqueue(48);
console.log(q.front());
q.dequeue();
console.log(q.front());
q.dequeue();
console.log(q.front());
// Task 6- Use the queue class to simulate a simple printer queue where print jobs are added to the queue and processed in order.
let printer = new Queue;
printer.enqueue("Task 1");
printer.enqueue("Task 2");
printer.enqueue("Task 3");
printer.enqueue("Task 4");
printer.enqueue("Task 5");
while(!printer.isEmpty()) {
let front = printer.dequeue();
console.log(front + " Completed.");
}
// Activity 4: Binary Tree
// Task 7 Implement a TreeNode class to represent a node in a binary tree with properties value left right.
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
let root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(7);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(19);
console.log(root.left.val + " <- " + root.val + " -> " + root.right.val);
// Task 8 Implement a BinaryTree class with moethods for inserting values and performing in-order traversal to display nodes.
class BinaryTree {
constructor() {
this.root = null;
}
insert(val) {
let newNode = new TreeNode(val);
if(this.root == null) {
this.root = newNode;
return;
}
this.insertNode(this.root, newNode);
}
insertNode(node, newNode) {
if(node.val > newNode.val) {
if(node.left === null) {
node.left = newNode;
}
else {
this.insertNode(node.left, newNode);
}
}
else {
if(node.right === null) {
node.right = newNode;
}
else {
this.insertNode(node.right, newNode);
}
}
}
inOrderTraversal(root = this.root) {
if(root == null) return null;
this.inOrderTraversal(root.left);
process.stdout.write(root.val + " -> ");
this.inOrderTraversal(root.right);
}
}
let bt = new BinaryTree();
bt.insert(10);
bt.insert(5);
bt.insert(25);
bt.insert(3);
bt.insert(23);
bt.insert(10);
bt.inOrderTraversal();
// Activity 5: Graph (Optional)
// Task 9 Implement a Graph class wtih methods to vertices, add edeges and perform a breadth-frsit search(BFS).