+ All Categories
Home > Documents > Data structures

Data structures

Date post: 08-May-2015
Category:
Upload: sneha-chopra
View: 713 times
Download: 2 times
Share this document with a friend
59
Data Structures Introduction Stacks Queues Trees Linked List Sorting
Transcript
Page 1: Data structures

Data Structures

IntroductionStacks

QueuesTrees

Linked ListSorting

Page 2: Data structures

Chapter 01: Introduction

Page 3: Data structures

What is Data Structure?

• A data structure is a structured set of variables associated with one another in different ways, cooperatively defining components in the system and capable of being operated upon in the program.

• The following operations are done on data structures:– Data organization or clubbing– Accessing technique– Manipulating selections for information.

• In computer science, data structure is a way of storing data in a computer so it can be stored efficiently

Page 4: Data structures

Types of Data Structures

DATA STRUCTURE

LINEAR NON-LINEAR

ARRAYS

LINKED LIST

STACK

QUEUE

TREES

GRAPH

Page 5: Data structures

Types of Data Structures• Data structures are classified into two main categories linear and non-linear.

• A data is said to linear if its elements form a sequence. • An array is a finite collection of similar elements stored in adjacent memory

locations. • Linked list is a collection of elements called nodes each of which stores two

items one information and the other a link to the next element. • A stack is a data structure in which addition of new elements or deletion of

old elements takes place at same end called top of stack. • Queue is a linear data structure that permits insertion of new elements takes

place at one end and deletion of elements takes place at other end.

Page 6: Data structures

Types of Data Structures

• Elements in a data structure which do not form a sequence are called as non-linear data structure.

• When any two vertices are connected by exactly one path is

called as tree. • Forest can be defined as any two vertices connected by at

most one path. • A graph can be defined as a set of dots (vertices and nodes)

joined by lines (the edges)

Page 7: Data structures

Chapter 02: Stacks

Page 8: Data structures

Introduction

Page 9: Data structures

Various types of stack

Page 10: Data structures

Stack

• A stack is a data structure in which addition of new elements and deletion of old elements always takes place at same end known as top of stack

• Stack is one of the most useful concepts which play a permanent role in programming.

• In stack he last item to be inserted will be the first to be removed.

• This is why stack is referred to as LAST IN FIRST OUT [LIFO].

• If there are no items in stack then it is called as empty stack.

Page 11: Data structures

Empty stack

Page 12: Data structures

Stack operations

Page 13: Data structures

Stack Operations

• We can insert or delete an element from a list, which takes place from one end.

• When an item is added to the stack the operation is called "push" and when it is removed the operation is called "pop".

• Due to the push operation from one end, elements are added to the stack, the stack is also known as pushdown list.

Page 14: Data structures

Stack Operations

• The following fundamental operations on stack:– Creating a stack– Checking stack—either empty or full– Initializing a stack– Insert (push) an element in the stack– Delete (pop) an element from the stack– Access the top element (peek)– Display elements of stack– Status: to identify present position of stack.

Page 15: Data structures

Stack Algorithm – Adding Elements

• Variables: int size=10, top=-1; Object stack[10];

• Proc: void push(Object item)• begin

if top is equal to size then display stack_full; else

top = top+1; stack[top] = item; end

Page 16: Data structures

Stack – Removing Elements

• Variables: int size=10, top=-1; Object stack[10];

• Proc: Object pop()• begin

if top is equal to -1 then stack_empty; else

item = stack[top]; top = top-1; end

Page 17: Data structures

Stack Application• Evaluation of arithmetic expressions.

– Infix to postfix conversion– Evaluating arithmetic expressions

• Runtime memory management. • Solving search problems. • Calling subroutine. • Syntax parsing. • Reversing of string

Page 18: Data structures

Infix to Postfix conversion

• Rules– If incoming operator has more precedence than top of stack

than push operator into stack.– If incoming operator has less or equal precedence than top of

stack than operator is popped and incoming operator is pushed. – If we encounter a right parenthesis “)”, keep performing pop

from the stack until we get matching left parenthesis. Do not output parenthesis.

• HIGHEST PRIORITY: exponentiation ($, ^)• MIDDLE PRIORITY: multiply (*) and divide (/)• LOWEST PRIORITY: addition (+) and subtraction (-)

Page 19: Data structures

Infix to Postfix ExampleA + B * C - D / E

Infix Stack(bot->top) Postfix A + B * C - D / E + B * C - D / E A B * C - D / E + A * C - D / E + A B C - D / E + * A B - D / E + * A B C D / E + - A B C * / E + - A B C * D E + - / A B C * D

+ - / A B C * D EA B C * D E / -

+

