Trees, part 2 Lecture 13 CS2110 – Spring 2019

Slides:



Advertisements
Similar presentations
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advertisements

CS 171: Introduction to Computer Science II
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
CS261 Data Structures Tree Traversals. Goals Euler Tours Recursive Implementation Tree Sort Algorithm.
Binary Trees. Node structure Data A data field and two pointers, left and right.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
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.
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Trees Saurav Karmakar
ASTs, Grammars, Parsing, Tree traversals
Data Structure By Amee Trivedi.
Data Structures and Design in Java © Rick Mercer
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
ASTs, Grammars, Parsing, Tree traversals
Trees.
CSC 172– Data Structures and Algorithms
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.
TREES General trees Binary trees Binary search trees AVL trees
8.2 Tree Traversals Chapter 8 - Trees.
CS212: Data Structures and Algorithms
Find in a linked list? first last 7  4  3  8 NULL
Trees.
ADTs, Grammars, Parsing, Tree traversals
ASTs, Grammars, Parsing, Tree traversals
Section 9.3 by Andrew Watkins
Binary Trees, Binary Search Trees
CSC 143 Java Applications of Trees.
CS 261 – Data Structures Trees.
CS210- Lecture 9 June 20, 2005 Announcements
B.Ramamurthy Chapter 9 CSE116A,B
Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees, part 2 Lecture 13 CS2110 – Spring 2019 I started this lecture by finishing what I didn’t get to in L12: worst-case search in BSTs Lecture 13 CS2110 – Spring 2019

Finish Lec12

Announcements Prelim conflict quiz was due last night. Too late now to make changes. We won’t be sending confirmations about time swaps (5:30 vs 7:30); if you requested it, you got it. Room assignments for the prelim (including SDS accommodations) will be announced by Monday. Please be patient.

JavaHyperText topics Tree traversals (preorder, inorder, postorder) Stack machines …will be added by the end of this weekend

Trees, re-implemented Last time: lots of null comparisons to handle empty trees A more OO design: Interface to represent operations on trees Classes to represent behavior of empty vs. non-empty trees And we could improve further by not revealing Empty vs Node in implementation, but that would require factory methods… Demo

Iterate through data structure Iterate: process elements of data structure Sum all elements Print each element … Data Structure Order to iterate Array Forwards: 2, 1, 3, 0 Backwards: 0, 3, 1, 2 Linked List Binary Tree ??? There’s no notion of forwards or backwards 2 1 3 2 1 3 2 1 3

Iterate through data structure 5 2 8 3 7 9 Demo: add to Tree.java: public void print() { if (left != null) left.print(); if (right != null) right.print(); } Discuss when to print v; three possibilities! Discuss: What would a reasonable order be? Demo

Tree Traversals

Tree traversals Iterating through tree is aka tree traversal Well-known recursive tree traversal algorithms: Preorder Inorder Postorder Another, non-recursive: level order (later in semester)

Preorder “Pre:” process root before subtrees 1st 2nd 3rd value left right subtree

Inorder “In:” process root in-between subtrees 2nd 1st 3rd value left right subtree

Postorder “Post:” process root after subtrees 3rd 2nd 1st value left right subtree

Poll Which traversal would print out this BST in ascending order? 5 2 8 3 7 9

Example: Syntax Trees

Syntax Trees Trees can represent (Java) expressions – * 2 1 + This could be extended to other Java syntax—like classes and methods!

Traversals of expression tree – * 2 1 + Preorder traversal Visit the root Visit the left subtree Visit the right subtree - * 2 1 + 1

Traversals of expression tree – * 2 1 + Preorder traversal Postorder traversal Visit the left subtree Visit the right subtree Visit the root - * 2 1 + 1 2 1 * 1 + -

Traversals of expression tree – * 2 1 + Preorder traversal Postorder traversal Inorder traversal Visit the left subtree Visit the root Visit the right subtree - * 2 1 + 1 2 1 * 1 + - 2 * 1 - 1 +

Traversals of expression tree – * 2 1 + Preorder traversal Postorder traversal Inorder traversal - * 2 1 + 1 2 1 * 1 + - What might preorder and postorder correspond to? 2 * 1 - 1 + Original expression, except for parens

Prefix notation Function calls in most programming languages use prefix notation: e.g., add(37, 5). Aka Polish notation (PN) in honor of inventor, Polish logician Jan Łukasiewicz Some languages (Lisp, Scheme, Racket) use prefix notation for everything to make the syntax uniform. (- (* 2 1) (+ 1 0)) (define (fib n) (if (<= n 2) 1 (+ (fib (- n 1) (fib (- n 2)))))

Postfix notation Some languages (Forth, PostScript, HP calculators) use postfix notation Aka reverse Polish notation (RPN) 2 1 mul 1 0 add sub /fib { dup 3 lt { pop 1 } { dup 1 sub fib exch 2 sub fib add } ifelse } def

Postfix notation In about 1974, Gries paid $300 for an HP calculator, which had some memory and used postfix notation. Still works. In about 1993, Clarkson paid $150 for an HP calculator with more memory, buttons, and screen. Demo computation of 2 1 * 1 0 + * on a calculator In defense of postfix: no parens needed! Same can be true of prefix if # operands known in advance for each operator. Demo Mac Calculator also does RPN

Syntax trees: in code (see website for full code) public interface Expr { int eval(); String inorder(); } public class Int implements Expr { private int v; public int eval() { return v; } public String inorder() { return " " + v + " "; } } This is like our more OO implementation of trees: an interface with several implementations to customize behavior public class Add implements Expr { private Expr left, right; public int eval() { return left.eval() + right.eval(); } public String inorder() { return "(" + left.infix() + "+" + right.infix() + ")"; } (see website for full code)

Java syntax Java compiler: translates your text file (list of characters) into a syntax tree decides whether program is legal Grammar for legal programs: https://docs.oracle.com/javase/specs/jls/se8/html/jls-19.html You could use it to generate every possible Java program. (That would take forever.)

Back to Trees

Recover tree from traversal Suppose inorder is B C A E D. Can we recover the tree uniquely? Discuss.

Recover tree from traversal Suppose inorder is B C A E D. Can we recover the tree uniquely? No! C B E A D A B C E D

Recover tree from traversals Suppose inorder is B C A E D preorder is A B C D E Can we determine the tree uniquely?

Recover tree from traversals Suppose inorder is B C A E D preorder is A B C D E Can we determine the tree uniquely? Yes! What is root? Preorder tells us: A What comes before/after root A? Inorder tells us: Before: B C After: E D Now recurse! Figure out left/right subtrees using same technique.

Recover tree from traversals Suppose inorder is B C A E D preorder is A B C D E Root is A; left subtree contains B C; right contains E D Left: Inorder is B C Preorder is B C What is root? Preorder: B What is before/after B? Inorder: Before: nothing After: C Right: Inorder is E D Preorder is D E What is root? Preorder: D What is before/after D? Inorder: Before: E After: nothing

Recover tree from traversals Suppose inorder is B C A E D preorder is A B C D E Tree is A B C D E