Skip to content

Commit f4bfb76

Browse files
committed
traversal_bst_range
1 parent 4efe515 commit f4bfb76

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { MyLinkedList } from "./lfu-cache/MyLinkedList.ts";
6161
import { level } from "./binary-tree-level-order-traversal-ii/level.ts";
6262
import { inorder } from "./binary-tree-inorder-traversal/inorder.ts";
6363
import { buildBST } from "./convert-sorted-list-to-binary-search-tree/buildBST.ts";
64+
import { traversal_bst_range } from "./tweet-counts-per-frequency/traversal_bst_range.ts";
6465
export {
6566
TrieNode,
6667
TrieNodeCountWordsEqualTo,
@@ -102,3 +103,4 @@ export { Node as MultilevelDoublyLinkedListNode } from "./flatten-a-multilevel-d
102103
export { MyLinkedList };
103104
export { level };
104105
export { buildBST, inorder };
106+
export { traversal_bst_range };

tweet-counts-per-frequency/index.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { BinarySearchTree, BinarySearchTreeNode } from "../deps.ts";
1+
import { BinarySearchTree } from "../deps.ts";
2+
import { traversal_bst_range } from "./traversal_bst_range.ts";
23

34
export default class TweetCounts {
45
#name_to_bst = new Map<string, BinarySearchTree<number, number>>();
@@ -48,25 +49,3 @@ export default class TweetCounts {
4849
return result;
4950
}
5051
}
51-
export function traversal_bst_range<
52-
K extends string | number = number,
53-
V = number,
54-
>(
55-
node: BinarySearchTreeNode<K, V> | null | undefined,
56-
low: number,
57-
high: number,
58-
callback: (key: K, value: V) => void,
59-
) {
60-
if (!node) return;
61-
62-
const value = node.getKey();
63-
if (value <= high && value >= low) {
64-
callback(value, node.getValue());
65-
}
66-
if (value > low) {
67-
traversal_bst_range(node.getLeft(), low, high, callback);
68-
}
69-
if (value < high) {
70-
traversal_bst_range(node.getRight(), low, high, callback);
71-
}
72-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { BinarySearchTreeNode } from "../deps.ts";
2+
3+
export function traversal_bst_range<
4+
K extends string | number = number,
5+
V = number,
6+
>(
7+
node: BinarySearchTreeNode<K, V> | null | undefined,
8+
low: number,
9+
high: number,
10+
callback: (key: K, value: V) => void,
11+
) {
12+
if (!node) return;
13+
14+
const value = node.getKey();
15+
if (value <= high && value >= low) {
16+
callback(value, node.getValue());
17+
}
18+
if (value > low) {
19+
traversal_bst_range(node.getLeft(), low, high, callback);
20+
}
21+
if (value < high) {
22+
traversal_bst_range(node.getRight(), low, high, callback);
23+
}
24+
}

0 commit comments

Comments
 (0)