Binary Search in Trees

Introduction to Binary Search Trees

In the first section, we alluded to the varying uses of trees, especially in the context of sorting and searching. The task of sorting consists of taking data and arranging it in some sort of predetermined order. Searching consists of trying to find a particular piece of data from the total set of data. As one might expect, searching is easier once the data have been sorted. For example, if one had a list of numbers, searching would mean checking whether or not a specific number is in the list and if it is finding exactly where in the list it is. For a more comprehensive discussion of sorting and searching, with particular emphasis on the complexity of the different sorts and searches, see the sorting and searching SparkNotes. Here we will be covering binary search trees more from a practical, rather than a theoretical, perspective.

A binary search tree is one where all of the data in the nodes in the left subtree come before the data in the current node with regard to some ordering scheme, and all the nodes in the right subtree come after. This condition must be true for all of the nodes in the tree. For example:

Figure %: Binary Search Tree

The above is a binary search tree for integers, while the following is not:

Figure %: Binary Search Tree

In a binary search tree, the smallest element will always be the one found by following the subtrees to the left until you reach a leaf. Similarly, the largest is found by traveling to the right until a leaf is reached.

In this topic, we will cover both how to build a binary search tree from a data set as well as how to use it in searching.

Related to this topic is the heap, a tree in which the root node is greater than all of its descendants and in which the subtrees are also heaps.

Take a Study Break

Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

Are you afraid of relationships?

Auntie SparkNotes can help!

Sexy starlet style

See every single look from the Met Gala!

Geeky Actors: Then and Now

Travel back in time!

Villains We Want These Actresses to Play

From super cute to super bad!

10 Movies Better Than Their Books

What do you think?

How To Look Like J-Law...

When you don't look like J-Law.

12 Scientific Inaccuracies in Into Darkness

What did Star Trek get wrong?

The Book

Read What You Love, Anywhere You Like

Get Our FREE NOOK Reading Apps