4.2.8

Binary Search Trees

Test yourself

Binary Search Trees

A binary search tree is a tree data structure used for efficient searching of data.

Illustrative background for Binary search treesIllustrative background for Binary search trees ?? "content

Binary search trees

  • Binary search trees share the same properties as trees, with the exception that each node can have no more than two ‘child’ nodes.
  • Binary search trees are very quick to search.
Illustrative background for Storing dataIllustrative background for Storing data ?? "content

Storing data

  • After storing the first piece of data in the root node:
    • The second entry is stored in a child node to the left of the root if it has a lesser value.
    • The second entry is stored in a child node to the right if it has a greater value.
Illustrative background for ExampleIllustrative background for Example ?? "content

Example

  • The procedure for storing ‘Tiger’, ‘Zebra’, ‘Lion’, ‘Gazelle’ and ‘Rhino’ in the order given is as follows:
  • 'Tiger’ takes the root node.
  • ‘Zebra’ is greater than ‘Tiger’ alphabetically, so is stored as a child node to the right of the root.
Illustrative background for Step 2Illustrative background for Step 2 ?? "content

Step 2

  • ‘Lion’ comes before ‘Tiger’ and so is stored as a child node to the left of the root.
Illustrative background for Step 3Illustrative background for Step 3 ?? "content

Step 3

  • ‘Gazelle’ comes before ‘Tiger’ and before ‘Lion’, so is stored as a left child to the ‘Lion’ node.
  • ‘Rhino’ also comes before ‘Tiger’, but after ‘Lion’ so is stored on the right of this node.

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Ahead

6.3Thinking Procedurally

6.4Thinking Logically

6.5Thinking Concurrently

7Problem Solving & Programming

8Algorithms

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