Skip to content

Commit e4313b3

Browse files
David AlimazoyaDavid Alimazoya
David Alimazoya
authored and
David Alimazoya
committed
first commit
0 parents  commit e4313b3

11 files changed

+778
-0
lines changed

BinarySearchTree.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Is a tree like data structure
3+
**Rules**
4+
*It starts with a root node
5+
*each node ca have at most 2 nodes
6+
*descreasing items are placed in the left
7+
*Ascending items are placed at the right
8+
*it uses divide and conquer mode of operation
9+
log node = 2^height
10+
**Tpes**
11+
*Balanced:
12+
*each node has 2 siblings
13+
*it is O(log N)
14+
*Unbalanced:
15+
*each node has unqual siblings
16+
*it is O(N)
17+
*/

DoublyLinkedList.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class DoublyLinkedList {
2+
constructor(value) {
3+
this.head = {
4+
value: value,
5+
next: null,
6+
prev: null
7+
};
8+
this.tail = this.head;
9+
this.length = 1;
10+
}
11+
append(value) {
12+
const newNode = {
13+
value: value,
14+
next: null,
15+
prev: null
16+
}
17+
console.log(newNode)
18+
newNode.prev = this.tail
19+
this.tail.next = newNode;
20+
this.tail = newNode;
21+
this.length++;
22+
return this;
23+
}
24+
prepend(value) {
25+
const newNode = {
26+
value: value,
27+
next: null,
28+
prev: null
29+
}
30+
newNode.next = this.head;
31+
this.head.prev = newNode
32+
this.head = newNode;
33+
this.length++;
34+
return this;
35+
}
36+
printList() {
37+
const array = [];
38+
let currentNode = this.head;
39+
while(currentNode !== null){
40+
array.push(currentNode.value)
41+
currentNode = currentNode.next
42+
}
43+
return array;
44+
}
45+
insert(index, value){
46+
//Check for proper parameters;
47+
if(index >= this.length) {
48+
return this.append(value);
49+
}
50+
51+
const newNode = {
52+
value: value,
53+
next: null,
54+
prev: null
55+
}
56+
const leader = this.traverseToIndex(index-1);
57+
const follower = leader.next;
58+
leader.next = newNode;
59+
newNode.prev = leader;
60+
newNode.next = follower;
61+
follower.prev = newNode;
62+
this.length++;
63+
console.log(this)
64+
return this.printList();
65+
}
66+
traverseToIndex(index) {
67+
//Check parameters
68+
let counter = 0;
69+
let currentNode = this.head;
70+
while(counter !== index){
71+
currentNode = currentNode.next;
72+
counter++;
73+
}
74+
return currentNode;
75+
}
76+
}
77+
78+
let myLinkedList = new DoublyLinkedList(10);
79+
myLinkedList.append(5)
80+
myLinkedList.append(16)
81+
myLinkedList.prepend(1)
82+
myLinkedList.insert(2, 99)
83+
// myLinkedList.insert(20, 88)
84+
// myLinkedList.printList()
85+
// myLinkedList.remove(2)
86+
// myLinkedList.reverse()

QueueWithLinkedList.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Node {
2+
constructor(value){
3+
this.value = value;
4+
this.next = null;
5+
}
6+
}
7+
8+
class Queue {
9+
constructor(){
10+
this.first = null;
11+
this.last = null;
12+
this.length = 0;
13+
}
14+
peek() {
15+
return this.first;
16+
}
17+
enqueue(value){
18+
const newNode = new Node(value);
19+
if (this.length === 0) {
20+
this.first = newNode;
21+
this.last = newNode;
22+
} else {
23+
this.last.next = newNode;
24+
this.last = newNode;
25+
}
26+
this.length++;
27+
return this.printList();
28+
}
29+
dequeue(){
30+
if (!this.first) {
31+
return null;
32+
}
33+
if (this.first === this.last) {
34+
this.last = null;
35+
}
36+
const holdingPointer = this.first;
37+
this.first = this.first.next;
38+
this.length--;
39+
return this;
40+
}
41+
printList(){
42+
let current = this.first;
43+
let array =[]
44+
while (current) {
45+
array.push(current.value)
46+
current = current.next
47+
}
48+
return array
49+
}
50+
//isEmpty;
51+
}
52+
53+
const myQueue = new Queue();
54+
myQueue.peek();
55+
myQueue.enqueue('Joy');
56+
myQueue.enqueue('Matt');
57+
console.log(myQueue.enqueue('Pavel'))
58+
// myQueue.peek();
59+
// myQueue.dequeue();
60+
// myQueue.dequeue();
61+
// myQueue.dequeue();
62+
// myQueue.peek();

