Skip to content

Commit 507ef49

Browse files
committed
Merge branch 'master' of https://github.com/DevDelvo/algorithms
2 parents e18831f + 69595fc commit 507ef49

File tree

6 files changed

+580
-1
lines changed

6 files changed

+580
-1
lines changed

TreeMap.js

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
// https://github.com/somdipdey/JavaScript-implementation-of-java.util.TreeMap-Class/blob/master/treeMap.js
2+
module.exports = class TreeMap {
3+
constructor(){
4+
this.dict = {}
5+
}
6+
7+
/*
8+
* <summary>
9+
* Value get(Object key)
10+
* This method returns the value to which the specified key is mapped,
11+
* or null if this map contains no mapping for the key.
12+
* </summary>
13+
*/
14+
get(key) {
15+
return this.dict[key];
16+
};
17+
18+
/*
19+
* <summary>
20+
* boolean containsKey(Object key)
21+
* This method returns true
22+
* if this map contains a mapping for the specified key.
23+
* </summary>
24+
*/
25+
containsKey(key) {
26+
if( this.get(key) !== undefined) {
27+
return true;
28+
} else {
29+
return false;
30+
}
31+
};
32+
33+
/*
34+
* <summary>
35+
* Value put(K key, V value)
36+
* This method associates the specified value with the specified key in this map.
37+
* </summary>
38+
*/
39+
put(key, value) {
40+
this.dict[key] = value;
41+
if(isNumber(key))
42+
{
43+
if(allKeysAreNumeral(this.dict)) {
44+
this.dict = sortOnKeys(this.dict);
45+
}
46+
}
47+
};
48+
49+
/*
50+
* <summary>
51+
* Value remove(Object key)
52+
* This method removes the mapping for this key from this TreeMap if present.
53+
* </summary>
54+
*/
55+
remove(key) {
56+
'use strict';
57+
delete this.dict[key];
58+
};
59+
60+
/*
61+
* <summary>
62+
* void clear()
63+
* This method removes all of the mappings from this map.
64+
* </summary>
65+
*/
66+
clear() {
67+
this.dict = {};
68+
};
69+
70+
71+
/*
72+
* <summary>
73+
* treeMap.foreach(V value)
74+
* This method returns each value for each keys in the TreeMap.
75+
* </summary>
76+
*/
77+
forEach(callback){
78+
var len = this.size();
79+
for (i = 0; i < len; i++) {
80+
var item = this.get( Object.keys(this.dict)[i] );
81+
callback(item);
82+
}
83+
}
84+
85+
86+
/*
87+
* <summary>
88+
* int size()
89+
* This method returns the number of key-value mappings in this map.
90+
* </summary>
91+
*/
92+
size() {
93+
return Object.keys(this.dict).length;
94+
};
95+
96+
/*
97+
* <summary>
98+
* boolean isEmpty()
99+
* This method returns a boolean
100+
* determining whether the TreeMap is empty or not.
101+
* </summary>
102+
*/
103+
isEmpty() {
104+
return Object.keys(this.dict).length == 0;
105+
};
106+
107+
108+
/*
109+
* <summary>
110+
* Key floorKey(K key)
111+
* This method returns the greatest key less than or equal
112+
* to the given key, or null if there is no such key.
113+
* </summary>
114+
*/
115+
floorKey(key) {
116+
if(!isNumber(key))
117+
throw "Invalid Operation: key has to be an integer value";
118+
119+
if(this.containsKey(key))
120+
return this.get(key);
121+
122+
return this.floorKey(key - 1);
123+
}
124+
125+
126+
/*
127+
* <summary>
128+
* Key ceilingKey(K key)
129+
* This method returns the least key greater than or equal
130+
* to the given key, or null if there is no such key.
131+
* </summary>
132+
*/
133+
ceilingKey(key) {
134+
if(!isNumber(key))
135+
throw "Invalid Operation: key has to be an integer value";
136+
137+
if(this.containsKey(key))
138+
return this.get(key);
139+
140+
return this.floorKey(key + 1);
141+
}
142+
143+
144+
/*
145+
* <summary>
146+
* Object clone()
147+
* This method returns a shallow copy of this TreeMap instance.
148+
* </summary>
149+
*/
150+
clone(){
151+
return this.dict;
152+
}
153+
154+
155+
/*
156+
* <summary>
157+
* boolean containsValue(Object value)
158+
* This method returns true if this map maps one or more keys to the specified value.
159+
* </summary>
160+
*/
161+
containsValue(value){
162+
var len = this.size();
163+
for (i = 0; i < len; i++) {
164+
var item = this.get( Object.keys(this.dict)[i] );
165+
if(value === item)
166+
return true;
167+
}
168+
169+
return false;
170+
}
171+
172+
173+
/*
174+
* <summary>
175+
* Set<K> keySet()
176+
* This method returns a Set view of the keys contained in this map.
177+
* </summary>
178+
*/
179+
keySet(){
180+
var set = [];
181+
var len = this.size();
182+
for (i = 0; i < len; i++) {
183+
set.push(Object.keys(this.dict)[i]);
184+
}
185+
186+
return set;
187+
}
188+
189+
190+
/*
191+
* <summary>
192+
* Key firstKey()
193+
* This method returns the first (lowest) key currently in this map.
194+
* </summary>
195+
*/
196+
firstKey(){
197+
return Object.keys(this.dict)[0];
198+
}
199+
200+
201+
/*
202+
* <summary>
203+
* Key lastKey()
204+
* This method returns the last (highest) key currently in this map.
205+
* </summary>
206+
*/
207+
lastKey(){
208+
var len = this.size();
209+
return Object.keys(this.dict)[len - 1];
210+
}
211+
212+
}
213+
214+
// some more functions which might be required to complete the operations of TreeMap -->
215+
216+
// Checks if the input is a number or not
217+
function isNumber( input ) {
218+
return !isNaN( input );
219+
}
220+
221+
// Sorts a JavaScript dictionary by key
222+
function sortOnKeys(dict) {
223+
224+
var sorted = [];
225+
for(var key in dict) {
226+
sorted[sorted.length] = key;
227+
}
228+
sorted.sort();
229+
230+
var tempDict = {};
231+
for(var i = 0; i < sorted.length; i++) {
232+
tempDict[sorted[i]] = dict[sorted[i]];
233+
}
234+
235+
return tempDict;
236+
}
237+
238+
// Checks if all the keys in the JavaScript dictionary are numeral.
239+
// If Yes, then it returns true or else it returns false
240+
function allKeysAreNumeral(dict) {
241+
for(var key in dict) {
242+
if(!isNumber(key))
243+
return false;
244+
}
245+
246+
return true;
247+
}

