What is Covered Everything up to and including Lecture 11 Types Recursion (including grammars) Lists and Trees GUIs Does not include Big-O notation (Lecture.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
Graphical User Interfaces
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
The Interdisciplinary Center, Herzelia Lecture 10, Introduction to CS - Information Technologies Slide #1 Lecture #10 - Design with Interfaces, Recursion,
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Types in programming languages1 What are types, and why do we need them?
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
(c) University of Washington20-1 CSC 143 Java Trees.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
Building Java Programs
Object Oriented Programming (OOP) LAB # 8
Find in a linked list? first last 7  4  3  8 NULL
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
Trees.
Lecture 12 CS203 1.
Building Java Programs
Binary Trees, Binary Search Trees
Stacks with Dynamic Memory
Trees, part 2 Lecture 13 CS2110 – Spring 2019
CSC 143 Java Trees.
BINARY TREE CSC248 – Data Structure.
CS210- Lecture 9 June 20, 2005 Announcements
Binary Search Trees reading: 17.3 – 17.4
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
A Binary Tree is a tree in which each node has at most 2 children
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

What is Covered Everything up to and including Lecture 11 Types Recursion (including grammars) Lists and Trees GUIs Does not include Big-O notation (Lecture 12) Other topics Software Design Patterns How Java works

Primitive vs. Reference Types Primitive types int, boolean, float, char,... Variable stores the actual data itself Reference types Object, String, Gene, … Basically any class Variable stores a reference to the actual object Reference is the memory location of the object Creating a variable does not create a new object new keyword creates an object, returns a reference to it

== vs. equals() == Compares the contents of two variables For primitive types, this is the actual data For reference types, this is the reference, not the object Two variables are == if they point to the same object Two different objects with the same contents are not == different location in memory equals() is the smarter version Looks at the contents of the objects Need to override equals() if you create a new class

== vs. equals() StringBuilder x = new StringBuilder(); x.append(“a”); StringBuilder y = x; StringBuilder z = new StringBuilder(); z.append(“a”); x == y; // true x == z; // false x.equals(z); // true

Call By Value Java is call by value Creates a copy of each argument for function calls Primitive types Java copies the data itself; original data is unchanged Reference types Java makes a copy of the reference to the object Both references point to the same object Changes affecting the object are permanent If new reference changes, old reference is unchanged

Primitive Argument Type void f(int x) { x--; } int x = 10; f(x); // x = 10

Reference Argument Type void f(ArrayList l) { l.add(2); l = new ArrayList (); } ArrayList l = new ArrayList (); l.add(1); f(l); // l contains 1, 2

Suppose type B implements or extends type A B is a subtype of A; A is a supertype of B Each variable has a static type List x; – List is the static type Can safely assign x a static subtype of List x = new ArrayList ; Static type can differ from actual type at runtime Actual type when the program runs is the dynamic type The dynamic type cannot be an interface Typing

Variable x has a static type of List Its dynamic type at runtime is ArrayList Suppose x needs to behave like an ArrayList Will not compile; compiler only know the static type Need to use a cast: (ArrayList )x Creates a copy of x with a different static type Static, dynamic type of x does not change Dynamic type must be a subtype of the type you cast to We now use the dynamic type, but the rule is the same! Casting

List l; ArrayList al; LinkedList ll; l = new ArrayList (); al = l; // does not compile al = (ArrayList )l; // safe ll = (LinkedList )l; // exception!

If B extends A, and B and A both have function foo() Which foo gets called? Answer depends on the dynamic type If the dynamic type is B, B’s foo() will be called Static type of A may be an inteface! Exception: static functions Static functions are not associated with any object Thus, they do not have any type This does not apply to variables Inheritance

interface A { /* has foo() */ } class B implements A { /* has foo() */ } A a = new B(); // dynamic type is B a.foo(); // call B’s foo()

Recursion A procedure or subroutine whose implementation references itself Examples Fibonacci Factorial Grammar Parsing

Grammars and Parsing Refer to the following grammar (ignore spaces). is the start symbol of the grammar. (Note that P → a | b is really two rules, P → a and P → b) → → + | - | + → | |. → 8 | 9 → 5 | 6 | 7 → 0 | 1 | 2 | 3 | 4

Grammars and Parsing Refer to the following grammar (ignore spaces). is the start symbol of the grammar. (Note that P → a | b is really two rules, P → a and P → b) → → + | - | + → | |. → 8 | 9 → 5 | 6 | 7 → 0 | 1 | 2 | 3 | 4 Is “ – 7” a valid sentence?

Grammars and Parsing Refer to the following grammar (ignore spaces). is the start symbol of the grammar. (Note that P → a | b is really two rules, P → a and P → b) → → + | - | + → | |. → 8 | 9 → 5 | 6 | 7 → 0 | 1 | 2 | 3 | 4 Is “ ” a valid sentence?

Grammars and Parsing Refer to the following grammar (ignore spaces). is the start symbol of the grammar. (Note that P → a | b is really two rules, P → a and P → b) → → + | - | + → | |. → 8 | 9 → 5 | 6 | 7 → 0 | 1 | 2 | 3 | 4 Is “ ” a valid sentence?

Grammars and Parsing Refer to the following grammar (ignore spaces). is the start symbol of the grammar. (Note that P → a | b is really two rules, P → a and P → b) → → + | - | + → | |. → 8 | 9 → 5 | 6 | 7 → 0 | 1 | 2 | 3 | 4 Is “ ” a valid sentence?

Grammars and Parsing Refer to the following grammar (ignore spaces). is the start symbol of the grammar. (Note that P → a | b is really two rules, P → a and P → b) → → + | - | + → | |. → 8 | 9 → 5 | 6 | 7 → 0 | 1 | 2 | 3 | 4 Is “ ” a valid sentence?

Grammars and Parsing Refer to the following grammar (ignore spaces). is the start symbol of the grammar. (Note that P → a | b is really two rules, P → a and P → b) → → + | - | + → | |. → 8 | 9 → 5 | 6 | 7 → 0 | 1 | 2 | 3 | 4 Which rule makes the grammar infinite?

Lists Definition: A data structure that contains a sequence of elements such that each element contains a reference to the next element public interface List { public void insert(T element); public void delete(T element); public boolean contains(T element); public int size(); }

Trees A tree has a single root node Ancestor of all nodes New node is added as a child of a node in the tree Node can have arbitrary number of children Each node (except the root) has only one parent Single path exists from root to every other node No cycles in the tree No constraints on where values can go in the tree

Tree

Not a Tree Cycle between 2, 3, 4

Not a Tree 4 has two parents

Technically Not a Tree It is a forest

Binary Trees Each node can have at most two children Usually we care whether we have a left or right child Still no constraints on where values can go

Binary Tree

Not a Binary Tree 1 has three children

Binary Search Tree Used to sort data inside a tree There is a difference between the left and right child For every node with value x: Every node in the left subtree has a value < x Every node in the right subtree has a value > x Binary search tree is not guaranteed to be balanced If it is balanced, we can find a node in O(log n) time

Binary Search Tree

Adding to a Binary Search Tree Adding 4 to the BST Start at root (5) 4 < 5 Go left to 2 4 > 2 Go right to 3 4 > 3 Add 4 as right child of 3

Binary Search Tree Completely unbalanced, but still a BST

Not a Binary Search Tree 8 is in the left subtree of 5

Not a Binary Search Tree 3 is the left child of 2

Not a Binary Search Tree 0 is in the right subtree of 2

Tree Traversals Converts the tree into a list Works on any binary tree It matters whether we have a left child or right child Do not need a binary search tree Traverse node and its left and right subtrees Order is different for each traveral type Subtrees are traversed recursively

Tree Traversals Preorder Traverse node, left subtree, right subtree Inorder Traverse left subtree, node, right subtree Produces a sorted list for binary search trees Postorder Traverse left subtree, right subtree, node

Tree Traversals Preorder/Postorder Traversals Can easily find the root of the tree Impossible to distinguish between left, right subtree Inorder Traversal Impossible to find the root of the tree If given the root, can easily find left, right subtree

Preorder Traversal Pre(5) 5, Pre(2), Pre(7) 5, 2, 1, Pre(3), Pre(7) 5, 2, 1, 3, 4, Pre(7) 5, 2, 1, 3, 4, 7, 6, 9

Inorder Traversal In(5) In(2), 5, In(7) 1, 2, In(3), 5, In(7) 1, 2, 3, 4, 5, In(7) 1, 2, 3, 4, 5, 6, 7, 9

Postorder Traversal Post(5) Post(2), Post(7), 5 1, Post(3), 2, Post(7), 5 1, 4, 3, 2, Post(7), 5 1, 4, 3, 2, 6, 9, 7, 5

GUIs Classes to be familiar with: JButton JLabel JFrame JPanel ActionListener LayoutManager

JPanel import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Panels extends JFrame { private JPanel panel1 = new JPanel(); private JPanel panel2 = new JPanel(); public Panels() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager panel1.setBorder(BorderFactory.createEtchedBorder()); panel2.setBorder(BorderFactory.createEtchedBorder()); panel1.add(new JLabel("Dijkstra-")); panel2.add(new JLabel("Programming: you are doing it completely wrong")); add(panel1); //add components add(panel2); pack(); setVisible(true); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception exc) {} new Panels(); }

JLabel import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Labels extends JFrame { private String myText="Testing shows the presence, not the absence of bugs” private JLabel myLabel = new JLabel(myText); public Labels() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(myLabel); //add components pack(); setVisible(true); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception exc) {} new Labels(); } JLabel

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Buttons extends JFrame { private JButton myButton = new JButton("Push Me!"); private String[] text={"Push Me!","Click me!"}; public Buttons() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(myButton); //add components myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myButton.setText(text[(int)(Math.random()+.5)]); } }); pack(); setVisible(true); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception exc) {} new Buttons(); } JButton

Lecture Example import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Intro extends JFrame { private int count = 0; private JButton myButton = new JButton("Push Me!"); private JLabel label = new JLabel("Count: " + count); public Intro() { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(myButton); //add components add(label); label.setPreferredSize(new Dimension(60, 10)); myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { count++; label.setText("Count: " + count); } }); pack(); setVisible(true); } public static void main(String[] args) { new Intro(); }