CSE 12 Binary Search Trees
CSE 12 Binary Search Trees
15
BinarySearchTree ( )
pre-condition:
none
responsibilities: constructor; create an empty BinarySearchTree
post-condition:
size is set to 0
root is set to a null value
returns:
nothing
5
iterator ()
pre-condition:
responsibilities:
post-condition:
returns:
none
create and return an Iterator for this tree.
The iterator performs an
inorder traversal of this trees elements
the tree is unchanged
an Iterator for this tree
Tree traversals
Why?
/**
* A binary search tree is composed of <tt>BSTNode</tt>s.
* The element stored must be comparable with other
* elements in the BST.
*/
public class BSTNode<E extends Comparable<? super E>> {
protected BSTNode<E> parent;
Note: the data fields are
protected BSTNode<E> leftChild;
protected BSTNode<E> rightChild; protected instead of
private. Why?
protected E element;
where:
node the root of the subtree to search (initially the root of the tree)
target the element for which we are searching
Call and return paths for successful and unsuccessful contains() calls
15
/**
* Determine if <tt>target</tt> is in the tree.
* @param target the element to look for; cant be <tt>null</tt>
* @return <tt>true</tt> if found, <tt>false</tt> otherwise
* @throws SearchTreeException if <tt>target</tt> is <tt>null</tt>
*/
public boolean contains( E target) {
This is the public
if ( target == null )
version clients see
throw new SearchTreeException();
return contains( this.root(), target );
}
}
16
where:
node
the root of the subtree to add (initially it is the root of the tree)
element the element to be added to the tree
Important!
Note that a node
reference is passed
back up the tree all
the way to the root
(a) The path followed to
find 17s insertion point.
20
Implementing remove()
Removing a node is a little trickier
Need to keep the tree connected, and
Need to maintain the BST invariants
One way to do it is to consider 3 separate cases:
Case 1 The node to remove is a leaf
Case 2 The node to remove is an internal node with one
child
Case 3 The node to remove is an internal node with two
children
21
22
23
15s successor
in the tree is 20
Bad tree!
Good tree!
AVL Trees
An AVL tree is a BST with the following additional
structural balance property:
For every node X, the height of Xs left and right
subtrees differ by no more than 1
A balance of 0 means
the subtrees have the
same height
A balance >1 or <-1 means
the tree is no longer AVL15-29/43
balanced
Detecting Imbalance
AVL rotations
32
Case LL
The balance at Xs left child, XL, is 1, so Xs Left childs
Left subtree rooted at XLL is taller
34
Case LR
The balance at Xs left child, XL, is 1, so Xs Left childs
Right subtree rooted at XLR is taller
35
Case RR
The balance at Xs right child, XR, is 1, so the Right
childs Right subtree rooted at XRR is taller
36
Case RL
The balance at Xs right child, XR, is 1, so the Right childs
Left subtree rooted at XRL is taller
37
Case LL Rotation
Solution: make a single right (clockwise) rotation around X by
moving the tree rooted at XL up a level such that:
X becomes XLs right child
XLs right child (T2) becomes Xs left child
XL is the new root of the subtree
(a) An LL imbalance at X
Balance is restored and the BST property is retained: T2 and XL are moved relative to X, and what we
know about the values in T2 is this: XL < values in T2 < X
Thus T2 must be between XL (on the left) and X (on the right) in a BST. This is true in the rebalanced
tree, so the rebalanced tree is still a BST.
38
39
40
second rotation
first rotation
42
43
Next time
Reading: Gray, Ch 11
45