I need to develop a unique value generator that produces values within a specified range. The criteria are:
- all generated values must be distinct
- the order of values remains consistent upon each run of the generator
- each value should be significantly different from previously emitted values
- the number of values to be generated is unknown
To achieve this, I have decided to represent the problem as a tree structure and utilize a breadth-first search algorithm to divide the range into evenly-sized sections. Then, I plan to traverse each layer systematically in a manner that avoids visiting adjacent nodes.
While my current solution involves recursion, I am exploring ways to rewrite it without recursion, perhaps using a queue instead. I'm a bit stuck at the moment. This is what I have so far:
function* orderedSubdivisor(start: number, end: number): Generator<number> {
const mid = (end - start) / 2 + start
yield mid
const left = orderedSubdivisor(start, mid)
const right = orderedSubdivisor(mid, end)
while (true) {
yield left.next().value
yield right.next().value
}
}
const iter = orderedSubdivisor(0, 64)
console.log(Array.from({length: 63}, () => iter.next().value))
Any suggestions or alternative approaches would be greatly appreciated. Thank you!