BINARY SEARCH TREES CS 200 RECITATION 7

Similar documents
CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Pointers, Arrays and Parameters

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

CSE 113 A. Announcements - Lab

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

Largest Online Community of VU Students

Writeup for first project of CMSC 420: Data Structures Section 0102, Summer Theme: Threaded AVL Trees

Programming Exercise 14: Inheritance and Polymorphism

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Homework Assignment #3

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

Binary Search Trees. Chapter 21. Binary Search Trees

CmpSci 187: Programming with Data Structures Spring 2015

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

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Computer Science 62 Lab 8

Inheritance and Interfaces

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS2102, D15 Exam 1. Name:

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

Lecture 23, Fall 2018 Monday October 29

Second Examination Solution

Lecture 7. Log into Linux New documents posted to course webpage

CS165 Practice Midterm Problems Fall 2018

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

CS ) PROGRAMMING ASSIGNMENT 11:00 PM 11:00 PM

CSE143 Notes for Monday, 7/06/15

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CS 134 Programming Exercise 7:

Announcements. 1. Forms to return today after class:

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

STUDENT LESSON AB30 Binary Search Trees

CSE115 Lab 2 Fall 2016

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

Data Structures and Algorithms

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

Data Structure (CS301)

CS16 Exam #1 7/17/ Minutes 100 Points total

XC Total Max Score Grader

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Introduction to Data Structures

Bjarne Stroustrup. creator of C++

CS 134 Programming Exercise 9:

Topic 4 Expressions and variables

CS 251 Intermediate Programming Inheritance

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Programming Abstractions

Object-Oriented Programming Classes, Objects, Variables

Starting to Program in C++ (Basics & I/O)

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

EECS168 Exam 3 Review

Project 1 Balanced binary

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Final Exam Practice. Partial credit will be awarded.

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

Prelim One Solution. CS211 Fall Name. NetID

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Algorithms & Datastructures Laboratory Exercise Sheet 1

Chapter 4 Defining Classes I

CS 206 Introduction to Computer Science II

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Programming Exercise 7: Static Methods

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CMSC 341 Lecture 10 Binary Search Trees

Programming Assignment IV Due Thursday, June 1, 2017 at 11:59pm

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Lab 3, Tutorial 1 Comp 412

Generic BST Interface

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

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Last time s big ideas

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Lecture 02, Fall 2018 Friday September 7

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Week 3 Web site:

Lab5. Wooseok Kim

Module 9: Binary trees

ICS 311, Fall 2017, Problem Set 04, Topics 7 & 8

AVL Trees. See Section 19.4of the text, p

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS 101 Computer Programming

12 Abstract Data Types

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

CS 3114 Data Structures and Algorithms READ THIS NOW!

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Programming II (CS300)

Unit 10: Data Structures CS 101, Fall 2018

Transcription:

BINARY SEARCH TREES CS 200 RECITATION 7 Binary Search Tree (BST) Are binary trees (every node has at most two child nodes) For any given node, left sub-tree is less than it s value, and right sub-tree is greater than it s value. 3 7 1 4 They can be easily searched. From an given node: if query equals node s data, done! if query < node s data, recurse into left sub-tree if query > node s data, recurse into right sub-tree Can be thought of as a branching linked list. Generics Are a feature of Java allowing you to build an object (such as a BST or other data structure!) without knowing the type of data it will be storing. Instead, the unknown type is given a name and treated much as a regular type. When something else uses the code, the type gets plugged in at that time (usually during compile). Example: 1

public class Thing <T>{ private T data ; public Thing ( T i n p u t ) { data = i n p u t ; public T getdata ( ) { return data ; public void setdata ( T i n p u t ) { data = i n p u t ; Then later: public s t a t i c void main ( S t r i n g [ ] args ) { Thing < String > v a r i a b l e = new Thing < String >( " a s t r i n g " ) ; Note that many of the data structures in the Java standard library (List!) are programmed this way. This is why Eclipse tends to prompt you about generics when using them: L i s t < Integer > m y l i s t = new L i s t < Integer > ( ) ; But there is still a problem trying to apply generics to a BST: The tree must be able to compare it s elements! T thing1 = new T ( ) ; T thing2 = new T ( ) ; i f ( thing1 < t h i n g 2 ) { / / Does not compile!... This is because it does not know what type T will be, and thus cannot tell if the < operator is valid for that type. The way around this is to force T to be an object implementing the comparable interface. This is one when the class is defined: 2

public class Thing <T extends Comparable<T> >{ T thing1 = new T ( ) ; T thing2 = new T ( ) ; i f ( t h i n g 1. compareto ( Thing2 ) > 0 ) { / / Compiles!........ Here we are using an interface (Comparable) to promise to the compiler that type T will have a compareto(...) method. Notice that the generic declaration says extends when it should say implements. This is the correct syntax for a generic type which implements an interface. Given Files mainclass.java - Contains the main method and acts as a driver for our trees. tree.java - Encapsulates the tree. Keeps track of the root node, implements insert(...) and search(...), prints the tree, and can format the tree for drawing with dot. insert(t input) inserts the input into a new node in the correct position in the tree search(t query) searches for query in the tree. If it finds a matching node, it returns that node. Node.java - is a node in the tree. It should store at least one type T object, and have references to two other nodes (left and right). It implements the interface cs200_bst_node to ensure that it has methods expected by tree. cs200_bst_node.java - interface implemented by Node.java. Has methods expected by by classes that use Node, like tree. Tip about passing variables Be careful when reassigning objects passed to a function: public void foo ( Object t h i n g ) { t h i n g = new Object ( ) ; / / only has LOCAL a f f e c t, o r i g i n a l unaffected public void foo ( Object t h i n g ) { t h i n g. setdata ( ) ; / / e f f e c t s o r i g i n a l o b j e c t When Java passes an object to a function, it sends a copy of that object s reference. If the function reassigns that reference to a different object, the original is not affected. Calling methods on the reference instead of reassigning it will effect the original object, since the reference still points to it. 3

Dot There is a program called dot on the department computers which we can use to draw our trees. It is part of the Graphviz package of software. We just have to print the tree out in the right format: Digraph BST { > 3 3 > 1 3 > 4 > 7 Save that to a file (I called it test_data), then run dot on the file. corn> dot Tps test_data o graph. ps This results in a postscript file (graph.ps), which contains a picture of the graph: 3 7 1 4 The lab machines should be able to open this file. But if not, change the -Tps command line flag to be -Tpng to generate a.png image. The method print_dot() in tree.java is designed to print our BSTs in the correct format, I ve tried to do the work for you on this one. Exercise Need to convert the skeleton files I gave into a working Binary Search Tree. 4

Node.java - needs to be implemented. Look in the interface (cs200_bst_node) for a list of methods that it needs. Have it store one piece of data as type T, and have links to it s left and right children. In tree.java - implement the insert(...) and search(...) methods. These are very similar to each other, only differing in what they do at the end. Grading Search() moves through the tree looking for a particular node, when it finds that node it returns it. Insert() moves through the tree in the same way, but it creates a new node when it finds the right place. Duplicate nodes are not allowed, so just return if the value you are trying to insert already exists. Sign the attendance sheet Checkin a.tar file containing your Binary Search Tree code Here are checkin commands for each recit: Monday: ~cs200/bin/checkin R7L01 R7L01.tar Tuesday: ~cs200/bin/checkin R7L02 R7L02.tar Wednesday: ~cs200/bin/checkin R7L03 R7L03.tar Thursday: ~cs200/bin/checkin R7L04 R7L04.tar