levo

Algorithms

Trees and binary search trees

  • 3 things to know
  • Component 02

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.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

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

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

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

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

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

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

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.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

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