Download presentation
Presentation is loading. Please wait.
Published byLasse Mikkonen Modified over 5 years ago
1
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
CSE 143 Lecture 21 Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
2
Binary search trees binary search tree ("BST"): a binary tree that is either: empty (null), or a root node R such that: every element of R's left subtree contains data "less than" R's data, every element of R's right subtree contains data "greater than" R's, R's left and right subtrees are also binary search trees. BSTs store their elements in sorted order, which is helpful for searching/sorting tasks. 91 60 87 29 55 42 -3 overall root
3
Exercise Which of the trees shown are legal binary search trees? x k q
m e b 18 10 11 5 8 4 2 7 20 42 21.3 8.1 9.6 1.9 7.2 -7 -1 -5
4
Searching a BST Describe an algorithm for searching the tree below for the value 31. Then search for the value 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
5
Exercise Add a method contains to the IntTree class that searches the tree for a given integer, returning true if it is found in the tree and false if not. Assume that the elements of the tree constitute a legal binary search tree. If an IntTree variable tree referred to the tree below, the following calls would have the following results: tree.contains(29) true tree.contains(55) true tree.contains(63) false tree.contains(35) false 91 60 87 29 55 42 -3 overall root
6
Exercise solution // Returns whether this tree contains the given integer. public boolean contains(int value) { return contains(overallRoot, value); } private boolean contains(IntTreeNode root, int value) {
7
Exercise solution // Returns whether this tree contains the given integer. public boolean contains(int value) { return contains(overallRoot, value); } private boolean contains(IntTreeNode root, int value) { if (root == null) { return false; } else if (root.data == value) { return true; } else if (value < root.data) { return contains(root.left, value); } else { // value > root.data return contains(root.right, value);
8
Adding to a BST Suppose we want to add the value 14 to the BST below.
Where should the new node be added? Where would we add the value 3? Where would we add 7? If the tree is empty, where should a new value be added? What is the general algorithm? 18 10 11 5 8 4 2 7 20
9
Adding exercise Draw what a binary search tree would look like if the following values were added to an initially empty tree in this order: 50 20 75 98 80 31 150 39 23 11 77
10
Adding exercise Draw what a binary search tree would look like if the following values were added to an initially empty tree in this order: 50 20 75 98 80 31 150 39 23 11 77 50 20 75 11 31 98 23 39 80 150 77
11
Exercise Add a method add to the IntTree class that adds a given integer value to the tree. Assume that the elements of the IntTree constitute a legal binary search tree, and add the new value in the appropriate place to maintain ordering. tree.add(49); 91 60 87 29 55 42 -3 overall root 49
12
An incorrect solution Why doesn't this solution work?
// Adds the given value to this BST in sorted order. public void add(int value) { add(overallRoot, value); } private void add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (value < root.data) { add(root.left, value); } else if (value > root.data) { add(root.right, value); // else value == root.data; // a duplicate (don't add) Why doesn't this solution work? 91 60 87 29 55 42 -3 overallRoot
13
x = change(x) Why not? Let’s look at an example using Point objects x
7 42 13
14
x = change(x) import java.awt.*; public class PointTest { public static void main(String[] args) { Point x = new Point(7, 42); System.out.println("x = " + x); change(x); System.out.println("now x = " + x); } public static void change(Point p) { p.translate(3, 8); p = new Point(-33, -17); System.out.println("p = " + p); 14
15
x = change(x) Given this code that manipulates a Point, what will it output? The first two lines are straightforward, but the last one might be surprising: x = java.awt.Point[x=7,y=42] p = java.awt.Point[x=-33,y=-17] now x = java.awt.Point[x=10,y=50] x does not refer to our new Point, but the old translated Point 15
16
x = change(x) When we pass an object as a parameter, the parameter gets a copy of the reference to the object change(x); x y 7 42 x p 16
17
x = change(x) So when we call a method on p, it affects the object that x refers to because they refer to the same object p.translate(3, 8); x y 10 50 x p 17
18
x = change(x) But this statement actually makes p refer to an entirely new point! p = new Point(-33, -17); x y 10 50 x x y -33 -17 p 18
19
x = change(x) We can also think of these references like cell phone numbers Just like when we passed the parameter, I can give you a copy of Alyssa’s cell phone number Just like the method call, you can call her too But if you scratch out your copy of her number that has no effect on my copy! 19
20
x = change(x) How do we get around this?
We want our original variable, x, to refer to the same object as p A strategy we call “x assign change of x” x = change(x) 20
21
x = change(x) To get the reference to our new object back to our original variable, we can return it public static Point change(Point p) { p.translate(3, 8); p = new Point(-33, -17); System.out.println("p = " + p); return p; } 21
22
x = change(x) Then, when we call the method, we assign our original variable to the returned reference public class PointTest { public static void main(String[] args) { Point x = new Point(7, 42); System.out.println("x = " + x); x = change(x); System.out.println("now x = " + x); } Now our last print statement uses our new Point object 22
23
The problem Much like with linked lists, if we just modify what a local variable refers to, it won't change the collection. private void add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); In the linked list case, how did we correct this problem? How did we actually modify the list? 49 root 91 60 87 29 55 42 -3 overallRoot
24
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 In order to actually change the tree, you must reassign: root = change(root, parameters); root.left = change(root.left, parameters); root.right = change(root.right, parameters); node before parameter return your method node after
25
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 root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (value < root.data) { add(root.left, value); } else if (value > root.data) { add(root.right, value); // else value == root.data; // a duplicate (don't add) 91 60 87 29 55 42 -3 overallRoot
26
A correct solution // Adds the given value to this BST in sorted order. public void add(int value) { overallRoot = add(overallRoot, value); } private IntTreeNode add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (value < root.data) { root.left = add(root.left, value); } else if (value > root.data) { root.right = add(root.right, value); } // else a duplicate return root; 91 60 87 29 55 42 -3 overallRoot What about the case where root is unmodified?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.