Published by Dimitris Kougioumtzis · Updated
Traverse a binary tree stored in level-order slots. x represents an absent node.
Visit left subtree, node, then right subtree; the root separates its two subtree traversals.
When to use Binary tree inorder
Inspecting a binary search tree's ordering invariant.
Choosing an alternative
Preorder is parent-first; level-order groups visits by depth.
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: 1, 2.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [ 1 ], "kind": "graph", "tree": true, "visited": [ 3, 1 ], "chosen": [], "output": [ 1, 2 ] }Step 4: Visit node 0 (value 4); output: 1, 2, 3, 4.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [ 0 ], "kind": "graph", "tree": true, "visited": [ 3, 1, 4, 0 ], "chosen": [], "output": [ 1, 2, 3, 4 ] }Step 5: Visit node 2 (value 6); output: 1, 2, 3, 4, 6.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [ 2 ], "kind": "graph", "tree": true, "visited": [ 3, 1, 4, 0, 2 ], "chosen": [], "output": [ 1, 2, 3, 4, 6 ] }Step 7: Traversal complete: 1 → 2 → 3 → 4 → 6 → 7.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, "∅", 7 ], "active": [], "kind": "graph", "tree": true, "visited": [ 3, 1, 4, 0, 2, 6 ], "chosen": [], "output": [ 1, 2, 3, 4, 6, 7 ] }
Expected final state
Traversal complete: 1 → 2 → 3 → 4 → 6 → 7.
Complexity and input bounds
Family: Trees. Time complexity: O(n).
Space complexity: O(h) traversal stack for height h.
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
The output is sorted only when the input is actually a valid binary search tree.
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 stack = Array(256).fill(0); let top = 0; let current = 0; let count = 0; while (top > 0 || (current < n && a[current] >= 0)) { while (current < n && a[current] >= 0) { stack[top++] = current; current = 2 * current + 1; } current = stack[--top]; output[count++] = a[current]; current = 2 * current + 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.