levo

Algorithms

Trees and binary search trees

  • 3 things to know
  • Component 02
Start revising · £5/month

Overview

Recall questions on rooted trees and their terms, the binary search tree rule, adding values, how insertion order changes search cost, searching with comparison counts, time complexity, finding the smallest value, uses of trees and expression trees.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Describe the structure of a rooted tree.

Key term

Take a binary search tree with root 50, whose left child is 30 with children 20 and 40, and whose right child is 70 with children 60 and 80. Describe the algorithm for searching it for 60 and count the comparisons.

Key term

In a binary search tree with root 50, children 30 and 70, and leaves 20, 40, 60, 80, what happens searching for 45?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Describe the structure of a rooted tree.

    Model answer

    A tree is a connected structure of nodes joined by edges with no cycles. One node is designated the root and sits at the top. Every other node has exactly one parent above it and any number of children below it. A node with no children is a leaf, and any node together with everything below it forms a subtree.

  2. 2

    Define these tree terms: root, child, parent, leaf, subtree.

    Model answer

    The root is the single node with no parent, from which the whole tree hangs. A child is a node directly below and connected to a given node; that given node is its parent. A leaf is a node with no children. A subtree is a node together with all of its descendants, and it is itself a valid tree.

  3. 3

    Why does the shape of a binary search tree, rather than the number of nodes it holds, determine the cost of searching it?

    Model answer

    Because a search follows one path from the root downwards, comparing once per level, so the number of comparisons is the depth of the tree rather than its size. A balanced tree of n nodes has a depth of about log₂n, so a thousand nodes cost about ten comparisons and a million cost about twenty. A degenerate tree built by inserting values in sorted order has a depth of n, so a thousand nodes cost up to a thousand comparisons. Both trees hold identical data and support identical operations; only the shape differs, and the shape is decided by the order the values were inserted in.

  4. 4

    What extra rule does a binary search tree obey?

    Model answer

    For every node, every value in its left subtree is less than that node's value, and every value in its right subtree is greater than it. The rule applies at every node, not just the root, which is what allows searching to discard a whole subtree at each step, and close to half of what remains while the tree stays balanced.

  5. 5

    Describe the algorithm for adding a value to a binary search tree.

    Model answer

    If the tree is empty, the new value becomes the root. Otherwise start at the root and compare the new value with the current node: if it is smaller move to the left child, if larger move to the right child. Repeat until the required child position is empty, and place the new node there as a leaf. Duplicates are either rejected or handled by a stated convention.

  6. 6

    The same seven values are inserted into a binary search tree in two different orders: 50, 30, 70, 20, 40, 60, 80 and then 20, 30, 40, 50, 60, 70, 80. How does the search cost differ?

    Model answer

    The first order gives a balanced tree of three levels: 50 at the root, 30 and 70 below it, and 20, 40, 60 and 80 as leaves. A search compares at most three values, and the cost is O(log n). The second order gives a degenerate tree: each value is larger than the one before, so every node becomes the right child of its predecessor and the tree is a straight line of seven levels. A search for 80 compares all seven values, and the cost is O(n) — no better than a linear search of a list, and with the memory overhead of the pointers on top. This is why the O(log n) figure quoted for a binary search tree holds only while the tree is reasonably balanced, and why self-balancing variants exist.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Algorithms, on a schedule.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.