Published by Dimitris Kougioumtzis · Updated
Traverse a binary tree stored in level-order slots. x represents an absent node.
Queue each existing child after its parent to visit nodes in increasing depth order.
When to use Binary tree level-order
Level-based displays and shallowest tree matches.
Choosing an alternative
Depth-first traversals follow subtree order rather than breadth layers.
Worked example: input and state changes
Example input
4,2,6,1,3,x,7
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Tree ready. Values below the nodes are their stored values.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [], "kind": "graph", "tree": true, "visited": [], "chosen": [], "output": [] }Step 2: Visit node 1 (value 2); output: 4, 2.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [ 1 ], "kind": "graph", "tree": true, "visited": [ 0, 1 ], "chosen": [], "output": [ 4, 2 ] }Step 4: Visit node 3 (value 1); output: 4, 2, 6, 1.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [ 3 ], "kind": "graph", "tree": true, "visited": [ 0, 1, 2, 3 ], "chosen": [], "output": [ 4, 2, 6, 1 ] }Step 5: Visit node 4 (value 3); output: 4, 2, 6, 1, 3.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [ 4 ], "kind": "graph", "tree": true, "visited": [ 0, 1, 2, 3, 4 ], "chosen": [], "output": [ 4, 2, 6, 1, 3 ] }Step 7: Traversal complete: 4 → 2 → 6 → 1 → 3 → 7.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [], "kind": "graph", "tree": true, "visited": [ 0, 1, 2, 3, 4, 6 ], "chosen": [], "output": [ 4, 2, 6, 1, 3, 7 ] }
Expected final state
Traversal complete: 4 → 2 → 6 → 1 → 3 → 7.
Complexity and input bounds
Family: Trees. Time complexity: O(n).
Space complexity: O(w) queue for maximum width w, at most O(n).
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
1–15 comma-separated values (0–99) or x. Children of index i are 2i+1 and 2i+2; absent nodes cannot have present children.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Missing array slots represent absent nodes, not values to enqueue.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 | // Binary tree in level-order slots, 1–15 entries; -1 denotes an absent node with no present descendants. Root must be present. Writes visited VALUES to output and returns their count. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n, output) { let queue = Array(256).fill(0); let head = 0; let tail = 1; let count = 0; queue[0] = 0; while (head < tail) { let i = queue[head++]; if (i >= n || a[i] < 0) { continue; } output[count++] = a[i]; queue[tail++] = 2 * i + 1; queue[tail++] = 2 * i + 2; } return count; } |
Source
No original paper has been verified for this entry. This source is a reference text or teaching reference, not an asserted original publication.
- The Art of Computer Programming, Volume 1: Fundamental Algorithms (3rd edition)
Donald E. Knuth (1997). Reference book.