Obourn Marty Stepp Binary Search Trees A Binary Search Tree BST data structure is a binary tree with an ordering property BSTs are used to maintain order and faster retrieval insertion and removal of individual elements ID: 728338
Download Presentation The PPT/PDF document "Binary Search Trees Rick Mercer, Alliso..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Binary Search
Trees
Rick Mercer, Allison
Obourn
, Marty
SteppSlide2
Binary Search Trees
A Binary Search Tree (BST) data structure is a binary tree with an ordering property
BSTs are used to maintain order and faster retrieval, insertion, and removal of individual elements
A Binary Search Tree (BST) is
an
empty
tree
consists of a node called the
root
, and two children,
left
and
right
, each of which are themselves binary search trees. Each BST contains a key at the root that is greater than all keys in the left BST while also being less than all keys in the right BST. Key fields are unique, duplicates not allowed.Slide3
A Binary Search Tree
integers represent the keys
50
75
25
12
35
28
41
66
90
81
95
91
100
54
rootSlide4
Are these BSTs?
only the keys are shown
50
75
25
12
45
66
90
50
75
25
12
55
73
90
Is this a BST?
Is this a BST?
root
rootSlide5
BST Algorithms
only the keys are shown
Think about an algorithm that search for 36
It is similar to Binary Search
Start at root
50
75
25
12
36
65
90
t
rootSlide6
adding Elements
no duplicates can ever be added
The search element 36 < 50 so go left
50
75
25
12
36
65
90
t
rootSlide7
adding Elements
no duplicates can ever be added
36
> 25 so go right
36
found, stop, return true
50
75
25
12
36
65
90
t
rootSlide8
adding Elements
no duplicates can ever be added
If we were looking for 29, we would go to
t.left
and set
t to
null, return false
What is the runtime of search in a BST?
50
75
25
12
36
65
90
t
rootSlide9
Insert
But how did we build this tree?
Don’t want to hard code it
There is no prefix expression to be
usedCould be recursiveCould be iterative
Since trees are recursive data structures, we’ll use recursion
9Slide10
First, example insert 11
elements
What
a binary search tree would look like if
these
values were added in
this order (first element is always at root)
50 20
75 98
80 31 150 39 23 11 775020
758098
1139
31150
23
77rootSlide11
Exercise
Add a method
add
to the
SearchTree
class that inserts an element into the BST.
Add the new value in the proper place to maintain BST orderingbst.insert
(49);
91
60872955
42-3
root49Slide12
An incorrect solution
public
void
insert(E element)
{
add(root,
element);}
private void add(TreeNode
t, E value) { if (t == null) { t = new TreeNode(value); else if (value.compareTo(t.data) < 0) add(t.left, value); else if (value.compareTo(t.data) > 0
) add(t.right, value); // else t.data.equals(value), so // it's a duplicate (don't add)}Why doesn't this solution work?91
60872955
42-3
rootSlide13
The x = change(x
) patternSlide14
Change a point?
Will the assertions pass?
@Test
public void
testChange
() {
Point p = new Point(1, 2);
change(p);
assertEquals(3, p.x
); assertEquals(4, p.y);}public void change(Point thePoint) { thePoint.x = 3; thePoint.y = 4;}
2y1x
pSlide15
Change
a
point?
Will the assertions pass?
@Test
public void
testChange() {
Point p = new Point(1, 2); change(p
); assertEquals
(3, p.x); assertEquals(4, p.y);}public void change(Point thePoint) { thePoint = new Point(3, 4);}
2y1x
p
4
y
3
xSlide16
Changing references
If a method
dereferences a variable
(with
.
) and modifies the object it refers to, that change will be seen by the caller
void change(Point thePoint) {
thePoint.x = 3; // affects the argument
thePoint.y = 4;
// affects the argumentIf a method reassigns a variable to refer to a new object, that change will not affect the variable void change(Point thePoint) { thePoint = new Point(3, 4); // argument unchanged thePoint = null; // argument unchangedSlide17
Change point, version 3
Will
these
assertions pass?
@Test
public void
testChange() { Point
p = new Point(1, 2); change(p);
assertEquals(3, p.x);
assertEquals(4, p.y);}public Point change(Point thePoint) { thePoint = new Point(3, 4); return thePoint;}
2y1x
p
4
y
3
xSlide18
Change point, version 4
Will these assertions pass?
@Test
public void
testChange() {
Point p = new Point(1, 2); p =
change(p); assertEquals(3
, p.x); assertEquals(4, p.y); } public Point change(Point thePoint) { thePoint = new Point(3, 4); return thePoint; }
2y1
x
p
4
y
3
xSlide19
The algorithmic pattern x
= change(x);
If you want to write a method that can change the object that a variable refers to, you must do three things:
pass
in the original state of the object to the method
return
the new (possibly changed) object from the methodre-assign the caller's variable to store the returned result
p
= change(p); public Point
change(Point thePoint) { thePoint = new Point(99, -1); return thePoint;Also seen with strings, methods return a new String s = s.toUpperCase(); s = s.substring(0, 3);Slide20
The
problem was
Much like with linked
structure,
if we
only modify what a local variable refers to, it won't change the collection
private void add(TreeNode t, E
value) { if (node == null) {
node = new TreeNode
(value); }In linked structures, how did weactually modify the object?by changing firstby changing a Node's next field9160
872955
42-3
root49tSlide21
Applying x = change(x)
Methods that modify a tree should have the following pattern:
input (parameter): old state of the node
output (return): new state of the node
your
method
node
before
node
after
parameterreturn
To change the tree, we must reassign t = change(t, parameters); t.left = change(t.left, parameters); t.right = change(
t.right, parameters); root = change(root, parameters);Slide22
A correct solution
//
Insert the given element into this
BST
public void insert(E
el) { root
= insert(root, el);
} private TreeNode insert(TreeNode t, E el) { if (t == null) t = new TreeNode(el); else if (el.compareTo(t
.data) < 0) t.left = insert(t.left, el); else t.right = insert(t.right, el); return t; }What happens when t is a leaf?
91608729
5542
-3
root