
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Searching a BST. Describe an algorithm for searching a binary search tree. Try searching for the value 31, then 6. What is the maximum.
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















contains
91 60
87 29
55
42
overallRoot
private boolean contains(IntTreeNode root,
int value) {
if (root == null) {
return false;
} else if (root.data == value) {
return true;
} else {
return contains(root.left, value)
|| contains(root.right, value);
}
}
root
1
root
2
root
3
root
5
root
4
root
6
F F
F T
T
Case study: contains w/ arrays
-3 87 42 55 91 29 60
-3 29 42 55 60 87 91
BST examples
k x
g q
m
e
b
10 18
5 11
8
4
2 7
20
18
42
-1 -
8.1 21.
1.9 9.
Searching a BST
Try searching for the value 31, then 6.
12
18
7
4 15
overall root
-2 16 13
35
31
22 58
19 87 40
Exercise solution
// Returns whether this BST contains the given integer.
public boolean contains(int value) {
return contains(overallRoot, value);
private boolean contains(IntTreeNode node, int value) {
if (node == null) {
return false; // base case: not found here
} else if (node.data == value) {
return true; // base case: found here
} else if (node.data > value) {
return contains(node.left, value);
} else { // root.data < value
return contains(node.right, value);
Adding to a BST
Where should the value 14 be added?
Where should 3 be added? 7?
If the tree is empty, where
should a new value be added?
10 19
5 11
8
4
2 7
25
22
overall root
Exercise
Add the new value in the proper place to maintain BST
ordering.
tree.add(49);
60 91
29 87
55
-3 42
overall root
49
An incorrect solution
// Adds the given value to this BST in sorted order.
public void add(int value) {
add(overallRoot, value);
private void add(IntTreeNode node, int value) {
if (node == null) {
node = new IntTreeNode(value);
} else if (node.data > value) {
add(node.left, value);
} else if (node.data < value) {
add(node.right, value);
// else node.data == value, so
// it's a duplicate (don't add)
60 91
29 87
55
-3 42
overallRoot
A tangent: Change a point
public static void main(String[] args) {
Point p = new Point(1, 2);
change(p);
System.out.println(p);
public static void change(Point thePoint) {
thePoint.x = 3;
thePoint.y = 4;
// answer: (3, 4)
y 1
x p
Change point, version 2
public static void main(String[] args) {
Point p = new Point(1, 2);
change(p);
System.out.println(p);
public static void change(Point thePoint) {
thePoint = new Point(3, 4);
// answer: (1, 2)
y 1
x p
y 3
x
Change point, version 3
public static void main(String[] args) {
Point p = new Point(1, 2);
change(p);
System.out.println(p);
public static Point change(Point thePoint) {
thePoint = new Point(3, 4);
return thePoint;
// answer: (1, 2)
y 1
x p
y 3
x
Change point, version 4
public static void main(String[] args) {
Point p = new Point(1, 2);
p = change(p);
System.out.println(p);
public static Point change(Point thePoint) {
thePoint = new Point(3, 4);
return thePoint;
// answer: (3, 4)
y 1
p x
y 3
x