1 / 32

CSE 326: Data Structures Lists

CSE 326: Data Structures Lists. Lecture 5: Wednesday, Jan 15, 2003. Outline. Finish lists Stacks and queues Begin trees Remember: reading assignment Chapter 3 this week (including weekend). Iterators. Introduce a new public class to explicitly represent a position in a list Then:.

shaw
Télécharger la présentation

CSE 326: Data Structures Lists

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 326: Data Structures Lists Lecture 5: Wednesday, Jan 15, 2003

  2. Outline • Finish lists • Stacks and queues • Begin trees Remember: reading assignment Chapter 3 this week (including weekend)

  3. Iterators • Introduce a new public class to explicitly represent a position in a list • Then: public class LinkedListItr {ListNode current; public Object retrieve() { return current.element; } public void advance() { current = current.next; } public boolean pastEnd() { return current == NULL; } LinkedListItr i; for (i = mylist.first(); !i.pastEnd(); i.advance){ do something on each v.retrieve() }

  4. Abstract Iterators • Iterators can also be defined for an array implementation of lists: • We can create an abstract iterator that works for both linked list and array implements of List public class ArrayListItr { Object [] data; integer current; public Object retrieve() { return data[current]; } public void advance() { current = current+1; public boolean pastEnd() {return current>size;}}

  5. Abstract Iterator • Why do this? abstract class ListItr { abstract Object retrieve(); abstract void advance(); abstract boolean pastEnd(); } class LinkedListItr extends ListItr { … } class ArrayListItr extends ListItr { … }

  6. Another Array Implementation of Linked Lists 1 7 9 2 3 4 5 6 8 10 Data F O A R N R T Next 3 8 6 4 -1 10 5 First = 2 • How do we implement • Delete(position) ? • Insert(element, position)?

  7. Free Cell Management 1 7 9 2 3 4 5 6 8 10 Data F O A R N R T Next 7 9 0 3 8 6 4 -1 10 5 First = 2 Free = 1 When an item is removed from the list, must “reclaim” the unused cell for later use Can use same array to manage a second list of unused cells

  8. Memory Management • Keeping a free cell list is an example of a memory management strategy • How is memory managed in C? • C++? • Java?

  9. ( 5 2 3 ) 5 + 2x + 3x2 ( 7 8 ) 7 + 8x ( 3 0 2 ) 3 + x2 List ADT Polynomial ADT Possible linked list implementation: Ai is the coefficient of the xi-1 term: Problem?

  10. 4 + 3x2001 ( 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 )

  11. 4 0 3 2001 Sparse Vector Data Structure:4 + 3x2001 (<4 0> <2001 3>)

  12. Addition of Two Polynomials? Complexity? 15+10x50+3x1200 p 15 0 10 50 3 1200 5+30x50+4x100 q 5 0 30 50 4 100

  13. Addition of Two Polynomials • One pass down each list: (n+m) 15+10x50+3x1200 p 15 0 10 50 3 1200 5+30x50+4x100 q 5 0 30 50 4 100 r 20 0 40 50 4 100 3 1200

  14. Sparse Matrices • How could we represent this compactly? ( <row (<column data> <column data> …)> <row (<column data> <column data> …)> … ) ( <1 (<1 18> <2 33>)> <4 (<4 99>)> <5 (<5 27>) )

  15. a b c a b c Various List Implementations • Without header: • With header: • Variation: a b c Advantage of the header: easier to insert/delete at the beginning/end

  16. b a c Various List Implementations • Simply linked: • Double linked: a b c Advantage of double linked: delete is easier; also: traverse backwards

  17. Various List Implementations • Open: • Circular: a b c a b c Advantage: none, really, but circular lists are popular

  18. Old list 2 5 8 Newlist 2 5 6 Copy semantics v.s. destructive semantics Example: insert x in a sorted list: • Copy semantics (assume no header) public ListNode insert(int x, ListNode l) { if (l==null || l.element > x) /* insert here */ return new ListNode(x, l); else /* copy here */ return new ListNode(l.element, insert(x,l.next))

  19. Copy semantics v.s. destructive semantics • Destructive semantics (assume header now) public void insert(int x, LinkList h) { if (h.header==null || h.header.element > x) header = new ListNode(x, h.header); else { ListNode l = h.header; while (l.next != null && l.next.element < x) l = l.next; l.next = new ListNode(x, l.next); } 2 5 8 6

  20. Summary: Some Complexities

  21. The Stack ADT Mathematical definition: • The operators: • The axioms: newStack :  stackpush: stack  element  stack top : stack  element pop : stack  stack isEmpty : stack  boolean constructors top(newStack) = error top(push(s, x)) = x pop(newStack) = error pop(push(s,x)) = s isEmpty(newStack) = true isEmpty(push(s, x)) = false Express eachoperator in terms ofthe constructors

  22. Stack Implementations • Implementation with arrays: • newStack, push, pop, top, isEmpty = O(1) • Issue: size of the array is a limitation • Solution: make it stretchy ! top

  23. a b c Stack Implementations • Implementation with linked list: • newStack, push, pop, top, isEmpty = O(1) Where is the top of the stack ?

  24. Applications of Stacks • Tree traversals, graph traversals • Will see in coming lectures • Parsing expressions • Read the book !

  25. The Queue ADT Mathematical definition: • Operators: • The axioms: newQueue :  queueenqueue: queue  element  queue front : queue  element dequeue : queue  stack isEmpty : queue  boolean constructors front(newQueue) = error front(enqueue(q, x) = if isEmpty(q) then x else front(q) dequeue(newQueue) = error dequeue(enqueue(q, x) = if isEmpty(q) then q else dequeue(q) isEmpty(newQueue) = true isEmpty(enqueue) = false Express eachoperator in terms ofthe constructors

  26. Queue Implementations • Implementation with “circular” arrays: • newQueue, enqueue, dequeue, front, isEmpty = O(1) • Stretchy arrays, of course  front rear

  27. Queue Implementations • Implementation with linked list: • newQueue, enqueue, dequeue, front, isEmpty = O(1) a b c Where is the front and where is the rear ?

  28. Applications of Queues • Tree traversals, graph traversals • Will see in coming lectures • Wherever fairness is needed: • Printer queues • Packets in a network • http requests at a web server • Etc.

  29. Trees Material: Weiss Chapter 4 • N-ary trees • Binary Search Trees • AVL Trees • Splay Trees

  30. Tree Jargon • Nodes: A, B, …, F • Root node: A • Leaf nodes: B, E, F, D • Edges: (A,B), (A,C), …, (C, F) • Path: sequence of nodes connected by edges. • Path examples: (B), (A,B),(A,C), (A,C,E), (A,C,F), (C), etc A C D B F E Questions. A tree has N nodes.How many edges does it have ? How many paths ?

  31. Tree Jargon • Length of a path = number of edges • Depth of a node x = length of path from root to x • Height of node x = length of longest path from x to a leaf • Depth and height of tree = height of root • The label of a node: A, B, C, … depth=0, height = 2 A C D B F depth = 2, height=0 E

  32. Definition and Tree Trivia Graph-theoretic definition of a Tree: A tree is a graph for which there exists a node, called root, such that: -- for any node x, there exists exactly one path from the root to x Recursive Definition of a Tree: A tree is either: a. empty, or b. it has a node called the root, followed by zero or more trees called subtrees

More Related