Basic data-structures-v.1.1

8,963 views

Published on

Basic types of data structures. Implementation in Java.

Published in: Technology, Education
1 Comment
3 Likes
Statistics
Notes
  • Hi My friend
    Thank you very much
    good luck
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
8,963
On SlideShare
0
From Embeds
0
Number of Embeds
880
Actions
Shares
0
Downloads
580
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • Basic data-structures-v.1.1

    1. 1. Lists, Stacks, Queues, Trees, Hash Tables Basic Data Structures
    2. 2. Contents <ul><li>Abstract Data Types (ADT) </li></ul><ul><li>Lists – ArrayList Class </li></ul><ul><li>Stacks – Stack Class </li></ul><ul><li>Queues – Queue Class </li></ul><ul><li>Trees – Terminology and Types </li></ul><ul><li>Dictionaries – HashMap Class </li></ul>
    3. 3. Abstract Data Types <ul><li>An Abstract Data Type (ADT) is a data type together with the operations, whose properties are specified independently of any particular implementation </li></ul><ul><ul><li>ADT are set of definitions of operations (like the interfaces in Java) </li></ul></ul><ul><ul><li>Can have several different implementations </li></ul></ul><ul><ul><li>Different implementations can have different efficiency </li></ul></ul>
    4. 4. Basic Data Structures <ul><li>Linear structures </li></ul><ul><ul><li>Lists: Variable-size </li></ul></ul><ul><ul><li>Stacks: LIFO (last in first out) structure </li></ul></ul><ul><ul><li>Queues: FIFO (first in first out) structure </li></ul></ul><ul><li>Trees </li></ul><ul><li>Dictionaries (maps) </li></ul><ul><ul><li>Contain pairs (key, value) </li></ul></ul><ul><ul><li>Hash tables: Unordered lists which use a ‘hash function’ to insert and search </li></ul></ul>
    5. 5. What Is a List?
    6. 6. The List ADT <ul><li>List is linear data structure (container) that contains a sequence of elements (objects) </li></ul><ul><ul><li>Has variable size </li></ul></ul><ul><ul><li>Objects are arranged linearly </li></ul></ul><ul><li>Can be implemented in several ways </li></ul><ul><ul><li>Statically (using array) </li></ul></ul><ul><ul><li>Dynamically (linked implementation) </li></ul></ul><ul><ul><li>Using the ArrayList class </li></ul></ul>
    7. 7. Static and Linked Lists
    8. 8. Static List <ul><li>Implemented by an array </li></ul><ul><ul><li>Provide direct access by index </li></ul></ul><ul><ul><li>Usually has limited capacity </li></ul></ul><ul><ul><ul><li>Resizing is slow operation </li></ul></ul></ul><ul><ul><li>Slow insert and deletion </li></ul></ul>0 1 2 3 4 5 6 7 8 11 7 18 14 L 5 2 33 47 3
    9. 9. Linked List <ul><li>Dynamic (pointer-based) implementation </li></ul><ul><li>Different forms </li></ul><ul><ul><li>Singly-linked and doubly-linked </li></ul></ul><ul><ul><li>Sorted and Unsorted </li></ul></ul><ul><li>Singly-linked List - each “Object” has “ value ” and “ next ” fields </li></ul>11 next 7 next 18 next 14 next head null
    10. 10. Linked List (2) <ul><li>Doubly-linked List - each “Object” has “ value ”, “ next ” and “ prev ” fields </li></ul>11 next prev head tail 7 next prev 18 next prev 14 next prev null null
    11. 11. Using the ArrayList class
    12. 12. The java.util.ArrayList Class <ul><li>Implements the list data structure using an array whose size is dynamically increased as needed </li></ul><ul><ul><li>Allocates in advance buffer space for new elements (for better performance) </li></ul></ul><ul><li>Insertion methods: </li></ul><ul><ul><li>add(Object) – adds an object at the end </li></ul></ul><ul><ul><li>add (index , Object) – inserts an object to the list at a specified position </li></ul></ul><ul><li>size() – returns the number of elements </li></ul>
    13. 13. The ArrayList Class <ul><li>Deletion methods: </li></ul><ul><ul><li>remove(Object) – removes the first occurrence of a specific object </li></ul></ul><ul><ul><li>remove( index) – removes the element at the specified position </li></ul></ul><ul><ul><li>clear() – removes all elements </li></ul></ul><ul><li>Other supported methods: </li></ul><ul><ul><li>c ontains() , t oArray() </li></ul></ul>
    14. 14. The ArrayList Class(2) <ul><li>ArrayList can contain any data type </li></ul><ul><ul><li>Elements are added directly </li></ul></ul><ul><ul><li>Typecasting is required when extracting elements unless we use Generics </li></ul></ul><ul><ul><li>Converting to array </li></ul></ul>List list = new ArrayList(); list. a dd(5); // Add integer value list. a dd(&quot;some string&quot;); // Add string value int firstElement = ((Integer)(list.get(0))).intValue(); String secondElement = (String)list.get(1); Integer[] arr = list.toArray(new Integer[list.size()]);
    15. 15. What are Generics <ul><li>Generics are classes or interfaces that can be instantiated with a variety of types </li></ul><ul><ul><li>They have 1 or more formal type parameters </li></ul></ul><ul><ul><li>When using a generic you specify an actual type </li></ul></ul>List<String> list = new ArrayList<String>(); String s = new String(&quot; li1 &quot;); list.add(s); list.add(5); // This will cause compile time error Specifies that String is actual type of this List 5 is not a String
    16. 16. Primes[n..m] – Example <ul><li>Find all prime numbers in a specified interval </li></ul>public static ArrayList<Integer> getPrimes(int start, int end) { List<Integer> primesList = new ArrayList<Integer>(); for (int num = start; num <= end; num++) { boolean prime = true; for (int div = 2; div <= Math.sqrt(num); div++) { if (num % div == 0) { prime = false; break; } } if (prime) { primesList.add(num); } } return primesList; }
    17. 17. Primes[n..m] Live Demo
    18. 18. Union and Intersection of Lists – Example public static Integer[] union(Integer[] firstArr, Integer[] secondArr) { List<Integer> union = new ArrayList<Integer>(); for (Integer item : firstArr) { union.add(item); } for (Integer item : secondArr) { if (!union.contains(item)) { union.add(item); } } return union.toArray(new Integer[union.size()]); } //Example continues...
    19. 19. Union and Intersection of Lists – Example(2) public static Integer[] intersect(Integer[] firstArr, Integer[] secondArr) { List<Integer> intersect = new ArrayList<Integer>(); for (Integer item : firstArr) { if (Arrays.binarySearch(secondArr, item) >= 0) { intersect.add(item); } } return intersect.toArray( new Integer[intersect.size()]); }
    20. 20. Union and Intersection Live Demo
    21. 21. What is a Stack?
    22. 22. The Stack ADT <ul><li>LIFO (last in first out) structure </li></ul><ul><li>Elements inserted (push) at “top” </li></ul><ul><li>Elements removed (pop) from “top” </li></ul><ul><li>Can be implemented in several ways </li></ul><ul><ul><li>Statically (using array) </li></ul></ul><ul><ul><li>Dynamically (linked implementation) </li></ul></ul><ul><ul><li>Using the Stack class </li></ul></ul>
    23. 23. Static Stack <ul><li>Array-based (static) implementation </li></ul><ul><ul><li>Usually has limited capacity </li></ul></ul><ul><ul><li>Has top variable, pointing to the top </li></ul></ul><ul><ul><li>IsEMPTY( S ) </li></ul></ul><ul><ul><ul><li>Check if S.top = -1 </li></ul></ul></ul>0 1 2 3 4 5 6 7 8 11 7 18 14 S top
    24. 24. Linked Stack <ul><li>Dynamic (pointer-based) implementation </li></ul><ul><ul><li>Each “object” has “value” and “next” fields </li></ul></ul><ul><ul><li>Dynamically create and delete objects </li></ul></ul>11 next 7 next 18 next 14 next top null
    25. 25. Using the Stack class
    26. 26. The Stack Class – Overview <ul><li>Implements the stack data structure using an array whose size is dynamically increased as needed </li></ul><ul><li>Major methods: </li></ul><ul><ul><li>push(object) – inserts elements to the stack </li></ul></ul><ul><ul><li>pop() – removes and returns the top element from the stack </li></ul></ul><ul><ul><li>peek() – returns the top element of the stack without removing it </li></ul></ul>
    27. 27. The Stack Class – More Methods <ul><li>Other methods: </li></ul><ul><ul><li>size() – returns the number of elements </li></ul></ul><ul><ul><li>clear() – removes all elements </li></ul></ul><ul><ul><li>contains(object) – determines whether given element is in the stack </li></ul></ul><ul><ul><li>toArray() – converts the stack to array </li></ul></ul>
    28. 28. Examples Using the Stack class
    29. 29. Stack – Example <ul><li>Using push() , pop() and peek() methods </li></ul>public static void main(String[] args) { Stack<String> stack = new Stack<String>(); stack.push(&quot;1. Ivan&quot;); stack.push(&quot;2. Nikolay&quot;); stack.push(&quot;3. Maria&quot;); stack.push(&quot;4. George&quot;); System.out.println(&quot;Top = &quot; + stack.peek()); while (stack.size() > 0) { String personName = stack.pop(); System.out.println(personName); } }
    30. 30. Live Demo Using the Stack class
    31. 31. Matching Brackets – Example <ul><li>We are given an arithmetical expression with brackets that can be nested. We want to extract all parts of the expression that are closed in brackets. </li></ul><ul><ul><li>Example: 1 + (3 + 2 - (2+3) * 4 - ((3+1)*(4-2))) </li></ul></ul><ul><ul><li>Result: </li></ul></ul><ul><ul><ul><li>(2+3) </li></ul></ul></ul><ul><ul><ul><li>(3+1) </li></ul></ul></ul><ul><ul><ul><li>(4-2) </li></ul></ul></ul><ul><ul><ul><li>((3+1)*(4-2)) </li></ul></ul></ul><ul><ul><ul><li>(3 + 2 - (2+3) * 4 - ((3+1)*(4-2))) </li></ul></ul></ul>
    32. 32. Matching Brackets – Solution with a Stack String expression = &quot;1 + (3 + 2 - (2+3) * 4 - ((3+1)*(4-2)))&quot;; Stack<Integer> stack = new Stack<Integer>(); for (int i = 0; i < expression.length(); i++) { char ch = expression.charAt(i); if (ch == '(') { stack.push(i); } else if (ch == ')') { int startIndex = (int) stack.pop(); String contents = expression.substring(startIndex, i + 1); System.out.println(contents); } }
    33. 33. Live Demo Matching Brackets
    34. 34. What is a Queue?
    35. 35. The Queue ADT <ul><li>FIFO (first in first out) structure </li></ul><ul><li>Elements inserted at tail (enqueue) </li></ul><ul><li>Elements removed from head ( dequeue ) </li></ul><ul><li>Useful in many situations </li></ul><ul><ul><li>Processing jobs, print queues, messages </li></ul></ul><ul><li>Can be implemented in several ways </li></ul><ul><ul><li>Statically (using array) </li></ul></ul><ul><ul><li>Dynamically (using pointers) </li></ul></ul><ul><ul><li>Using the LinkedList class </li></ul></ul>
    36. 36. Static Queue <ul><li>Static (array-based) implementation </li></ul><ul><ul><li>Queue has limited (fixed) capacity </li></ul></ul><ul><ul><li>Implement as a “circular array” </li></ul></ul><ul><ul><li>Maintain Q.Capacity and Q.Length </li></ul></ul><ul><ul><li>Has head and tail variables, pointing </li></ul></ul><ul><ul><li>to the head and the tail </li></ul></ul>0 1 2 3 4 5 6 7 8 11 7 18 14 Q head tail
    37. 37. Linked Queue <ul><li>Dynamic (pointer-based) implementation </li></ul><ul><ul><li>Each “object” has “ value ” and “ next ” fields </li></ul></ul><ul><ul><li>Dynamically create and delete objects </li></ul></ul>11 next 7 next 18 next 14 next head tail null
    38. 38. Using the LinkedList class
    39. 39. The LinkedList Class – Overview <ul><li>Implements the queue data structure using a doubly-linked list </li></ul><ul><li>Major methods: </li></ul><ul><ul><li>offer(object) – adds an object to the end of the queue </li></ul></ul><ul><ul><li>poll() – removes and returns the object at the beginning of the queue </li></ul></ul><ul><ul><li>peek() – returns the object at the beginning of the queue without removing it </li></ul></ul>
    40. 40. The LinkedList Class – More Methods <ul><li>Other methods: </li></ul><ul><ul><li>size() – gets the number of elements contained in the queue </li></ul></ul><ul><ul><li>clear() – removes all elements from the queue </li></ul></ul><ul><ul><li>contains(object) – determines whether given element is in the queue </li></ul></ul><ul><ul><li>toArray () – converts the queue to array </li></ul></ul>
    41. 41. Examples Using the LinkedList class
    42. 42. Queue – Example <ul><li>Using offer () and poll () methods </li></ul>public static void main(String[] args) { Queue<String> queue = new LinkedList<String>(); queue.offer(&quot;Message One&quot;); queue.offer(&quot;Message Two&quot;); queue.offer(&quot;Message Three&quot;); queue.offer(&quot;Message Four&quot;); queue.offer(&quot;Message Five&quot;); while (queue.size() > 0) { String msg = queue.poll(); System.out.println(msg); } }
    43. 43. Live Demo Using the LinkedList class
    44. 44. Sequence N, N+1, 2*N <ul><li>We are given the sequence: </li></ul><ul><ul><li>S = N, N+1, 2*N, N+2, 2*(N+1), 2*N+1, 4*N, ... </li></ul></ul><ul><li>Write a program to find the first index of given number P </li></ul><ul><li>Example: N = 3, P = 16 </li></ul><ul><ul><li>S = 3, 4, 6, 5, 8, 7, 12, 6, 10, 9, 16, 8, 14, ... </li></ul></ul><ul><ul><li>Index of P = 11 </li></ul></ul>+1 *2 +1 *2 +1 *2
    45. 45. Sequence – Solution int n = 3; int p = 16; Queue<Integer> queue = new LinkedList<Integer>(); queue.offer(n); int index = 0; while (queue.size() > 0) { index++; int current = queue.poll(); if (current == p) { System.out.println(&quot;Index = &quot; + index); return; } queue.offer(current + 1); queue.offer(2 * current); }
    46. 46. Live Demo Sequence N, N+1, 2*N
    47. 47. Definition, Types of Trees What is Tree?
    48. 48. Trees <ul><li>Terminology </li></ul><ul><ul><li>Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth, height </li></ul></ul>17 15 14 9 6 5 8 Height = 2 Depth 0 Depth 1 Depth 2
    49. 49. Binary Trees <ul><li>Binary trees: most used form </li></ul><ul><ul><li>Each node has at most 2 children </li></ul></ul>10 17 15 9 6 5 8 “ right child” “ left subtree” “ root” “ left child”
    50. 50. Binary Trees Traversals <ul><li>Traversal can be done in pre-order, in-order and post-order </li></ul><ul><li>Pre-order: left, root, right – 6, 9, 12, 17, 19, 25 </li></ul><ul><li>In-order: root, left, right – 17, 9, 6, 12, 19, 25 </li></ul><ul><li>Post-order: left, right, root – 6, 12, 9, 25, 19, 17 </li></ul>17 19 9 6 12 25
    51. 51. Binary Search Trees <ul><li>Binary search trees are ordered </li></ul><ul><li>A binary tree in which binary-search-tree property holds: </li></ul><ul><ul><li>For each node x in the tree </li></ul></ul><ul><ul><ul><li>All the elements of the left subtree of x are ≤ x </li></ul></ul></ul><ul><ul><ul><li>All the elements of the right subtree of x are > x </li></ul></ul></ul><ul><li>Binary search trees can be balanced </li></ul><ul><ul><li>Balanced trees has low height </li></ul></ul>
    52. 52. Binary Search Trees <ul><li>Example of binary search tree </li></ul><ul><li>If the tree is balanced, adding, searching, and deletion operations take approx. log( n ) steps </li></ul>17 19 9 6 12 25
    53. 53. What is a Dictionary (Map)?
    54. 54. The Dictionary (Map) ADT <ul><li>The ADT &quot;dictionary&quot; maps key to values </li></ul><ul><ul><li>Also known as &quot;map&quot; or &quot;associative array&quot; </li></ul></ul><ul><ul><li>Contains a set of (key, value) pairs </li></ul></ul><ul><li>Dictionary ADT operations: </li></ul><ul><ul><li>ADD(key, value) </li></ul></ul><ul><ul><li>FIND_BY_KEY(key)  value </li></ul></ul><ul><ul><li>DELETE(key) </li></ul></ul><ul><li>Can be implemented in several ways </li></ul><ul><ul><li>List, array, hash table, balanced tree, ... </li></ul></ul>
    55. 55. What is a Hash Table?
    56. 56. Hash Table <ul><li>A hash table is an array that holds a set of (key, value) pairs </li></ul><ul><li>The process of mapping a key to a position in a table is called hashing </li></ul>0 1 2 3 4 5 m-1 ... ... ... ... T h( k ) ... ... ...
    57. 57. Hash Functions and Hashing <ul><li>A hash function maps keys to positions </li></ul><ul><ul><li>It is denoted by h </li></ul></ul><ul><li>The hash table has m slots, indexed from 0 to m-1 </li></ul><ul><li>For any value k in the key range and some hash function h </li></ul><ul><li>h( k ) = i </li></ul><ul><li>0  i < m </li></ul>0 1 2 3 4 5 m-1 ... ... ... ... T h( k ) ... ... ...
    58. 58. Mapping Functions <ul><li>Perfect hashing function (PHF) </li></ul><ul><ul><li>h(k) : one-to-one mapping from each key k to integers in [ 0 , m -1 ] </li></ul></ul><ul><li>The PHF maps each key to a distinct integer within some manageable range </li></ul><ul><li>Finding a perfect hashing function is in most cases impossible </li></ul><ul><li>More realistically </li></ul><ul><ul><li>Hash functions h(k) map most of the keys onto unique integers, but not all </li></ul></ul>
    59. 59. Collisions in Hash Tables <ul><li>Collision is the situation when different keys can have the same hash value </li></ul><ul><ul><li>h(k 1 ) = h(k 2 ) for k 1 ≠ k 2 </li></ul></ul><ul><li>When the number of collisions is sufficiently small, the hash tables work quite well (fast) </li></ul><ul><li>Several collisions resolution strategies </li></ul><ul><ul><li>Chaining in a list, re-hashing, using the neighboring slots (linear probing), ... </li></ul></ul>
    60. 60. Collision Resolution - Chaining <ul><li> </li></ul>h( &quot;Pesho&quot; ) = 4 h( &quot;Lili&quot; ) = n-1 h( &quot;Kiro&quot; ) = 2 h(&quot;Mimi&quot;) = 1 h(&quot;Ivan&quot;) = 2 null T 0 1 2 3 4 5 n-1 null ... null chaining Kiro Ivan collision null Mimi null Lili null Pesho null
    61. 61. Using the Hash Map class
    62. 62. The Hash Map Class – Overview <ul><li>Implements the ADT dictionary as array dynamically increased as needed </li></ul><ul><li>Contains a collection of key-and-value pairs arranged by the hash code of the key </li></ul><ul><li>Collisions are resolved by chaining </li></ul><ul><li>The Hash Map class relies on </li></ul><ul><ul><li>Object. h ashCode() method for calculating the hash codes of the elements </li></ul></ul><ul><ul><li>Object. equals( ) method for comparing elements </li></ul></ul>
    63. 63. The Hash Map Class – Major Operations <ul><li>Major operations: </li></ul><ul><ul><li>put (key , value) – adds an element with the specified key and value into the hash table </li></ul></ul><ul><ul><li>remove(key ) – removes the element with the specified key from the hash table </li></ul></ul><ul><ul><li>get(key ) – returns element by key </li></ul></ul><ul><ul><li>clear() – removes all elements from the hash table </li></ul></ul>
    64. 64. The Hash Map Class – More Operations <ul><li>More operations: </li></ul><ul><ul><li>size() – returns the number of elements </li></ul></ul><ul><ul><li>c ontains Key (key ) – determines whether the hash table contains given key </li></ul></ul><ul><ul><li>c ontainsValue(value) – determines whether the hash table contains given value </li></ul></ul><ul><ul><li>keySet( ) – returns a set of the keys </li></ul></ul><ul><ul><li>values() – returns a collection of the values </li></ul></ul>
    65. 65. Examples Using the HashMap Class
    66. 66. Hashtable - Example Map<String, Integer> studentsMarks = new HashMap<String, Integer>(); studentsMarks.put(&quot;Ivan&quot;, 4); studentsMarks.put(&quot;Peter&quot;, 6); studentsMarks.put(&quot;Maria&quot;, 6); studentsMarks.put(&quot;George&quot;, 5); int peterMark = studentsMarks.get(&quot;Peter&quot;); studentsMarks.remove(&quot;Peter&quot;); System.out.println(&quot;Is Peter in the hash table: &quot; + studentsMarks.containsKey(&quot;Peter&quot;)); for (Map.Entry<String, Integer> studentMark : studentsMarks.entrySet()) { System.out.printf(&quot;%s --> %d%n&quot;, studentMark.getKey(), studentMark.getValue()); }
    67. 67. Live Demo Using the HashMap Class
    68. 68. Counting Words in a Text String s = &quot;Welcome to our Java course. In this &quot; + &quot;course you will learn how to write simple &quot; + &quot;programs in Java&quot;; String[] words = s.split(&quot;[ ,.]&quot;); Map<String, Integer> wordsCount = new HashMap<String, Integer>(); for (String word : words) if (!&quot;&quot;.equalsIgnoreCase(word)) { int count = 1; if (wordsCount.containsKey(word)) count += wordsCount.get(word); wordsCount.put(word, count); } for (String word : wordsCount.keySet()) System.out.printf(&quot;%s --> %d%n&quot;, word, wordsCount.get(word));
    69. 69. Live Demo Counting Words in a Text
    70. 70. Summary <ul><li>ADT are defined by list of operations independent of the implementation </li></ul><ul><li>The basic data structures in the computer programming are </li></ul><ul><ul><li>List – ArrayList class in Java </li></ul></ul><ul><ul><li>Stack – Stack class in Java </li></ul></ul><ul><ul><li>Queue – LinkedList class in Java </li></ul></ul><ul><ul><li>Trees – can be binary, balanced, search trees, etc. </li></ul></ul><ul><ul><li>Dictionaries – Hash Map class in Java </li></ul></ul>
    71. 71. Basic Data Structures Questions?
    72. 72. Exercises <ul><li>Write a program that reads from the console a sequence of positive integer numbers. The sequence ends when the number 0 is entered. Calculate and print the sum and average of the elements of the sequence. Use the ArrayList class. </li></ul><ul><li>Write a method that finds the longest subsequence of equal numbers in given array. Use the ArrayList class. </li></ul><ul><li>Write a program that reads N integers from the console and reverses them using a stack. Use the Stack class. </li></ul>
    73. 73. Exercises (2) <ul><li>We are given the following sequence: </li></ul><ul><li>S 1 = N; </li></ul><ul><li>S 2 = S 1 + 1; </li></ul><ul><li>S 3 = 2*S 1 + 1; </li></ul><ul><li>S 4 = S 1 + 2; </li></ul><ul><li>S 5 = S 2 + 1; </li></ul><ul><li>S 6 = 2*S 2 + 1; </li></ul><ul><li>S 7 = S 2 + 2; </li></ul><ul><li>... </li></ul><ul><li>Write a program to print its first 100 elements for given N. Use the LinkedList class. </li></ul><ul><li>Example: N=2 </li></ul><ul><li>Sequence: 2, 3, 5, 4, 4, 7, 5, 6, 11, 7, 5, 9, 6, ... </li></ul>
    74. 74. Exercises (3) <ul><li>Write a program that reads a sequence of integers ending with 0 and sorts them in an increasing order. Use the ArrayList class. </li></ul><ul><li>Write a program that finds in a given array of integers how many times each of them presents. Use Hash Map and ArrayList . </li></ul><ul><ul><li>Example: array = {3, 4, 4, 2, 3, 3, 4, 3, 2} </li></ul></ul><ul><ul><li>2  2 times </li></ul></ul><ul><ul><li>3  4 times </li></ul></ul><ul><ul><li>4  3 times </li></ul></ul>
    75. 75. Exercises (4) <ul><li>Write a program that removes from a given sequence all negative numbers. </li></ul><ul><li>Write a program that removes from a given sequence all the numbers that present in it odd number of times. Example: </li></ul><ul><ul><li>{4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2}  {5, 3, 3, 5} </li></ul></ul><ul><li>By definition the majorant of an array is a value that occur in the least half of the elements of the array. Write a program to find the majorant of given array (if any). Example: </li></ul><ul><ul><li>{2, 2, 3, 3, 2, 3, 4, 3, 3}  3 </li></ul></ul>
    76. 76. Exercises (7) <ul><li>Write a program that counts how many times each word from a given text presents in it. The casing differences should be ignored. The result words should be ordered by their number of occurrences in the text. Example: </li></ul><ul><li>is  2 </li></ul><ul><li>the  2 </li></ul><ul><li>this  3 </li></ul><ul><li>text  6 </li></ul>This is the TEXT. Text, text, text – THIS TEXT! Is this the text?
    77. 77. Exercises (5) <ul><li>We are given numbers N and M and the following operations: </li></ul><ul><ul><li>N = N+1 </li></ul></ul><ul><ul><li>N = N+2 </li></ul></ul><ul><ul><li>N = N*2 </li></ul></ul><ul><li>Write a program that finds the shortest sequence of operations from the list above that starts from N and finishes in M </li></ul><ul><ul><li>Example: N = 5, M = 16 </li></ul></ul><ul><ul><li>Sequence: 5  7  8  16 </li></ul></ul>
    78. 78. Exercises (6) <ul><li>We are given a labyrinth of size N x N. Some of its cells are empty (0) and some are full (x). We can move from an empty cell to another empty cell if they share common wall. Given a starting position (*) calculate and fill in the array the minimal distance from this position to any other cell in the array. Use &quot;u&quot; for the unreachable cells. Example: </li></ul>0 0 0 x 0 x 0 x 0 x 0 x 0 * x 0 x 0 0 x 0 0 0 0 0 0 0 x x 0 0 0 0 x 0 x 3 4 5 x u x 2 x 6 x u x 1 * x 8 x 10 2 x 6 7 8 9 3 4 5 x x 10 4 5 6 x u x

    ×