data structures

29
Data Structures

Upload: manaswi-sharma

Post on 20-Jan-2015

1.299 views

Category:

Documents


9 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Data structures

Data Structures

Page 2: Data structures

What are Data Structures?• Data may be organized in many different ways.• A Data Structure is an arrangement of data in a

computer’s memory or on a disk.• The logical or mathematical model of a particular

organization of a data is called Data Structures.• The choice of a data model depends on two

consideration:1) It must be rich enough in structure to mirror the actual

relationships of the data in the real world.2) The structure should be simple enough that one can

effectively process the data when necessary.

Page 3: Data structures

Types Of Data Structures• The more commonly used data structures include lists,

arrays, stacks, queues, heaps, trees, and graphs.

1) Arrays: The simplest type of data structure is linear array. A linear array is list of a finite number n of similar data elements referenced respt by a set of n consecutive numbers.

2) Linked List: A list is an ordered set of data. It is often used to store objects that are to be processed sequentially. A list can be used to create a queue.

3) Trees: Data frequently contain a hierarchical relationships between various elements. The data structure which reflects this relationships is called a rooted tree graph or a tree.

Page 4: Data structures

4) Stack: A stack also called a last-in first-out (LIFO) system, is a linear list in which insertions and deletions can take place only at one end, called top.

5) Queue: A queue also called first-in first-out (FIFO) system, is a linear list in which deletion can take place only at one end of the list, the “front” of the list, and insertion can take place only at the other end of the list, the “rear” of the list.

6) Graph: Graphs are data structures rather like trees. Graphs often have a shape dictated by a physical or abstract problem. For example, nodes in a graph may represent cities, while edges may represent airline flight routes between the cities. Nodes are traditionally called vertices

Page 5: Data structures

Data Structure Operations• The data appearing in data structures are processed by

means of operations. The following are operations are major operations:a) Traversing: Accessing each record exactly once so that certain items in the record may be processed.b) Searching: Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions.c) Inserting: Adding a new record to the structure.d) Deleting: Removing a record from the structure.

Following two are special operations:

a) Sorting: Arranging the records in some logical order.b) Merging: Combining the records in two different sorted files into a single sorted file.

Page 6: Data structures

What are Algorithms?• An algorithm is a well-defined list of steps for solving a

particular problem.• Algorithms manipulate the data present in data structures in

various ways, such as searching for a particular data item and sorting the data.

• Flowcharts are pictorial representations of the algorithms.• Properties/ characteristics of the algorithm:

a) Finiteness: Total number of steps used in an algorithm should be finite.b) Definiteness: Each step of algorithm must be clear and unambiguous.c) Effectiveness: Every step must be basic and essential.d) Input/Output: The algorithm must accept zero or more input and must produce at least on output.

Page 7: Data structures

Algorithmic Problem

• There are mainly two types of algorithm:

a) Simple Flow Problem: these are very simple problems and the procedure flow is sequential manner ie step-by-step.

b) Branching Flow Problem: These sentences are not simple. It consists of condition, it tests the condition and accordingly transfers the flow.

Eg: simpleFlowProblem.txt branchingFlowPrblm.txt

Page 8: Data structures

Complexity of Algorithm• The analysis of algorithm is a major task. In order to compare

algorithms, we must have some criteria to measure the efficiency of an algorithm. The efficiency of an algorithm depends on time and space factors.

• Suppose M is an algorithm, and suppose n is the size of the input data. The time is measured by counting the number of key operations ,eg in sorting and searching algo, key operation is number of comparisons. The space is measured by counting the maximum of memory needed by the algorithm.

• The complexity of an algo M is the function f(n) which gives the running time and/or storage space requirement of the algorithm in terms of size n of the input data.

Page 9: Data structures

Example• Suppose we are given an English short story TEXT, and

suppose we want to search through TEXT for the first occurrence of a gvn 3-letter word W. If W is the 3-letter word “the” then it is likely that W occurs near the beginning of TEXT. So f(n) will be small. On the other hand, if W is the 3-letter word “zoo”, then W may not appear in TEXT at all, so f(n) will be large.

• From the above example complexity can be investigated as follows:a) Worst Case: When any searching algo finds required element in last attempt.b) Average Case: When any searching algo finds required element in some avg number of attempts.c) Best Case: When any searching algo finds required element in first attempt.

Page 10: Data structures

ARRAYS

Page 11: Data structures

• An array is a group of related data items that share a common name.

• An array should be of a single type, comprising of integers or strings and so on.

• Array elements are accessed by using index which is mentioned inside the subscript operator ([ ]).

Page 12: Data structures

Types of Array1) One-Dimensional Array: Creating array involves three

steps:a) Declaration:

type[ ] arrayname;or

type arrayname[ ];b) Creating new object of array:

arrayname= new type[size];c) Initialization of array:

arrayname[index] = value;or

type arrayname[ ]={list of values};

*****Give one example

Page 13: Data structures

2) Two-Dimensional Array:

Eg: int table[][]; //declaration

