Title: Binary Search Trees
1Binary Search Trees
2Goals
- Binary search supports Find in O( log N )
worst-case time, but Insert and Remove are O( N
). - Would like to support all three operations in O(
log N ) worst-case time. - Today's result can support all three operations
in O( log N ) average-case time. - Later do it in O( log N ) worst-case time.
3Basic Ideas
- Use a "tree" to show the logic applied by a
binary search.
4Binary Search Expanded
5Binary Tree
- Recursive View Binary tree is either
- Empty
- Contains a root and two subtrees
6Binary Search Tree Order
- Ordering property
- for every node in the tree,
- all items in left subtree are smaller,
- all items in right subtree are larger. (Assume no
duplicates)
X
gtX
ltX
7The TreeNode in Java
- The tree is a collection of nodes
- class TreeNode
-
- Comparable Key / Item stored in node /
- TreeNode Left
- TreeNode Right
-
- The tree stores a reference to the root node,
which is the starting point.
8Binary search tree
- Definition A labeled binary tree, with the
following property - for every node n in the tree,
- n.key gt key of any node in nth left
subtree, - n.key lt key of any node in nth right
subtree
gt
lt
9Examples
3
5
7
3
7
8
n 6, h 4
5
2
8
5
n 6, h 2
5
10Binary search tree
A binary search tree T defines a total ordering
on all Ts nodes.
gt
?
lt
11Binary search trees
- The BST is a data structure used for many
applications on dynamic sets, since typical
operations required with dynamic sets like
search, insert and delete, are very efficient. - Binary search trees are particularly efficient
for dynamic sets with a total ordering.
12Binary search treesOrdered printout of all
nodes
// inorder traversal void inorder( ) if
(left ! null) left.inorder( ) print
data if (right ! null) right.inorder(
)
2
3
5
7
7
3
5
8
8
5
5
2
13Minimal/Maximal elements
I
II
IV
III
- maximal - minimal
14Operations on binary search treesfind-minimal-
(maximal)-element
Find minimal (maximal) element in a set
(iterative) Node treemin (Node n) while (n
.left ! null) n t.left return
n
Node treemax (Node n) while (n .right !
null) n t.right return n
O(h)
15Operations on binary search treessearch
Searching for a node with given key
recursive Node treesearch (Node t, int key)
if (t null or key t.key)
return t if (key lt t.key)
return treesearch (t.left, key) else
return treesearch (t.right, key)
iterative
Node it-treesearch (Node t, int key)
while (t ! null and k ! t.key) if
key lt t.key t t.left
else t
t.right return (t)
O(h) where h height of tree (lt n, of
nodes)
16Operations on binary search treesfind (inorder)
successor
I
Tree tree-successor (Node t) Node y if (
t.right ! null) return treemin (t.right) y
t.parent while (y ! null) and (t y.right)
t y y y.parent return y
n
O(h)
successor(n)
II
III
17Operations on binary search treesfind (inorder)
predeccessor
I
Tree tree-predeccessor (Node t) Node y if (
t.left ! null) return treemax(t.left) y
t.parent while (y ! null) and (t y.left)
t y y y.parent return y
O(h)
III
II
18Operations on binary search treesInsert-element
- Insert an element to the set
- Base
- if T is empty, make the new node the root of T.
- Induction
- Let T be a non-empty tree, with root r.
- If new-nodes key is smaller then r.key, insert
new node to the left subtree of T (r.left). - Else insert new node to the right subtree of T
(r.right).
19Operations on binary search treesInsert-element
(cont)
public class Node public Node insert (int d)
// insert to non-empty tree if (d lt key)
if (left null) left new Node
(d) return left else left.insert (d)
else if (d gt key ) if (right null)
right new Node(d) return right
else right.insert (d)
20Operations on binary search treesInsert-element
(cont)
public class Tree private Node root public
Tree() root null // constructor public
Node insert (int d) if ( root null )
root new Node(d) return root else
root.insert (d)
21Operations on binary search treesDelete-element
- Deletion algorithm is complicated. Basic problem
if the node is deleted, it potentially
disconnects the tree. - Standard algorithm breaks into three cases
- Node to be deleted is a leaf
- Node to be deleted has one child
- Node to be deleted has two children
22Operations on binary search treesDelete-element
I
- Given a tree T and a pointer n to a node in T,
- delete node n from T.
- Three cases
- 1. Node to be removed is a leaf Remove the
leaf. - 2. Node n has a single successor Delete n, and
- connect n single successor to ns parent.
- Note
- Root with one child is a special case, because it
does not - have a parent.
- In this case, we simply obtain a new root
y
II
y
23Operations on binary search treesDelete-element
3. Node n is an interior node with two successor
(a left subtree T1eft and a right subtree
Tright) Delete n and replace it by the minimal
element in Tright, or, the maximal
element from Tleft.
z
y
1
3
2
Tright
24Operations on binary search treesDelete-element
(replace maximal element of Tleft)
z
Tleft
2
1
3
25Operations on binary search treesDelete-element
public Node delete (Tree t, Node n) \\ t is
a reference to the tree ref. Node x, y if
(n.left null or n.right null) y n
// case a or b else y
tree-successor (n) // case c if (y.left !
null) x y.left else x y.right
// can be null if ( x ! null) x.parent
y.parent // if (y.parent null) t.root
x // deleted node root else if (y
y.parent.left) y.parent.left x // x is
a left child of y else y.parent.right
x // x is a right child of y if (y ! n)
// case c exchange nodes n and x (copy key
data) return y // needed for other
applications
26Efficiency of binary search tree operations
- Search, insert, delete , all are O(h)
- worst case linear tree, h n
- best case a complete binary tree h lt lg
n
20
21
22
23
n lt 20 21 2h
Thus, if can maintain the tree balanced,
then all three operations are of O(lgn).
n lt 2h1 - 1