Programming foundations

Stage 4 · Lesson 9 of 10

Trees at a first look

Data structures and Big-O

A is a set of nodes (items) where each node has children and exactly one parent. The only is the root, the node at the top. Folders on your disk, the HTML of a web page and an organisation chart are all trees.

A binary tree gives each node at most two children. A tree also keeps smaller values on the left and larger ones on the right. So finding a value takes O(log n) steps when the tree is balanced (evenly filled), and O(n) when it degrades into a long chain.

Most tree code uses : handle the node, then call the same on each child. At this stage, aim to build a small binary search tree and walk it, not to master balancing.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Step through linked lists, hash tables and sorts with your own input

  • The Collections Framework section: ArrayList, LinkedList, HashMap, ArrayDeque

Also worth your time

Mark this stage as not done?