Skip to content

Commit eae15e0

Browse files
committed
fixing sorted bst
1 parent 0d0918f commit eae15e0

File tree

17 files changed

+177
-112
lines changed

17 files changed

+177
-112
lines changed

Dockerfile.r2g

Lines changed: 0 additions & 48 deletions
This file was deleted.

scripts/git/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/git/push.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

scripts/npm/.gitkeep

Whitespace-only changes.

scripts/npm/install-dev.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/npm/publish.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.
File renamed without changes.

scripts/to-fixed.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
const [
4+
e,
5+
π,
6+
φ
7+
] = [
8+
2.71828182845904,
9+
3.14159265358979,
10+
1.61803398874989,
11+
];
12+
13+
14+
console.log(
15+
Number(e.toFixed(5)) === 2.71828 // true
16+
);
17+
18+
console.log(
19+
Number(e.toFixed(3)) === 2.718 // true
20+
);
21+
22+
console.log(
23+
Number(e.toFixed(3)) === 2.7182 // false
24+
);
25+
26+
console.log(
27+
Number(e.toFixed(3)) === 2.71 // false
28+
);
29+
30+

scripts/travis/README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

scripts/travis/after_script.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/travis/before_install.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/travis/install.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

scripts/travis/script.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

scripts/tsc/project.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

scripts/tsc/tests.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

scripts/vscode/.gitkeep

Whitespace-only changes.

src/sorted-queue.ts

