Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATA STRUCTURE.

Similar presentations


Presentation on theme: "DATA STRUCTURE."— Presentation transcript:

1 DATA STRUCTURE

2 What is Data Structure? A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly used data structures include lists, arrays, stacks, queues, heaps, trees, and graphs.

3 The way in which the data is organized affects the performance of a program for different tasks.
Computer programmers decide which data structures to use based on the nature of the data and the processes that need to be performed on that data.

4 Strings Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Declaring a string is as simple as declaring a one dimensional array. Syntax: char str_name[size]; Initializing the string: char str[] = "stringrepresentation"; char str[50] = " stringrepresentation ";

5 Example Program

6 Arrays An array is collection of items stored at continuous memory locations. The idea is to declare multiple items of same type together. Syntax: data_type array_name[array_size]; // Array declaration by specifying size int arr[10]; // Array declaration by initializing elements int arr[] = {10, 20, 30, 40}

7 Algorithm Development
Development of Algorithm & Flowcharts is first step towards learning programming language. Generally you need to develop algorithm/flowchart of some process with step by step procedure. What is Algorithm? An algorithm is a finite sequence of well-defined steps or operations for solving a problem in a systematic manner. In another words we can say that an algorithm is a step-by-step problem-solving procedure. An algorithm generally takes some input, carries out a number of effective steps in a finite amount of time, and produces some output.

8 Rules for writing Algorithm
Each instruction should be precise and unambiguous. Each instruction should be executed in a finite time. One or more instructions should not be repeated infinitely. After executing the instructions, the desired results are obtained.

9 Write an algorithm to add two numbers entered by user.

10 Complexity Algorithm Analysis of algorithm is the process of analysing the problem-solving capability of the algorithm in terms of the time and size required (the size of memory for storage while implementation). However, the main concern of analysis of algorithms is the required time or performance. Generally, we perform the following types of analysis Worst-case − The maximum number of steps taken on any instance of size a. Best-case − The minimum number of steps taken on any instance of size a. Average case − An average number of steps taken on any instance of size a. Amortized − A sequence of operations applied to the input of size a averaged over time.

11 Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily.

12 Stack Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Mainly the following three basic operations are performed in the stack: Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Peek or Top: Returns top element of stack. isEmpty: Returns true if stack is empty, else false.

13 Representation of Stack

14 Queue Queue is a linear structure which follows a particular order in which the operations are performed. The order is First in First out (FIFO). The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

15 Circular Queue Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’.

16 Operations on Circular Queue
Front: Get the front item from queue. Rear: Get the last item from queue. enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at Rear position. deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position.

17 Representation of Circular Queue:

18 Single Linked Lists: Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers.

19 Doubly Linked Lists A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. Syntax:

20 Circular Linked Lists Why Circular Linked Lists?
In a singly linked list, for accessing any node of linked list, we start traversing from the first node. If we are at any node in the middle of the list, then it is not possible to access nodes that precede the given node. This problem can be solved by slightly altering the structure of singly linked list. In a singly linked list, next part (pointer to next node) is NULL, if we utilize this link to point to the first node then we can reach preceding nodes.

21 Sorting and Searching Techniques
Merge Sort: Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The concept of Divide and Conquer involves three steps: Divide the problem into multiple small problems. Conquer the subproblems by solving them. The idea is to break down the problem into atomic subproblems, where they are actually solved. Combine the solutions of the subproblems to find the solution of the actual problem.

22

23 Insertion Sort Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.

24 Quick Sort Quick Sort is also based on the concept of Divide and Conquer, just like merge sort. But in quick sort all the heavy lifting(major work) is done while dividing the array into subarrays, while in case of merge sort, all the real work happens during merging the subarrays. In case of quick sort, the combine step does absolutely nothing. It is also called partition-exchange sort. This algorithm divides the list into three main parts: 1. Elements less than the Pivot element 2. Pivot element(Central element) 3. Elements greater than the pivot element

25 Representation of Quick Sort

26 Heap Sort Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case running time. Heap sort involves building a Heap data structure from the given array and then utilizing the Heap to sort the array. What is heap? Heap is a special tree-based data structure, that satisfies the following special heap properties: Shape Property: Heap data structure is always a Complete Binary Tree, which means all levels of the tree are fully filled.

27 Heap Property: All nodes are either greater than or equal to or less than or equal to each of its children. If the parent nodes are greater than their child nodes, heap is called a Max-Heap, and if the parent nodes are smaller than their child nodes, heap is called Min- Heap.

28 Selection Sort Selection sort is conceptually the most simplest sorting algorithm. This algorithm will first find the smallest element in the array and swap it with the element in the first position, then it will find the second smallest element and swap it with the element in the second position, and it will keep on doing this until the entire array is sorted. It is called selection sort because it repeatedly selects the next- smallest element and swaps it into the right place.

29 How selection sort works?

30 TREES, TABLES AND SET Binary Search Tree operation: Binary Search Tree, is a node- based binary tree data structure which has the following properties: 1. The left subtree of a node contains only nodes with keys lesser than the node’s key. 2. The right subtree of a node contains only nodes with keys greater than the node’s key. 3. The left and right subtree each must also be a binary search tree. 4. There must be no duplicate nodes.

31 Searching a Key: To search a given key in Bianry Search Tree, we first compare it with root, if the key is present at root, we return root. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur for left subtree. Insertion of a Key: A new key is always inserted at leaf. We start searching a key from root till we hit a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.

32 Traversal of Binary Tree
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Following are the generally used ways for traversing trees.

33 Inorder Traversal: Preorder Traversal: Postorder Traversal:

34 AVL Trees AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes. An Example Tree that is an AVL Tree

35 An Example Tree that is NOT an AVL Tree

36 Hash Function A function that converts a given big phone number to a small practical integer value. The mapped integer value is used as an index in hash table. In simple terms, a hash function maps a big number or string to a small integer that can be used as index in hash table. Hashing is an improvement over Direct Access Table. The idea is to use hash function that converts a given phone number or any other key to a smaller number and uses the small number as index in a table called hash table. A good hash function should have following properties 1) Efficiently computable. 2) Should uniformly distribute the keys (Each table position equally likely for each key)

37 Collision Handling: Since a hash function gets us a small number for a big key, there is possibility that two keys result in same value. The situation where a newly inserted key maps to an already occupied slot in hash table is called collision and must be handled using some collision handling technique. Chaining:The idea is to make each cell of hash table point to a linked list of records that have same hash function value. Chaining is simple, but requires additional memory outside the table. Open Addressing: In open addressing, all elements are stored in the hash table itself. Each table entry contains either a record or NIL. When searching for an element, we one by one examine table slots until the desired element is found or it is clear that the element is not in the table.


Download ppt "DATA STRUCTURE."

Similar presentations


Ads by Google