Building Java Programs, Study notes of Java Programming

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

2022/2023

Uploaded on 03/01/2023

dylanx
dylanx 🇺🇸

4.7

(21)

286 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Building Java Programs
Binary Search Trees
reading: 17.3 – 17.4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Building Java Programs and more Study notes Java Programming in PDF only on Docsity!

Building Java Programs

Binary Search Trees

reading: 17.3 – 17.

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

 What is the Big-O efficiency to see if a value is contained in

an unsorted array?

 What about if the array is sorted?

-3 87 42 55 91 29 60

-3 29 42 55 60 87 91

BST examples

 Which of the trees shown are legal binary search trees?

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

 Describe an algorithm for searching a binary search tree.

 Try searching for the value 31, then 6.

 What is the maximum

number of nodes you

would need to examine

to perform any search?

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

 Suppose we want to add new values to the BST below.

 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?

 What is the general algorithm?

10 19

5 11

8

4

2 7

25

22

overall root

Exercise

 Add a method add to the SearchTree class that adds a

given integer value to the BST.

 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)

 Why doesn't this solution work?

60 91

29 87

55

-3 42

overallRoot

A tangent: Change a point

 What is the state of the object referred to by p after this

code?

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

 What is the state of the object referred to by p after this

code?

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

 What is the state of the object referred to by p after this

code?

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

 What is the state of the object referred to by p after this

code?

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