Skip to content

Commit 89f4e4c

Browse files
committed
added few more challenged
1 parent aba1fd7 commit 89f4e4c

File tree

7 files changed

+183
-0
lines changed

7 files changed

+183
-0
lines changed

tree.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
class Node {
2+
constructor(value) {
3+
this.value = value;
4+
this.children = [];
5+
}
6+
}
7+
8+
// const current_level = parent_level + 1;
9+
const listNodesAtLevel = (root, level) => {
10+
const list = [];
11+
const queue = [];
12+
};
13+
14+
//can do via bfs too
15+
const countSum = root => {
16+
if (root === null) {
17+
return 0;
18+
}
19+
let sum = 0;
20+
const { children } = root;
21+
22+
sum = sum + root.value;
23+
24+
for (let index = 0; index < children.length; index++) {
25+
sum += countSum(children[index]);
26+
}
27+
28+
return sum;
29+
};
30+
31+
const leavesCount = root => {
32+
if (root === null) return 0;
33+
34+
let count = 0;
35+
36+
const { children } = root;
37+
38+
if (children.length === 0) return 1;
39+
40+
for (let index = 0; index < children.length; index++) {
41+
count += leavesCount(children[index]);
42+
}
43+
return count;
44+
};
45+
46+
const leavesCountBfs = root => {
47+
const queue = [];
48+
let leavesCount = 0;
49+
queue.push(root);
50+
while (queue.length) {
51+
const front = queue.shift();
52+
const { children } = front;
53+
if (children.length === 0) {
54+
leavesCount++;
55+
} else {
56+
children.forEach(child => {
57+
queue.push(child);
58+
});
59+
}
60+
}
61+
62+
return leavesCount;
63+
};
64+
65+
const root = new Node(2);
66+
67+
const children = [];
68+
69+
const one = new Node(3);
70+
const two = new Node(4);
71+
const three = new Node(5);
72+
73+
children.push(one);
74+
children.push(two);
75+
children.push(three);
76+
77+
root.children = children;
78+
79+
console.log(countSum(root));

tuts/data-structures/heap/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const swap = (heap, aIndex, bIndex) => {
2+
const temp = heap[aIndex];
3+
heap[aIndex] = heap[bIndex];
4+
heap[bIndex] = temp;
5+
};
6+
7+
const insert = (heap, element) => {
8+
const parentIndex = Math.floor((index - 1) / 2);
9+
let swapIndex = null;
10+
11+
heap[index] = element;
12+
13+
if (heap[parentIndex] !== undefined && heap[parentIndex] < heap[index]) {
14+
swap(heap, parentIndex, index);
15+
swapIndex = index;
16+
}
17+
18+
if (swapIndex) {
19+
insert(heap, swapIndex, heap[swapIndex]);
20+
}
21+
};
22+
23+
const heap = [];
24+
insert(heap, 2);
25+
insert(heap, 1);
26+
insert(heap, 5);
27+
insert(heap, 9);
28+
29+
console.log(heap);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Crossbar.io node private key - KEEP THIS SAFE!
2+
3+
creator: root@2085fffc81f2
4+
created-at: 2019-03-18T18:44:35.881Z
5+
machine-id: 2085fffc81f2
6+
public-key-ed25519: c395bd0430dc7bd103b4b12c3cac4595a7ea511ab4d39d84dbe4c4e2bb9c12fd
7+
private-key-ed25519: 652050d2f7b8de15f0cd54afed4c6a41c99962bbfc564f133431fcbee5388b3c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Crossbar.io node public key
2+
3+
creator: root@2085fffc81f2
4+
created-at: 2019-03-18T18:44:35.881Z
5+
machine-id: 2085fffc81f2
6+
public-key-ed25519: c395bd0430dc7bd103b4b12c3cac4595a7ea511ab4d39d84dbe4c4e2bb9c12fd

tuts/data-structures/queue/index.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class RingBuffer {
2+
constructor() {
3+
this.head = -1;
4+
this.tail = -1;
5+
this.size = 7;
6+
this.buffer = [];
7+
}
8+
read() {
9+
if(this.tail)
10+
}
11+
write(data) {
12+
this.head = (this.head + 1) % this.size;
13+
14+
}
15+
}
16+
17+
const ringBuffer = new RingBuffer();
18+
ringBuffer.write(1);

tuts/data-structures/stack/index.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Stack {
2+
constructor(capacity = 5) {
3+
this.top = -1;
4+
this.data = new Array(capacity);
5+
}
6+
7+
isEmpty() {
8+
return this.top === -1;
9+
}
10+
11+
isFull() {
12+
return this.top === this.data.length;
13+
}
14+
15+
push(value) {
16+
if (this.isFull()) {
17+
return;
18+
}
19+
this.data[++this.top] = value;
20+
}
21+
22+
pop() {
23+
if (this.isEmpty()) {
24+
return false;
25+
}
26+
return this.data[this.top--];
27+
}
28+
29+
peek() {
30+
return this.data[this.top];
31+
}
32+
}
33+
34+
const stack = new Stack(5);
35+
stack.push(1);
36+
console.log(stack.peek());
37+
stack.push(2);
38+
console.log(stack.peek());
39+
stack.push(3);
40+
console.log(stack.peek());
41+
stack.push(4);
42+
console.log(stack.peek());
43+
stack.push(5);
44+
console.log(stack.peek());

0 commit comments

Comments
 (0)