2 min read
|
Saved February 14, 2026
|
Copied!
Do you care about this?
This article discusses two methods for representing hierarchical structures like trees. It contrasts using an array of child pointers with a more memory-efficient approach that employs first-child and next-sibling pointers. Each method has its trade-offs in terms of memory management and access speed.
If you do, here's more
Hierarchical structures often arise in programming, whether it's for HTML elements, file systems, or 3D scene graphs. The author presents two main methods for representing these hierarchies. The first method involves using an array of pointers to child nodes within a node structure. This allows easy access to children by index, which can be beneficial in scenarios where specific child nodes need to be accessed. However, this approach has downsides. It requires dynamic memory allocation, increasing overhead and the potential for cache misses due to the extra pointer dereferencing.
The second method simplifies the structure by using two pointers: one for the first child and another for the next sibling. This allocation-free representation eliminates many memory management issues and reduces indirection. Itβs particularly useful in scenarios where traversing the hierarchy is more common than accessing children by index. The trade-off is that accessing a child at a specific index becomes less efficient, as it requires linear traversal through sibling pointers. The author mentions finding this approach beneficial in contexts like 3D transform hierarchies, where traversal is more frequent than direct child access.
Questions about this article
No questions yet.