Binary Search Trees
Ric Glassey
glassey@kth.se
Outline
•
Binary Search Trees
–
Aim: Demonstrate how a BST can maintain order
and fast performance relative to its height
–
Properties
–
Operations
•
Min/Max
•
Search
•
Insertion
•
*Deletion
–
Summary of performance
2PROPERTIES
Binary Search Tree
•
Ordered binary tree
•
Binary search tree property
–
Keys in left sub-tree of P are < k
–
Keys in right sub-tree of P are > k
4
k
> k < k
Example Binary Search Tree
5 44 17 88 65 97 8 32 28 41Applications of BST
•
Store a set of ordered keys
•
Fast operations and maintain order
–
Priority Queues
–
Ordered Maps/Dictionaries
•
e.g. Java’s TreeMap
•
n.b. if order is not important, regular hash tables still offer
better average time complexity O(1)
MIN/MAX
Finding Min/Max in a BST
•
What is the fastest path to find the
minimum
and
maximum
values in a BST?
8 44 17 88 65 97 8 32 28 41
Finding Min/Max in a BST
9
min(node)
while node.left != null:
node = node.left
return node
max(node)
while node.right != null:
node = node.right
return node
Pathological BSTs
10
Order of inserts cannot be predicted in advance
Pathological cases can emerge where some opera5ons will take O(n) Solu5ons include Randomised BSTs/Treaps and Balancing Procedures
44 46 54 67 71 88 insert(54) insert(46) insert(67) insert(71) insert(88)
SEARCH
Finding x within a BST
•
x = 41
12 44 17 88 65 97 8 32 28 41Finding x within a BST
•
x = 41
13 44 17 88 65 97 8 32 28 41 x < 44 x > 17 x > 32 x = 41BST Search Algorithms
•
Recursive approach:
•
Iterative option (
see homework
)
–
Why is this the better option?
14
search(node, x)
if node == null or x == node.key: return node if x < node.key: return search(node.left, x) else: return search(node.right, x)
BST Search Algorithms
•
Further operations use
search(n,x)
as a
sub-routine
–
insert(k, v)
–
remove(k)
•
As a consequence, all primary operations on a
BST are considered
fast
, proportional to the
height of the tree
–
An exception is in-order-traversal which should be
expected to be O(n) as all nodes ‘must’ be visited
INSERTION
Insertion into a BST
•
Search for key
•
Two cases:
–
Key exists
, update value
–
Key does not exist
, extend leaf at end of failed
search with new node (key, value)
Insertion into a BST: insert(68, value)
18 44 17 88 80 65 97 8 32 54 82 93 28 76 21 29Insertion into a BST: Find position
19 44 17 88 80 65 97 8 32 54 82 93 28 76 21 29 ??? 68 > 44 68 < 88 68 > 65 68 < 76 68 < 82Insertion into a BST: Extend Leaf
20 44 17 88 80 65 97 8 32 54 82 93 28 76 21 29 68 > 44 68 < 88 68 > 65 68 < 76 68 < 82 68BST Insertion Algorithm
•
We can use search(x) as a
subroutine
to
simplify insertion into a BST
–
n.b. homework demands iterative solution!
21
insert(key, value)
leaf =
search(root, key)
if key == leaf.key:
leaf.value = value
else if key < leaf.key:
leaf.left = node(key, value)
else:
*DELETION
*Deletion for a BST
•
Insertions always occur at a leaf (trivial)
•
Deletions can occur anywhere within a tree
•
Three cases to consider for
remove(z)
:
–
Case 1: z has no children
–
Case 2: z has one child
–
Case 3: z has two children
*Case 1: z has no children
•
Simply remove from tree by setting z’s parent’s
pointer to null
24 z
*Case 2: z has one child
25 Z LC R LC R Z RC R RC R*Case 3: z has two children (i)
26 R Z LC X Y R X LC Y*Case 3: z has two children (ii)
27
We transplant Y with X first, and relink W to X to maintain the BST-‐property Finally, we link Z’s LC to Y and Y to the root R
Hint: subs5tute valid integers to show why this works
R Z LC X Y R Z LC W Y W X R Y LC X W
Summary of BST Performance
28 Data Structure Opera5on Array LinkedList† Hash Table Binary Search Tree
average worst expected worst
Search* O(1) O(n) O(1) O(n) O(h) O(n) Insert O(n) O(1) O(1) O(n) O(h) O(n) Delete O(n) O(1) O(1) O(n) O(h) O(n)
* Index or Key based search † Assume doubly linked list
Readings
•
Algorithms and Data Structures
*required*
–
Stefan Nilsson’s text on the Binary Search Tree
–
http://www.nada.kth.se/~snilsson/algoritmer/trad/
•
Introduction to Algorithms, 3
rdEdition
–
Chapter 12: Binary Search Trees
–
Full text available via KTH Library
• http://kth-primo.hosted.exlibrisgroup.com/
KTH:KTH_SFX2560000000068328
•
Data Structures and Algorithms in Java, 6
thEdition
–
Goodrich et al.
–
Chapter 11: Search Trees
–
Full text available via KTH Library
• http://kth-primo.hosted.exlibrisgroup.com/
KTH:KTH_SFX3710000000333147