Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
CSE 143 Lecture 19 Binary Search Trees read 17.3 slides created by Marty Stepp
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Building Java Programs Chapter 17 Binary Trees. 2 Creative use of arrays/links Some data structures (such as hash tables and binary trees) are built around.
Building Java Programs Binary Search Trees; TreeSet.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
Building Java Programs Binary Trees reading: 17.1 – 17.3.
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
slides adapted from Marty Stepp and Hélène Martin
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Lecture 19: Binary Trees reading: 17.1 – 17.3
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
CSE 373 Binary search trees; tree height and balance
Building Java Programs
Trees.
slides created by Marty Stepp
Binary Search Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Building Java Programs
Building Java Programs Chapter 17
Rick Mercer, Allison Obourn, Marty Stepp
slides created by Marty Stepp and Alyssa Harding
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Building Java Programs
slides created by Alyssa Harding
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
slides adapted from Marty Stepp and Hélène Martin
Lecture 12 CS203 1.
Building Java Programs
slides created by Marty Stepp and Hélène Martin
Building Java Programs
CSE 373: Data Structures and Algorithms
Binary Search Trees continued; Tree Sets
Lecture 21: Binary Search Trees; TreeSet
slides created by Alyssa Harding
CSE 143 Lecture 5 References and Linked Nodes
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
CSC 143 Binary Search Trees.
slides created by Marty Stepp
Building Java Programs
Building Java Programs
slides created by Marty Stepp
Lecture 21: Binary Search Trees; TreeSet
Binary Search Trees reading: 17.3 – 17.4
Hashing based on slides by Marty Stepp
slides adapted from Marty Stepp
slides created by Marty Stepp and Hélène Martin
Trees.
Lecture 6: References and linked nodes reading: 16.1
slides adapted from Marty Stepp
slides created by Marty Stepp
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

x = change(x) Why not? Let’s look at an example using Point objects x 7 42 13

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

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

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

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

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

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

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

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

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

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

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

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

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?