andersch.dev

<2024-06-17 Mon>

Tree

A tree is a data type that represents a hierarchical tree structure with a set of connected nodes. It is a type of graph that does not allow cycles and every node must be connected to one parent.

Encoding n-ary trees as binary trees

Any n-ary tree can be converted to a binary tree that is fully traversable. Only the semantic meaning of the connected nodes for the binary tree change.

struct Node
{
    Node* first; // first child of the node (instead of 'left')
    Node* next;  // next sibling of the node (instead of 'right')
};