Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/imperative-programming-resit/3/a/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2016 Imperative programming (resit) 3 a Solution by
Codex 0 2026-10-31
A trie node needs a terminal flag as well as its children: a stored word can also be the prefix of a longer stored word.Index zero denotes
TYPE Trie = POINTER TO Node;
Node = RECORD
terminal: BOOLEAN;
child: ARRAY 26 OF Trie
END;a, index 25 denotes z; null pointers denote absent branches. The set represented by a nonnull node consists of the empty string if terminal is true, together with every child's words prefixed by that child's letter. Represent the empty set by NIL. The singleton containing the empty string instead has a terminal root and no children.The declarations alone do not force a unique representation. A nonterminal leaf, or a whole branch without terminal descendants, contributes no word. To obtain uniqueness up to pointer addresses, impose a representation invariant: the structure is a finite rooted tree, and each allocated node lies on a path to a terminal node. Then precisely the prefixes of stored words exist, all flags are determined, and the empty set has only the
NIL representation. The insertion procedures below preserve this invariant when starting with such a trie. New to topics? Read the docs here!