* Lists, Stacks, Queues, Trees, Hash Tables

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Chapter 24 Lists, Stacks, and Queues
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
John Hurley Cal State LA
The ADT Hash Table What is a table?
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Chapter 10: Data Structures II
CS252: Systems Programming Ninghui Li Program Interview Questions.
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Skip List & Hashing CSE, POSTECH.
Hashing as a Dictionary Implementation
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TCSS 342, Winter 2005 Lecture Notes
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Arrays, Lists, Stacks, Queues Static and Dynamic Implementation Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Dictionaries, Hash Tables, Hashing, Collisions, Sets Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009.
Hashing as a Dictionary Implementation Chapter 19.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
CSE373: Data Structures & Algorithms
ADT description Implementations
Data Structure By Amee Trivedi.
Data Abstraction & Problem Solving with C++
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Problems with Linked List (as we’ve seen so far…)
Hashing Exercises.
Cse 373 April 26th – Exam Review.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CS313D: Advanced Programming Language
Lesson 6. Types Equality and Identity. Collections.
Advanced Implementation of Tables
Advanced Implementation of Tables
Presentation transcript:

* Lists, Stacks, Queues, Trees, Hash Tables 3/25/201707/16/96 Basic Data Structures Lists, Stacks, Queues, Trees, Hash Tables (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Contents Abstract Data Types (ADT) Lists – ArrayList Class * 3/25/201707/16/96 Contents Abstract Data Types (ADT) Lists – ArrayList Class Stacks – Stack Class Queues – Queue Class Trees – Terminology and Types Dictionaries – HashMap Class (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Abstract Data Types An Abstract Data Type (ADT) is a data type together with the operations, whose properties are specified independently of any particular implementation ADT are set of definitions of operations (like the interfaces in Java) Can have several different implementations Different implementations can have different efficiency

Basic Data Structures Linear structures Trees Dictionaries (maps) Lists: Variable-size Stacks: LIFO (last in first out) structure Queues: FIFO (first in first out) structure Trees Dictionaries (maps) Contain pairs (key, value) Hash tables: Unordered lists which use a ‘hash function’ to insert and search

* 3/25/201707/16/96 What Is a List? (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The List ADT List is linear data structure (container) that contains a sequence of elements (objects) Has variable size Objects are arranged linearly Can be implemented in several ways Statically (using array) Dynamically (linked implementation) Using the ArrayList class

Static and Linked Lists * 3/25/201707/16/96 Static and Linked Lists (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Static List Implemented by an array Provide direct access by index Usually has limited capacity Resizing is slow operation Slow insert and deletion 1 2 3 4 5 6 7 8 L 11 7 18 14 5 2 33 47 3

Linked List Dynamic (pointer-based) implementation Different forms Singly-linked and doubly-linked Sorted and Unsorted Singly-linked List - each “Object” has “value” and “next” fields null 11 7 18 14 head next next next next

* 3/25/201707/16/96 Linked List (2) Doubly-linked List - each “Object” has “value”, “next” and “prev” fields tail head 11 7 18 14 next next next next prev prev prev prev null null (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Using the ArrayList class * 3/25/201707/16/96 Using the ArrayList class (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The java.util.ArrayList Class Implements the list data structure using an array whose size is dynamically increased as needed Allocates in advance buffer space for new elements (for better performance) Insertion methods: add(Object) – adds an object at the end add(index, Object) – inserts an object to the list at a specified position size() – returns the number of elements

The ArrayList Class Deletion methods: Other supported methods: remove(Object) – removes the first occurrence of a specific object remove(index) – removes the element at the specified position clear() – removes all elements Other supported methods: contains(), toArray()

The ArrayList Class(2) ArrayList can contain any data type Elements are added directly Typecasting is required when extracting elements unless we use Generics Converting to array List list = new ArrayList(); list.add(5); // Add integer value list.add("some string"); // Add string value int firstElement = ((Integer)(list.get(0))).intValue(); String secondElement = (String)list.get(1); Integer[] arr = list.toArray(new Integer[list.size()]);

Specifies that String is actual type of this List What are Generics Generics are classes or interfaces that can be instantiated with a variety of types They have 1 or more formal type parameters When using a generic you specify an actual type Specifies that String is actual type of this List List<String> list = new ArrayList<String>(); String s = new String("li1"); list.add(s); list.add(5); // This will cause compile time error 5 is not a String

Primes[n..m] – Example Find all prime numbers in a specified interval 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;

Primes[n..m] Live Demo * 3/25/201707/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

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)) { return union.toArray(new Integer[union.size()]); //Example continues...

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()]);

Union and Intersection * 3/25/201707/16/96 Union and Intersection Live Demo (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* 3/25/201707/16/96 What is a Stack? (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The Stack ADT LIFO (last in first out) structure Elements inserted (push) at “top” Elements removed (pop) from “top” Can be implemented in several ways Statically (using array) Dynamically (linked implementation) Using the Stack class

Static Stack Array-based (static) implementation Usually has limited capacity Has top variable, pointing to the top IsEMPTY(S) Check if S.top = -1 1 2 3 4 5 6 7 8 S 11 7 18 14 top

Linked Stack Dynamic (pointer-based) implementation Each “object” has “value” and “next” fields Dynamically create and delete objects 11 7 18 14 next next next next null top

Using the Stack class * 3/25/201707/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The Stack Class – Overview Implements the stack data structure using an array whose size is dynamically increased as needed Major methods: push(object) – inserts elements to the stack pop() – removes and returns the top element from the stack peek() – returns the top element of the stack without removing it

The Stack Class – More Methods Other methods: size() – returns the number of elements clear() – removes all elements contains(object) – determines whether given element is in the stack toArray() – converts the stack to array

Using the Stack class Examples * 3/25/201707/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Stack – Example Using push(), pop() and peek() methods public static void main(String[] args) { Stack<String> stack = new Stack<String>(); stack.push("1. Ivan"); stack.push("2. Nikolay"); stack.push("3. Maria"); stack.push("4. George"); System.out.println("Top = " + stack.peek()); while (stack.size() > 0) { String personName = stack.pop(); System.out.println(personName); }

Using the Stack class Live Demo * 3/25/201707/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Matching Brackets – Example 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. Example: 1 + (3 + 2 - (2+3) * 4 - ((3+1)*(4-2))) Result: (2+3) (3+1) (4-2) ((3+1)*(4-2)) (3 + 2 - (2+3) * 4 - ((3+1)*(4-2)))

Matching Brackets – Solution with a Stack String expression = "1 + (3 + 2 - (2+3) * 4 - ((3+1)*(4-2)))"; 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); }

Matching Brackets Live Demo * 3/25/201707/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* 3/25/201707/16/96 What is a Queue? (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The Queue ADT FIFO (first in first out) structure Elements inserted at tail (enqueue) Elements removed from head (dequeue) Useful in many situations Processing jobs, print queues, messages Can be implemented in several ways Statically (using array) Dynamically (using pointers) Using the LinkedList class

Static Queue Static (array-based) implementation Queue has limited (fixed) capacity Implement as a “circular array” Maintain Q.Capacity and Q.Length Has head and tail variables, pointing to the head and the tail 1 2 3 4 5 6 7 8 Q 11 7 18 14 head tail

Linked Queue Dynamic (pointer-based) implementation Each “object” has “value” and “next” fields Dynamically create and delete objects 11 7 18 14 next next next next null head tail

Using the LinkedList class * 3/25/201707/16/96 Using the LinkedList class (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The LinkedList Class – Overview Implements the queue data structure using a doubly-linked list Major methods: offer(object) – adds an object to the end of the queue poll() – removes and returns the object at the beginning of the queue peek() – returns the object at the beginning of the queue without removing it

The LinkedList Class – More Methods Other methods: size() – gets the number of elements contained in the queue clear() – removes all elements from the queue contains(object) – determines whether given element is in the queue toArray() – converts the queue to array

Using the LinkedList class * 3/25/201707/16/96 Using the LinkedList class Examples (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Queue – Example Using offer() and poll() methods public static void main(String[] args) { Queue<String> queue = new LinkedList<String>(); queue.offer("Message One"); queue.offer("Message Two"); queue.offer("Message Three"); queue.offer("Message Four"); queue.offer("Message Five"); while (queue.size() > 0) { String msg = queue.poll(); System.out.println(msg); }

Using the LinkedList class * 3/25/201707/16/96 Using the LinkedList class Live Demo (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Sequence N, N+1, 2*N We are given the sequence: S = N, N+1, 2*N, N+2, 2*(N+1), 2*N+1, 4*N, ... Write a program to find the first index of given number P Example: N = 3, P = 16 S = 3, 4, 6, 5, 8, 7, 12, 6, 10, 9, 16, 8, 14, ... Index of P = 11 +1 +1 +1 *2 *2 *2

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("Index = " + index); return; } queue.offer(current + 1); queue.offer(2 * current);

Sequence N, N+1, 2*N Live Demo * 3/25/201707/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* Definition, Types of Trees 3/25/201707/16/96 What is Tree? Definition, Types of Trees (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Trees Terminology Node, edge, root, child, children, siblings, parent, ancestor, descendant, predecessor, successor, internal node, leaf, depth, height 17 Depth 0 9 15 Height = 2 14 Depth 1 6 5 8 Depth 2

Binary Trees Binary trees: most used form Each node has at most 2 children “root” “right child” “left subtree” 17 9 15 6 5 8 10 “left child”

Binary Trees Traversals * 3/25/201707/16/96 Binary Trees Traversals Traversal can be done in pre-order, in-order and post-order Pre-order: left, root, right – 6, 9, 12, 17, 19, 25 In-order: root, left, right – 17, 9, 6, 12, 19, 25 Post-order: left, right, root – 6, 12, 9, 25, 19, 17 17 19 9 6 12 25 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Binary Search Trees Binary search trees are ordered * 3/25/201707/16/96 Binary Search Trees Binary search trees are ordered A binary tree in which binary-search-tree property holds: For each node x in the tree All the elements of the left subtree of x are ≤ x All the elements of the right subtree of x are > x Binary search trees can be balanced Balanced trees has low height (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Binary Search Trees Example of binary search tree * 3/25/201707/16/96 Binary Search Trees Example of binary search tree If the tree is balanced, adding, searching, and deletion operations take approx. log(n) steps 17 19 9 6 12 25 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

What is a Dictionary (Map)? * 3/25/201707/16/96 What is a Dictionary (Map)? (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The Dictionary (Map) ADT The ADT "dictionary" maps key to values Also known as "map" or "associative array" Contains a set of (key, value) pairs Dictionary ADT operations: ADD(key, value) FIND_BY_KEY(key)  value DELETE(key) Can be implemented in several ways List, array, hash table, balanced tree, ...

What is a Hash Table? * 3/25/201707/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* 3/25/201707/16/96 Hash Table A hash table is an array that holds a set of (key, value) pairs The process of mapping a key to a position in a table is called hashing 1 2 3 4 5 m-1 T ... ... ... ... ... ... ... h(k) (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Hash Functions and Hashing A hash function maps keys to positions It is denoted by h The hash table has m slots, indexed from 0 to m-1 For any value k in the key range and some hash function h h(k) = i 0  i < m 1 2 3 4 5 m-1 T ... ... ... ... ... ... ... h(k)

Mapping Functions Perfect hashing function (PHF) h(k) : one-to-one mapping from each key k to integers in [0, m-1] The PHF maps each key to a distinct integer within some manageable range Finding a perfect hashing function is in most cases impossible More realistically Hash functions h(k) map most of the keys onto unique integers, but not all

Collisions in Hash Tables Collision is the situation when different keys can have the same hash value h(k1) = h(k2) for k1 ≠ k2 When the number of collisions is sufficiently small, the hash tables work quite well (fast) Several collisions resolution strategies Chaining in a list, re-hashing, using the neighboring slots (linear probing), ...

Collision Resolution - Chaining h("Pesho") = 4 h("Lili") = n-1 h("Kiro") = 2 h("Mimi") = 1 h("Ivan") = 2 collision 1 2 3 4 5 n-1 T ... null null null Mimi Kiro Pesho Lili chaining Ivan null null null null

Using the HashMap class * 3/25/201707/16/96 Using the HashMap class (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

The HashMap Class – Overview Implements the ADT dictionary as array dynamically increased as needed Contains a collection of key-and-value pairs arranged by the hash code of the key Collisions are resolved by chaining The HashMap class relies on Object.hashCode() method for calculating the hash codes of the elements Object.equals() method for comparing elements

The HashMap Class – Major Operations put(key, value) – adds an element with the specified key and value into the hash table remove(key) – removes the element with the specified key from the hash table get(key) – returns element by key clear() – removes all elements from the hash table

The HashMap Class – More Operations size() – returns the number of elements containsKey(key) – determines whether the hash table contains given key containsValue(value) – determines whether the hash table contains given value keySet() – returns a set of the keys values() – returns a collection of the values

Using the HashMap Class * 3/25/201707/16/96 Using the HashMap Class Examples (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Hashtable - Example Map<String, Integer> studentsMarks = new HashMap<String, Integer>(); studentsMarks.put("Ivan", 4); studentsMarks.put("Peter", 6); studentsMarks.put("Maria", 6); studentsMarks.put("George", 5); int peterMark = studentsMarks.get("Peter"); studentsMarks.remove("Peter"); System.out.println("Is Peter in the hash table: " + studentsMarks.containsKey("Peter")); for (Map.Entry<String, Integer> studentMark : studentsMarks.entrySet()) { System.out.printf("%s --> %d%n", studentMark.getKey(), studentMark.getValue()); }

Using the HashMap Class * 3/25/201707/16/96 Using the HashMap Class Live Demo (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Counting Words in a Text String s = "Welcome to our Java course. In this " + "course you will learn how to write simple " + "programs in Java"; String[] words = s.split("[ ,.]"); Map<String, Integer> wordsCount = new HashMap<String, Integer>(); for (String word : words) if (!"".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("%s --> %d%n", word, wordsCount.get(word));

Counting Words in a Text * 3/25/201707/16/96 Counting Words in a Text Live Demo (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* 3/25/201707/16/96 Summary ADT are defined by list of operations independent of the implementation The basic data structures in the computer programming are List – ArrayList class in Java Stack – Stack class in Java Queue – LinkedList class in Java Trees – can be binary, balanced, search trees, etc. Dictionaries – HashMap class in Java (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 Questions?

* 3/25/201707/16/96 Exercises 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. Write a method that finds the longest subsequence of equal numbers in given array. Use the ArrayList class. Write a program that reads N integers from the console and reverses them using a stack. Use the Stack class. (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Exercises (2) We are given the following sequence: S1 = N; S2 = S1 + 1; S3 = 2*S1 + 1; S4 = S1 + 2; S5 = S2 + 1; S6 = 2*S2 + 1; S7 = S2 + 2; ... Write a program to print its first 100 elements for given N. Use the LinkedList class. Example: N=2 Sequence: 2, 3, 5, 4, 4, 7, 5, 6, 11, 7, 5, 9, 6, ...

Exercises (3) Write a program that reads a sequence of integers ending with 0 and sorts them in an increasing order. Use the ArrayList class. Write a program that finds in a given array of integers how many times each of them presents. Use HashMap and ArrayList. Example: array = {3, 4, 4, 2, 3, 3, 4, 3, 2} 2  2 times 3  4 times 4  3 times

Exercises (4) Write a program that removes from a given sequence all negative numbers. Write a program that removes from a given sequence all the numbers that present in it odd number of times. Example: {4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2}  {5, 3, 3, 5} 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: {2, 2, 3, 3, 2, 3, 4, 3, 3}  3

* 3/25/201707/16/96 Exercises (7) 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: is  2 the  2 this  3 text  6 This is the TEXT. Text, text, text – THIS TEXT! Is this the text? (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* 3/25/201707/16/96 Exercises (5) We are given numbers N and M and the following operations: N = N+1 N = N+2 N = N*2 Write a program that finds the shortest sequence of operations from the list above that starts from N and finishes in M Example: N = 5, M = 16 Sequence: 5  7  8  16 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* 3/25/201707/16/96 Exercises (6) 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 "u" for the unreachable cells. Example: x * 3 4 5 x u 2 6 1 * 8 10 7 9 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*