1 / 78

Chapter 12 Data Structures and Collections

Chapter 12 Data Structures and Collections. Knowledge Goals. Understand the difference between array and linked implementations of a list Know how to a stack works Know how a queue works Know how a binary tree works Know how a hash table works

Télécharger la présentation

Chapter 12 Data Structures and Collections

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. Chapter 12 Data Structures and Collections

  2. Knowledge Goals • Understand the difference between array and linked implementations of a list • Know how to a stack works • Know how a queue works • Know how a binary tree works • Know how a hash table works • Understand the concepts behind the java collections framework

  3. Skill Goals • Develop a linked data structure • Use the ArrayList and LinkedList classes • Use the HashSet and TreeSet classes • Use the Stack class • Use the LinkedList class to implement a queue • Choose when to use an array-based versus a linked implementation of a data structure

  4. Linked Structures Array- based list is fixed size

  5. Linked Structures Array- based is physically ordered; linked is logically ordered, which means …

  6. Linked Structures Insertions and deletions are easier

  7. Linked Structure • Linked list • A list in which the order of the components is determined by an explicit link field in each node, rather than by the sequential order of the components in memory • External reference (pointer) • A named variable that references (points to) the first node (head) of a linked list Yes, but how does it work?

  8. Linked Structure List External pointer A node Each node contains item and a link

  9. Linked Structures class Node { Node link = null; String item; Node() { item = ""; } Node(String data) { item = data; } } null is a keyword meaning points to nothing default constructor parameterized constructor

  10. Linked Structures Let's create a linked list with three nodes, containing "Adams", "Baker", and "Carter" We begin by creating two variables Node current; // Pointer used to keep track // of where we are in the list Node list; // External point to the list Result

  11. Linked Structures • list = new Node("Adams"); • current = list; Result

  12. Linked Structures • current.link = new Node("Baker"); Result

  13. Linked Structures • current = current.link; Result

  14. Linked Structures • current.link = new Node("Carter"); • current = current.link; Result

  15. Linked Structures • Traversal • Visiting every node in a data structure following an organized pattern of access current = list; while (current.link != null) { System.out.println(current.item); current = current.link; } What is the pattern of access?

  16. Linked Structures Which of these will change with the linked version?

  17. Linked Structures Does the CRC card for a class change with the implementation?

  18. Linked Structures • Observer operations public boolean isEmtpy() { return list == null; } public boolean contains(String item) { Node current = list; while (current != null && curent.item.compareTo(item) != 0) curent = current.link; return current != null }

  19. Linked Structures if (list.contains("Doggett"))… Search when item is not in the list

  20. Linked Structures • Size in an array-based list just returns numItems; What about in a linked-implementation? • Can keep a counter increment with each insertion decrement with each deletion • Can count the number of items each time Which is best (better)?

  21. Linked Structures • public int size() • { • Node current = list; • int numItems = 0; • while (current != null) • { • numItems++; • current = current.link; • } • return numItems; • } Is this an example of a traversal?

  22. Linked Structures • Mutators • Add • Insert where?To match array-based traversal, new item must go at the end • How to find end? Search or keep a pointer • Special case(s)? Empty list tail

  23. Linked Structure • public void add(String newItem) • { • Node item = new Node(newItem); • if (list == null) • { • list = item; • tail = item; • } • else • { • tail.link = item; • tail = tail.link; • } • } Empty list Set list and tail to item Not empty list Set item as last node Set tail to last node

  24. Linked Structures • Remove • Item not there?Do nothing • What if item is the first? Reset external pointer • What if item is the last?Reset tail pointer • What if item is anywhere else? General case

  25. Linked Structures Can't be reached; System recaptures it

  26. Linked Structures Remove "Adams" What is prior?

  27. Linked Structures Remove "Carter"

  28. Linked Structures Remove "Adams" (only node)

  29. public void remove(String item) { Node current = list; Node prior = null; while (current != null && current.item.compareTo(item) != 0) { prior = current; current = current.link; } if (current != null) { if (current == tail) tail = prior; if (prior == null) list = list.link; else prior.link = current.link; } } Search for item item in last node item in first node item in interior node

  30. Linked Structures • Iterator "trio" • public void resetList() • { current = list; } • public boolean hasNext() • { return current != null; } • public String next() • { • String item = current.item; • current = current.link; • return item; • } What assumptions must be in documentation?

  31. Other Data Structures What do these objects have in common?

  32. Other Data Structures • Stack • A data structure in which insertions and deletions can be made from only one end • LIFO • Last In First Out • Can you name some additional LIFO structures in every day life?

  33. Other Data Structures First item called "top"

  34. Other Data Structures What properties does this illustration exhibit?

  35. Other Data Structures Queue A data structure in which insertions are made at one end and deletions are made at the other end FIFO First In First Out Can you name some additional FIFO structures in every day life?

  36. Other Data Structures

  37. Other Data Structures

  38. Other Data Structures Jake's Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len What properties does this illustration exhibit?

  39. ROOT NODE Other Data Structures Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

  40. LEAF NODES Other Data Structures Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

  41. Other Data Structures Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEFT SUBTREE OF ROOT NODE

  42. Other Data Structures Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len RIGHT SUBTREE OF ROOT NODE

  43. Other Data Structures • Binary tree • A data structure, each of whose nodes refer to left and right child nodes

  44. Other Data Structures • Binary search tree • A binary tree in which the value in any node is greater than the value in the left child and any of its children and less than the value in its right child and any of its children

  45. Other Data Structures • Binary search trees provide rapid searches Search for 50

  46. Other Data Structures Search for 18 Where would 18 be if there?

  47. Other Data Structures Traversals PreOrder(tree) if tree is not NULL Visit Info(tree) Preorder(Left(tree)) Preorder(Right(tree)) • Inorder(tree) • if tree is not NULL • Inorder(Left(tree)) • Visit Info(tree) • Inorder(Right(tree)) • PostOrder(tree) • if tree is not NULL • Postorder(Left(tree)) • Postorder(Right(tree)) • Visit Info(tree) alphabetic order expression evaluation visits leaves first

  48. Other Data Structures

  49. Other Data Structures Graph A data structure in which the nodes can be arranged in any pattern

  50. Other Data Structures • Hashing • A technique to • perform insertions • and access to an • item in constant • time by using the • value to identify • its location in the • structure

More Related