linked vs. sequential allocation linked structures goal ...€¦ · 2/7/13 1 linked structures...

21
2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes.' " "Oh, that's the name of the song, is it?" Alice said, trying to feel interested. "No, you don't understand," the Knight said, looking a little vexed. "That's what the name is called. The name really is 'The Aged Aged Man.' " "Then I ought to have said 'That's what the song is called' ?" Alice corrected herself. "No, you oughtn't: that's quite another thing! The song is called 'Ways and Means,' but that is only what it's called, you know!" "Well, what is the song, then?" said Alice, who was by this time completely bewildered. "I was coming to that," the Knight said. "The song really is 'A- sitting On A Gate,' and the tune's my own invention." 2 Linked vs. Sequential Allocation Goal: process a collection of objects. Sequential allocation: put object one after another. TOY: consecutive memory cells. Java: array of objects. Linked allocation: include in each object a link to the next one. TOY: link is memory address of next object. Java: link is reference to next object. Key distinctions. Sequential allocation: random access, fixed size. Linked allocation: sequential access, variable size. 3 Linked list of strings. A recursive data structure. A string plus a pointer to another linked list (or empty list). Unwind recursion: linked list is a sequence of strings. Linked lists in Java. Easy to define as a Java class. A reference to a String. A reference to another List. Use special value null to terminate list. Linked Lists public class List { private String codon; private List next; } TAC null CCA ATG List x 5 Linked List Demo List a = new List(); a.name = ATG"; a.next = null; List b = new List(); b.name = CCA"; b.next = a; List c = new List(); c.name = TAC"; c.next = b; main memory addr value C0 null C1 null C2 0 C3 0 C4 0 C5 0 C6 0 C7 0 C8 0 C9 0 CA 0 CB 0

Upload: others

Post on 19-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

1

Linked Structures

Lewis Caroll Through the Looking Glass

"The name of the song is called 'Haddocks' Eyes.' " "Oh, that's the name of the song, is it?" Alice said, trying to feel interested. "No, you don't understand," the Knight said, looking a little vexed. "That's what the name is called. The name really is 'The Aged Aged Man.' " "Then I ought to have said 'That's what the song is called' ?" Alice corrected herself. "No, you oughtn't: that's quite another thing! The song is called 'Ways and Means,' but that is only what it's called, you know!" "Well, what is the song, then?" said Alice, who was by this time completely bewildered. "I was coming to that," the Knight said. "The song really is 'A-sitting On A Gate,' and the tune's my own invention."

2

Linked vs. Sequential Allocation

Goal: process a collection of objects. Sequential allocation: put object one after another.   TOY: consecutive memory cells.   Java: array of objects.

Linked allocation: include in each object a link to the next one.   TOY: link is memory address of next object.   Java: link is reference to next object.

Key distinctions.   Sequential allocation: random access, fixed size.   Linked allocation: sequential access, variable size.

3

Linked list of strings.   A recursive data structure.   A string plus a pointer to another linked list (or empty list).   Unwind recursion: linked list is a sequence of strings.

Linked lists in Java.   Easy to define as a Java class.   A reference to a String.   A reference to another List.   Use special value null to terminate list.

Linked Lists

public class List { private String codon; private List next; }

TAC null CCA ATG

List x

5

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

main memory

addr value

C0 null

C1 null

C2 0

C3 0

C4 0

C5 0

C6 0

C7 0

C8 0

C9 0

CA 0

CB 0

Page 2: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

2

6

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 null

C1 null

C2 0

C3 0

C4 0

C5 0

C6 0

C7 0

C8 0

C9 0

CA 0

CB 0

main memory

a C0

registers

a

List

7

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 “ATG"

C1 null

C2 0

C3 0

C4 0

C5 0

C6 0

C7 0

C8 0

C9 0

CA 0

CB 0

main memory

a C0

registers ATG

a

List

8

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 “ATG"

C1 null

C2 0

C3 0

C4 0

C5 0

C6 0

C7 0

C8 0

C9 0

CA 0

CB 0

main memory

a C0

registers ATG null

a

List

9

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 “ATG"

C1 null

C2 0

C3 0

C4 0

C5 0

C6 0

C7 0

C8 0

C9 null

CA null

CB 0

main memory

a C0

registers

b C9

List

ATG null

b a

List

Page 3: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

3

10

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 “ATG"

C1 null

C2 0

C3 0

C4 0

C5 0

C6 0

C7 0

C8 0

