Data Structures1 Basic Data Structures Elementary Structures Arrays Lists Search Structures Binary search Tree Hash Tables Sequence Structures Stacks Queues.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Fundamentals of Python: From First Programs Through Data Structures
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
Hashing / Hash tables Chapter 20 CSCI 3333 Data Structures.
Linked Lists CENG 213 Data Structures.
Stack & Queues COP 3502.
Fall 2002CMSC Discrete Structures1 Now it’s Time for… RecurrenceRelations.
Intro. to Data Structures 1CSCI 3333 Data Structures - Roughly based on Chapter 6.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
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.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Chapter 5 Linked Lists II
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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
CS 367 Introduction to Data Structures Lecture 5.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Elementary Data Structures
Data Structure By Amee Trivedi.
CS2006- Data Structures I Chapter 5 Linked Lists I.
Stacks.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
public class StrangeObject { String name; StrangeObject other; }
CMSC 341 Lecture 5 Stacks, Queues
Stacks Linked Lists Queues Heaps Hashes
More Data Structures (Part 1)
Lecture 16 Stacks and Queues CSE /26/2018.
CSI 1340 Introduction to Computer Science II
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

Data Structures1 Basic Data Structures Elementary Structures Arrays Lists Search Structures Binary search Tree Hash Tables Sequence Structures Stacks Queues Graphs

Data Structures2 data structure What is a data structure? A data structure (DS) is essentially a device that collects and allows the manipulation of data within the structure. DS = Model (idea) + operations A DS is independent of its implementation. In Java, use interface to specify operations of DS The interface describes the operation, but says nothing about its implementation. Example: A Stack Model: A Stack Operations: Add to stack Remove from stack check for empty look into stack empty the stack. Functionality specified using interface public interface Stack{ public void push(Object o); public Object pop(); public Object peek(); public boolean isEmpty(); public void makeEmpty(); }

Data Structures3 Stack Structure An array implementation of a Stack public class StackArray impements Stack{ private Objects[] s; private int top; public StackArray(int size){ s = new Object[size]; top = -1; } public void push(Object item){ s[++top] = item;} public Object pop(){return s[top--];} public Object peek(){return s[top];} public boolean isEmpty(){return (top==-1);} public void makeEmpty(){top = -1;} public boolean isFull(){return (top==s.length-1);} } max top 3

Data Structures4 Queue Structure DS: A Queue Idea: A Queue or a line Operations: Add to the Queue Remove from the Queue peek at front element check for empty Make empty public interface Queue{ public void enQueue(Object x); public Object deQueue(); public boolean isEmpty(); public void makeEmpty(); public Object peek(); } front back Implementation 1: Front always at array[0] Implementation 2: With wrap around. front back Can overflow

Data Structures5 Queue Structure public class QueueCircularArray implements Queue{ private Object[] q; private int front, back; private int usedSpace; public QueueCircularArray(int size){ q = new Object[size]; front = 0; back = size-1; usedSpace=0; } public void enqueue(Object item){ back = (back+1)%q.length; q[back] = item; usedSpace++; } public Object dequeue(){ Object item = q[front]; q[front] = null; front = (front+1)%q.length; usedSpace--; return item; } public boolean isEmpty(){ (usedSpace==0); } public boolean isFull() { (usedSpace==q.length);} public Object peek(){return q[front];} }

Data Structures6 What is a Dynamic Structure? A data structure where there is “no” size limitation. Characteristics of dynamic structures: Max size usually limited by machine memory Amount of memory used is a function of data stored in the structure. Data access is usually not random There is overhead involved in creating and maintaining such a structure

Data Structures7 Fixed (or static) verses Dynamic structures Disadvantages: Fixed size results in either overflow or under use of internal resources. Resizing is a computationally expensive operation. Advantages: Random/Direct access to data Usually easy to develop data structures using static structures Disadvantages: Lose of random access. Usually harder to implement. Advantages: No overflow or under use problems. Resizing is usually not expensive. Static structures: Dynamic Structures:

Data Structures8 Data Structure - a generic view: method1() method2() method3() method4() Implementation Either static of Dynamic Interface …dequeue(…)… …enqueue(…)… …isEmpty(…)… …makeEmpty(…) Methods of Queue Example: Queue Array or dynamic implementation of queue

