Midterm Review2.1

Midterm Review2.1 - Euler Tour Traversal Generic traversal...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Euler Tour Traversal Generic traversal of a binary tree Includes as special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times: on the left (preorder) from below (inorder) on the right (postorder) + L R B 2 3 5 CSE 2011 Prof. J. Elder 2 1 - 51 - Last Updated: 1/7/10 10:14 AM Template Method Pattern Generic algorithm that can be specialized by redefining certain steps Implemented by means of an abstract Java class Visit methods that can be redefined by subclasses Template method eulerTour Recursively called on the left and right children A Result object with fields leftResult, rightResult and finalResult keeps track of the output of the recursive calls to eulerTour CSE 2011 Prof. J. Elder public abstract class EulerTour { protected BinaryTree tree; protected void visitExternal(Position p, Result r) { } protected void visitLeft(Position p, Result r) { } protected void visitBelow(Position p, Result r) { } protected void visitRight(Position p, Result r) { } protected Object eulerTour(Position p) { Result r = new Result(); if tree.isExternal(p) { visitExternal(p, r); } else { visitLeft(p, r); r.leftResult = eulerTour(tree.left(p)); visitBelow(p, r); r.rightResult = eulerTour(tree.right(p)); visitRight(p, r); return r.finalResult; }… - 52 - Last Updated: 1/7/10 10:14 AM Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT insert(k, x) inserts an entry with key k and value x removeMin() removes and returns the entry with smallest key Additional methods min() returns, but does not remove, an entry with smallest key size(), isEmpty() Applications: Process scheduling Standby flyers CSE 2011 Prof. J. Elder - 53 - Last Updated: 1/7/10 10:14 AM Total Order Relations Keys in a priority queue can be arbitrary objects on which an order is defined Mathematical concept of total order relation Reflexive property: xx Antisymmetric property: xy yx x=y Two distinct entries in a priority queue can have the same key CSE 2011 Prof. J. Elder Transitive property: xy yz xz - 54 - Last Updated: 1/7/10 10:14 AM Entry ADT An entry in a priority queue is simply a keyvalue pair As a Java interface: /** * Interface for a key-value Methods: * pair entry key(): returns the key for this entry **/ public interface Entry { value(): returns the value for this entry public Object key(); public Object value(); } CSE 2011 Prof. J. Elder - 55 - Last Updated: 1/7/10 10:14 AM ...
View Full Document

Ask a homework question - tutors are online