csc 212 – data structures. traversing binary trees trees are another collection of data position...

18
LECTURE 35: IMPLEMENTING TRAVERSALS CSC 212 – Data Structures

Upload: frank-moore

Post on 24-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

LECTURE 35:IMPLEMENTING TRAVERSALS

CSC 212 – Data Structures

Page 2: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Traversing Binary Trees

Trees are another Collection of data Positions required in the Tree ADT methods ADT uses generic type to specify type of

element Want to iterate through tree’s elements

This is not optional; Trees are Iterable Could use any traversal algorithm; choose

logical one But how do we implement traversals & Iterator?

Page 3: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order

traversal

Page 4: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order

traversal Post-order traversal does kids before doing

parents

Page 5: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Traversal Methods

Many traversals, differ in order nodes visited Do parent then do each kid in pre-order

traversal Post-order traversal does kids before doing

parents Do left kid, parent, then right kid in in-order

traversal

Page 6: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Traversal Algorithms

So, how do we do each one of these?

Page 7: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Binary Tree Traversal

Algorithm traverse(tree, v)// Do node here for pre-order traversaltraverse(tree, tree.left(v));// Do node here for in-order traversaltraverse(tree, tree.right(v));// Do node here for post-order traversal

Page 8: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Important Feature of Any Coder Nearly all of the great coders are

inherently lazy Hope for you all -- you have this part

completed Unfortunately, true laziness requires lot

of work Debugging code wastes time could be

websurfing To make small change, more time lost

rewriting code Small amount of planning save lots of

time later

Page 9: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Template Method Pattern

Common design pattern used to simplify code Implement class of algorithms using an

abstract class Class leaves all actual work in abstract

methods Subclasses written for each member

algorithm To make concrete, each overrides abstract

method Method performs specific actions for that

algorithm New algorithms are very quick & easy

to write Most bugs already found & fixed, so less

work here Leaves only actions & actions usually are

small

Page 10: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Binary Tree Traversal Templatepublic class EulerTour<E, R> {

abstract void visit(Position<E> p);public R tour(BinaryTree<E> t, Position<E> p) { TourResult<R> r = new TourResult<R>(); if (t.hasLeft(p)) { r.left = tour(t, t.left(p)); } visit(p); if (t.hasRight(p)) { r.right = tour(t, t.right(p)); } return r.out;}

Page 11: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Oops…

public class EulerTour<E, R> {abstract void visit(Position<E> p);public R tour(BinaryTree<E> t, Position<E> p) { TourResult<R> r = new TourResult<R>(); if (t.hasLeft(p)) { r.left = tour(t, t.left(p)); } visit(p); if (t.hasRight(p)) { r.right = tour(t, t.right(p)); } return r.out;}

Pre-order traversal needs visit here

Page 12: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

More Oops…

public class EulerTour<E, R> {abstract void visit(Position<E> p);public R tour(BinaryTree<E> t, Position<E> p) { TourResult<R> r = new TourResult<R>(); if (t.hasLeft(p)) { r.left = tour(t, t.left(p)); } visit(p); if (t.hasRight(p)) { r.right = tour(t, t.right(p)); } return r.out;}

Post-order traversal needs visit here

Page 13: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Better Solution

public class Traversal<E, R> {// Perform actions before kids processedabstract void visitLeft(Position<E> p);

// Perform actions after kids processedabstract void visitRight(Position<E> p);

// Perform actions between when kids processedabstract void visitBelow(Position<E> p);

Page 14: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Better Solution

public class EulerTour<E, R> {public R tour(BinaryTree<E> t, Position<E> p) { TourResult<R> r = new TourResult<R>(); visitLeft(p); if (t.hasLeft(p)) { r.left = tour(t, t.left(p)); } visitBelow(p); if (t.hasRight(p)) { r.right = tour(t, t.right(p)); } visitRight(p); return r.out;}

Page 15: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Better Solution

class InOrder<E> extends EulerTour<E,String>{public String execute(BinaryTree<E> t) { init(t); return eulerTour(t.root());}void visitLeft(TourResult<String> r,Position<E> p){ // Do nothing – we are not Woody Allen (pre-order traversal) }void visitRight(TourResult<String> r,Position<E> p){ // Do nothing – we are not Michael Jackson (post-order traversal) }void visitBelow(TourResult<String> r,Position<E> p){

r.out = r.left + " " + p.element() + }

Page 16: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Wait A Minute…

Lecture began talking about Tree's Iterator Implementing traversals discussed for rest

of lecture I may not know much, but traversals != Iterator

How to implement next() & hasNext() Recursion can't be used, since calls occur

over time How can we reuse these ideas to make an

iterator? Was this a ruse; is lecture completely

worthless?

Page 17: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

Cheating To Win

next() & hasNext() very hard to implement Much easier for Sequences (which are Iterable)

In fact, we have already done this for Sequence

Use that Iterator by adding elements to Sequence

Define new subclass for this type of traversal Valid to build entire Iterator all at one go Add & combine Sequences in visit*

methods execute method returns a Sequence

Page 18: CSC 212 – Data Structures. Traversing Binary Trees  Trees are another Collection of data  Position s required in the Tree ADT methods  ADT uses generic

For Next Lecture

Read parts of GT 7.1 & 7.3 for Wednesday How do we implement a tree? Binary trees are similar; how do we

implement them? I loved CSC111; can’t we use arrays for

binary trees?

Week #12 assignment posted & due tomorrow

Programming Assignment #3 available today