Problem of the day
This presentation is the property of its rightful owner.
Sponsored Links
1 / 27

Problem of the Day PowerPoint PPT Presentation


  • 84 Views
  • Uploaded on
  • Presentation posted in: General

Problem of the Day. Solve this equation by moving the numbers: 7 6 = 2 4. Problem of the Day. Solve this equation by moving the numbers: 7 6 = 2 4 7 2 = 4 9. CSC 212 – Data Structures. Lecture 34 5 : Implementing Traversals. Traversing Binary Trees.

Download Presentation

Problem of the Day

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Problem of the day

Problem of the Day

  • Solve this equation by moving the numbers:

    76 = 24


Problem of the day1

Problem of the Day

  • Solve this equation by moving the numbers:

    76 = 24

    72=49


Csc 212 data structures

CSC 212 – Data Structures

Lecture 345:Implementing Traversals


Traversing binary trees

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?


Traversal methods

Traversal Methods

  • Many traversals, differ in order nodes visited

    • Do parent then do each kid in pre-order traversal


Traversal methods1

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


Traversal methods2

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


Traversal algorithms

Traversal Algorithms

  • So, how do we do each one of these?


Traversal algorithms1

Traversal Algorithms

  • So, how do we do each one of these?implement each traversal?

    • Recursion usually answer to questions involving trees

    • The algorithm uses same approach for all three

    • Where Nodes done only difference; rest is identical


Binary tree traversal

Binary Tree Traversal

Algorithmtraverse(tree, v)

// Do node here for pre-order traversal

traverse(tree, tree.left(v));

// Do node here for in-order traversal

traverse(tree, tree.right(v));

// Do node here for post-order traversal


Important feature of any coder

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


Template method pattern

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


Binary tree traversal template

Binary Tree Traversal Template

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


Problem of the day

Oops…

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

Pre-order traversal needsprocessing here


Problem of the day

Oops…

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

Post-order traversal needsprocessing here


Better solution

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


Better solution1

Better Solution

public class EulerTour<E, R> {Rtour(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;}


Better solution2

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> v){r.out = r.left + " " + v.element() +}


Better solution3

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> v){r.out = r.left + " " + v.element() +...}


Better solution4

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 … I have no joke (post-order traversal) }void visitBelow(TourResult<String> r,Position<E> p){r.out = r.left + " " + p.element() +...}


Better solution5

Better Solution

class InOrder<E> extends EulerTour<E,String>{// execute from before is here, but not enough space… void visitLeft(TourResult<String> r,Position<E> p) { // Do nothing – its too early }void visitBelow(TourResult<String> r,Position<E> p) { // Do nothing – its too early }


Better solution6

Better Solution

class InOrder<E> extends EulerTour<E,String>{void visitRight(TourResult<String> r,Position<E> v){r.out = ""; if (getTree().hasLeft(v)) {r.out = r.left + " "; }r.out += p.element(); if (getTree().hasRight(v)) {r.out += " " + r.right; }}


Wait a minute

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?


Wait a minute1

Wait A Minute…


Cheating to win

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

    • executemethod returns a Sequence


Your turn

Your Turn

  • Get into your groups and complete activity


For next lecture

For Next Lecture

  • Read parts of GT 7.1 & 7.3 for Monday

    • 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 posted & due next Tuesday (after break)

  • Programming Assignment #3 available today


  • Login