CmpSci 187: Programming with Data Structures Spring 2015

Similar documents
CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

CS24 Week 8 Lecture 1

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

If you took your exam home last time, I will still regrade it if you want.

INF2220: algorithms and data structures Series 1

Binary Search Trees 1

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

Algorithms. Deleting from Red-Black Trees B-Trees

ECE 242. Data Structures

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CMSC 341 Lecture 10 Binary Search Trees

Programming II (CS300)

CMSC 341 Lecture 14: Priority Queues, Heaps

Programming II (CS300)

CMSC 341. Binary Search Trees CMSC 341 BST

TREES Lecture 12 CS2110 Spring 2018

TREES. Trees - Introduction

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Spring 2018 Mentoring 8: March 14, Binary Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Algorithms. AVL Tree

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

CS102 Binary Search Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Data Structures and Algorithms

TREES Lecture 12 CS2110 Fall 2016

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Lecture 23: Binary Search Trees

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Lecture 15 Notes Binary Search Trees

CSE 214 Computer Science II Heaps and Priority Queues

Binary Search Trees. Analysis of Algorithms

Lecture 15 Binary Search Trees

Binary Trees, Binary Search Trees

CmpSci 187: Programming with Data Structures Spring 2015

STUDENT LESSON AB30 Binary Search Trees

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

DATA STRUCTURES AND ALGORITHMS

Advanced Set Representation Methods

Trees. (Trees) Data Structures and Programming Spring / 28

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

CMSC 341 Lecture 15 Leftist Heaps

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

CIS 121. Binary Search Trees

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

Lecture Notes on Binary Search Trees

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

Search Trees - 1 Venkatanatha Sarma Y

Fall, 2015 Prof. Jungkeun Park

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

Lecture 27. Binary Search Trees. Binary Search Trees

CSCI Trees. Mark Redekopp David Kempe

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

TREES Lecture 10 CS2110 Spring2014

Trees. Eric McCreath

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

CS 307 Final Spring 2009

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Data Structures in Java

COMP : Trees. COMP20012 Trees 219

