composition announcements linked lists linked list
play

Composition Announcements Linked Lists Linked List Structure A - PowerPoint PPT Presentation

Composition Announcements Linked Lists Linked List Structure A linked list is either empty or a first value and the rest of the linked list 4 Linked List Structure A linked list is either empty or a first value and the rest of the linked list


  1. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) Note: The actual environment diagram is much more complicated. 10

  2. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 Note: The actual environment diagram is much more complicated. 10

  3. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest Note: The actual environment diagram is much more complicated. 10

  4. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s Note: The actual environment diagram is much more complicated. 10

  5. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first Note: The actual environment diagram is much more complicated. 10

  6. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first 5 Note: The actual environment diagram is much more complicated. 10

  7. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first 5 >>> s.rest.rest.rest.rest.rest.first Note: The actual environment diagram is much more complicated. 10

  8. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first 5 >>> s.rest.rest.rest.rest.rest.first 2 Note: The actual environment diagram is much more complicated. 10

  9. Linked Lists Can Change Attribute assignment statements can change first and rest attributes of a Link The rest of a linked list can contain the linked list as a sub-list >>> s = Link(1, Link(2, Link(3))) >>> s.first = 5 >>> t = s.rest >>> t.rest = s >>> s.first 5 >>> s.rest.rest.rest.rest.rest.first 2 Global frame Rest Rest First First Note: The actual 2 5 s environment diagram is much more complicated. t 10

  10. Linked List Mutation Example

  11. Adding to an Ordered List Link instance Link instance Link instance first: 1 first: 3 first: 5 s: rest: rest: rest: 12

  12. Adding to an Ordered List Link instance Link instance Link instance first: 1 first: 3 first: 5 s: rest: rest: rest: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” 12

  13. Adding to an Ordered List Link instance Link instance Link instance first: 1 first: 3 first: 5 s: rest: rest: rest: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” ( Note : If v is already in s, then don't modify s, but still return it.) 12

  14. Adding to an Ordered List Link instance Link instance Link instance first: 1 first: 3 first: 5 s: rest: rest: rest: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” ( Note : If v is already in s, then don't modify s, but still return it.) add(s, 0) 12

  15. Adding to an Ordered List Link instance Link instance Link instance first: 1 0 first: 3 first: 5 s: rest: rest: rest: Link instance first: 1 rest: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” ( Note : If v is already in s, then don't modify s, but still return it.) add(s, 0) 13

  16. Adding to an Ordered List Link instance Link instance Link instance first: 1 0 first: 3 first: 5 s: rest: rest: rest: Link instance first: 1 rest: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” ( Note : If v is already in s, then don't modify s, but still return it.) add(s, 0) add(s, 3) 13

  17. Adding to an Ordered List Link instance Link instance Link instance first: 1 0 first: 3 first: 5 s: rest: rest: rest: Link instance first: 1 rest: def add(s, v): """Add v to an ordered list s with no repeats, returning modified s.””” ( Note : If v is already in s, then don't modify s, but still return it.) add(s, 0) add(s, 3) add(s, 4) 13

  18. Adding to an Ordered List Link instance Link instance Link instance first: 1 0 first: 3 first: 5 4 s: rest: rest: rest: Link instance Link instance first: 1 first: 5 rest: rest: def add(s, v): """Add v to an ordered list s with no repeats...””” add(s, 0) add(s, 3) add(s, 4) 14

  19. Adding to an Ordered List Link instance Link instance Link instance first: 1 0 first: 3 first: 5 4 s: rest: rest: rest: Link instance Link instance first: 1 first: 5 rest: rest: def add(s, v): """Add v to an ordered list s with no repeats...””” add(s, 0) add(s, 3) add(s, 6) add(s, 4) 14

  20. Adding to an Ordered List Link instance Link instance Link instance first: 1 0 first: 3 first: 5 4 s: rest: rest: rest: Link instance Link instance first: 1 first: 5 rest: rest: def add(s, v): Link instance """Add v to an ordered list s with no repeats...""" first: 6 rest: add(s, 0) add(s, 3) add(s, 6) add(s, 4) 15

  21. Adding to a Set Represented as an Ordered List s: 16

  22. Adding to a Set Represented as an Ordered List def add(s, v): s: 16

  23. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: 16

  24. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) 16

  25. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) 16

  26. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) 16

  27. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) 16

  28. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ 16

  29. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty 16

  30. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ 16

  31. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ 16

  32. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s 16

  33. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: v s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s 16

  34. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: v Link(s.first, s.rest) s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s 16

  35. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: v Link(s.first, s.rest) s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): Link(v) s.rest = ___________________________________________________________________ elif s.first < v: ____________________________________________________________________________ return s 16

  36. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: v Link(s.first, s.rest) s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): Link(v) s.rest = ___________________________________________________________________ elif s.first < v: add(s.rest, v) ____________________________________________________________________________ return s 16

  37. Adding to a Set Represented as an Ordered List def add(s, v): """Add v to s, returning modified s.””” s: >>> s = Link(1, Link(3, Link(5))) >>> add(s, 0) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 3) Link(0, Link(1, Link(3, Link(5)))) >>> add(s, 4) Link(0, Link(1, Link(3, Link(4, Link(5))))) >>> add(s, 6) Link(0, Link(1, Link(3, Link(4, Link(5, Link(6)))))) """ assert s is not List.empty if s.first > v: v Link(s.first, s.rest) s.first, s.rest = __________________________ , _____________________________ elif s.first < v and empty(s.rest): Link(v) s.rest = ___________________________________________________________________ elif s.first < v: add(s.rest, v) ____________________________________________________________________________ return s . 16

  38. Tree Class

  39. Tree Abstraction (Review) 3 1 2 0 1 1 1 0 1 18

  40. Tree Abstraction (Review) 3 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): 18

  41. Tree Abstraction (Review) 3 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches 18

  42. Tree Abstraction (Review) Root label 3 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches 18

  43. Tree Abstraction (Review) Root label 3 Branch 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches 18

  44. Tree Abstraction (Review) Root label 3 Branch 1 2 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree 18

  45. Tree Abstraction (Review) Root label 3 Branch 1 2 (also a tree) 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree 18

  46. Tree Abstraction (Review) Root label 3 Branch 1 2 (also a tree) 0 1 1 1 0 1 Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf 18

  47. Tree Abstraction (Review) Root label 3 Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf 18

  48. Tree Abstraction (Review) Root label 3 Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root 18

  49. Tree Abstraction (Review) Root of the whole tree Root label 3 Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root 18

  50. Tree Abstraction (Review) Root of the whole tree Root label 3 Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root 18

  51. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree A tree with zero branches is called a leaf A tree starts at the root 18

  52. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf A tree starts at the root 18

  53. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf A tree starts at the root 18

  54. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root 18

  55. Tree Abstraction (Review) Root of the whole tree Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root The top node is the root node 18

  56. Tree Abstraction (Review) Root of the whole tree or Root Node Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root The top node is the root node 18

  57. Tree Abstraction (Review) Root of the whole tree or Root Node Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root The top node is the root node People often refer to labels by their locations: "each parent is the sum of its children" 18

  58. Tree Abstraction (Review) Root of the whole tree or Root Node Nodes Root label 3 Labels Root of a branch Branch 1 2 (also a tree) 0 1 1 1 Leaf 0 1 Path (also a tree) Recursive description (wooden trees): Relative description (family trees): A tree has a root label and a list of branches Each location in a tree is called a node Each branch is a tree Each node has a label that can be any value A tree with zero branches is called a leaf One node can be the parent / child of another A tree starts at the root The top node is the root node People often refer to labels by their locations: "each parent is the sum of its children" 18

  59. Tree Class A Tree has a label and a list of branches; each branch is a Tree 19

  60. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: 19

  61. Tree Class A Tree has a label and a list of branches; each branch is a Tree class Tree: def __init__(self, label, branches=[]): 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend