Data structures

  • 566 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
566
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
33
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • A * B - ( C + D ) + E

Transcript

  • 1. Data Structures Introduction Stacks Queues Trees Linked List Sorting
  • 2. Chapter 01: Introduction
  • 3. 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
  • 4. Types of Data Structures DATA STRUCTURE LINEAR NON-LINEAR TREESARRAYS STACK QUEUE GRAPH LINKED LIST
  • 5. 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.
  • 6. 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)
  • 7. Chapter 02: Stacks
  • 8. Introduction
  • 9. Various types of stack
  • 10. 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.
  • 11. Empty stack
  • 12. Stack operations
  • 13. 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.
  • 14. 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.
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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 (-)
  • 19. Infix to Postfix Example A + B * C - D / EInfix 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 E A B C * D E / - +
  • 20. 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
  • 21. Chapter 03: Queue & Circular Queue
  • 22. 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.
  • 23. Queue Operations• Insertion – adding elements to the queue.• Deletion – removing elements from the queue.
  • 24. 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
  • 25. Deleting elements from queue• begin if front = rear then queue_empty else begin front := front+1 item := q[front]; end; end;
  • 26. Chapter 04: Linked List
  • 27. 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
  • 28. 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
  • 29. Linked List A B CHead node A data pointer
  • 30. Chapter 05: Sorting
  • 31. 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
  • 32. 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.
  • 33. 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; } } }
  • 34. 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
  • 35. Bubble Sort – AlgorithmFOR (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; } } }
  • 36. 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
  • 37. 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.
  • 38. 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 doesnt 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.
  • 39. 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.
  • 40. 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.
  • 41. 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 step 3. MERGE (A, p, q) // Conquer step. 4. MERGE (A, q + 1, r) // Conquer step. 5. MERGE (A, p, q, r) // Conquer step.
  • 42. Chapter 06: Searching
  • 43. 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.
  • 44. 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
  • 45. Unsorted List – Linear SearchFOR (I=0; I<N-1;I++) { IF (ARR [ I ] = = NUM) BREAK }IF (I = = 10) PRINT (NO. NOT PRESENT)ELSE PRINT (NO. PRESENT)
  • 46. Sorted List – Linear SearchFOR (I=0; I<N-1;I++) { IF (ARR [9] < NUM | | ARR [I] >= NUM) { IF (ARR [I] = = NUM) PRINT (PRESENT) ELSE PRINT (ABSCENT) BREAK } }
  • 47. 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.
  • 48. 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
  • 49. Binary Search – AlgorithmWHILE (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 } }
  • 50. Chapter 07: Binary Tree
  • 51. 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
  • 52. Binary Tree
  • 53. 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
  • 54. 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