leetcode/dataStructures/pacificAtlanticWaterFlow.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,22 @@ const grid2 = [
9090
[1, 1]
9191
]
9292

93+
const grid3 = [
94+
[19,16,16,12,14,0,17,11,2,0,18,9,13,16,8,8,8,13,17,9,16,9,4,7,1,19,10,7,0,15],
95+
[0,11,4,14,9,0,6,13,16,5,19,9,4,5,4,12,0,13,0,7,9,12,13,15,3,7,4,9,15,1],
96+
[13,14,12,12,12,16,6,15,13,1,8,9,11,14,14,10,19,11,10,0,5,18,4,12,7,13,17,15,18,1],
97+
[16,14,19,5,8,2,11,17,7,1,4,6,5,18,7,15,6,19,18,12,1,14,2,2,0,9,15,14,13,19],
98+
[17,4,12,9,12,10,12,10,4,5,12,7,2,12,18,10,10,8,6,1,5,13,10,3,5,3,11,4,8,11],
99+
[8,19,18,9,6,2,7,3,19,6,0,17,9,12,11,1,15,11,18,1,8,11,1,11,16,7,8,17,15,0],
100+
[7,0,5,11,1,7,12,18,12,1,5,2,11,7,18,12,0,11,9,18,5,2,3,1,1,1,8,14,19,5],
101+
[2,14,2,16,17,19,10,16,1,16,16,3,19,12,13,17,19,12,16,10,16,8,16,12,6,12,13,17,9,12],
102+
[8,1,10,5,7,0,15,19,8,15,4,12,18,18,13,11,5,2,8,3,15,4,3,7,7,14,15,11,6,16],
103+
[0,5,13,19,1,1,2,4,16,2,16,9,15,15,10,10,18,11,17,1,5,14,5,19,7,0,13,7,13,7],
104+
[11,6,16,12,4,2,9,11,17,19,12,10,6,16,17,5,1,18,19,7,15,1,14,0,3,19,7,3,4,13],
105+
[4,11,8,10,10,19,7,18,4,2,2,14,6,9,18,14,2,16,5,3,19,17,4,3,7,1,12,2,4,3],
106+
[14,16,3,11,13,13,6,16,18,0,17,19,4,1,14,12,4,17,5,19,8,13,15,3,15,4,1,14,12,10],
107+
[13,2,12,2,16,12,19,10,19,12,19,14,12,17,16,3,13,7,3,15,16,7,10,15,14,10,6,5,2,18]
108+
]
93109
console.log(pacificAtlantic(grid1)) // [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]
94-
console.log(pacificAtlantic(grid2)) // [[0,0],[0,1],[1,0],[1,1],[2,0],[2,1]]
110+
console.log(pacificAtlantic(grid2)) // [[0,0],[0,1],[1,0],[1,1],[2,0],[2,1]]
111+
console.log(pacificAtlantic(grid3)) // [[0,29],[1,28],[2,28],[12,0],[12,1],[13,0]]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class BST {
2+
constructor(val) {
3+
this.left = null;
4+
this.right = null;
5+
this.val = val
6+
}
7+
8+
insert(val) {
9+
const newNode = new BST(val);
10+
11+
let current = this;
12+
while (true) {
13+
if (val < current.val) {
14+
if (current.left === null) {
15+
current.left = newNode;
16+
break;
17+
} else {
18+
current = current.left
19+
}
20+
} else {
21+
if (current.right === null) {
22+
current.right = newNode;
23+
break;
24+
} else {
25+
current = current.right;
26+
}
27+
}
28+
}
29+
return this;
30+
}
31+
}
32+
33+
const tree1 = new BST(4).insert(2).insert(7).insert(1).insert(3).insert(6).insert(9);
34+
35+
// O(n) time: have to visit all the nodes | O(h) space: where h is height. height could be n so it is actually O(n)
36+
function invertBST(head) {
37+
if (!head) return null;
38+
39+
const right = invertBST(head.right);
40+
const left = invertBST(head.left);
41+
head.left = right;
42+
head.right = left;
43+
return head;
44+
}
45+
46+
console.log('recursive -> ',invertBST(tree1));
47+
48+
// O(n) time | O(n) space
49+
function invertBSTIterative(head) {
50+
if (!head) return null;
51+
const stack = [head];
52+
while (stack.length) {
53+
const current = stack.pop();
54+
const temp = current.left;
55+
current.left = current.right;
56+
current.right = temp;
57+
if (current.left !== null) stack.push(current.left);
58+
if (current.right !== null) stack.push(current.right);
59+
}
60+
return head;
61+
}
62+
63+
console.log('iterative -> ', invertBSTIterative(tree1));

0 commit comments

Comments
 (0)