Title: Binary Search Trees Motivation
1Binary Search Trees - Motivation
- Recall our discussion on linked lists
- O(1) insertion and deletion BUT O(n) search
- we have to traverse all nodes of the list to
search for an element - Can we do better? That is, can we decrease the
search time? - Yes Organize the data in a search tree structure
that supports efficient search operation - Binary search tree (BST)
- AVL Tree
- Splay Tree
- Red-Black Tree
- B Tree and B Tree
2Binary Search Trees
- A Binary Search Tree (BST) is a binary tree in
which the value in every node is - gt all values in the nodes left subtree
- lt all values in the nodes right subtree
3Implementation and Operations
C Declaration struct SBSTNode SBSTNode
left int key SBSTNode right
x
left
key
right
- SBSTNode Search(SBSTNode root, int key)
- int FindMin(SBSTNode root)
- int FindMax(SBSTNode root)
- SBSTBode Insert(SBSTNode root, int newItem)
- SBSTBode Delete(SBSTNode root, SBSTNode x)
4Operations on BSTs - Search
SBSTNode Search(SBSTNode r, int key) if (r
NULL) return NULL if (key r-gtkey)
return r else if (key lt r-gtkey) return
Search(r-gtleft) else / key gt r-gtkey /
return Search(r-gtright) //end-Search
Key 13
- Nodes visited during a search for 13 are colored
with blue - Notice that the running time of the algorithm is
O(d), where d is the depth of the tree
5Iterative BST Search
- The same algorithm can be written iteratively by
unrolling the recursion into a while loop
SBSTNode Search(SBSTNode r, int key) while
(r ! NULL) if (key r-gtkey)
return r else if (key lt r-gtkey) r
r-gtleft else / key gt r-gtkey / r
r-gtright //end-while return NULL
//end-Search
- Iterative version is more efficient than the
recursive version
6Operations on BSTs - FindMin
- FindMin(SBSTNode root) Given a pointer to the
root of the tree, returns a pointer to the node
that contains the minimum element in the tree - Notice that the node with the minimum element can
be found by following left child pointers from
the root until a NULL is encountered
SBSTNode FindMin(SBSTNode r) if (r NULL)
return NULL while (r-gtleft ! NULL) r
r-gtleft //end-while return r
//end-FindMin
7Operations on BSTs - FindMax
- FindMin(SBSTNode root) Given a pointer to the
root of the tree, returns a pointer to the node
that contains the maximum element in the tree - Notice that the node with the minimum element can
be found by following right child pointers from
the root until a nil is encountered
SBSTNode FindMax(SBSTNode r) if (r NULL)
return NULL while (r-gtright ! NULL) r
r-gtright //end-while return r
//end-FindMin
8Operations on BSTs - Insertion
- SBSTNode Insert(SBSTNode root, SBSTNode z)
Given a new node z, inserts the node into the
BST
z
12
NULL
NULL
Node z to be inserted z-gtkey 12
- Idea
- Begin at the root and trace a path down the tree
as if we are searching for the node that contains
the key of z 12 - The new node must be a child of the leaf node
where we stop the search
9Insertion Example
10Operations on BSTs - Deletion
- Delete is a bit trickier. 3 cases exist
- Node to be deleted has no children (leaf node)
- Delete 9
- Node to be deleted has a single child
- Delete 7
- Node to be deleted has 2 children
- Delete 6
Root
15
18
6
30
7
3
13
2
4
14
9
11Deletion Case 1
Root
15
18
6
30
7
3
13
2
4
9
Deleting 9 Simply remove the node and adjust
the pointers
12Deletion Case 2
Root
Root
15
15
18
18
6
6
13
30
30
7
3
3
13
2
2
4
4
9
9
After 7 is deleted
Deleting 7 Splice out the node By making a
link between its child and its parent
13Deletion Case 3
Root
Root
17
17
18
6
18
7
30
14
3
30
14
3
16
2
10
4
16
2
10
4
7
13
8
13
8
Deleting 6 Splice out 6s successor 7, which
has no left child, and replace the contents of 6
with the contents of the successor 7
After 6 is deleted
Note Instead of zs successor, we could have
spliced out zs predecessor
14Laziness in Data Structures
- A lazy operation is one that puts off work as
much as - possible in the hope that a future operation will
make the - current operation unnecessary
15Lazy Deletion
- Idea Mark node as deleted no need to reorganize
tree - Skip marked nodes during Find or Insert
- Reorganize tree only when number of marked nodes
exceeds a percentage of real nodes (e.g. 50) - Constant time penalty only due to marked nodes
depth increases only by a constant amount if 50
are marked undeleted nodes (N nodes max N/2
marked) - Modify Insert to make use of marked nodes
whenever possible e.g. when deleted value is
re-inserted - Gain
- Makes deletion more efficient (Consider deleting
the root) - Reinsertion of a key does not require
reallocation of space - Can also use lazy deletion for Linked Lists
16Handling Duplicates in BSTs
- Handling Duplicates
- Increment a counter stored in items node
- Or
- Use a linked list or another search tree at
items node - Application Look-up table
- E.g. Academic records systems
- Given SSN, return student record SSN stored in
each node as the key value - E.g. Given zip code, return city/state
- E.g. Given name, return address/phone no.
- Can use dictionary order for strings
Root
5
2
7
4
3
1
2
2
4
3
8
6
17Sorting by inorder traversal of a BST
- BST property allows us to print out all the keys
in a BST in sorted order by an inorder traversal
Inorder traversal results 2 3 4 5 7 8
Root
5
- Correctness of this claim follows by induction in
BST property
7
3
8
4
2
18Proof of the Claim by Induction
- Base One node 5 ? Sorted
- Induction Hypothesis Assume that the claim is
true for all tree with lt n nodes. - Claim Proof Consider the following tree with n
nodes - Recall Inorder Traversal LST R RST
- LST is sorted by the Induction hypothesis since
it has lt n nodes - RST is sorted by the Induction hypothesis since
it has lt n nodes - All values in LST lt R by the BST property
- All values in RST gt R by the property
- This completes the proof.
19Taxonomy of BSTs
- O(d) search, FindMin, FindMax, Insert, Delete
- BUT depth d depends upon the order of
insertion/deletion - Ex Insert the numbers 1 2 3 4 5 6 in this order.
The resulting tree will degenerate to a linked
list-gt All operations
will take O(n)
- Can we do better? Can we guarantee an upper bound
on the height of the tree? - AVL-trees
- Splay trees
- Red-Black trees
- B trees, B trees
root
1
2
3
4
5
6