You are on page 1of 28

Binary Search Trees

•If we want to perform a large number of searches in a


particular list of items, it can be worthwhile to arrange
these items in a binary search tree to facilitate the
subsequent searches.
•A binary search tree is a binary tree in which each child of
a vertex is designated as a right or left child, and each
vertex is labeled with a key, which is one of the items.
•When we construct the tree, vertices are assigned keys so
that the key of a vertex is both larger than the keys of all
vertices in its left subtree and smaller than the keys of all
vertices in its right subtree.

Applied Discrete Mathematics


May 2, 2013 1
Week 14: Trees
Binary Search Trees
•Example: Construct a binary search tree for the strings
math, computer, power, north, zoo, dentist, book.

math

Applied Discrete Mathematics


May 2, 2013 2
Week 14: Trees
Binary Search Trees
•Example: Construct a binary search tree for the strings
math, computer, power, north, zoo, dentist, book.

math

computer

Applied Discrete Mathematics


May 2, 2013 3
Week 14: Trees
Binary Search Trees
•Example: Construct a binary search tree for the strings
math, computer, power, north, zoo, dentist, book.

math

computer power

Applied Discrete Mathematics


May 2, 2013 4
Week 14: Trees
Binary Search Trees
•Example: Construct a binary search tree for the strings
math, computer, power, north, zoo, dentist, book.

math

computer power

north

Applied Discrete Mathematics


May 2, 2013 5
Week 14: Trees
Binary Search Trees
•Example: Construct a binary search tree for the strings
math, computer, power, north, zoo, dentist, book.

math

computer power

north zoo

Applied Discrete Mathematics


May 2, 2013 6
Week 14: Trees
Binary Search Trees
•Example: Construct a binary search tree for the strings
math, computer, power, north, zoo, dentist, book.

math

computer power

dentist north zoo

Applied Discrete Mathematics


May 2, 2013 7
Week 14: Trees
Binary Search Trees
•Example: Construct a binary search tree for the strings
math, computer, power, north, zoo, dentist, book.

math

computer power

book dentist north zoo

Applied Discrete Mathematics


May 2, 2013 8
Week 14: Trees
Binary Search Trees
•To perform a search in such a tree for an item x, we can
start at the root and compare its key to x. If x is less than
the key, we proceed to the left child of the current vertex,
and if x is greater than the key, we proceed to the right
one.
•This procedure is repeated until we either found the item
we were looking for, or we cannot proceed any further.
•In a balanced tree representing a list of n items, search
can be performed with a maximum of
log(n + 1) steps (compare with binary search).

Applied Discrete Mathematics


May 2, 2013 9
Week 14: Trees
Binary Search Trees (BST)
• A data structure for efficient searching,
inser-tion and deletion
• Binary search tree property
– For every node X
– All the keys in its left
subtree are smaller than
the key value in X
– All the keys in its right
subtree are larger than the
key value in X
Binary Search Trees

A binary search tree Not a binary search tree


Binary Search Trees
The same set of keys may have different BSTs

• Average depth of a node is O(log N)


• Maximum depth of a node is O(N)
Searching BST
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then we
should search in the left subtree.
• If we are searching for a key > 15, then we
should search in the right subtree.
Searching (Find)
• Find X: return a pointer to the node that has
key X, or NULL if there is no such node

• Time complexity: O(height of the tree)


Inorder Traversal of BST
• Inorder traversal of BST prints out all the
keys in sorted order

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20


findMin/ findMax
• Goal: return the node containing the smallest
(largest) key in the tree
• Algorithm: Start at the root and go left (right)
as long as there is a left (right) child. The
stopping point is the smallest (largest)
element
Insertion
• Time complexity = O(height of the tree)
• Proceed down the tree as you would with a
find
• If X is found, do nothing (or update something)
• Otherwise, insert X at the last spot on the
path traversed
Deletion
• When we delete a node, we need to consider
how we take care of the children of the
deleted node.
– This has to be done such that the property of the
search tree is maintained.
Deletion under Different Cases
• Case 1: the node is a leaf
– Delete it immediately
• Case 2: the node has one child
– Adjust a pointer from the parent to bypass that
node
Deletion Case 3
• Case 3: the node has 2 children
– Replace the key of that node with the minimum
element at the right subtree
– Delete that minimum element
• Has either no child or only right child because if it
has a left child, that left child would be smaller and
would have been chosen. So invoke case 1 or 2.

• Time complexity = O(height of the tree)


Spanning Trees

•Definition: Let G be a simple graph. A spanning tree of G is


a subgraph of G that is a tree containing every vertex of G.
•Note: A spanning tree of G = (V, E) is a connected graph on
V with a minimum number of edges
(|V| - 1).
•Example: Since winters in Boston can be very cold, six
universities in the Boston area decide to build a tunnel
system that connects their libraries.

Applied Discrete Mathematics


May 2, 2013 22
Week 14: Trees
Spanning Trees
•The complete graph including all possible tunnels:
Brandeis Harvard

MIT

BU Tufts

UMass

The spanning trees of this graph connect all libraries with a minimum number of tunnels.

Applied Discrete Mathematics


May 2, 2013 23
Week 14: Trees
Spanning Trees
•Example for a spanning tree:
Brandeis Harvard

MIT

BU Tufts

UMass

Since there are 6 libraries, 5 tunnels are sufficient to connect all of them.

Applied Discrete Mathematics


May 2, 2013 24
Week 14: Trees
Spanning Trees

•Now imagine that you are in charge of the tunnel project.


How can you determine a tunnel system of minimal cost
that connects all libraries?
•Definition: A minimum spanning tree in a connected
weighted graph is a spanning tree that has the smallest
possible sum of weights of its edges.
•How can we find a minimum spanning tree?

Applied Discrete Mathematics


May 2, 2013 25
Week 14: Trees
Spanning Trees
•The complete graph with cost labels (in billion $):
Brandeis Harvard
7

8 4 MIT
2
9 5
3
6 3
9 Tufts
BU 4
4 6
5 4

UMass

The least expensive tunnel system costs $20 billion.

Applied Discrete Mathematics


May 2, 2013 26
Week 14: Trees
Spanning Trees

•Prim’s Algorithm:
• Begin by choosing any edge with smallest weight
and putting it into the spanning tree,
• successively add to the tree edges of minimum
weight that are incident to a vertex already in
the tree and not forming a simple circuit with
those edges already in the tree,
• stop when (n – 1) edges have been added.

Applied Discrete Mathematics


May 2, 2013 27
Week 14: Trees
Spanning Trees

•Kruskal’s Algorithm:
•Kruskal’s algorithm is identical to Prim’s algorithm, except
that it does not demand new edges to be incident to a
vertex already in the tree.
•Both algorithms are guaranteed to produce a minimum
spanning tree of a connected weighted graph.

Applied Discrete Mathematics


May 2, 2013 28
Week 14: Trees

You might also like