Nested Array Generator

Updated on 15 July, 2025
Nested Array Generator header image

Problem Statement

The task is to create a generator object that can perform an inorder traversal on a given multi-dimensional array of integers. A multi-dimensional array can consist of nested arrays at various levels, each potentially containing integers or further nested arrays.

The generator should sequentially visit each element of the multi-dimensional structure from left to right. During its traversal, the generator should yield individual integers and recursively process deeper nested arrays using the same inorder traversal logic. Essentially, the goal is to flatten the nested structure into a sequence of integers while preserving their original nested order.

Examples

Example 1

Input:

arr = [[[6]],[1,3],[]]

Output:

[6,1,3]

Explanation:

const generator = inorderTraversal(arr);
generator.next().value; // 6
generator.next().value; // 1
generator.next().value; // 3
generator.next().done; // true

Example 2

Input:

arr = []

Output:

[]

Explanation:

There are no integers so the generator doesn't yield anything.

Constraints

  • 0 <= arr.flat().length <= 105
  • 0 <= arr.flat()[i] <= 105
  • maxNestingDepth <= 105

Approach and Intuition

  1. Begin by understanding the structure of the multi-dimensional array. It's a nested structure where each element can either be an integer or another multi-dimensional array.
  2. Implement the generator logic:
    • If the current element is an integer, yield it directly.
    • If the element is another nested array, recursively apply the same generator logic to this nested array, thus diving deeper into the structure.
  3. Use generator functions (features that allow a function to yield multiple values over time, pausing between outputs until the next one is requested) to efficiently handle the traversal without the need for an auxiliary data structure to store intermediate results.
  4. Iterate from left to right, processing elements as they come and yielding integers while digging deeper into nested arrays as needed.

We can visually understand the process using the given examples:

  • In Example 1, the multi-dimensional array is [[[6]], [1,3], []]. The generator starts at the outermost array, checks the first element, which itself is an array [[[6]]]. It goes into this array to find [6], then retrieves 6, which it yields. Next, it moves to [1,3], where it yields 1 and then 3, as both are direct integers. The last element, an empty array [], yields nothing.
  • In Example 2, since the input array is empty, the generator does not yield any values, leading to an empty output.

This approach effectively handles varying levels of nesting within the array, ensuring that all integers are yielded in the correct inorder sequence determined by their original placement within the structure.

Solutions

  • JavaScript
js
/**
 * @param {Array} elements
 * @return {Generator}
 */
var flattenArray = function*(elements) {
  yield* elements.flat(Infinity);
};

The solution provided addresses the task of flattening a nested array using JavaScript, utilizing an ES6 generator function. The given function flattenArray accepts an array, potentially containing multiple levels of nested arrays, as its parameter. Utilizing the generator function, it yields a new, flat array with all nested arrays expanded using the flat() method with Infinity as the depth argument. This ensures that no matter how deeply nested the arrays are, they get flattened into a single array structure.

To use this function, simply pass the nested array to flattenArray and iterate over the generator to access the flattened values. This approach is useful for handling complex data structures where arrays of varying depths need to be processed sequentially.

Comments

No comments yet.