1 / 25

Data S tructures and A lgorithms

Data S tructures and A lgorithms. Graphs page 2 Lists page 3-6 Java API: Collection, List and LinkedList page 7-9 Queue page 10 Stack page 11 Recursion page 12 Trees incl. binary search trees page 13-17 Java API: Map and TreeMap page 18-20 Hash tables page 21-25 .

Télécharger la présentation

Data S tructures and A lgorithms

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. Data Structures and Algorithms Graphs page 2Lists page 3-6Java API: Collection, List and LinkedList page 7-9Queue page 10Stack page 11Recursion page 12Trees incl. binary search trees page 13-17Java API: Map and TreeMap page 18-20Hash tables page 21-25 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  2. Graphs • Applications • visualizations of computer networks • logistics • formal mapping of an object oriented program system 2 vertex 2 4 12 edge 9 5 An undirected graph A directed and weighted graph Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  3. Lists • Data is ordered sequentially in the same way as an array • Relevant operations for a list • Insert element into list: first, last or in given position. • Remove element from list. • Go through all elements of list. • Lists compared to arrays • Both data structures fix the data in a particular order. • The size of a list is dynamic, it is changed by inserting or removing an element. An array can’t change its size after creation. • Finding a given element in a list can be time consuming because we always must start at the end of the list. Any element in an array may be accessed with the index. Single-linked list Double-linked list Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  4. Some List Operations in More Detail • Inserting element at the end • Inserting element in given position (before *) * Why can’t we do the two operations in reverse order? * Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  5. Example, a List of Names class NameElement { private NameElement next; private String name; public NameElement(String initName) { name = initName; next = null; } public NameElement(String initName, NameElement initNext) { name = initName; next = initNext; } public String getName() { return name; } public NameElement getNext() { return next; } public void setName(String newName) { name = newName; } public void setNext(NameElement newNext) { next = newNext; } } NameElement next name getName getNext setName setNext next next neste next next Joe Aron Liza Anne null Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  6. The NameList Class NameList firstElement (toString()) toString insertNameAtEnd findName deleteName Show program listing 17.2 and 17.3, page 530 and so on. Solve problems page 534. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  7. Interface java.util.Collection • Common features for classes implementing this interface: • a collection of elements of different types • methods (remember we have no implementations so far!) • public boolean add(Object add) • public boolean contains(Object obj) • public boolean containsAll(Collection otherCollection) • public boolean remove(Object obj) • public boolean removeAll(Collection collection) • public int size() • Collections can be classified by two parameters • is duplicates allowed? • are the elements ordered? Ordered Not ordered Multiset Duplicates allowed List Hashtable (the table itself, inmany cases) Set Duplicates not allowed Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  8. Interface java.util.List • List is a sub-interface to Collections: • The elements must be ordered • List requires more methods implemented: • public void add(int position, Object obj) • public Object set(int position, Object obj) • public Object remove(int position) • public Object get(int position) • Familiar? The class ArrayList implements this interface. • May well have identical data in a list, even several references to the same object (like ArrayList may). • The implementation of add() decides such things, not dictated by the interface. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  9. The Class java.util.LinkedList • Implements List (and hence Collection) • Will have the performance characteristics expected from a doubly linked list • Offers more methods than the interface demands • public void addFirst() • public void addLast() • public Object removeFirst() • public Object removeLast() Solve the problem at page 538. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  10. Queue • A queue is an ordered collection of data where data is inserted and removed in one particular place. • FIFO queue • First In First Out • Operations • insert element • remove element import java.util.*; class FIFOQueue { private LinkedList list = new LinkedList(); public void insertElement(Object anObject) { list.addLast(anObject); } public Object removeElement() { return list.removeFirst(); } public int findNumberOfElements() { return list.size(); } } Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  11. Stack • A stack is a LIFO queue (Last In First Out) import java.util.*; class Stack { private LinkedList list = new LinkedList(); public void insertElement(Object anObject) { list.addLast(anObject); } public Object removeElement() { return list.removeLast(); } public int findNumberOfElements() { return list.size(); } } Solve the problem at page 540. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  12. Recursion • Technically, recursion is when a method calls itself. • For this not to go on forever, recursion must have a stop condition. • Example: public int factorial(int n) { if (n <= 1) return 1; else return n * factorial(n-1); } Solve the problems at page 541. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  13. Trees • A tree is a special case of graph • may not contain cycles • must be connected • always exactly one path between two vertices • one vertex is defined as root • the tree spreads out from the root • any vertex will suffice as root, if thus defined not a tree tree Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  14. The Vertices of a Tree root parent vertices leaf child vertices leaf leaf Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  15. Binary Search Trees • A tree where each parent has at most twochildren is called a binary tree. • The children are the roots of right and left subtree. • For each vertex there is data. • the left child’s data is ”smaller than or equal to” the parent’s data • the right child’s data is ”larger than” the parent’s data 15 10 28 3 14 20 BinarySearchTree 16 21 root toString insertValue searchForValue left subtree Show program listing 17.4, pp. 54-545. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  16. The SubTree Class SubTree rightTree leftTree parent value Show program listing 17.5, p.. 546-548. toString insertValue searchForValue Show program listing 17.6, page 549. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  17. Binary Search Trees Compared to Linked Lists • Searching for data is much faster in a search tree compared to a linked list. • The search time in a linked list is at worst proportional to the number of elements (= n) in the list. • To search through a binary search tree requires to descend through the tree until we find the right object, potentially much faster. • The tree is balanced if it’s as symmetrical as possible. • The search is most efficient if the tree is balanced. • We can show that the search time in a balanced tree is at worst proportional to log(n). • log(n) is always smaller than n. • Inserting data takes longer time in a binary search tree than in a list. Solve the problem, page 549. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  18. Interface java.util.Map • A map maintains a connection between keys and data. • All the keys are different. • Each key is connected to exactly one value. • The mapping shall make it quick to fetch data for a given key. • Interface java.util.Map • Keys and data are of arbitrary type. • If you attempt to insert an already existing key, that one key will be connected to the new data value you are inserting. • Methods: • public Object get(Object key) • public Object put(Object key, Object data) • public Object remove(Object key) Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  19. The Class java.util.TreeMap • The class implements a binary search tree well balanced • Implements Map and also SortedMap: • public Object firstKey() • public Object lastKey() • Requires the keys to be possible to sort • either a ”comparator” must be passed as argument to the constructor, example collator from page TBA: • TreeMap aTree = new TreeMap(java.text.Collator.getInstance()); • or the class which the keys belong to must implement the interface Comparable • Example, tree from chapter 17.4 (slide 15). • The numbers are keys for the vertices. • We let the data be person names. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  20. We Try TreeMap Printout:Contents of the tree: {3=Harry, 10=Rob,14=Ted, 15=Rick, 16=Edina, 20=Mildred,21=Patsy, 28=Melissa} 11 is not there 10 is there: Rob Lowest key value: 3 Highest key value: 28 class TreeMapTest { public static void main(String[] args) { TreeMap treeExample = new TreeMap(); treeExample.put(new Integer(15), "Rick"); treeExample.put(new Integer(10), "Rob"); treeExample.put(new Integer(28), "Melissa"); treeExample.put(new Integer(3), "Harry"); treeExample.put(new Integer(14), "Ted"); treeExample.put(new Integer(20), "Mildred"); treeExample.put(new Integer(16), "Edina"); treeExample.put(new Integer(21), "Patsy"); System.out.println("Contents of the tree: " + treeExample.toString()); String test = (String) treeExample.get(new Integer(11)); if (test == null ) System.out.println("11 is not there"); else System.out.println("11 is there: " + test); test = (String) treeExample.get(new Integer(10)); if (test == null ) System.out.println("10 is not there"); else System.out.println("10 is there: " + test); System.out.println("Lowest key value: " + treeExample.firstKey()); System.out.println("Highest key value: " + treeExample.lastKey()); } } Solve the problem pp. 552-553. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  21. Hashtables • A hashtable differs from • a binary search tree in that the elements don’t have an intrinsic ordering • a linked list in that search is faster • Is among other things used in operating systems to organize the files in a directory • The keys are placed in slots • The slots • are usually ordered • must be unique (duplicates not allowed) • Searching and inserting is fast • calculate the correct slot • search for the value in the relatively short list in the slot, or • insert the value, for example at the end of the list • The load factor is equal to the ratio between the number and elements and slots. • high load factor: long search time, good utilization of space • low load factor: short search time, needs more space, many slots with few elements hash function key value slot value Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  22. Example of a Hashtable slot no. 0 11 31 1 2 2 3 Hash function: Take the last digit of thekey value. 4 94 55 35 5 85 6 16 Each key value could correspond to dataabout a person. We have ommitted these. 7 8 18 108 9 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  23. The Class java.util.Hashtable • The standard constructor creates a hashtable with 101 slots (a prime number is good!) • Rehashing is done when the loadfactor becomes 0,75 • the number of slots is changed, and all addresses recalculated • Every class can have its hash function, if not, the implementation of hashCode() is inherited from the class Object. • The class Hashtable implements the interface map, with familiar methods: • public Object get(Object key) • public Object put(Object key, Object data) • public Object remove(Object key) Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  24. Class Describing the Key from Slide 22 class Ident { private int value; public Ident(int initValue) { value = initValue; } /* Gets the last numeral in the value */ public int hashCode() { // redefines the hashCode() inherited from Object int power = 10; while (value / power > 10) power *= 10; int remaining = value % power; while (remaining > 10) { power /= 10; remaining %= power; } System.out.println("Value " + value + " gives code " + remaining); return remaining; } public String toString() { return "" + value; } public boolean equals(java.lang.Object obj) { return ((Ident) obj).value == value; } } Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  25. Client Program Which Creates and Tests the Hashtable class HashTest { public static void main(String[] args) { Hashtable hashExample = new Hashtable(10); // to fit the figure hashExample.put(new Ident(2), "Matt"); hashExample.put(new Ident(11), "Tony"); hashExample.put(new Ident(16), "Celine"); hashExample.put(new Ident(18), "Baldrick"); hashExample.put(new Ident(31), "Arnie"); hashExample.put(new Ident(35), "Vinnie"); hashExample.put(new Ident(55), "Quentin"); hashExample.put(new Ident(85), "Alexander"); hashExample.put(new Ident(94), "Pat"); hashExample.put(new Ident(108), "Put"); System.out.println("The contents of the hashtable: " + hashExample.toString()); String test = (String) hashExample.get(new Ident(11)); if (test == null ) System.out.println("11 is not there"); else System.out.println("11 is there: " + test); test = (String) hashExample.get(new Ident(6)); if (test == null ) System.out.println("6 is not there"); else System.out.println("6 is there: " + test); } } Printout: Value 2 gives code 2 Value 11 gives code 1 Value 16 gives code 6 Value 18 gives code 8 Value 31 gives code 1 Value 35 gives code 5 Value 55 gives code 5 Value 85 gives code 5 Value 94 gives code 4 Value 108 gives code 8 The contents of the hashtable: {108=Put, 18=Baldrick, 16=Celine, 85=Alexander, 35=Vinnie, 55=Quentin, 94=Pat, 2=Matt, 11=Tony, 31=Arnie} Value 11 gives code 1 11 is there: Tony Value 6 gives code 6 6 is not there Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

More Related