Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 12: Circular Linked Lists And Graphs: Lists and Trees That Loop.

Similar presentations


Presentation on theme: "Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 12: Circular Linked Lists And Graphs: Lists and Trees That Loop."— Presentation transcript:

1 Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 12: Circular Linked Lists And Graphs: Lists and Trees That Loop

2 Chapter Learning Goals

3 Story Cell animation An example of when it’s useful to have a list reference itself. Graphs: Trees that reference themselves

4 Mario didn’t *really* move

5 Characters from MediaSources

6 Walking (roughly, like Frankenstein)

7 As a Circular Linked List

8 Can we do this? Sure! Why not? There are lots of real information in the world that “cycles” or “loops” or “connects” Maps of roads Electrical diagrams Pipe diagrams Subway routes The trick is Don’t try to traverse the list!

9 Creating a WalkingKid

10 WalkingKid movie

11 WalkingKid Class

12 WalkingKid circular list

13 Walking

14 Walking (part 2)

15 Try it! How would you walk right-to-left? Flip the images Start on the right Decrement x instead of incrementing it

16 Try it! Maybe this would be better? Just change the constructor and the rest should work!

17 CharNode: A safe picture linked list > CharNode ch = new CharNode(new Picture(20,20)); > ch.last() Don't try to find last() from a circular list! CharNode with picture: Picture, filename null height 20 width 20 > ch.remove(new CharNode(new Picture(20,20))) Very dangerous to try to remove a node from this list! Disable those methods in the LLNode that could cause traversals!

18 CharNode class /* * CharNode is a class representing a drawn picture * that is one in a sequence of Pictures to * use for a given character. Don't ever try to traverse this one! **/ public class CharNode extends LLNode { /** * The picture I'm associated with **/ public Picture myPict; /* * Make me with this picture * @param pict the Picture I'm associated with **/ public CharNode(Picture pict){ super(); // Call superclass constructor myPict = pict; } Nothing much surprising here.

19 Preventing bad things from happening /** * Don't try to remove() from a circular list! **/ public void remove(LLNode node){ System.out.println("Very dangerous to try to remove a node from this list!"); } /** * Don't try to get the last() from a circular list! **/ public LLNode last() { System.out.println("Don't try to find last() from a circular list!"); return this; } These override the code in LLNode

20 NON-recursive toString() /** * Method to return a string with information * about this node */ public String toString() { return "CharNode with picture: "+myPict; }

21 Just draw as bluescreen /* * Use the given turtle to draw oneself * @param pen the Turtle to draw with **/ public void drawWith(Turtle pen){ // Assume that we're at the lower-left corner pen.setHeading(0); pen.forward(myPict.getHeight()); Picture bg = pen.getPicture(); myPict.bluescreen(bg,pen.getXPos(),pen.getYPos()); }

22 How could we make this work without errors? Lots of ways! Here’s two: Have a visited flag. Mark it true when visited. Don’t visit already visited. Need a reset() method. Have two next links. One for rendering. The other for walking all nodes for traversing for add() or remove().

23 If we can have lists that loop, can we have trees that loop? Sure! These are called graphs. A graph is a series of points (vertices) and edges (lines) that connect them. If there is a direction to the line, it’s called directed. If not, undirected. If a graph ever loops (cycles), it’s called cyclic. There can also be costs associated with edges.

24 Example Graphs MARTA subway It’s an acyclic graph, where vertices are stations and the north-south and east-west lines are the edges. Cost might be the time between stations. Interstate system It’s a cyclic graph where vertices are intersections or exits or cities (depending on how you want to think about it). Cost is distance between vertices.

25 What can we do with graphs? Find all vertices that are reachable from a given vertex. “Can I get there from here?” Not always true—think about wiring for a house with a short in it. Find a spanning tree—an acyclic graph (a tree!) that touches all vertices. A minimal spanning tree does this while minimizing cost.

26 A Map as a Graph

27 Adding weights

28 Getting a minimal spanning tree

29

30

31

32

33

34 Question: Find the minimal spanning tree

35 Answer


Download ppt "Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 12: Circular Linked Lists And Graphs: Lists and Trees That Loop."

Similar presentations


Ads by Google