Test your knowledge with free interactive questions on Seneca — used by over 10 million students.

Trees

Unlike other data structures in computer science, trees represent data hierarchically.

Roots and branches

Roots and branches

  • Trees begin with a ‘root’ node, with one or more ‘branches’ beneath it.
  • Each branch, in turn, can have one or more sub-branches connected to it.
Tree construction

Tree construction

  • The rules for creating a tree are:
    • Each node must only be connected to one ‘parent’ node and one or more ‘children’.
    • There should be no connections directly between nodes on the same level of the hierarchy.
Tree traversal

Tree traversal

  • There are three ways to navigate, or ‘traverse’ a tree data structure:
    • Pre-order.
    • In-order.
    • Post-order.
Pre-order traversal

Pre-order traversal

  • Pre-order traversal starts with the root node, then visits each of the other nodes from left to right.
  • In the example shown, the nodes are visited in the following order:
    • Tiger, Zebra, Lion, Gazelle, Giraffe, Rhino, Hippo, Elephant
In-order traversal

In-order traversal

  • In-order traversal starts with the nodes in the left subtree, then visits the root node followed by the nodes in the right subtree.
  • In this case, the nodes above are visited in the following order:
    • Lion, Zebra, Gazelle, Giraffe, Tiger, Elephant, Hippo, Rhino.
Post-order traversal

Post-order traversal

  • Post-order traversal starts by visiting the nodes in the left subtree from bottom to top, then the right subtree bottom to top, before finishing with a visit to the root node. - The nodes in the example above are therefore visited in the following order:
    • Lion, Gazelle, Giraffe, Zebra, Elephant, Hippo, Rhino, Tiger.
Jump to other topics
1

Components of a Computer

2

Software & Software Development

3

Exchanging Data

4

Data Types, Data Structures & Algorithms

5

Legal, Moral, Cultural & Ethical Issues

6

Elements of Computational Thinking

6.1

Thinking Abstractly

6.2

Thinking Ahead

6.3

Thinking Procedurally

6.4

Thinking Logically

6.5

Thinking Concurrently

7

Problem Solving & Programming

8

Algorithms

Practice questions on Trees

Can you answer these? Test yourself with free interactive practice on Seneca — used by over 10 million students.

  1. 1
  2. 2
  3. 3
  4. 4
Answer all questions on Trees

Unlock your full potential with Seneca Premium

  • Unlimited access to 10,000+ open-ended exam questions

  • Mini-mock exams based on your study history

  • Unlock 800+ premium courses & e-books

Get started with Seneca Premium