CmSc 250 Intro to Algorithms


Binary Search Trees

Learning goals
Exam-like questions


Binary Search Trees

The left subtree of each node contains values that are smaller than the value in the given node.

The right subtree of each node contains values that are greater than the value in the given node.

Example:

 

Operations:

  1. Search - compare the values and proceed either to the left or to the right.
  2. Insert - unsuccessful search - insert the new node at the bottom where the search has stopped.
  3. Delete - replace the value in the node with the smallest value in the right subtree
    or the largest value in the left subtree.
  4. The idea behind is: all the values to the right are greater than all the values to the left,
    so the smallest one will be greater too (i.e. after the replacement its left subtree
    will contain smaller values.) And since we have taken the smallest from the right subtree,
    after the replacement the right subtree will contain only greater values.

Creating a binary search tree:

Initially the tree is empty.
The first element to be stored is placed in a node that would be the root of the tree.
Each next element is placed through the "unsuccessful search" procedure.

Retrieving the elements in sorted order - by in-order traversal.

Complexity of search in a binary tree: O(logN)

The search at worst will proceed to the leaves, down a path in the tree.
On average the depth of a binary tree with N elements is log(N).

This is not the worst case though. The worst case is when the items to be inserted
are sorted, then the tree degenerates to a list:

The tree is not balanced.

Disadvantages of Binary Search Trees:

  • The shape of the tree depends on the order of insertions, and it can be degenerated.
  • When inserting or searching for an element, the key of each visited node
    has to be compared with the key of the element to be inserted/found.
    Keys may be long and the run time may increase much.

Improving the efficiency of Binary Search Trees:

  • Keeping the tree balanced:
    AVL trees (Adelson - Velskii and Landis)
    Balance condition: left and right subtrees of each node can differ by at most one level.
    It can be proved that if this condition is observed the depth of the tree is O(logN).
  • Reducing the time for key comparison:
    Radix trees - comparing only the leading bits of the keys.

Learning goals:

  1. Know what a Binary Search Tree (BST) is - how the keys are located (smaller to the left, greater to the right).
  2. Be able to describe how a BST is created and how we search for a key.
  3. Be able to discuss the complexity of BST operations.
  4. Know the advantages and disadvantages of Binary Search Trees.

Exam-like questions:

  1. What is a Binary Search Tree, how are the keys located?
  2. How are the elements in a Binary Search Tree retrieved in sorted order?
  3. What is the complexity of search and insert operations in a BST?
  4. What is the complexity of creating a BST?
  5. Discuss the advantages and disadvantages of Binary Search Trees.
  6. Given a sequence of letters to serve as keys, draw the corresponding binary search tree.

Back to Contents page
Created by Lydia Sinapova