CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

Similar documents
Communications of the ACM Learning to Learn Denning & Flores learning-to-learn/fulltext

Last time s big ideas

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

Sorting race.

Building Java Programs

Data Structures in Java

CS 2230 CS II: Data structures. Meeting 20: generic types, exceptions, higher order functions Brandon Myers University of Iowa

CS 2230 CS II: Data structures. Meeting 31: Priority queue Brandon Myers University of Iowa

CS 2230 CS II: Data structures. Limits of comparison sorting, beyond comparison sorting Brandon Myers University of Iowa

== isn t always equal?

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

Topic 14. The BinaryTree ADT

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

Sorting race.

Sample Questions for Midterm Exam 2

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp


Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Why do we have to write a type for every variable in Java?

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

TREES Lecture 10 CS2110 Spring2014

Data Structures and Algorithms

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

INF2220: algorithms and data structures Series 1

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

tree nonlinear Examples

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

Data Structures and Algorithms

CS171 Final Practice Exam

CS350: Data Structures Tree Traversal

CS 2230 CS II: Data structures. Meeting 26: the Set ADT Brandon Myers University of Iowa

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS Introduction to Data Structures Week 5, 2017

CS171 Final Practice Exam

Principles of Computer Science

csci 210: Data Structures Trees

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

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

CS 206 Introduction to Computer Science II

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

CS 2630 Computer Organization. Meeting 13: Faster arithmetic and more operations Brandon Myers University of Iowa

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

Tree Structures. A hierarchical data structure whose point of entry is the root node

TREES Lecture 12 CS2110 Fall 2016

IMPLEMENTING BINARY TREES

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

Another interface: Comparable

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

Why Do We Need Trees?

Binary Search Trees. Ravi Chugh March 28, 2014

CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

CS II: Data Structures Discussion worksheet: Week 15

University of Palestine. Mid Exam Total Grade: 100

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

Section 4 SOLUTION: AVL Trees & B-Trees

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

CS24 Week 8 Lecture 1

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CSE 326: Data Structures Binary Search Trees

AVL Trees. See Section 19.4of the text, p

DO NOT REPRODUCE. CS61B, Fall 2008 Test #3 (revised) P. N. Hilfinger

CS 151. Binary Search Trees. Wednesday, October 10, 12

Sorting and Selection

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

ITI Introduction to Computing II

Binary Trees and Huffman Encoding Binary Search Trees

CS2012 Programming Techniques II

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Priority Queues (Heaps)

Compiling expressions

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

ITI Introduction to Computing II

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

BBM 201 Data structures

Before class. BSTs, Heaps, and PQs 2/26/13 1. Open TreeNodeExample.java Change main to:

Computer Science 1 Ah

CS 61B Discussion 5: Inheritance II Fall 2014

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

CSC 321: Data Structures. Fall 2012

CSC 321: Data Structures. Fall 2016

TREES Lecture 12 CS2110 Spring 2018

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU. Please put student id on last page. Instructions:

JAVA NOTES DATA STRUCTURES

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

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

CS 125 Practice Final Exam Solutions SOLUTION SET

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Day 10. COMP1006/1406 Summer M. Jason Hinek Carleton University

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Transcription:

CS 2230 CS II: Data structures Meeting 21: trees Brandon Myers University of Iowa

Today s learning objectives Define tree (a data structure that can store data hierarchically) and describe trees in terms of attributes Interpret code that uses a links to represent a tree Interpret code that uses recursion to explore all the branches of a tree

1 minute paper What is an example of data or real-life system that is organized hierarchically? https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Your computer s filesystem https://commons.wikimedia.org

Your family tree https://commons.wikimedia.org

Business management structure https://commons.wikimedia.org

Classification of life https://commons.wikimedia.org

Inheritance tree for Java exceptions Y X X is a Y IOException NullPointerException ArrayOutOfBoundsException

