levo

Fundamentals of Algorithms

Tree traversal

  • 2 things to know
  • Paper 1

Overview

Recall questions on pre-order, in-order and post-order traversal, what each is used for, and producing all three orders for a given binary tree.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Process

Name the three tree-traversal algorithms and state the order in which each deals with a node and its subtrees.

Process

Why are the tree-traversal algorithms naturally written recursively?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Name the three tree-traversal algorithms and state the order in which each deals with a node and its subtrees.

    Model answer

    Pre-order visits the node first, then traverses the left subtree, then the right subtree. In-order traverses the left subtree first, then visits the node, then traverses the right subtree. Post-order traverses the left subtree, then the right subtree, and visits the node last. In all three the left subtree is dealt with before the right; only the position of the node itself changes.

  2. 2

    Describe the pre-order traversal algorithm.

    Model answer

    Starting at the root: output the value at the current node, then apply the same procedure recursively to its left subtree, then apply it recursively to its right subtree. If a subtree is empty, nothing is done for it and the algorithm returns.

  3. 3

    Describe the in-order traversal algorithm.

    Model answer

    Starting at the root: apply the procedure recursively to the left subtree, then output the value at the current node, then apply the procedure recursively to the right subtree. If a subtree is empty, nothing is done for it and the algorithm returns.

  4. 4

    Describe the post-order traversal algorithm.

    Model answer

    Starting at the root: apply the procedure recursively to the left subtree, then apply it recursively to the right subtree, and only then output the value at the current node. If a subtree is empty, nothing is done for it and the algorithm returns.

  5. 5

    A binary tree has root A with left child B and right child C. B has children D and E. C has a right child F and no left child. Give the pre-order traversal.

    Model answer

    A, B, D, E, C, F. A is output first, then the left subtree rooted at B gives B, D, E, then the right subtree rooted at C gives C, F.

  6. 6

    For the same tree — root A, children B and C; B has children D and E; C has right child F — give the in-order traversal.

    Model answer

    D, B, E, A, C, F. The left subtree rooted at B gives D, B, E; then A itself; then the right subtree rooted at C gives C, F, because C has no left child so C is output before F.

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.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

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.

A driver is .
softwarehardwarestorage

Blank Blitz makes facts automatic

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

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It gets the exam shape right

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

Head means the first single element of a non-empty list.
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 Fundamentals of 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.