Lines changed: 147 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ class SortedQueueNode<V, K> {
1818
right: SortedQueueNode<V, K> = null;
1919
leftOrRight: 'left' | 'right' = null;
2020

21-
constructor({key, val, leftOrRight}: KeyVal<V, K>, left?: SortedQueueNode<V, K> | Symbol, right?: SortedQueueNode<V, K> | Symbol) {
21+
constructor({
22+
key,
23+
val,
24+
leftOrRight
25+
}: KeyVal<V, K>, left?: SortedQueueNode<V, K> | Symbol, right?: SortedQueueNode<V, K> | Symbol) {
2226
this.val = val;
2327
this.key = key;
2428
if (arguments.length > 2) {
@@ -149,6 +153,118 @@ class SortedQueue<V, K = any> {
149153

150154
}
151155

156+
logInOrder2(node: SortedQueueNode<any, any>) {
157+
158+
if (node === null) {
159+
return;
160+
}
161+
162+
if (node.left) {
163+
this.inOrder(node.left);
164+
}
165+
166+
console.log(node.val);
167+
168+
if (node.right) {
169+
this.inOrder(node.right);
170+
}
171+
172+
173+
}
174+
175+
* inOrder(node: SortedQueueNode<any, any>): any {
176+
177+
if (node === null) {
178+
return;
179+
}
180+
181+
if (node.left) {
182+
yield* this.inOrder(node.left);
183+
}
184+
185+
yield node.val;
186+
187+
if (node.right) {
188+
yield* this.inOrder(node.right);
189+
}
190+
191+
}
192+
193+
[Symbol.iterator](node: SortedQueueNode<any, any>): any {
194+
195+
const sq = this;
196+
let currentNode = this.rootNode;
197+
const queue = [this.rootNode];
198+
199+
return {
200+
next() {
201+
202+
if(!currentNode.left){
203+
204+
}
205+
206+
if (currentNode === null) {
207+
return {
208+
done: true,
209+
value: null
210+
}
211+
}
212+
213+
if (node.left) {
214+
currentNode = node.left;
215+
return {
216+
done: false,
217+
value: node.left.val
218+
}
219+
}
220+
221+
222+
if (node.right) {
223+
return
224+
}
225+
226+
return {
227+
done: false,
228+
value: null
229+
}
230+
231+
}
232+
}
233+
234+
235+
}
236+
237+
iterator() {
238+
239+
const sq = this;
240+
241+
return {
242+
243+
[Symbol.iterator](node: SortedQueueNode<any, any>): any {
244+
245+
return {
246+
next() {
247+
248+
for(const n of sq.iterator()){
249+
250+
}
251+
252+
if (node.right) {
253+
return
254+
}
255+
256+
return {
257+
done: false,
258+
value: <unknown>null
259+
}
260+
261+
}
262+
}
263+
264+
}
265+
}
266+
}
267+
152268
find(val: V) {
153269

154270
let currentNode = this.rootNode;
@@ -193,7 +309,6 @@ class SortedQueue<V, K = any> {
193309

194310
const newNode = new SortedQueueNode<V, K>({val, key});
195311
let currentNode = this.rootNode;
196-
197312
let numOfSearches = 0;
198313

199314
while (true) {
@@ -205,10 +320,10 @@ class SortedQueue<V, K = any> {
205320
if (v <= 0) {
206321

207322
if (!currentNode.left) {
208-
newNode.parent = currentNode;
323+
// newNode.parent = currentNode;
209324
currentNode.left = newNode;
210-
newNode.leftOrRight = 'left';
211-
this.doLeftRotation(currentNode);
325+
// newNode.leftOrRight = 'left';
326+
// this.doLeftRotation(currentNode);
212327
break;
213328
}
214329

@@ -220,10 +335,10 @@ class SortedQueue<V, K = any> {
220335

221336

222337
if (!currentNode.right) {
223-
newNode.parent = currentNode;
338+
// newNode.parent = currentNode;
224339
currentNode.right = newNode;
225-
newNode.leftOrRight = 'right';
226-
this.doRightRotation(currentNode);
340+
// newNode.leftOrRight = 'right';
341+
// this.doRightRotation(currentNode);
227342
break;
228343
}
229344

@@ -248,8 +363,8 @@ const getNode = <V, K>(v: number, count: number): SortedQueueNode<V, K> => {
248363
// console.log(v, count);
249364
return new SortedQueueNode<V, K>(
250365
{val: v as any, key: v as any},
251-
count > 19 ? emptyNodeSymbol : getNode(v / 2, count + 1),
252-
count > 19 ? emptyNodeSymbol : getNode(v * 3 / 2, count + 1),
366+
count > 23 ? emptyNodeSymbol : getNode(v / 2, count + 1),
367+
count > 23 ? emptyNodeSymbol : getNode(v * 3 / 2, count + 1),
253368
);
254369
}
255370

@@ -316,7 +431,7 @@ const sq = new SortedQueue(rootNode, {
316431
const vals = [];
317432

318433
console.time('start');
319-
for (let i = 0; i < 1000; i++) {
434+
for (let i = 0; i < 1; i++) {
320435
const r = Math.random();
321436
// console.time(String(r));
322437
sq.insert(r);
@@ -326,6 +441,25 @@ for (let i = 0; i < 1000; i++) {
326441
console.timeEnd('start');
327442

328443

444+
const runlog = () => {
445+
let previous = 0;
446+
let count = 0;
447+
448+
for (const z of sq.inOrder(sq.rootNode)) {
449+
if(z < previous){
450+
throw new Error('smaller.');
451+
}
452+
count++
453+
previous = z;
454+
console.log(z);
455+
}
456+
457+
console.log({count});
458+
}
459+
460+
runlog();
461+
throw 'fpoo'
462+
329463
const doRecurse = <K, V>(n: SortedQueueNode<V, K>, count: number) => {
330464

331465
if (!n) {
@@ -357,10 +491,12 @@ const doRecurse = <K, V>(n: SortedQueueNode<V, K>, count: number) => {
357491
doRecurse(rootNode, 0);
358492
console.log(sq);
359493

494+
360495
for (const v of vals) {
361496
console.log(sq.find(v).numOfSearches);
362497
}
363498
console.log(sq.find(0.375));
364499
console.log(sq.find(0.8125));
365500

366501
console.log(sq.findNextBiggest(0.11));
502+

0 commit comments

Comments
 (0)