CSE 373 Midterm 2 2/27/06 Sample Solution. Question 1. (6 points) (a) What is the load factor of a hash table? (Give a definition.

CMSC 341 Lecture 15 Leftist Heaps

Programming II (CS300)

COMP Analysis of Algorithms & Data Structures

Binary search trees (chapters )

Binary Search Tree. Revised based on textbook author s notes.

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Spring 2018 Profs Bill & Jon

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS302 - Data Structures using C++

Trees, Part 1: Unbalanced Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Chapter 4: Trees. 4.2 For node B :

Red-black trees (19.5), B-trees (19.8), trees

SFU CMPT Lecture: Week 9

Alex. Adam Agnes Allen Arthur

COMP Analysis of Algorithms & Data Structures

Self-Balancing Search Trees. Chapter 11

Title Description Participants Textbook

Binary Tree Applications

Priority Queues. 04/10/03 Lecture 22 1

CS 2604 Data Structures Midterm Summer 2000 U T. Do not start the test until instructed to do so!

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Transcription:

CmpSci 187: Programming with Data Structures Spring 2015 Lecture #17, Implementing Binary Search Trees John Ridgway April 2, 2015 1 Implementing Binary Search Trees Review: The BST Interface Binary search trees store objects in nodes, arranged according to the BST Rule everything in x s left subtree is x and everything in its right subtree is x. The binary search interface is very similar to the list interface. Remember that we have three ways to iterate through a tree, using the different iterators. Clicker Question #1 If I add a node with value 12 to this BST, without changing the links of any other nodes, where must the new node go? A. a right child of 7 B. a left child of 10 C. a left child of 13 8 3 10 1 6 14 4 7 13 12 D. it s not possible to add it Code for BSTInterface import java. util. Iterator ; public interface BSTInterface <T extends Comparable <T >> boolean isempty (); 1

int size (); boolean contains ( T element ); boolean remove ( T element ); T get (T element ); void add ( T element ); Iterator <T > preorderiterator (); Iterator <T > inorderiterator (); Iterator <T > postorderiterator (); Review: The BSTNode Class (Getters and setters omitted.) public class BSTNode <T > private T data ; private BSTNode <T > left ; private BSTNode <T > right ; public BSTNode ( T data, BSTNode <T > left, BSTNode <T > right ) this. data = data ; this. left = left ; this. right = right ; Review: The BinarySearchTree Header public class BinarySearchTree <T extends implements BSTInterface <T > Comparable <T >> private BSTNode <T> root ; public boolean isempty () return root == null ; Review: Recursive size Method We use a helper method, which clearly is correct on empty trees, and which uses a recursive definition to calculate the size of a nonempty tree from the size of its subtrees. public int size () return subtreesize ( root ); private int subtreesize ( BSTNode <T > node ) if ( node return == 0; null ) else return 1 + subtreesize ( node. getleft ()) + subtreesize ( node. getright ()); 2

Review: Recursive get public T get (T t) return getfromtree (t, root ); private T getfromtree ( T t, BSTNode <T > node ) if ( node == null ) return null ; if (t. compareto ( node. getdata ()) < 0) return getfromtree (t, node. getleft ()); if (t. compareto ( node. getdata ()) == 0) return node. getdata (); return getfromtree (t, node. getright ()); What s Left to Implement? We ve written two of the main observer methods of our BinarySearchTree class, size, and get, and hinted at contains. We still need to write the two main transformer methods, add and remove. Here our main problem will be to maintain the BST rule at every node as we change the tree structure. We also need to write the three iterators for the three different orders on the nodes. The add Method We use yet another recursive helper method to insert elements. The addto method takes a subtree as a parameter, in the form of its root node. It returns the new version of the same subtree, by returning a pointer to its root node. The recursive job is insert this element into the correct position within the subtree given by this node and return the new version of the subtree. Code for the add Method 3

public void add ( T t) root = addto (t, root ); private BSTNode <T > addto ( T t, BSTNode <T > node ) if ( node == null ) return new BSTNode <T >( t, null, null ); if (t. compareto ( node. getdata ()) <= 0) node. setleft ( addto (t, node. getleft ())); else node. setright ( addto (t, node. getright ())); return node ; Clicker Question #2 Suppose that this BST was built by successively adding each of the elements, starting from an empty tree. Which of these might have been the order in which they were added? 3 8 10 A. 8, 10, 14, 3, 6, 7, 1, 4, 13 1 6 14 B. 8, 3, 14, 1, 4, 13, 6, 10, 7 4 7 13 C. 8, 3, 6, 10, 1, 14, 13, 7, 4 D. either a or c is possible The remove Method Removing a node will be considerably more complicated because we have to preserve the BST property everywhere. Once again we ll use a recursive helper removefrom. Before we look at the code for the removefrom method, let s look less formally at the logic for removing a node. Logic for Removing The easiest case is when the node to be removed is a leaf. We just delete it. The next easiest case is when the node to be removed has only one child. We replace the node with its child, whether left or right. Removing a node that has two children is more complicated. We will do it by finding the in-order predecessor of the target node, moving its data to the target node, and then removing the predecessor node. We do this 4

last removal recursively, but actually the predecessor can t have a right child. Clicker Question #3 Let x be any node of a BST, with two children, and let y be its in-order predecessor. Which of these statements is not true? A. y cannot possibly have a right child. B. y must be the left child of x. C. y must be a member of x s left subtree. D. y is either the left child of x or y is a right child. Methods for Removing We will use four methods in all. The actual remove method calls a recursive helper method removefrom that returns the new version of the tree rooted at its parameter. removefrom uses two other methods: getlargest returns the data stored in the right-most node in a subtree; and removelargest removes the right-most node of the sub-tree and returns its (possibly modified) root. Code for the remove Method public boolean remove ( T t) boolean result = contains ( t); if ( result ) root = removefrom (t, root ); return result ; Code for the removefrom Method 5

private BSTNode <T > removefrom ( T t, BSTNode <T > node ) if (t. compareto ( node. getdata ()) < 0) node. setleft ( removefrom (t, node. getleft ())); else if ( t. compareto ( node. getdata ()) > 0) node. setright ( removefrom (t, node. getright ())); else if ( node. getleft () == null ) return node. getright (); else if ( node. getright () == null ) return node. getleft (); else // neither child is null node. setdata ( getlargest ( node. getleft ())); node. setleft ( removelargest ( node. getleft ())); return node ; getlargest and removelargest private T getlargest ( BSTNode <T > node ) if ( node. getright () == null ) return node. getdata (); else return getlargest ( node. getright ()); private BSTNode <T > removelargest ( BSTNode <T > node ) if ( node. getright () == null ) return node. getleft (); else node. setright ( removelargest ( node. getright ())); return node ; Clicker Question #4 This method returns the data from what node? private T getlargest ( BSTNode <T > node ) if ( node. getright () == null ) return node. getdata (); else return getlargest ( node. getright ()); A. node B. the leftmost node in the tree of node s sibling C. the parent of node D. the rightmost node in the tree of node 6

The Three Iterators The iterators are all similar; we create a queue and enqueue all of the elements in the appropriate order, then return an iterator on the queue. public Iterator <T > inorderiterator () Queue <T > queue = new LinkedQueue <T >(); inordertraverse ( queue, root ); return queue. iterator (); private void inordertraverse ( Queue <T > queue, BSTNode <T > node ) if ( node == null ) return ; inordertraverse ( queue, node. getleft ()); queue. enqueue ( node. getdata ()); inordertraverse ( queue, node. getright ()); Comparing Trees and Lists It s natural to compare the performance of BST s and linear lists on the same tasks. There s a problem, though, with our worst-case analysis. In the worst case, when every node has at most one child, a BST is essentially a linear list. Searching into a BST takes a worst-case time equal to the height of the tree. If the tree is balanced, this height is O(log n) for an n-node tree, but for an unbalanced tree it could be as large as O(n). DJW have a chart on page 579 comparing balanced BST s, array-based lists, and linked lists. The only BST operations that take more than O(log n) are the three iterator methods; they copy all data into a queue. The array-based sorted list can perform get in O(log n), but add and remove each take O(n) because of the need to move data. The linked list takes O(1) to process an add or remove operation, but O(n) to find where to do it. How to Balance a Tree? How can we balance a tree? In CMPSCI 311 you will learn about selfbalancing trees, in at least one of the several versions. These trees always have a height of O(log n) when their size is n. Adding and removing take O(log n) time, because there may be a restructuring step of O(log n) time after each such move to restore the balance. 7

Wrong Ways to Balance DJW consider only a simpler method of rebalancing that takes O(n) time whenever we choose to do it. This is to copy the entire tree into an array of elements, then add each element of the array in turn back into a new tree. We naturally form our array by using one of our three traversal methods. If we use in-order, the array becomes a sorted list. Unfortunately, adding the elements in sorted order produces a very unbalanced tree. If we form the array using pre-order, and then load the array back into the BST, it turns out that the new tree is identical to the old one. What happens if we form the array from the post-order traversal, and then load it back in? An Example Consider a perfectly balanced BST of Integers with 15 nodes with values 1 through 15. The post-order traversal of this tree visits the nodes in order 1, 3, 2, 5, 7, 6, 4, 9, 11, 10, 13, 15, 14, 12, 8. If we insert the nodes into a new BST in this order, we get a tree of height 9 instead of the original height 3. A Perfectly Balanced BST 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Its post-order traversal: 1, 3, 2, 5, 7, 6, 4, 9, 11, 10, 13, 15, 14, 12, 8 Adding them in Post-Order 8

1 3 2 5 Original in post-order: 1, 3, 2, 5, 7, 6, 4, 9, 11, 10, 13, 15, 14, 12, 8 4 7 6 9 8 11 10 13 12 15 14 Balancing a BST Effectively The right idea here is to study how we want the new tree to be arranged. Suppose we have used in-order to form the array, so that the array is sorted. If the tree is balanced, with an equal number of nodes on each side, the root node will be the middle element of the array. Its left child should be about 1/4 of the way along, and its right child 3/4 of the way, and so forth. This should suggest a recursion. Code to Balance a BST - Part I public void balance () T[] values = (T []) new Comparable [ size ()]; Iterator <T > int index = iterator 0; = inorderiterator (); while ( iterator. hasnext ()) values [ index ] = index += 1; iterator. next (); root = null ; inserttree ( values, 0, index - 1); Code to Balance a BST - Part II private void inserttree ( T[] values, int low, int high ) if ( low == high ) add ( values [ low ]); else if ( low + 1 == high ) add ( values [ low ]); add ( values [ high ]); else 9

int mid = ( low + high )/2; add ( values [ mid ]); inserttree ( values, low, mid - 1); inserttree ( values, mid + 1, high ); 10