Page 20: Data structures

Postfix EvaulationOperand: pushOperator: pop 2 operands, do the math, push result back onto stack

1 2 3 + *

Postfix Stack( bot -> top )a) 1 2 3 + *b) 2 3 + * 1c) 3 + * 1 2d) + * 1 2 3e) * 1 5 // 5 from 2 + 3f) 5 // 5 from 1 * 5

Page 21: Data structures

Chapter 03: Queue & Circular Queue

Page 22: Data structures

Introduction

• Queue is a linear data structure that permits insertion of a new element at one end and deletion of element at the other end.

• The end at which deletion of element takes place is known as FRONT and the end at which addition of new elements takes place is known as REAR.

• The first element that gets added into queue is the first to get deleted.

• Hence queue is also referred to as FIFO list First In First Out.

Page 23: Data structures

Queue Operations

• Insertion – adding elements to the queue.• Deletion – removing elements from the

queue.

Page 24: Data structures

Adding elements in queue• Variables: int size=10, front=0, rear=-1;

Object stack[10];

• Proc: void add(Object item)

• begin if rear is equal to n then queue_full else begin rear :=rear+1; q[rear]:=item; end; end

Page 25: Data structures

Deleting elements from queue

• begin if front = rear then queue_empty else begin front := front+1 item := q[front]; end; end;

Page 26: Data structures

Chapter 04: Linked List

Page 27: Data structures

Introduction

• Alternate approach to maintaining an array of elements

• Rather than allocating one large group of elements, allocate elements as needed

• Q: how do we know what is part of the array?A: have the elements keep track of each other– use pointers to connect the elements together as a LIST of

things

Page 28: Data structures

Linked List

• A linked list is a series of connected nodes• Each node contains at least– A piece of data (any type)– Pointer to the next node in the list

• Head: pointer to the first node• The last node points to NULL

Page 29: Data structures

A

Head

B C

A

data pointer

node

Linked List

Page 30: Data structures

Chapter 05: Sorting

Page 31: Data structures

Introduction

• A sorting algorithm is an algorithm that puts elements of a list in a certain order.

• Some of the sorting techniques are:– Selection sort– Bubble sort– Quick sort– Merge sort

Page 32: Data structures

Selection Sort

• In this method to sort data in ascending order the 0th element is compared with all the other elements.

• If the 0th element is found to be greater than the compared element then they are interchanged.

• So after the first iteration the smallest element is placed at the 0th position.

• The same procedure is repeated for the first element and so on.

• So if there are n elements than after n-1 iterations the array becomes sorted.

Page 33: Data structures

Selection Sort - AlgorithmFOR (I=0; I<=N-1; I++) { FOR (J=I+1; J<=N; J++) { IF (ARR [I]>ARR [J]) { TEMP=ARR [I]; ARR [I] =ARR [J]; ARR [J] =TEMP; } } }

Page 34: Data structures
Page 35: Data structures

Bubble Sort• In this method to sort data in ascending order the 0th element is compared with

the 1st element.

• If found to b greater than the positions are interchanged.

• Than the 1st element is compared with the 2nd element if found to be greater than they are interchanged.

• In the same way all elements except the last are compared with the next element and interchanged if required.

• This is the 1st iteration and on completion the largest gets stored at the last position.

• As a result after all iterations the list becomes a sorted list.

• If there are n elements than after n-1 iterations the array becomes sorted

Page 36: Data structures

Bubble Sort – Algorithm FOR (I=0; I<=N-1; I++) { FOR (J=0; J<N-1-I; J++) { IF (ARR [I]>ARR [J]) { TEMP=ARR [I]; ARR [I] =ARR [J]; ARR [J] =TEMP; } } }

Page 37: Data structures
Page 38: Data structures

Quick Sort

• This algorithm is based on the fact that it is easier and faster to sort two small arrays than one large one.

• The basic strategy of quick sort is to divide and conquer.

• Quick sort is also known as partition exchange sort. • Quick sort may be defined conveniently by using

recursive procedure

Page 39: Data structures

Quick Sort• The basic divide-and-conquer process for sorting a subarray

S[p..r] is summarized in the following three easy steps:

Divide: Partition S[p..r] into two subarrays S[p..q-1] and S[q+1..r] such that each element of S[p..q-1] is less than or equal to S[q], which is, in turn, less than or equal to each element of S[q+1..r]. Compute the index q as part of this partitioning procedure

Conquer: Sort the two subarrays S[p...q-1] and S[q+1..r] by recursive calls to quicksort.

Combine: Since the subarrays are sorted in place, no work is needed to combing them: the entire array S is now sorted.

Page 40: Data structures

