Post on 19-Dec-2015
Binary Search Trees
Briana B. Morrison
Adapted from Alan Eugenio
Binary Trees 2
Topics
Applications Binary Search Trees
Retrieve Insert Delete
Binary Trees 3
Ordered Dictionaries
Keys are assumed to come from a total order.
New operations: closestBefore(k) closestAfter(k)
Binary Trees 4
Binary Search (§8.3.3)
Binary search performs operation find(k) on a dictionary implemented by means of an array-based sequence, sorted by key
similar to the high-low game at each step, the number of candidate items is halved terminates after O(log n) steps
Example: find(7)
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
0
0
0
0
ml h
ml h
ml h
lm h
Binary Trees 5
BINARY SEARCH TREES
Binary Trees 6
Overview of Binary Search TreeBinary search tree definition:
T is a binary search tree if either of these is true T is empty; or Root has two subtrees:
Each is a binary search tree Value in root > all values of the left subtree Value in root < all values in the right subtree
Binary Trees 7
HERE IS AN EXAMPLE OF A BINARY SEARCH TREE: 80
45 92
30 100 20 40 95 110 97
Binary Trees 8
50
30 90
12 40 86 100
25 60
71
Binary Trees 9
ANOTHER BINARY SEARCH TREE:
50
32
41
38
40
Binary Trees 10
Binary Search Trees
6 53 0
3 71 0
2 5
1 5
B in ary Search T ree 1
B in ary Search T ree 2
5 3
3 0
5 9
6 2
5 0
B in ary Search T ree 3
Binary Trees 11
A BINARY SEARCH TREE NEED NOT
BE FULL, COMPLETE OR A TWO-TREE,
BUT IT COULD BE ANY OF THOSE.
IF A BINARY SEARCH TREE IS FULL
OR COMPLETE, ITS HEIGHT IS
LOGARITHMIC IN n.
Binary Trees 12
IF A BINARY SEARCH TREE IS A
CHAIN, ITS HEIGHT IS LINEAR IN n.
EVEN BINARY SEARCH TREES THAT
ARE NOT CHAINS MAY HAVE HEIGHT
THAT IS LINEAR IN n. FOR EXAMPLE,
SUPPOSE THERE ARE EXACTLY TWO
ELEMENTS AT LEVEL 1, LEVEL 2, … .
SEE THE FOLLOWING TREE:
Binary Trees 13
80
45 92
30 100
95 110
93 97
Binary Trees 14
IN THE BinSearchTree CLASS, THE
“AVERAGE” HEIGHT OF A
BinSearchTree OBJECT IS
LOGARITHMIC IN n, AND SO THE
AVERAGE TIME TO INSERT, REMOVE
OR SEARCH IS LOGARITHMIC IN n.
Binary Trees 15
BUT A BinSearchTree OBJECT CAN BE
A CHAIN; THEN THE TIME TO INSERT,
REMOVE OR SEARCH IS LINEAR IN n.
Binary Trees 16
FOR AN (UNSORTED) ARRAY, vector,
deque, OR list CONTAINER, THE TIME
TO INSERT, REMOVE OR SEARCH IS
LINEAR IN n.
Binary Trees 17
THE BinSearchTree CLASS
Binary Trees 18
THE BinarySearchTree CLASS IS AN
ASSOCIATIVE-CONTAINER CLASS: THE
LOCATION OF AN ITEM IS
DETERMINED BY COMPARING THE
ITEM TO ITEMS ALREADY IN THETREE.
Binary Trees 19
THE BinSearchTree CLASS IS NOT PART
OF THE STANDARD TEMPLATE
LIBRARY, SO WE WILL TAKE A
PARSIMONIOUS APPROACH.
HERE ARE THE METHOD INTERFACES
FOR A MINIMAL BinarySearchTree
CLASS:
Binary Trees 20
1. // Postcondition: this BinSearchTree is empty.BinSearchTree( );
2. // Postcondition: the number of items in this// BinSearchTree has been returned.unsigned size( ) const;
3. // Postcondition: if there is an item in this BinSearchTree
// that equals item, the value returned is an// iterator pointing to that item. Otherwise,// the value returned is an iterator with the// same value as the iterator returned by// the end( ) method. The averageTime(n)// is O(log n) and worstTime(n) is O(n).Iterator find (const T& item) const;
Binary Trees 21
4. // Postcondition: item has been inserted into this// BinSearchTree. An iterator positioned at// the inserted item has been returned. The// averageTime(n) is O(log n) and// worstTime(n) is O(n).Iterator insert (const T& item);
5. // Precondition: itr is positioned at an item in this// BinSearchTree.// Postcondition: the item that itr is positioned at has been// deleted from this BinSearchTree. The// averageTime(n) is constant and// worstTime(n) is O(n). The// amortizedTime(n) is constant.void erase (iterator itr);
Binary Trees 22
6. // Postcondition: if this BinSearchTree is non-empty, an// iterator positioned at the smallest item// in the tree has been returned.// Otherwise, the iterator returned has// the same value as the iterator// returned by the end( ) method.Iterator begin( );
7. // Postcondition: the value returned is an iterator that// can be used in a comparison for// ending the traversal of this// BinSearchTree. If this BinSearchTree// is non-empty, the largest item is in the// position just before the position of the// iterator returned.Iterator end( );
Binary Trees 23
8. // Postcondition: The space allocated for this BinSearchTree// has been deallocated. The worstTime(n) is// O(n).~BinSearchTree( );
MISSING: push_back, push_front (BECAUSE USER CANNOT SAY WHERE TO PUT AN ITEM)
pop_front, pop_back (USE erase INSTEAD)
Binary Trees 24
EXERCISE: DETERMINE THE OUTPUT
FROM THE FOLLOWING CODE AFTER
THIS INPUT: summer spring winter maybe fall always ***
Binary Trees 25
BinSearchTree<string> words; BinSearchTree<string>::Iterator itr1; string word; cout << "Please enter a word; the sentinel is ***: "; cin >> word; while (word != "***") { words.insert (word); cout << "Please enter a word; the sentinel is ***: "; cin >> word; } // while for (itr1 = words.begin( ); itr1 != words.end( ); itr1++) cout << *itr1 << endl; words.erase (words.find ("maybe")); words.erase (words.begin( )); words.erase (--words.end( )); for (itr1 = words.begin( ); itr1 != words.end( ); itr1++) cout << *itr1 << endl;
summerspringwintermaybefallalways***
Binary Trees 26
Using Binary Search Trees Application: Removing Duplicates
7
52
37 3 2 5 3 2 9 3 2 3 5 7 9
9v v
Binary Trees 27
FIELDS AND ONE POSSIBLE IMPLEMENTATION OF THE
BinarySearchTree CLASS
Binary Trees 28
Binary Tree Nodes
Abs tract T re e M ode l
A
E F
H
D
CB
G
l e ft A ri gh t
T re e N ode M ode l
l e ft B ri gh t
l e ft E ri gh t
l e ft G ri gh t
l e ft D ri gh t
l e ft C ri gh t
l e ft H ri gh t
l e ft F ri gh t
Binary Trees 29
Searching a Binary Tree: Algorithm1. if root is NULL
2. item not in tree: return NULL3. compare target and root->data4. if they are equal5. target is found, return root->data6. else if target < root->data7. return search(left subtree)8. else9. return search(right subtree)
Note that in the STL, trees are not implemented with recursion but with loops
Binary Trees 30
50
62373210
60533525
5530
15
Current Node Action --LOCATING DATA IN A TREE-Root = 50 Compare item = 37 and 50
37 < 50, move to the left subtreeNode = 30 Compare item = 37 and 30
37 > 30, move to the right subtreeNode = 35 Compare item = 37 and 35
37 > 35, move to the right subtreeNode = 37 Compare item = 37 and 37. Item found.
Binary Trees 31
80
40 90
60
50 75
WHAT ARE THE STEPS IF THE CALL IS find (60)?
Binary Trees 32
80
40 90
60
50 75
WHAT ARE THE STEPS IF THE CALL IS find (70)?
Binary Trees 33
FOR A SUCCESSFUL SEARCH:
THE AVERAGE HEIGHT OF A BINARY SEARCH
TREE IS LOGARITHMIC IN n. SO averageTime(n) IS
O(log n). IN FACT, averageTime(n) IS LOGARITHMIC
IN n.
THE worstTime(n) OCCURS IF THETREE IS A
CHAIN. SO worstTime(n) IS ????
Binary Trees 34
Logic for Insert
Insertion is similar to find, but you must keep track of parent to be able to insert a new node
Find the spot in the tree that the value would be at if it were already in the tree,
When you reach a null link, insert the value there
Binary Trees 35
Update Operations: 1st of 3 steps1)- The function begins at the root node and compares item 32
with the root value 25. Since 32 > 25, we traverse the right subtree and look at node 35.
2 5
4 01 2
3 52 0
p aren t
t
(a)St ep 1 : C o m p are 3 2 an d 2 5 .T rav ers e t h e righ t s u b t ree.
Binary Trees 36
Update Operations: 2nd of 3 steps2)- Considering 35 to be the root of its own subtree, we compare
item 32 with 35 and traverse the left subtree of 35.
(b )St ep 2 : C o m p are 3 2 an d 3 5 .T rav ers e t h e left s u b t ree.
2 5
4 01 2
3 52 0
t
p aren t
Binary Trees 37
Update Operations: 3rd of 3 steps3)- Create a leaf node with data value 32. Insert the new node
as the left child of node 35.
newNode = getSTNode(item,NULL,NULL,parent);
parent->left = newNode;
(c)St ep 3 : In s ert 3 2 as left ch ildo f p aren t 3 5
2 5
4 01 2
3 52 0 p aren t
3 2
Binary Trees 38
insert (73) 80
40 90
60
50 75
Binary Trees 39
80
40 90
60
50 75
73
WILL THE INSERTED ITEM ALWAYS BE A LEAF?
Binary Trees 40
FOR INSERTING AN ITEM, WHAT IS THE WORST CASE? WHAT IS THE WORST HEIGHT?
THE worstTime(n) IS LINEAR IN n.
WHAT IS THE AVERAGE HEIGHT? THE averageTime(n) IS LOGARITHMIC IN n.
Binary Trees 41
WE NOW TACKLE THE MOST
DIFFICULT METHOD:
void erase (Iterator itr);
Binary Trees 42
TO DELETE THE NODE THAT itr IS
POSITIONED AT, WE NEED TO MAKE
AN ADJUSTMENT TO THE PARENT OF
THAT NODE.
Binary Trees 43
Deletion
There are three possible cases to deletion: Value to be deleted is a leaf node: just adjust
parent link and delete node Value to be deleted has one child: child
takes parent’s place (value to be deleted’s parent now points to grandchild) and delete node
Value to be deleted has two children: swap and delete node
Binary Trees 44
IF THE NODE THAT link IS POINTING TO
HAS NO CHILDREN OR ONE CHILD,
WE SIMPLY PRUNE THAT NODE FROM
THE TREE. FOR EXAMPLE,
Binary Trees 45
SUPPOSE link IS POINTING TO THE NODE WITH 50.
80
40 90
60
50 75
73
Binary Trees 46
80
40 90
60
75
73
Binary Trees 47
Removing an Item From a Binary Tree
1 0D
P
D e le te le a f n o d e 1 0 .p N o d e P tr-> le ft is d N o d e
N o re p la c e m e n t is n e c e s s a ry.p N o d e P tr-> le ft is N U L L
B efo re A ft er
4 0
3 5
6 53 0
5 0
3 32 6
2 5
3 42 9
2 8
P
4 0
3 5
6 53 0
5 0
3 32 6
2 5
3 42 9
2 8
Binary Trees 48
WHAT IF link IS POINTING TO THE NODE WITH 40?
80
40 90
60
75
73
Binary Trees 49
80
60 90
75
73
Binary Trees 50
Removing an Item From a Binary Tree
D
P
D e le te n o de 3 5 with o n ly a le f t ch ild: No de R is th e le f t ch ild.
B e fo re
R
40
35
6530
50
3310 26
25
3429
28
P
A tta ch n o de R to th e pa re n t .
A fte r
R
40
33
6530
50
10 26
25
34
29
28
Binary Trees 51
Removing an Item From a Binary Tree
P
D e le te n o d e 2 6 w ith o n ly a rig h t c h ild : N o d e R is th e rig h t c h ild .
P
A tta c h n o d e R to th e p a re n t.
B efo re A ft er
R
R
4 0
3 5
6 53 0
5 0
3 31 0 2 6
2 5
3 42 9
2 8
4 0
3 5
6 53 0
5 0
3 31 0 2 9
2 5
3 42 8
D
Binary Trees 52
WHAT IF THE NODE HAS TWO
CHILDREN?
Binary Trees 53
Removing an Item From a Binary Tree
4 0
3 5
6 53 0
D e le te n o d e 3 0 w ith tw o c h ild re n .
5 0
3 31 0 2 6
2 5
3 42 9
2 8
4 0
3 5
6 5
O rp h a n e d s u b tre e s .
5 0
3 31 0
2 5
3 4
2 6
2 9
2 8
Binary Trees 54
Removing a Binary Search Tree Node
6 53 0
3 71 0
2 5
1 5
\\ //
D elet e n o d e 2 5
3 0
6 51 0
3 7
1 56 5
3 71 0
3 0
1 5
B ad So lu t io n : 3 0 is o u t o f p lace G o o d So lu t io n
(a) (b )
Binary Trees 55
SUPPOSE link IS POINTING TO 80’S NODE:
80
60 110
75 100 150
73 85 105
95
Binary Trees 56
THE ITEM 80 HAS TWO CHILDREN, SO WE
CANNOT SIMPLY UNLINK 80 FROM THE TREE:
THAT WOULD CREATE A HOLE.
OF THE ITEMS ALREADY IN THE TREE, WHICH
TWO COULD REPLACE 80 WITHOUT
DESTROYING THE STRUCTURE OF THE TREE?
Binary Trees 57
SUPPOSE link IS POINTING TO 80’S NODE:
80
60 110
75 100 150
73 85 105
95
Binary Trees 58
WE CAN REPLACE 80 WITH EITHER ITS PREDECESSOR, 75, OR ITS SUCCESSOR, 85.
WE’LL CHOOSE ITS SUCCESSOR. THE SUCCESSOR OF AN ITEM IS THE LEFTMOST ITEM IN THE RIGHT SUBTREE.
REPLACE 80 WITH 85, AND THEN REMOVE 85.
Binary Trees 59
HERE IS THE RESULTING TREE:
85
60 110
75 100 150
73 95 105
Binary Trees 60
CAN REMOVING THE SUCCESSOR GET
COMPLICATED? CAN THE SUCCESSOR
HAVE TWO CHILDREN?
Binary Trees 61
Removing an Item From a Binary Tree
P
D
R
p O fR N o d eP t r = d N o d eP t r
rN o d eP t r
p N o d eP t r
4 0
3 5
6 53 0
5 0
3 31 0 2 6
2 5
3 42 9
2 8
P
R
R
4 0
3 5
6 53 0
5 0
3 31 0
2 6
3 4
2 9
2 8
B efo re rep lacin g D b y R A ft er rep lacin g D b y R
Binary Trees 62
Removing an Item From a Binary Tree
D
R
d N o d eP t r
rN o d eP t rp O fR N o d eP t r
Pp N o d eP t r 4 0
3 5
6 53 0
5 0
3 31 0 2 6
2 5
3 42 9
2 8
P
R
4 0
3 5
6 53 0
5 0
3 41 0
2 6
3 3
2 9
2 8
p N o d eP t r
DR
rN o d eP t r
p O fR N o d eP t r
B efo re u n lin k in g R A ft er u n lin k in g R
Binary Trees 63
Removing an Item From a Binary Tree
P
4 0
3 5
6 53 3
5 0
3 41 0
2 6
2 9
2 8
p N o d eP t r
RrN o d eP t r
P
4 0
3 5
6 53 0
5 0
3 41 0
2 6
2 9
2 8
p N o d eP t r
D3 3R
rN o d eP t r
B efo re rep lacin g D b y R A ft er rep lacin g D b y R
Binary Trees 64
Removing from a Binary Search Tree Item not present: do nothing
Item present in leaf: remove leaf (change to null) Item in non-leaf with one child:
Replace current node with that child Item in non-leaf with two children?
Find largest item in the left subtree Recursively remove it Use it as the parent of the two subtrees (Could use smallest item in right subtree)
Binary Trees 65
Where is itr++ positioned if itr is positioned at 61? At 59? At 36?
50 37 75 25 61 15 30 55 68 28 32 59 36
Binary Trees 66
RECALL THAT link IS THE ONLY FIELD IN THE Iterator
CLASS.
if (link has a right child)
// make link point to the leftmost node in link’s right subtreeelse // go up the tree to the left as far as possible, then go up // to the right. Make link point to that ancestor.
Binary Trees 67
FOR operator++( ), worstTime(n) IS
LINEAR IN n IF itr IS POSITIONED AT
THE ROOT AND THE ONLY LEAF IS
THE SUCCESSOR OF THE ROOT.
Binary Trees 68
DURING AN ITERATION OF ALL ITEMS,
EACH NODE IS ACCESSED AT MOST 3
TIMES (FOR ITS LEFT SUBTREE, FOR
ITSELF, AND FOR THE SUCCESSOR OF
ITS RIGHT SUBTREE). SO
amortizedTime(n), AND THEREFORE
averageTime(n) IS CONSTANT.
Binary Trees 69
EXERCISE: Suppose itr is positioned at the header, and the BinSearchTree is as follows:
50
40 60
70 65
WHERE IS ++itr POSITIONED?
Binary Trees 7070
Summary Slide 1§- A binary search tree stores data by
value instead of position- It is an example of an associative container.
§- The simple rules“== return”
“< go left”
“> go right”
until finding a NULL subtree make it easy to build a binary search tree that does not allow duplicate values.
Binary Trees 71
50
30 90
12 40 100 94 THIS IS A BINARY SEARCH TREE: EACH ITEM IN THE
LEFT SUBTREE IS LESS THAN THE ROOT ITEM, EACH
ITEM IN THE RIGHT SUBTREE IS GREATER THAN
THE ROOT ITEM, AND THE LEFT AND RIGHT SUB-
TREES ARE THEMSELVES BINARY SEARCH TREES.
Binary Trees 7272
Summary Slide 2§- The insertion algorithm can be used to define the
path to locate a data value in the tree.
§- The removal of an item from a binary search tree is more difficult and involves finding a replacement node among the remaining values.