Tree traversal algorithms for a binary tree

From Wikibooks, open books for an open world
Jump to: navigation, search

UNIT 3 - ⇑ Programming Concepts ⇑

← Graphs Trees Tree traversal algorithms for a binary tree →


Binary tree.svg

Exercise: Unordered Binary Trees

For the following tree note the root, leafs and the left sub tree

CPT BinaryTree NumberEx1.svg

Answer :

  • Root = 5
  • Left subtree = 2, 1, 4, 3
  • Leafs = 1, 3, 7, 9
Exercise: Ordered Binary Trees

Create a binary tree for the following data input:

5, 2, 6, 8, 4, 1, 9, 7, 3

Answer :

Sorted tree

Create a binary tree for the following major city input:

Monaco, Paris, Vatican, Rome, Norwich, Lewisham, New York, Partington

Answer :

sorted major global cities. This tree is very right heavy and if you continue Computer Science to University level you would be asked to find ways of balancing trees

Create a binary tree for the following list of animals:

Elephant, Cat, Dog, Hippo, Giraffe, Lion, Bear

Answer :