Quick Sort• The divide-and-conquer strategy is used in quicksort. Below the recursion step

is described:

• Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array.

• Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts.

• Sort both parts. Apply quicksort algorithm recursively to the left and the right parts.

Page 41: Data structures
Page 42: Data structures

Merge Sort

• Merging means combining two sorted lists into one list. • For this the elements from both the sorted lists are

compared. • The smaller of both the elements is than sorted in the

third array. • The sorting s complete when all the elements from both

the lists are placed in the third list.

Page 43: Data structures

Merge Sort• Suppose array a & b contain 5 elements each. Than merge sort algorithm

works as follows:

• The arrays a & b are sorted using any algorithm

• The 0th element from 1st array is compared with the 0th element in the 2nd array. The smaller of the two is placed in the 3rd array.

• Now the biggest of them is compared with the 1st element of the other array.

• The smaller of the two is placed in the third array.

• The same procedure is repeated till end of one of the arrays is reached.

• The remaining elements from the other array are placed directly into the third list as they are already in the sorted order.

Page 44: Data structures

Merge Sort – Algorithm

• To sort the entire sequence A[1 .. n], make the initial call to the procedure MERGE-SORT (A, 1, n).

• MERGE-SORT (A, p, r) 1. IF p < r // Check for base

2. THEN q = FLOOR[(p + r)/2] // Divide step3. MERGE (A, p, q) // Conquer step.4. MERGE (A, q + 1, r) // Conquer step.5. MERGE (A, p, q, r) // Conquer step.

Page 45: Data structures
Page 46: Data structures

Chapter 06: Searching

Page 47: Data structures

Introduction

• Searching is an operation which finds the location of a given element in a list.

• The search is said to be successful or unsuccessful depending on whether the element that is being searched for is found or not.

Page 48: Data structures

Linear Search

• This is the simplest method of searching.• In this method the element to be found is sequentially

searched in the list.• This method can be applied to a sorted or an unsorted array.• Searching in case of a sorted list starts from the 0th element

and continues until the element is found or an element whose value is greater than the value being searched is reached.

• Searching in case of an unsorted list starts from 0th element and continues until element is found or end or list is reached

Page 49: Data structures

Unsorted List – Linear Search

FOR (I=0; I<N-1;I++) { IF (ARR [ I ] = = NUM) BREAK }IF (I = = 10) PRINT (NO. NOT PRESENT)ELSE PRINT (NO. PRESENT)

Page 50: Data structures

Sorted List – Linear Search

FOR (I=0; I<N-1;I++) { IF (ARR [9] < NUM | | ARR [I] >= NUM) { IF (ARR [I] = = NUM) PRINT (PRESENT) ELSE PRINT (ABSCENT) BREAK } }

Page 51: Data structures

Binary Search

– This method is very fast and efficient.

– This search requires the list of elements to be in sorted order.

– In this method to search for an element we compare with the

element present at the centre of the list.

– If it matches than the search is successful.

Page 52: Data structures

Binary Search– Otherwise the list is divided into two halves:

– One from the 0th element to the centre of the list.

– The other from the centre to the end of the list.

– As a result all the elements in the first half are smaller than the centre element

whereas all elements in the second half are greater than the center element.

– The searching will now precede in either of t he two halves depending on

whether the element is greater or smaller than the middle element.

– Same process of comparing the center element and then dividing the array is

repeated for the first or second half.

– This process is repeated till element is found or division results in one element

Page 53: Data structures

Binary Search – Algorithm WHILE (NOT END OF INPUT) { LOW = 0 HI = N-1 WHILE (LOW<= HI) { MID = (LOW + HI) / 2 IF (KEY = = K [MID]) RETURN MID IF (KEY < K [MID]) HI = MID – 1 ELSE LOW = MID + 1 } }

Page 54: Data structures

Chapter 07: Binary Tree

Page 55: Data structures

Introduction

• Definition: A binary tree, T, is either empty or such that:– T has a special node called the root node;– T has two sets of nodes, LT and RT, called the left

subtree and right subtree of T, respectively; – LT and RT are binary trees

Page 56: Data structures

Binary Tree

Page 57: Data structures

Binary Tree - Terms

• Leaf: node that has no left and right children

• Parent: node with at least one child node

• Level of a node: number of branches on the path from root to node

• Height of a binary tree: number of nodes no the longest path from root to node

Page 58: Data structures

Binary Tree – Traversal • Inorder

– Traverse the left subtree– Visit the node– Traverse the right subtree

• Preorder– Visit the node– Traverse the left subtree– Traverse the right subtree

• Postorder– Traverse the left subtree– Traverse the right subtree– Visit the node

Page 59: Data structures

Recommended