C9 “CCA"

CA null

CB 0

main memory registers ATG null CCA

b a

a C0

b C9

List

List

11

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 “ATG"

C1 null

C2 0

C3 0

C4 0

C5 0

C6 0

C7 0

C8 0

C9 “CCA"

CA C0

CB 0

main memory registers ATG null CCA

b a

a C0

b C9

List

List

12

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 “ATG"

C1 null

C2 0

C3 null

C4 null

C5 0

C6 0

C7 0

C8 0

C9 “CCA"

CA C0

CB 0

main memory

a C0

registers

b C9

List

c C3

List

ATG null CCA

c b a

List

13

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 “ATG"

C1 null

C2 0

C3 “TAC"

C4 null

C5 0

C6 0

C7 0

C8 0

C9 “CCA"

CA C0

CB 0

main memory

a C0

registers

b C9

List

c C3

List

ATG null CCA TAC

c b a

List

Page 4: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

4

14

Linked List Demo

List a = new List(); a.name = “ATG"; a.next = null; List b = new List(); b.name = “CCA"; b.next = a; List c = new List(); c.name = “TAC"; c.next = b;

addr value

C0 “ATG"

C1 null

C2 0

C3 “TAC"

C4 C9

C5 0

C6 0

C7 0

C8 0

C9 “CCA"

CA C0

CB 0

main memory

a C0

registers

b C9

List

c C3

List

ATG null CCA TAC

c b a

List

15

Traversing a List

Paradigm for traversing a null-terminated linked list.

for (List x = c; x != null; x = x.next) { System.out.println(x.name); }

TAC null CCA ATG

% java List x

16

Traversing a List

Paradigm for traversing a null-terminated linked list.

for (List x = c; x != null; x = x.next) { System.out.println(x.name); }

TAC null CCA ATG

x % java List ATG

17

Traversing a List

Paradigm for traversing a null-terminated linked list.

for (List x = c; x != null; x = x.next) { System.out.println(x.name); }

TAC null CCA ATG

% java List ATG x

Page 5: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

5

18

Traversing a List

Paradigm for traversing a null-terminated linked list.

for (List x = c; x != null; x = x.next) { System.out.println(x.name); }

TAC null CCA ATG

x % java List ATG CCA

19

Traversing a List

Paradigm for traversing a null-terminated linked list.

for (List x = c; x != null; x = x.next) { System.out.println(x.name); }

TAC null CCA ATG

% java List ATG CCA

x

20

Traversing a List

Paradigm for traversing a null-terminated linked list.

for (List x = c; x != null; x = x.next) { System.out.println(x.name); }

TAC null CCA ATG

x % java List ATG CCA TAC

21

Traversing a List

Paradigm for traversing a null-terminated linked list.

for (List x = c; x != null; x = x.next) { System.out.println(x.name); }

TAC null CCA ATG

% java List ATG CCA TAC

x

Page 6: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

6

22

Null Terminated Linked List and Circular Linked List

first

first

null-terminated linked list

circular linked list

23

Delete, Insert

z y x

z y x

y x

t

y x

Stacks and Queues

25

Stack and Queue ADTs

Fundamental data type.   Set of operations (add, remove, test if empty) on generic data.   Intent is clear when we insert.   Which object do we remove?

Stack.   Remove the object most recently added. ("last in first out")   Analogy: cafeteria trays.

Queue.   Remove the object least recently added. ("first in first out")   Analogy: Supermarket line.

Multiset.   Remove any object.   CS professor calls on arbitrary student.

Page 7: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

7

26

Queue

Queue operations.   enqueue Insert a new object onto queue.   dequeue Delete and return the object least recently added.   isEmpty Is the queue empty?

public static void main(String[] args) { Queue q = new Queue(); q.enqueue("Vertigo"); q.enqueue("Just Lose It"); q.enqueue("Pieces of Me"); q.enqueue("Pieces of Me"); System.out.println(q.dequeue()); q.enqueue("Drop It Like It's Hot"); while(!q.isEmpty()) System.out.println(q.dequeue()); }

A simple queue client

27

More Applications of Queues

Real world applications.   iTunes playlist.   Dispensing requests on a shared resource (printer, processor).   Asynchronous data transfer (file IO, pipes, sockets).   Data buffers (iPod, TiVo).   Graph processing (stay tuned).

Simulations of the real world.   Traffic analysis of Lincoln tunnel.   Waiting times of customers in McDonalds .   Determining number of cashiers to have at a supermarket.