SinglyLiinkedList.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// add a method remove() to the linked list that deletes a node to the specified index.
2+
3+
class LinkedList {
4+
constructor(value) {
5+
this.head = {
6+
value: value,
7+
next: null
8+
};
9+
this.tail = this.head;
10+
this.length = 1;
11+
}
12+
append(value) {
13+
const newNode = {
14+
value: value,
15+
next: null
16+
}
17+
this.tail.next = newNode;
18+
this.tail = newNode;
19+
this.length++;
20+
return this;
21+
}
22+
prepend(value) {
23+
const newNode = {
24+
value: value,
25+
next: null
26+
}
27+
newNode.next = this.head;
28+
this.head = newNode;
29+
this.length++;
30+
return this;
31+
}
32+
printList() {
33+
const array = [];
34+
let currentNode = this.head;
35+
while(currentNode !== null){
36+
array.push(currentNode.value)
37+
currentNode = currentNode.next
38+
}
39+
return array;
40+
}
41+
insert(index, value){
42+
//Check for proper parameters;
43+
if(index >= this.length) {
44+
console.log('yes')
45+
return this.append(value);
46+
}
47+
48+
const newNode = {
49+
value: value,
50+
next: null
51+
}
52+
const leader = this.traverseToIndex(index-1);
53+
const holdingPointer = leader.next;
54+
leader.next = newNode;
55+
newNode.next = holdingPointer;
56+
this.length++;
57+
return this.printList();
58+
}
59+
traverseToIndex(index) {
60+
//Check parameters
61+
let counter = 0;
62+
let currentNode = this.head;
63+
while(counter !== index){
64+
currentNode = currentNode.next;
65+
counter++;
66+
}
67+
return currentNode;
68+
}
69+
}
70+
71+
let myLinkedList = new LinkedList(10);
72+
myLinkedList.append(5);
73+
myLinkedList.append(16);myLinkedList.prepend(1);
74+
myLinkedList.insert(2, 99);
75+
myLinkedList.insert(20, 88);
76+
77+
78+
79+

StackWithArrays.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//LIFO
2+
3+
class Stack {
4+
constructor(){
5+
this.data = [];
6+
}
7+
peek() {
8+
return this.data[this.data.length-1]
9+
}
10+
push(value){
11+
this.data.push(value);
12+
return this.data;
13+
}
14+
pop(){
15+
this.data.pop();
16+
return this.printList()
17+
}
18+
printList(){
19+
return this.data
20+
}
21+
isEmpty(){
22+
if(this.data.length == 0) return true;
23+
return false;
24+
}
25+
}
26+
27+
const myStack = new Stack();
28+
myStack.push(12)
29+
myStack.push(16)
30+
myStack.push(19)
31+
//console.log(myStack.printList())
32+
// console.log(myStack.peek())
33+
console.log(myStack.isEmpty())
34+
console.log(myStack.peek())

StackWithLinkedList.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Node {
2+
constructor(value){
3+
this.value = value;
4+
this.next = null;
5+
}
6+
}
7+
8+
class Stack {
9+
constructor(){
10+
this.top = null;
11+
this.bottom = null;
12+
this.length = 0;
13+
}
14+
peek() {
15+
return this.top;
16+
}
17+
push(value){
18+
const newNode = new Node(value);
19+
if (this.length === 0) {
20+
this.top = newNode;
21+
this.bottom = newNode;
22+
} else {
23+
const holdingPointer = this.top;
24+
this.top = newNode;
25+
this.top.next = holdingPointer;
26+
}
27+
this.length++;
28+
return this;
29+
}
30+
pop(){
31+
if (!this.top) {
32+
return null;
33+
}
34+
if (this.top === this.bottom) {
35+
this.bottom = null;
36+
}
37+
const holdingPointer = this.top;
38+
this.top = this.top.next;
39+
this.length--;
40+
return this;
41+
}
42+
//isEmpty
43+
}
44+
45+
const myStack = new Stack();
46+
myStack.peek();
47+
myStack.push('google');
48+
myStack.push('udemy');
49+
myStack.push('discord');
50+
myStack.peek();
51+
myStack.pop();
52+
myStack.pop();
53+
myStack.pop();
54+
55+
56+
//Discord
57+
//Udemy
58+
//google

firstRecurringCharacter.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//Google Question
2+
//Given an
3+
//let array = [2,5,1,2,3,5,1,2,4];
4+
//It should return 2
5+
6+
//Given an
7+
// let array = [2,1,1,2,3,5,1,2,4];
8+
//It should return 1
9+
10+
//Given an
11+
let array = [2,3,4,5];
12+
//It should return undefined
13+
14+
15+
function firstRecurringCharacter(input) {
16+
let set = new Set();
17+
let dup = [];
18+
for (let i = 0; i < input.length; i++) {
19+
if(set.has(input[i])){
20+
dup.push(input[i])
21+
} else{
22+
set.add(input[i])
23+
}
24+
}
25+
return dup[0]
26+
}
27+
28+
console.log(firstRecurringCharacter(array))
29+
30+
//O(n)

0 commit comments

Comments
 (0)