Algorithm to implement non-binary trees using 1-dimensional vector?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
You can represent a general non-binary tree in a one-dimensional vector if each node stores indices instead of pointers. The most practical array-based technique is the first-child / next-sibling representation, optionally with lastChild to make appends cheaper.
Use child and sibling indices instead of nested lists
The core idea is simple: every node lives in one flat vector, and relationships are expressed by integer indices into that vector.
A node record might store:
- the node's value
- the index of its first child
- the index of its next sibling
- optionally the index of its last child
That layout lets one vector represent a full general tree without storing a variable-length child array inside every node.
For example, if A has children B and C, and B has child D, the vector can look like this:
| Index | Value | First Child | Next Sibling | Last Child |
| 0 | A | 1 | -1 | 2 |
| 1 | B | 3 | 2 | 3 |
| 2 | C | -1 | -1 | -1 |
| 3 | D | -1 | -1 | -1 |
This is a flat structure, but it still preserves tree shape.
Appending a child is straightforward
The main operation is adding a new child to a parent. Append the new node to the vector, then update the parent's child links.
This gives you O(1) append-to-children behavior if last_child is maintained.
Traversal follows first child, then sibling chain
Tree traversal works by visiting a node, then recursively walking its child list through sibling links.
This prints the tree in depth-first order while using only one flat vector for storage.
Why this representation is useful
The biggest advantages are:
- contiguous storage
- no pointer allocation per edge
- stable integer references while the vector layout is controlled
- good fit for serialization or compact in-memory formats
This representation is especially useful in parsers, scene graphs, compact AST storage, and systems where you want tree structure without a heap object graph for every child list.
The main tradeoff is readability. Pointer-based or list-based tree nodes are often easier to understand at first glance. The array representation is better when compactness or flat storage matters more than convenience.
Common Pitfalls
- Forgetting that child traversal requires following the sibling chain, not only the first child pointer.
- Appending children without updating the previous last child's
next_sibling. - Using vector indices carelessly and breaking relationships when nodes are removed or reordered.
- Assuming this layout supports constant-time random access to the nth child without walking siblings.
- Choosing a flat indexed representation when a simpler list-of-children structure would be easier for the actual problem.
Summary
- A non-binary tree can be stored in one vector by keeping child and sibling indices in each node.
- The first-child / next-sibling representation is the standard compact approach.
- Adding
lastChildmakes child append operations efficient. - Traversal works by recursing through the first child and then walking sibling links.
- This design is best when compact flat storage matters more than a simple object model.
Related reading
- Algorithm to iterate through sample space of numbers
- Algorithm to loop over an array from the middle outwards?
- Algorithm to make a String nice or ugly
- Algorithm to make numbers from match sticks
- Algorithm to match pairs from two sets
- Algorithm to merge multiple sorted sequences into one sorted sequence in C
- Algorithm to map an interval to a smaller interval
- Algorithm to mix colours on 7 individual pieces of toy

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.