28

Queue: Linked List Implementation

Linked list implementation.   Maintain linked list of elements.   Let first be reference to first node on list.   Let last be reference last node on list.

first

now is the time

last

29

Queue: Linked List Implementation

Linked list implementation.   Maintain linked list of elements.   Let first be reference to first node on list.   Let last be reference last node on list.

Insert.

first

now is the time

last

List x = new List(); x.item = "for"; last.next = x; last = x;

x

Page 8: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

8

30

Queue: Linked List Implementation

Linked list implementation.   Maintain linked list of elements.   Let first be reference to first node on list.   Let last be reference last node on list.

Insert.

first

now is the time

last

for

List x = new List(); x.item = "for"; last.next = x; last = x;

x

31

Queue: Linked List Implementation

Linked list implementation.   Maintain linked list of elements.   Let first be reference to first node on list.   Let last be reference last node on list.

Insert.

first

now is the time

last

for

List x = new List(); x.item = "for"; last.next = x; last = x;

x

32

Queue: Linked List Implementation

Linked list implementation.   Maintain linked list of elements.   Let first be reference to first node on list.   Let last be reference last node on list.

Insert.

first

now is the time

last

for

List x = new List(); x.item = "for"; last.next = x; last = x;

33

Queue: Linked List Implementation

Linked list implementation.   Maintain linked list of elements.   Let first be reference to first node on list.   Let last be reference last node on list.

Delete.

first

now is the time

last

for

String val = first.item; first = first.next; return val;

now val

Page 9: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

9

34

Queue: Linked List Implementation

Linked list implementation.   Maintain linked list of elements.   Let first be reference to first node on list.   Let last be reference last node on list.

Delete.

first

now is the time

last

for

String val = first.item; first = first.next; return val;

now val

35

Queue: Linked List Implementation

Linked list implementation.   Maintain linked list of elements.   Let first be reference to first node on list.   Let last be reference last node on list.

Delete.

first

now is the time

last

for

String val = first.item; first = first.next; return val;

now val

36

Queue: Linked List Implementation

public class Queue { private List first; private List last; private class List { String item; List next; } public boolean isEmpty() { return (first == null); } public void enqueue(String anItem) { List x = new List(); x.item = anItem; x.next = null; if (isEmpty()) { first = x; last = x; } else { last.next = x; last = x; } } public String dequeue() { String val = first.item; first = first.next; return val; } }

reference to first element in queue reference to last element in queue

delete first element

nested class

37

Binary tree.   Organize homogeneous collection of values (all same type).   Associate two pointers with each value.   Use pointers to access each branch of the tree.

Binary Trees

Charles

Elizabeth II Philip

Elizabeth George VI Andrew Alice

George I Olga Louis Victoria George V Mary Claude Celia

dad mom

root

Page 10: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

10

38

Java implementation of a binary tree of strings is:   A reference to the String.   A reference to the left Tree.   A reference to the right Tree.

Binary Tree: Java Implementation

public class Tree { private String s; private Tree left; private Tree right; }

39

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

40

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

41

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null

Page 11: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

11

42

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

43

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

44

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

45

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null

Page 12: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

12

46

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null null

47

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null null

48

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null null

Philip

49

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null null

Philip

Page 13: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

13

50

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null null

Philip

51

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null null

Philip

52

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null null

Philip

Charles

53

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Andrew

null null

Alice

null null

Philip

Charles

Page 14: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

14

54

Binary Tree Demo

Tree a = new Tree(); a.name = "Andrew"; a.dad = null; a.mom = null; Tree b = new Tree(); b.name = "Alice"; b.dad = null; b.mom = null; Tree c = new Tree(); c.name = "Philip"; c.dad = a; c.mom = b; Tree d = new Tree(); d.name = "Charles"; d.dad = c; d.mom = null;

Charles

null Philip

Andrew Alice

null null null null

55

Array vs. Linked Representation

addr value

C0 "Carol"

C1 "Bob"

C2 "Alice"

C3 0

C4 0

C5 0

C6 0

C7 0

C8 0

C9 0

CA 0

CB 0

main memory

addr value

C0 "Alice"

C1 null

C2 0

C3 "Carol"

C4 C9

C5 0

C6 0

C7 0

C8 0

C9 "Bob"

CA C0

CB 0

main memory

a C0

registers

Node

b C9

c C3

Node

Node

a C0

String[]

56

Parse Tree Evaluation: Java Implementation