Data Structures9 Dynamic Structures - preliminaries Integer p; Integer q; p q a1a1 ? p = new Integer(5); 5 a1a1 p = new Integer(6); p a2a2 5 a1a1 6 a2a2 q = p; p a2a2 6 a2a2 q a2a2 q = new Integer(9); 6 a2a2 a2a2 p 9 a3a3 a3a3 q p = null; 6 a2a2 p 9 a3a3 a3a3 q q=p; 6 a2a2 p 9 a3a3 q

Data Structures10 Dynamic Structures - preliminaries public class IntegerNode{ private int item; private IntegerNode next; public IntegerNode(int item){ setItem(item); next =null; } public void setItem(int item){ this.item = item; } public int getItem(){ return item; } public void setNext(InegerNode next){ this.next = next; } public IntegerNode getNext(){ return next; } IntegerNode n1; n1 ? n1 = new IntegerNode(5); n1 a1a1 item next a1a1 5 IntegerNode n2 = new IntegerNode(7); n2 a2a2 item next a2a2 7 n1.setNext(n2); n2 a2a2 item next a2a2 7 item next a1a1 5a2a2 n1 a1a1

Data Structures11 n2 =null; n2 item next a2a2 7 item next a1a1 5a2a2 n1 a1a1 Dynamic Structures - preliminaries n2 = n1.getNext(); n1 a1a1 5 a 2 a1a1 7 a2a2 n2 a2a2 n2.setNext(new IntegerNode(9)); 5 a 2 a1a1 7 a 3 a2a2 9 a3a3 n1 a1a1 n2 a2a2 IntegerNode n3 = n2; n2 = n2.getNext(); 5 a 2 a1a1 7 a 3 a2a2 9 a3a3 n1 a1a1 n3 a2a2 n2 a3a3 n2.setNext(new IntegerNode(3)); n2 = null; 5 a 2 a1a1 7 a 3 a2a2 9 a 4 a3a3 n1 a1a1 n3 a2a2 n2 3 a4a4 n3.setNext(null); 5 a 2 a1a1 7 a2a2 9 a 4 a3a3 n1 a1a1 n3 a2a2 n2 3 a4a4

Data Structures12 A dynamic Stack public class DStack implements Stack{ class StackNode{ int data; StackNode next; StackNode(int d){data = d; next = null;} }//inner class private StackNode top; private int size; public DStack(){ Rules regarding Constructors: ALWAYS initialize instance variables to null. } public void push(int d){ Rules regarding adding to dynamic structures: Consider 2 cases Case 1: Adding to an empty structure Case 2: Adding to the front of structure (maybe covered in 1) Case 2: Inserting to an internal position. }

Data Structures13 A dynamic Stack public int pop(){ Rules regarding removing from a dynamic structure: Case 1: Deleting a “root” element Case 2: Deleting an internal element

Data Structures14 IntegerNode temp = front; while(temp != null){ temp = temp.getNext(); } temp a1a1 IntegerNode temp = front; while (temp!=null){ Integer item = temp.getItem(); System.out.println(item.toString()); temp = temp.getNext(); } Traversing a Structure 9 a 4 a3a3 a4a4 # anan 5 a 2 a1a1 7 a 3 a2a2 front a1a1 3 … Rules regarding traversing a structure: 1. Never move the reference pointing to the root of the structure 2. Check for null before moving. Moving back and fourth along the structure. Traversing the structure below: Plain traversal:Printing the nodes in the structure: view animation!

Data Structures15 Dynamic Queue 9 a 4 a3a3 a4a4 # anan 5 a 2 a1a1 7 a 3 a2a2 front a1a1 3 … …enQueue(…)… …deQueue(…)… …isEmpty(…)… …makeEmpty(…) back anan … peek(…)… Instance variables of class Queue

Data Structures16 Dynamic Queue public class DynamicQueue implements Queue{ class ListNode{ //inner class int data; ListNode next; ListNode(int d, ListNode n){ data = d; next = n; } private ListNode front; back; public Queue(){ front = null; back = null; } RULE Always initialize instance references

Data Structures17 Dynamic Queue public class DynamicQueue implements Queue{ class ListNode{ //inner class int data; ListNode next; ListNode(int d){ data = d; next = null; } private ListNode front; back; public void enQueue(Object o){ //Case 1- adding the initial element ListNode n = new ListNode(o,null); if (front == null){ //Case 2- adding subsequent elements

Data Structures18 Dynamic Linked List 9 a 4 a3a3 a4a4 # anan 5 a 2 a1a1 7 a 3 a2a2 front a1a1 3 … …get(…)… …remove(…)… …add(…)… …search(…)

Data Structures19 List Structure List Structure: public Interface List{ public Object get(int index) throws IndexOutOfBoundsException; public void remove(int index) throws IndexOutOfBoundsException; public void add(Object item, int index) throws IndexOutOfBoundsException; public int search(Object item); public boolean isEmpty(); public void makeEmpty(); public int size(); } back Cannot add/remove/get items beyond “back” Can you implement this?

Data Structures20 Dynamic Linked List public class DList implements List{ class ListNode{ int data; ListNode next; ListNode(int o, ListNode n){ data = o; n = next; } private ListNode front; public List(){ …} public void add(Object o, int pos) throws … {…} public Object get(int pos) throws… {…} public void remove(int pos) throws… {…} public int search(Object o){…} public boolean isEmpty(){…} public int size(){…} public void makeEmpty(){…} } Observe: Inner class A single instance variable, front Client’s view same for both dynamic and static implementations.

Data Structures21 Dynamic Linked List RULE: Always initialize references to null or other known value in all dynamic structures. public class DList implements List{ class ListNode{ int data; ListNode next; ListNode(int o, ListNode n){ data = o; n = next; } private ListNode front; public List(){ front = null; } … }

Data Structures22 Dynamic Linked List named variable RULE: When making a change to any dynamic structure, treat changing a node referenced by a named variable as special case. public void add(Object o, int pos) throws IndexOutOfBoundsException { 0. Check for exception case 1. Create a new ListNode with o as the data 2. if inserting at the front of the list, then //named variable, front, changing!!! 3. Insert the new ListNode in front so as not to lose the remaining elements } 4. else{ //front, named variable, does not change 5. traverse to one-before the place to add 6. Insert the new element in so as not to lose the remaining list. }

Data Structures23 Dynamic Linked List public class DList implements List{ … /** * This method will add the data object o into position pos * in the list. o the data item to be added pos the position to which o should be added in the list IndexOutOfBoundsException will be thrown if (pos<0) * or (pos > size of the list) */ public void add(int o, int pos) throws IndexOutOfBoundsException{ if (pos size()) throw new IndexOutOfBoundsException(); ListNode n = new ListNode(o,null); //create a new node if (pos == 0){//adding to front? n.next(front);//add carefully! front = n; } else{//adding internally ListNode temp = front;//traverse for(int i=0; i<pos-1; i++) temp = temp.next; n.next = temp.next;//insert in temp.next = n; }

Data Structures24 Dynamic Linked List RULE: When making a change to any dynamic structure, treat making a change to a node referenced by a named variable a special case. public void remove(int pos) throws IndexOutOfBoundsException { 0. Check for exception case 1. Remove the first Node? //Named variable, front, changing!!! 2. Remove so as not to lose the rest of the list 3. else 4. Traverse to one before the node to remove 5. Detach the node so as not to lose the rest of the list }

Data Structures25 public class DList implements List{ … /** * The purpose of this method is to remove a data object from * the list at the given position the item position to be removed IndexOutOfBoundsException will be thrown if (pos<0) * or (pos>=size()) */ public void remove(int pos) throws IndexOutOfBoundsException{ if (pos =size()) throw new IndexOutOfBoundsException(); ListNode temp; if (pos == 0){//remove the first node? temp = front;//carefully - so as not to lose front = front.next;//the list! } else{//remove an interior node ListNode n = front;//traverse for(int i=0; i<pos-1; i++) n = n.next; temp = n.next; n.next = temp.next;//remove } temp.next = null; } Dynamic Linked List

Data Structures26 public class DList …{ … public int search(int target){ IntegerNode temp = front; int location = 0; while (temp!=null){ if (temp.getItem() == target)return location; location++; temp = temp.getNext(); } return -1; } public boolean isEmpty(){ return (front == null); } public int size(){ 1. traverse the list counting the elements 2. return the count } public void makeEmpty(){ front = null; } Dynamic Linked List