BeginnerTopic 22 of 104Software & Computing
Trees
Hierarchical structures; binary search trees keep ordered data.
In depth
A tree is a hierarchy of nodes with a root, children and leaves. Binary trees have at most two children; binary search trees keep left<node<right so search is O(h). Traversals: in-order, pre-order, post-order, level-order. Used for file systems, parsing and indexes.
Key points
- Height and depth
- Degenerate trees become lists
- Tries for prefixes
ExampleIn-order traversal of a BST visits keys in sorted order.
Trees organise hierarchical data and ordered sets, which underpins file systems, databases and parsers.
What is Trees?
Hierarchical structures; binary search trees keep ordered data.
Why is it important to understand trees?
Trees organise hierarchical data and ordered sets, which underpins file systems, databases and parsers.
What is a binary search tree?
A tree where each node's left subtree holds smaller values and the right holds larger values, which allows fast search when the tree is balanced.
What happens if data is inserted in sorted order?
The tree becomes a long chain, and operations slow to linear time, which is why balanced trees are used.
How is Trees used in practice?
Typical applications include representing the folder structure of a project, storing ordered records in a binary search tree for range queries and modelling the syntax of an arithmetic expression.
What should I learn before Trees?
Study "Hash tables" first (topic 21 of 104 in Software & Computing). It covers the groundwork this topic builds on.
What are the key points of Trees?
Height and depth. Degenerate trees become lists. Tries for prefixes.
Can you give an example of Trees?
In-order traversal of a BST visits keys in sorted order.
What comes after Trees?
The next topic is "Balanced trees", which builds directly on this one.
How difficult is Trees?
It is classed as Beginner in Software & Computing (topic 22 of 104). It is an entry-level topic. It relies only on basic school mathematics and the ideas introduced earlier in this discipline.