table=new int[2][3]; //creating new object of 2D //array with 2 rows and 3 cols.

int table[][]={{0,0,0},{1,1,1}}; //initialization

****Give one example.

Duplicating Array: this is one of the properties of an array by which we can make duplication of array. Object.clone() method is used for duplication.

Eg: Duplication.java

Page 14: Data structures

Searching Techniques

• Searching is used to locate the position of specific item in the array. Search can be performed by using following techniques:

a) Sequential Search.

b) Binary Search.

1) Sequential Search: It is also called linear search since we can search element on after the other in the array. First we compare the search key value with first index variable, then second and so on till we get the search key.

Algorithm: sequentialSearchAlgo.txt

Eg: SequentialSearch.java

Page 15: Data structures

2) Binary Search: Binary search relies on a divide and conquer strategy to find a value within an already-sorted collection. Algorithm: binarySearch.txtEg: Find 6 in {-1, 5, 6, 18, 19, 25, 46, 78, 102, 114}.

• Step 1 (middle element is 19 > 6):     -1  5  6  18  19  25  46  78  102  114

• Step 2 (middle element is 5 < 6):      -1  5  6  18  19  25  46  78  102  114

• Step 3 (middle element is 6 == 6):     -1  5  6  18  19  25  46  78  102  114

Page 16: Data structures

STACK

Page 17: Data structures

• Stack is the collection of ordered elements, which can be accessed through LIFO manner. One end is used for insertion as well as deletion purpose.

Stack Interface: Stack interface contains various operations on the stack which are as follows:

1) public void push (E element): inserting element on the stack is called as a PUSH operation.

2) public E pop(): deleting element from stack is called as POP operation.

3) public boolean isEmpty(): function checks if stack is empty or not. This function basically required when we remove elements from stack.

4) public E peek(): Access the topmost element.

5) public int size(): To check size of the stack.

Page 18: Data structures

Algorithms for Handling Stacka) Create a stack: 1) Start. 2) Assign the initial capacity to the elements through its object.3) Stop.

b) To check stack is empty or not?1) Start.2) Check that size=0.

If yesThen return true.

elsereturn false.

3) Stop.

Page 19: Data structures

c) To insert element in the stack:1) If stack is full?

if yesresize the stack.

2) size=size+1;3) Stack[size]= element4) Return stack/top.

d) To delete element from stack:1) Check that stack is empty

if yesthen print stack underflow & throw exception for empty

stackotherwise follow next step.

2) item=stack[top].3) top=top-1.4) Return item.5) Stop.

Page 20: Data structures

e) To access the topmost element from stack:

1) Check that stack is empty.

if yes

then print stack overflow & throw exception for empty stack.

otherwise follow next step.

2) Return item of the top.

3) Stop.

Eg: usestack.java

Page 21: Data structures

Queue

Page 22: Data structures

• A queue is a linear list of elements in which deletions can take place only at one end, called the front, and insertions can take place only at the other end called rear.

• Queues access elements in FIFO manner.

• Real world example: queues in hospital, college office’s, bus stop etc.

• Logically size of queue is infinite but practically it should be finite.

• Operations provided for queue are present in Queue interface:

1) public void add(E element): inserts element in the queue.

2) public E remove(): deletes element from the queue.

3) public boolean isEmpty(): function which checks queue is empty or not.

4) public E element(): access the front side element.

5) public int size(): to check the size of queue.

Page 23: Data structures

• Types of the Queue:

1) Simple or Linear Queue.

2) Circular Queue.

3) Priority Queue.

4) Dequeue.

Page 24: Data structures

Algorithm of Indexed Implementation of Queue

1) To create a queue:a) Start. b) Declare space for queue that means assign capacity for the queue.c) Stop.

2) To check Queue is Empty or Not?a) Start b) Call size() method & check its value=1

If yesreturn true

elsereturn false

c) Stop

Page 25: Data structures

3) To insert element in the queue:a) Startb) Check queue is full or not by its size

if yesthen call resize() function for extra size

c) Add element by rear size.d) Check that rear < total element-1

if yesincrease rear size by 1.

elserear=0

e) Stop.

Page 26: Data structures

4) To delete element from queue:a) Startb) check that queue is empty or not by checking its size.

if yesthen throw exception

c) Item=queue[Front]d) check that Rear=Front

if yes thenRear=0.Front=0.

e) Check that front==total elements lengthif yes

then front=0f) stop.

Page 27: Data structures

5) To count size of the Queue:a) Start.b) Check that front <=rear

If yesReturn rear-front

elsereturn rear-front + element.length

c) Stop.

Page 28: Data structures

Applications of Queue

1) Queues are more useful in OS:

a) Processor management creates ready queue of the process by the CPU Scheduler Algorithm.

b) Batch Processing.

c) File Manager.

d) Job scheduling & Device scheduling.

e) Every IO devices has their own queue to collect requests from different applications or processing.

2) Queues are used to traversing all nodes of the graph.

3) Queues are used to traversing all nodes of the tree.

4) It is also used in different Artificial Intelligent programs.

Page 29: Data structures