Parse tree.   Abstract representation of expression.   Applications: compilers, computational linguistics.

Evaluating a parse tree.   If string is an integer return it.   Else, evaluate both subtrees recursively and return sum or product.

((10 * 12) + (7)) = 127

public class ParseTree { private String s; private ParseTree left; private ParseTree right; public int eval() { if (s.equals("+")) return left.eval() + right.eval(); else if (s.equals("*")) return left.eval() * right.eval(); else return Integer.parseInt(s); } }

represent data as a string, e.g., "+" or "1234" left subtree right subtree

convert from string to integer

+

* 7

10 12

57

Preorder Traversal

How do we print out the information?   Print string.   Print left subtree recursively.   Print right subtree recursively.

No parentheses!

public String toString() { if (s.equals("+") || s.equals("*")) return s + " " + left + " " + right; else return s; }

Preorder traversal: * + + 4 5 * 6 7 8

*

+ 8

+ *

6 7 4 5

((4 + 5) + (6 * 7)) * 8

Page 15: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

15

58

Parse Tree Construction

How do we read it back in and create the tree?   Read string from standard input.   If + or * operator, construct left and right subtrees recursively.

public ParseTree() { s = StdIn.readString(); if (s.equals("+") || s.equals("*")) { left = new ParseTree(); right = new ParseTree(); } }

% java ParseTree * + + 4 5 * 6 7 8 408

*

+ 8

+ *

6 7 4 5

((4 + 5) + (6 * 7)) * 8

constructor

59

Parse Tree Demo

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

10

null null

12

null null

* 7

null null

+

a b

c d

e

60

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

a

61

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10 a

Page 16: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

16

62

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null

a

63

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

a

64

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

a b

65

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12 a b

Page 17: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

17

66

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null

a b

67

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

a b

68

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

a b

c

69

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

*

a b

c

Page 18: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

18

70

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

*

a b

c

71

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

*

a b

c

72

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

*

a b

c d

73

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

* 7

a b

c d

Page 19: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

19

74

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

* 7

null a b

c d

75

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

* 7

null null a b

c d

76

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

* 7

null null a b

c d

e

77

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

* 7

null null

+

a b

c d

e

Page 20: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

20

78

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

* 7

null null

+

a b

c d

e

79

Tree a = new Tree(); a.s = "10"; a.left = null; a.right = null; Tree b = new Tree(); b.s = "12"; b.left = null; b.right = null; Tree c = new Tree(); c.s = "*"; c.left = a; c.right = b; Tree d = new Tree(); d.s = "7"; d.left = null; d.right = null; Tree e = new Tree(); e.s = "+"; e.left = c; e.right = d;

Parse Tree Demo

10

null null

12

null null

* 7

null null

+

a b

c d

e

80

Other Types of Trees

Other types of trees.   Family tree.   Parse tree.   Unix file hierarchy.

/

bin lib u etc

zrnye cs126

files

mandel stock

Point.java

submit

aaclarke

tsp

TSP.java tsp13509.txt

grades

81

Other Types of Trees

Other types of trees.   Family tree.   Parse tree.   Unix file hierarchy.   Phylogeny tree.

Page 21: Linked vs. Sequential Allocation Linked Structures Goal ...€¦ · 2/7/13 1 Linked Structures Lewis Caroll Through the Looking Glass "The name of the song is called 'Haddocks' Eyes

2/7/13

21

82

Other Types of Trees

Other types of trees.   Family tree.   Parse tree.   Unix file hierarchy.   Phylogeny tree.   GUI containment hierarchy.

Reference: http://java.sun.com/docs/books/tutorial/uiswing/overview/anatomy.html 83

Other Types of Trees

Other types of trees.   Family tree.   Parse tree.   Unix file hierarchy.   Phylogeny tree.   GUI containment hierarchy.   Binary search trees.   NCAA basketball tournament.   . . .

84

Linked Structures Overview

Linked structures. Simple abstraction for customized access to data. Singly linked structures.   Linked list.   Circular linked list.   Parent-link tree.

Doubly linked structures.   Binary tree.   Doubly linked circular list.

null

85

Conclusions

Sequential allocation: supports indexing, fixed size. Linked allocation: variable size, supports sequential access. Linked structures are a central programming abstraction.   Linked lists.   Binary trees.   Graphs.   Sparse matrices. 'Haddocks' Eyes'

'The Aged Aged Man'

'Ways and Means'

'A-sitting On A Gate'

Alice should have done this!