1 / 30

CS210- Lecture 8 Jun 16, 2005

CS210- Lecture 8 Jun 16, 2005. Agenda Circular Linked List Lists Doubly linked list implementation of Lists Sequences Trees Tree Traversals . Circular Linked List (Project 2). tail. add(3). current. 3. tail. add(4). current. 4. 3. current. tail. 4. 5. 3. add(5).

jhardesty
Download Presentation

CS210- Lecture 8 Jun 16, 2005

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. CS210- Lecture 8Jun 16, 2005 • Agenda • Circular Linked List • Lists • Doubly linked list implementation of Lists • Sequences • Trees • Tree Traversals CS210-Summer 2005, Lecture 8

  2. Circular Linked List (Project 2) tail add(3) current 3 tail add(4) current 4 3 current tail 4 5 3 add(5) CS210-Summer 2005, Lecture 8

  3. Circular Linked List (Project 2) current tail add(6) 4 5 6 3 current tail remove() 4 5 3 current tail add(999) 999 4 5 3 CS210-Summer 2005, Lecture 8

  4. Circular Linked List (Project 2) current tail remove() 4 5 3 current tail add(123) 4 123 5 3 current tail 4 123 456 5 3 add(456) CS210-Summer 2005, Lecture 8

  5. Circular Linked List (Project 2) current tail reset() 4 123 456 5 3 current tail 4 123 456 5 3 advance() 4 times current tail remove() 123 456 5 4 CS210-Summer 2005, Lecture 8

  6. Josephus Problem current tail 2 3 4 5 6 7 8 9 10 1 current tail Advance once 2 3 4 5 6 7 8 9 10 1 Call advance() countOff – 1 times remove(); CS210-Summer 2005, Lecture 8

  7. Lists • Using ranks is not the only way of referring to the place where an element appears in a sequence. • If the sequence S is implemented with a singly or doubly linked list, then it is more natural and efficient to use a node instead of a rank. CS210-Summer 2005, Lecture 8

  8. Node-Based Operations • We want to define methods for S (singly or doubly linked list) that takes nodes as parameters and provide nodes as return types. • Such methods provide significant speed ups over rank-based methods as finding the rank of the element in a linked list requires searching through the list incrementally, from its beginning or end. CS210-Summer 2005, Lecture 8

  9. Node Based Operations • Example: • Lets define some hypothetical methods: • removeAtNode(n): removes the element of S stored in node n of the list. What is the running time? • insertAfterNode(n , e): inserts the new element e after the node n. CS210-Summer 2005, Lecture 8

  10. The Position ADT models the notion of place within a data structure where a single object is stored It gives a unified view of diverse ways of storing data, such as a cell of an array a node of a linked list Just one method: object element(): returns the element stored at the position Position ADT CS210-Summer 2005, Lecture 8

  11. Position ADT • A position is always defined relatively, that is, in terms of its neighbors. In a list, a position p will always be “after” some position q and “before” some position s (unless p is first or last position). • A position p, which is associated with some element e in a list S, does not change, even if the rank of e changes in S, unless we explicitly remove e (destroy position p). CS210-Summer 2005, Lecture 8

  12. The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: size(), isEmpty() Accessor methods: first(), last() prev(p), next(p) Update methods: replace(p, e) insertBefore(p, e), insertAfter(p, e), insertFirst(e), insertLast(e) remove(p) List ADT CS210-Summer 2005, Lecture 8

  13. The List ADT CS210-Summer 2005, Lecture 8

  14. Doubly Linked list implementation of List ADT • A doubly linked list provides a natural implementation of the List ADT • Simply make the nodes of the linked list implement the position ADT. • Each node implement the position interface and therefore define a method element(). • Nodes themselves acts as positions. • Viewed internally by linked list as nodes, but from outside they are viewed as generic positions. CS210-Summer 2005, Lecture 8

  15. Doubly Linked List • Nodes implement Position and store: • element • link to the previous node • link to the next node • Special trailer and header nodes prev next elem node trailer nodes/positions header A B C D elements CS210-Summer 2005, Lecture 8

  16. Insertion • We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C CS210-Summer 2005, Lecture 8

  17. Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) {link v to its predecessor} v.setNext(p.getNext()) {link v to its successor} (p.getNext()).setPrev(v) {link p’s old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} CS210-Summer 2005, Lecture 8

  18. p A B C D Deletion • We visualize remove(p), where p = last() A B C p D A B C CS210-Summer 2005, Lecture 8

  19. Deletion Algorithm Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t CS210-Summer 2005, Lecture 8

  20. Sequences • The Sequence ADT is the union of the Vector and List ADTs • Elements accessed by • Rank, or • Position • Bridging methods • atRank(r): Return the position of the element with rank r. • rankOf(p): Return the rank of the element at position p. CS210-Summer 2005, Lecture 8

  21. Generic methods: size(), isEmpty() Vector-based methods: elemAtRank(r), replaceAtRank(r, o), insertAtRank(r, o), removeAtRank(r) List-based methods: first(), last(), prev(p), next(p), replace(p, o), insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o), remove(p) Bridge methods: atRank(r), rankOf(p) Sequence ADT CS210-Summer 2005, Lecture 8

  22. Doubly Linked List Implementation • A doubly linked list provides a reasonable implementation of the Sequence ADT • Nodes implement Position and store: • element • link to the previous node • link to the next node • Special trailer and header nodes • Position-based methods run in constant time • Rank-based methods require searching from header or trailer while keeping track of ranks; hence, run in linear time trailer nodes/positions header A B C D elements CS210-Summer 2005, Lecture 8

  23. Iterators • We generally require to march through the elements of vectors, lists or sequences one at a time. • Iterator ADT • hasNext: Test whether there are elements left in the iterator. • next: Return the next element in the iterator. • An iterator for a vector, list or sequence should return the elements according to their linear ordering. CS210-Summer 2005, Lecture 8

  24. Using Iterators in Lists and other ADTs • ADTs storing collection of objects should support the following method: • elements() / iterator() : Return an iterator of the elements in the collection. • Usage: public static void printVector(Vector vec) { Iterator it = vec.iterator(); while(it.hasNext()) System.out.println(it.next()); } CS210-Summer 2005, Lecture 8

  25. Using Iterators in Lists and other ADTs • ADTs that support the notion of position, such as List and sequence ADT, also provide the following method: • positions(): return an iterator of the positions in the collection. CS210-Summer 2005, Lecture 8

  26. A B C D E F G H I J K What is a Tree • In computer science, a tree is an abstract model of a hierarchical structure • A tree consists of nodes with a parent-child relation • Applications: • Organization charts • File systems • Programming environments CS210-Summer 2005, Lecture 8

  27. A C D B E G H F K I J Tree Terminology • Root: node without parent (A) • Internal node: node with at least one child (A, B, C, F) • External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) • Ancestors of a node: parent, grandparent, grand-grandparent, etc. • Depth of a node: number of ancestors • Height of a tree: maximum depth of any node (3) • Descendant of a node: child, grandchild, grand-grandchild, etc. • Subtree: tree consisting of a node and its descendants subtree CS210-Summer 2005, Lecture 8

  28. Tree ADT (§ 6.1.2) • We use positions to abstract nodes • Generic methods: • integer size() • boolean isEmpty() • Iterator elements() • Iterator positions() • Accessor methods: • position root() • position parent(p) • positionIterator children(p) • Query methods: • boolean isInternal(p) • boolean isExternal(p) • boolean isRoot(p) • Update method: • object replace (p, o) • Additional update methods may be defined by data structures implementing the Tree ADT CS210-Summer 2005, Lecture 8

  29. Preorder Traversal • A traversal visits the nodes of a tree in a systematic manner • In a preorder traversal, a node is visited before its descendants • Application: print a structured document AlgorithmpreOrder(v) visit(v) foreachchild w of v preorder (w) 1 A 2 5 9 B E I 6 8 7 3 4 G C D F H CS210-Summer 2005, Lecture 8

  30. Postorder Traversal • In a postorder traversal, a node is visited after its descendants • Application: compute space used by files in a directory and its subdirectories AlgorithmpostOrder(v) foreachchild w of v postOrder (w) visit(v) 9 A 8 3 7 B I E 4 5 6 1 2 H C D G F CS210-Summer 2005, Lecture 8

More Related