Motivation If we store this kind of data in a List, we ll lose the hierarchical relationships Your computer s filesystem Business management Your family tree Classification of life https://commons.wikimedia.org

height depth leaves inner nodes root children parent

1. Pick one of these trees (or your example) 2. identify what each means for the example: height, depth, leaves, inner nodes, root, children, parent 3. repeat for another example Your computer s filesystem Business management Your family tree Classification of life https://commons.wikimedia.org

Our first implementation of a tree Eventually we will want a Tree interface (your textbook defines one in Ch 8.1), but let s start with a specific implementation to get the intuition class LinkedBinaryTree<T> { private BinaryTreeNode<T> root; private class BinaryTreeNode { public T data; public BinaryTreeNode left; public BinaryTreeNode right; Example of a binary tree 2 1 3 4 1

class LinkedBinaryTree<T> { private BinaryTreeNode<T> root; public LinkedBinaryTree() { root = null; Abstract drawing of the tree 2 1 3 public static void main()(string[] args) { LinkedBinaryTree<Integer> tree = new LinkedBinaryTree<>(); tree.root = new BinaryTreeNode<>(2); 4 1 tree.root.left = new BinaryTreeNode<>(1); tree.root.left.left = new BinaryTreeNode<>(4); tree.root.left.right = new BinaryTreeNode<>(1); tree.root.right = new BinaryTreeNode<>(3); Box-and-arrow diagram root private class BinaryTreeNode { public T data; public BinaryTreeNode left; public BinaryTreeNode right; public BinaryTreeNode(T data) { this.data = data; this.left = null; this.right = null; 4 2 data left right 1 3 1

Peer instruction BinaryTreeNode<Integer> root = new BinaryTreeNode<>(1); root.right = new BinaryTreeNode<>(2); BinaryTreeNode<Integer> t1 = root.right; t1.left = new BinaryTreeNode<>(3); BinaryTreeNode<Integer> t2 = t1.left; t2.right = new BinaryTreeNode<>(4); a 1 c 1 2 3 2 3 4 4 b 1 d 1 2 3 4 3 4 https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Recursion on binary trees. What does getxelement() return when called on this tree? class LinkedBinaryTree<T> { private BinaryTreeNode<T> root; 2 1 3 private T getxelement() { return root.getxnode().data; 4 1 private class BinaryTreeNode { public T data; public BinaryTreeNode left; public BinaryTreeNode right; private BinaryTreeNode<T> getxnode() { if (this.left == null) return this; else return this.left.getxnode(); https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Peer instruction What is the worst case running time of LinkedBinaryTree.getXElement()? What is the best case running time of LinkedBinaryTree.getXElement()? a) O(1) b) O(logN) c) O(N) d) O(NlogN) e) O(N 2 ) private BinaryTreeNode<T> getxnode() { if (this.left == null) return this; else return this.left.getxnode(); https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

The real power of recursion on trees class LinkedBinaryTree<T> { private BinaryTreeNode root; 2 // only works on LinkedBinaryTree<Integer> public int sum() { return root.sum(); 1 3 4 1 private class BinaryTreeNode { public T data; public BinaryTreeNode left; public BinaryTreeNode right; private int sum() { int s = 0; if (this.left!= null) s += this.left.sum(); if (this.right!= null) s += this.right.sum(); //only works when T is Integer s += (int) this.data; return s; List the order of BinaryTreeNodes that have sum called on them https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Peer instruction private int sum() { int s = 0; if (this.left!= null) s += this.left.sum(); if (this.right!= null) s += this.right.sum(); //only works when T is Integer s += (int) this.data; return s; What is the running time of BinaryTreeNode.sum()? a) O(1) b) O(logN) c) O(N) d) O(NlogN) e) O(N 2 ) https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Today s learning objectives Define tree (a data structure that can store data hierarchically) Interpret code that uses a links to represent a tree Interpret code that uses recursion to explore all the branches of a tree