Binary Search Tree New

Costs

Balanced Unbalanced (Worst Case)
space
insert
lookup
delete

Quick reference

A binary search tree is a binary tree where the nodes are ordered in a specific way. For every node:

  • The nodes to the left are smaller than the current node.
  • The nodes to the right are larger than the current node.

Checking if a binary tree is a binary search tree is a favorite question from interviews.

A binary search tree with nodes containing integers. The root node contains the integer 50. Each child node to the left of the root contains integers less than 50, and each child node to the right of the root contains integers greater than 50.

Strengths:

  • Good performance across the board. Assuming they're balanced, binary search trees are good at lots of operations, even if they're not constant time for anything.

    • Compared to a sorted array, lookups take the same amount of time (), but inserts and deletes are faster ( for BSTs, for arrays).
    • Compared to hash maps, BSTs have better worst case performance— instead of . But, on average hash maps perform better than BSTs (meaning time complexity).
  • BSTs are sorted. Taking a binary search tree and pulling out all of the elements in sorted order can be done in using an in-order traversal. Finding the element closest to a value can be done in (again, if the BST is balanced!).

Weaknesses:

  • Poor performance if unbalanced. Some types of binary search trees balance automatically, but not all. If a BST is not balanced, then operations become .
  • No operations. BSTs aren't the fastest for anything. On average, an array or a hash map will be faster.

The Binary Search Tree Property

In a binary search tree, for each node:

  • All nodes to the left are smaller than it
  • All nodes to the right are larger than it.
mvp mvp mvp

Balanced Binary Search Trees

Two binary search trees can store the same values in different ways:

Two binary search trees: the one on the left is balanced and the one on the right is unbalanced. Both contain the same values: the integers 1 through 6.

Image Edit: "Non Balanced" should be "Unbalanced".

Even though both of these trees satisfy the binary search tree property, a balanced binary search tree has better performance than an unbalanced one. (Read on to see why!)

Some trees (like AVL trees or Red-Black trees) rearrange nodes to ensure the tree always stays balanced.

Looking up an item

To find an item in a binary search tree, start from the root:

mvp

Compare the root node to the value we're looking for:

  • If the value we're looking for is the root node, we're done!
  • If the value we're looking for is smaller than the root node, then it must be to the left.
  • If the value we're looking for is larger than the root node, then it must be to the right.
mvp

Repeat this process—comparing and then going left or right.

mvp

Until we find our value:

mvp

Lookups in unbalanced trees

Lookups involve walking from the top of the tree down to the leaves, so their time complexity is based on the height of the tree.

Balanced binary search trees are powerful because the number of levels in the tree grows very slowly even as more nodes are added. A balanced binary tree with n nodes has a height that's , making lookups in a balanced binary search tree .

In an unbalanced binary search tree, we might have very few nodes on each level of the tree. Look at this "worst case" scenario, where each node only has a right child:

Two binary search trees: the one on the left is balanced and the one on the right is unbalanced. Both contain the same values: the integers 1 through 6.

Image Edit: Just the unbalanced tree.

To find 5 in that tree, we'll have to touch every other node.

More generally, this tree has n nodes and a height that's . That means lookups on unbalanced binary search trees are . Oof.

Inserting an item

Insert starts off just like lookup.

Beginning at the root node, work downward through the tree by comparing each node's value to the value we're inserting.

mvp mvp mvp

Eventually, we'll get to a spot where we need to go left or right but there's nothing there:

mvp

This is the place where the new node goes:

mvp

Just like lookup, insert takes for balanced binary search trees and for unbalanced binary search trees.

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .