Data Structures in Java
Lecture 9: Binary Search Trees.
10/7/2015
1
Daniel Bauer
Data Structures in Java Lecture 9: Binary Search Trees. 10/7/2015 - - PowerPoint PPT Presentation
Data Structures in Java Lecture 9: Binary Search Trees. 10/7/2015 Daniel Bauer 1 Contents 1. Binary Search Trees 2. Implementing Maps with BSTs Map ADT A map is collection of (key, value) pairs. Keys are unique, values need not be.
Lecture 9: Binary Search Trees.
10/7/2015
1
Daniel Bauer
key1 key2 key3 key4 value1 value2 value3 How do we implement map operations efficiently?
1 4 8 6 2 3
the left subtree of n are smaller than x.
in the right subtree of n are larger than x.
1 4 8 6 2 3 7
This is not a search tree
the left subtree of n are smaller than x.
in the right subtree of n are larger than x.
consists of
by a directed edge from r.
r
Tl Tr
1 4 8 6 2 3
private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }
1 4 8 6 2 3
private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }
contains(3)
6
1 4 8 6 2 3
private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }
contains(3)
2
1 4 8 6 2 3
private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }
contains(3)
4
1 4 8 6 2 3
private ¡boolean ¡contains( ¡Integer ¡x, ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.left ¡); ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡contains( ¡x, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡// ¡Match ¡ }
contains(3)
3
1 4 8 6 2 3
private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ }
findMax is equivalent.
1 4 8 6 2 3
private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ }
findMin()
6
findMax is equivalent.
1 4 8 6 2 3
private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ }
findMin()
2
findMax is equivalent.
1 4 8 6 2 3
private ¡BinaryNode ¡findMin( ¡BinaryNode ¡t ¡) ¡{ ¡ ¡ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡else ¡if( ¡t.left ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t; ¡ ¡ ¡ ¡ ¡return ¡findMin( ¡t.left ¡); ¡ }
findMin()
1
findMax is equivalent.
Insert a new node for X as a left or right child of n.
Maintains the BST property.
1 4 8 6 2 3
private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }
Insert a new node for X as a left or right child of n.
insert(5) Maintains the BST property.
1 4 8 6 2 3
6
private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }
Insert a new node for X as a left or right child of n.
insert(5) Maintains the BST property.
1 4 8 6 2 3
2
private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }
Insert a new node for X as a left or right child of n.
insert(5) Maintains the BST property.
1 4 8 6 2 3
4
private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }
Insert a new node for X as a left or right child of n.
insert(5) Maintains the BST property.
1 4 8 6 2 3
5
private ¡BinaryNode ¡insert( ¡Integer ¡x, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡BinaryNode( ¡x, ¡null, ¡null ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡x ¡< ¡t.data ¡ ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.left ¡= ¡insert( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if( ¡t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡insert( ¡x, ¡t.right ¡); ¡ ¡ ¡return ¡t; ¡ ¡ ¡ }
1 4 8 6 2 3
parent of s, in place of s. Maintains the BST property.
remove(8)
1 4 8 6 2 3
6
parent of s, in place of s. Maintains the BST property.
remove(8)
1 4 8 6 2 3
8
parent of s, in place of s. Maintains the BST property.
remove(8)
1 4 6 2 3
parent of s, in place of s. Maintains the BST property.
1 4 6 2 3
parent of s, in place of s. Maintains the BST property. remove(4)
6
1 4 6 2 3
parent of s, in place of s. Maintains the BST property. remove(4)
2
1 4 6 2 3
parent of s, in place of s. Maintains the BST property. remove(4)
4
1 6 2 3
parent of s, in place of s. Maintains the BST property. remove(4)
1 6 2 3
parent of s, in place of s.
Maintains the BST property. remove(4)
1 4 8 6 2 3
To maintain the BST property, the node to replace s needs to be
left subtree
in the right subtree.
remove(2)
1 4 8 6 2 3
6
To maintain the BST property, the node to replace s needs to be
left subtree
in the right subtree.
remove(2)
1 4 8 6 2 3
2
To maintain the BST property, the node to replace s needs to be
left subtree
in the right subtree.
remove(2)
1 4 8 6 2 3
2
4
To maintain the BST property, the node to replace s needs to be
left subtree
in the right subtree.
remove(2)
1 4 8 6 2 3
2
3
To maintain the BST property, the node to replace s needs to be
left subtree
in the right subtree.
remove(2)
1 4 8 6 2
2
3
To maintain the BST property, the node to replace s needs to be
left subtree
in the right subtree.
remove(3)
1
4 8 6
3
2
remove(3)
4 8 6 2
1
private ¡BinaryNode ¡remove( ¡Integer ¡x, ¡BinaryNode ¡t ¡){ ¡ ¡ ¡if( ¡t ¡== ¡null ¡) ¡ ¡ ¡ ¡ ¡return ¡t; ¡// ¡Item ¡not ¡found; ¡do ¡nothing ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(x ¡< ¡t.data ¡) ¡ ¡ ¡ ¡ ¡t.left ¡= ¡remove( ¡x, ¡t.left ¡); ¡ ¡ ¡else ¡if(t.data ¡< ¡x ¡) ¡ ¡ ¡ ¡ ¡t.right ¡= ¡remove( ¡x, ¡t.right ¡); ¡ ¡ ¡else ¡//found ¡x ¡ ¡ ¡ ¡ ¡if( ¡t.left ¡!= ¡null ¡&& ¡t.right ¡!= ¡null ¡) ¡{ ¡// ¡2 ¡children ¡ ¡ ¡ ¡ ¡ ¡ ¡t.element ¡= ¡findMin( ¡t.right ¡).element; ¡ ¡ ¡ ¡ ¡ ¡ ¡t.right ¡= ¡remove( ¡t.element, ¡t.right ¡); ¡ ¡ ¡ ¡ ¡} ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(t.left ¡!= ¡null) ¡// ¡1 ¡or ¡0 ¡children. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t.left; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡t.right; ¡ }
tree to access some node s in depth(s) steps.
node in any BST?
1 2 3 4
branch height(T)=N
height(T)=log N
1 2 3 5 4
complete binary tree.
an initially empty BST. What is the average depth of a node? [1 2 3 4 ] [3 1 2 4]
1 2 3 4 2 1 3 4
[1 2 4 3 ]
1 2 4 3
[2 3 4 1]
2 1 3 4
… …
Source: Weiss, Data Structures and Algorithm Analysis in Java, 3rd Edition
Theoretical analysis of random BSTs: Average depth of a node in a random BST of N nodes is about
a random permutation of elements) is .
deletions?
subtrees. remove(2)
1 4 8 6
2
3
3
a random permutation of elements) is .
deletions?
subtrees. remove(2)
1 4 8 6
2 3
the expected depth is
Source: Weiss, Data Structures and Algorithm Analysis in Java, 3rd Edition
N=500, randomly generated as before. 500*500 = 250,000 random insertion/ deletion pairs
key1 key2 key3 key4 value1 value2 value3 How do we implement map operations efficiently?
convenient way of implementing maps.
A 1 2 3 4 5 6 D B C
sorted Sets.
element, including (key,value) pairs.
<, >, and =. The (key, value) pair class should implement Comparable.
key1 value1
… …
key2 value2
private ¡class ¡Pair<K ¡extends ¡Comparable<K>,V> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡implements ¡Comparable<Pair<K,?>>{ ¡ ¡ ¡ ¡ ¡public ¡K ¡key; ¡ ¡ ¡ ¡ ¡public ¡V ¡value; ¡ ¡ ¡ ¡ ¡public ¡Pair(K ¡theKey, ¡V ¡theValue) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡key ¡= ¡theKey; ¡value ¡= ¡theValue; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡@Override ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡int ¡compareTo(Pair<K,?> ¡other) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡key.compareTo(other.key); ¡ ¡ ¡ ¡ ¡} ¡ }
(see example code)