Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
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.
Binary Search Trees Chapter 7 Objectives
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
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
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
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
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
Binary Search Trees Chapter 7 Objectives
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
Recursive Objects (Part 4)
Building Java Programs
Binary Search Tree (BST)
Trees.
Binary Search Trees.
Chapter 20: Binary 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
Chapter 21: Binary Trees.
Building Java Programs
CSE 373: Data Structures and Algorithms
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
Lecture 12 CS203 1.
Building Java Programs
Building Java Programs
Binary Trees, Binary Search Trees
Binary Search Trees continued; Tree Sets
Lecture 21: Binary Search Trees; TreeSet
slides created by Alyssa Harding
CSE 373 Data Structures and Algorithms
Building Java Programs
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Building Java Programs
Building Java Programs
Lecture 21: Binary Search Trees; TreeSet
Binary Search Trees reading: 17.3 – 17.4
Trees.
Lecture 9: Intro to Trees
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
Binary Trees, Binary Search Trees
Presentation transcript:

Binary Search Trees Nilanjan Banerjee

2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm

3 Binary search trees binary search tree ("BST"): a binary tree where each non-empty node R has the following properties: elements of R's left subtree contain data "less than" R's data, elements of R's right subtree contain 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 overall root

4 BST examples Which of the trees shown are legal binary search trees? xk qg m e b

5 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? What is the computational Complexity of searching? overall root

6 Exercise Write a contains method that takes advantage of the BST structure. tree.contains(29)  true tree.contains(55)  true tree.contains(63)  false tree.contains(35)  false overall root

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

8 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? overall root

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:

10 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) ; overall root 49

11 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? overallRoot

The x = change(x) pattern

13 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) 2 y 1 x p

14 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) 2 y 1 x p 4 y 3 x

15 Changing references If a method dereferences a variable (with. ) and modifies the object it refers to, that change will be seen by the caller. public static void change(Point thePoint) { thePoint.x = 3; // affects p thePoint.setY(4); // affects p If a method reassigns a variable to refer to a new object, that change will not affect the variable passed in by the caller. public static void change(Point thePoint) { thePoint = new Point(3, 4); // p unchanged thePoint = null; // p unchanged What if we want to make the variable passed in become null ?

16 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) 2 y 1 x p 4 y 3 x

17 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) 2 y 1 x p 4 y 3 x

18 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: 1. pass in the original state of the object to the method 2. return the new (possibly changed) object from the method 3. re-assign the caller's variable to store the returned result p = change(p); // in main public static Point change(Point thePoint) { thePoint = new Point(99, -1); return thePoint;

19 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 node, int value) { if (node == null) { node = new IntTreeNode(value); } overallRoot 49 node

20 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 node, int value) { if (node == null) { node = new IntTreeNode(value); } else if (node.data > value) { node.left = add(node.left, value); } else if (node.data < value) { node.right = add(node.right, value); } // else a duplicate; do nothing return node; } What happens when node is a leaf? overallRoot

21 Deletion There are the following possible cases when we delete a node: The node to be deleted has no children. In this case, all we need to do is delete the node. The node to be deleted has only a right subtree. We delete the node and attach the right subtree to the deleted node’s parent. The node to be deleted has only a left subtree. We delete the node and attach the left subtree to the deleted node’s parent. The node to be deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to create very unbalanced trees.

22 Deletion from the middle of a tree Rather than simply delete the node, we try to maintain the existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of two ways.

23 Deletion from the middle of a tree We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data. We can find the smallest node on the deleted node’s right subtree and move its data to replace the deleted node’s data. Either of these moves preserves the integrity of the binary search tree.

24

25 (continued)

26 27

Performance of BST methods What is the asymptotic performance of each of the BST methods? Best CaseWorst CaseAverage Case searching insert remove

28 Exam Review Topic 1: Java Review Generics in Java, Type Erasure, Virtual Fuction invocation caused by Inheritance Asymptotic Analysis Big Oh notation --- you should be able to look at an implementation and figure it running time in Big Oh L’Hospital Rule Linked List Understand the difference between using an ArrayList and a LinkedList Running time of a LinkedList insertion, deletion, searching an element One problem where you will have to manipulate a linked list Stack and Queues and Adapter Classes

29 Exam Trees Definition of a tree One algorithmic question with trees How to traverse a tree Recursive implementation as well as the non-recursive implementation Inorder, Preorder, Postorder, LevelOrder