Skip to content

Trees

Trees

Members: Free account

Lessons on this topic

Intermediate · 25 minYou are hereRead

Binary trees

A binary tree is a set of nodes, each pointing to at most two children. Nodes scattered across the heap form a branching shape, and its height decides how much it can hold.

Intermediate · 20 minYou are hereRead

Storing trees

A binary tree can live in an array, with the children of cell i at 2i + 1 and 2i + 2. A complete tree fills it exactly; a lopsided one leaves gaps. Trees with any number of children use two pointers per node.

Intermediate · 30 minYou are hereRead

Tree traversals

A recursive walk visits every node of a tree once. Printing a node before, between or after its two subtrees gives preorder, inorder or postorder, and the same pattern measures a tree.

Intermediate · 25 minYou are hereRead

Traversals with a stack or a queue

A loop that keeps nodes in a stack walks a tree depth first, as the recursion did. Swap the stack for a queue and the same loop visits the tree level by level.

Related topics

Where to go next.

Phasorium

Account

Sign inWith Google, GitHub or your email

Subjects

Site

Look