UNIHIVE

Study Hub UNIPORT CSC 288

CSC 288 · Structured Programming

Recursion, binary search trees and recursive sorting

How recursion works, binary search trees for searching, and quicksort and merge sort.

Our notes

1 min read

Recursion

A subprogram is recursive when it calls itself. It needs:

  1. a base case that stops the calls, and
  2. a recursive case that moves towards the base case.

function Fact(n: integer): longint;
begin
  if n <= 1 then Fact := 1
  else Fact := n * Fact(n - 1)
end;
Fact(5) = 5 × 4 × 3 × 2 × 1 = 120. Each call waits on the stack for the one below it to return.

Binary search trees

Each node has a key, a left subtree with smaller keys and a right subtree with larger keys.
type
  TreePtr = ^TreeNode;
  TreeNode = record
    key: integer;
    left, right: TreePtr
  end;
Insert (recursive): if the tree is empty, make a node; else go left if the key is smaller, right if larger.
Search: the same path. In a balanced tree of n keys a search takes about log₂ n steps.

Worked example: building a tree

Insert 50, 30, 70, 20, 40, 60, 80 in that order: 50 is the root; 30 and 70 its children; 20 and 40 under 30; 60 and 80 under 70.
In-order traversal (left, node, right) prints 20 30 40 50 60 70 80: sorted. Searching for 60 visits 50, 70, 60: three steps.
Inserting already sorted keys (20, 30, 40, ...) gives a tree that is just a long chain, as slow as a list: balanced trees (AVL, B-trees) fix this.

Quicksort (Hoare, 1960)

Choose a pivot, partition the array so smaller items are on its left and larger on its right, then sort each part recursively. Average about n log₂ n comparisons; worst case n² (a bad pivot every time, such as the first element of an already sorted array).

Merge sort

Split the array in half, sort each half recursively, then merge the two sorted halves. Always about n log₂ n comparisons; needs extra space for merging.

Worked example: how much faster

For n = 10,000: n²/2 = 50,000,000 comparisons for selection sort, against n log₂ n ≈ 10,000 × 13.3 ≈ 133,000 for merge sort: about 376 times fewer.

When to use recursion

Natural for trees, divide-and-conquer sorting and nested structures. Avoid it when a simple loop does the job (Fact is clearer as a loop for large n), and beware very deep recursion that overflows the stack.

Exams usually ask

  • Write and trace a recursive function.
  • Build a binary search tree from given keys and traverse it.
  • Explain quicksort and merge sort and compare their efficiency.

Common mistake

Forgetting the base case, or a recursive case that does not move towards it: the calls never end and the stack overflows.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

No reading links here yet

Our team adds free books, videos and practice for each topic, and opens every link before it goes up.

All CSC 288 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.