Download presentation

Presentation is loading. Please wait.

Published byHerbert Long Modified about 1 year ago

1
Presented by : Preeti Banswal

3
What is data structure? A data structure is a way of organizing data that considers not only the items stored, but also their relationship to each other. Advance knowledge about the relationship between data items allows designing of efficient algorithms for the manipulation of data.

4
Areas in which data structures are applied extensively: Operating System. Database Management System,. Statistical analysis package, Numerical Analysis, Compiler Design. Graphics. Artificial Intelligence. Simulation.

5
Data structure Primitiveintcharfloat Non- Primitive LinearStackQueue Linked list Non- linear TreesGraphs

7
Basic Concepts Arrays and Structures. Stacks and Queue. Linked Lists. Trees, Binary trees. Graphs Efficient Binary Search Trees. Contents :

8
Basic Concepts : Pointers and dynamic memory allocation. Algorithm Specification. Data abstraction. Performance analysis. Performance measurement

9
Pointers: Pointers are fundamental to C. C provides extensive support for pointer. & : Address Operator * : Dereferencing or Indirection operator Example : int i, *pi; pi=&i;

10
Pointer Operations in C Creation & variableReturns variable’s memory address Dereference pointerReturns contents stored at address Indirect assignment * pointer = valStores value at address Assignment pointer = ptrStores pointer in another variable

11
Some pointers: Generic Pointer : void pointer in C is known as generic pointer. Generic pointer is a pointer which can point type of data. NULL pointer is a pointer which is pointing to nothing. int *ptr=NULL

12
Wild Pointer : A pointer in C which has not been initialized is known as wild pointer. Dangling pointers : Dangling pointers in computer programming are pointers that do not point topointers a valid object of the appropriate type.

13
Memory Allocation Memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. Memory allocation has two core types, 1. Static Memory Allocation: The program is allocated memory at compile time. 2.Dynamic Memory Allocation: The programs are allocated with memory at run time.

14
Difference between static & dynamic memory allocation: Pointers in C14 Static Memory AllocationDynamic Memory Allocation Memory is allocated before the execution of the program begins. (During Compilation) Memory is allocated during the execution of the program. No memory allocation or deallocation actions are performed during Execution. Memory Bindings are established and destroyed during the Execution. Variables remain permanently allocated. Allocated only when program unit is active. Implemented using stacks and heapsImplemented using data segments. Pointer is needed to accessing variables. No need of Dynamically allocated pointers. Faster execution than DynamicSlower execution than static More memory Space required.Less Memory space required.

15
Dynamic memory functions: malloc( ) Allocates specified number of bytes Syntax : void * malloc (size_t size); calloc( ) Allocates specified number of bytes and initializes to zero. Syntax : void *calloc(size_t nitems, size_t size) where, nitems -- This is the number of elements to be allocated. size -- This is the size of elements

16
realloc( ) Increases or decreases size of specified block of memory. Syntax : void *realloc(void *ptr, size_t size) free( ) De-allocate memory, returns specified block of memory back to the system.

17
Data type & Abstract Data type: A data type is a collection of objects and a set of operations that act on those object. Eg: int, char, float. An abstract data type (ADT) is a data type that is organized in such a way that the specification of objects is separated from representation of objects and the implementation of the operations.

18
Algorithm Specification: An algorithm is finite set of instruction that, if followed, accomplishes a particular task. Criteria that every algorithm should follow: 1.Input 2.Output 3.Definiteness 4.Finiteness 5.Effectiveness

19
Performance Analysis : Space complexity : Space complexity of a program is the amount of memory that it needs to run to completion. 1: Fixed space requirement. 2: Variable space requirement. Time complexity : The time complexity of a program is the amount of computer time that it needs to run to completion. S(P) = c+ Sp(I)

20
The recursive function has lower step count then its iterative counterpart. Recursive function typically run slower than the iterative version & takes more time than those of the iterative function. Recursive function also uses more memory space for its each call. For space complexity, iterative function is better than recursive function. For time complexity, recursive function is better than iterative function.

21
Arrays and Structures Arrays : An Array is a finite collection of similar elements stored in adjacent memory locations. Arrays One-dimensional Array Multidimensional Array

22
One dimensional array

23
Multi-dimensional Array: Representation of multidimensional arrays : Consider matrix of N*N,each entry of which is floating number. Float m[N][N] ; An element of this matrix m, say m i,j will be accessed as, m[i-1][j-1]; we can also define 4-dimensional integers array as follows, int k[N][M][K][L];

24
Structure A Structure is a collection of data of different type. struct is used. eg: struct { char name[10]; int age; float salary; } person; Notice the use of the (. ) as the structure member operator. We use this operator to select a particular member of the structure.

25
We can create our own structure data types by using the typedef statement below: typedef struct { char name[10]; int age; float salary; } person; person p1, p2 ;

26
Unions A union declaration is similar to a structure, but the field of a union must share their memory space. This means that only one field of the union is active at any given time.. eg: union abc { char c; long l; } ;

27
Self-Referential Structure A self-referential Structure is one in which one or more of its components is a pointer to itself. self-referential structure usually require dynamic storage management routines (malloc and free) to explicitly obtain and release memory. eg: typedef struct { char data; struct list *link ; } list;

28
Polynomial : Structure: MAX_TERMS 100 /*size of terms array*/ typedef struct { float coef; int expon ; } polynomial; polynomial terms[MAX_TERMS]; int avail=0;

29
Polynomial : Array representation: ex: A(x)= 2x^1000 + 1 B(x)=x^4+10x^3+3x^2+1 finish A avail startA startB finish B Coef: Expon: 2111031 100004320

30
Sparse matrix Structure : Define MAX_TERMS 101 /*maximum number of term +1*/ typedef struct { int cols; int rows; int value; }terms; Term a[MAX_TERMS];

32
Stacks : A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This end is called as top of the stack. Stack is also called as last–in-first-out (LIFO).

33
When an item is added to a stack, the operation is called as push. When an item is removed from the stack, the operation is called as pop.

34
Structure for stack: # define MAX_STACK_SIZE 100 /* maximum stack size*/ typedef struct { int key; /*other field */ } elements; element stack[ MAX_STACK_SIZE]; int top= -1;

35
Stack empty condition : top = MAX_STACK_SIZE - 1

36
int stack[5], top = -1; void push( ) { int item; if (top<=4) { printf(“\n Enter the number”); scanf(“%d”, &item); top=top+1; stack [top] = item; } else { printf(“\n stack overflow”); }

37
int stack [5],top; void pop ( ) { int item; if (top>=0) { item = stack [top]; top=top-1; printf("\n Number deleted is = %d ", item); } else { printf("\n stack is empty"); }

38
Application of stack Direct applications Page-visited history in a Web browser Undo sequence in a text editor Saving local variables when one function calls another, and this one calls another, and so on. Indirect applications Auxiliary data structure for algorithms Component of other data structures

40
Queue is a linear data structure that permits the insertion of new element at one end and deletion of an element at the other end. The end at which the deletion of an element takes place is called as front. Which insertion of new element can take place is called as rear. Queue is also called as first-in-first-out (FIFO). Queue

41
ENQUEUE BACKFRONT DEQUEUE

42
Structure of Queue: # define MAX_QUEUE_SIZE 100 /* maximum Queue size*/ typedef struct { int key; /*other field */ } elements; element queue[ MAX_QUEUE_SIZE]; int rear= -1; int front= -1;

43
Queue empty condition : front == rear Queue full condition: rear == MAX_QUEUE_SIZE -1

44
int queue [5], front = -1, rear = -1 ; Void queue ( ) { int item ; if (rear < 4) { printf(“\n Enter the number ”); scanf(“%d ” & item ); if (front = = -1 ) { front = 0 ; rear = 0; }

45
int queue [5], front, rear ; void delete ( ) { int item ; If ( front ! = -1) { item = queue [front]; if (front = = rear )

46
{ front = -1; rear = -1 ; } front = front +1 ; } printf (“\n Number deleted is = %d “, item ); } else { printf( “ Queue is empty”); }

47
Application of Queue 1) Serving requests of a single shared resource (printer, disk, CPU),transferring data asynchronously (data not necessarily received at same rate as sent) between two processes (IO buffers), e.g., pipes, file IO, sockets. 2) Call center phone systems will use a queue to hold people in line until a service representative is free. 3) When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling.

49
Expression using stack:

51
? ? ?

52
Disadvantage of Queue : In simple queue, if rear is present at the maxsize of queue, though there may empty slots at beginning or middle of queue, queue will be reported as full…. the solution here is to use circular queue

53
Circular queue:

54
Disadvantages of queue representation using static array: Drawback is that here we can allocate memory statically, so if array is not fully utilized, memory will be wasted and once array declared, we can not increase size of array …. So element in queue must be inserted within given array….

55
Drawbacks of sequential representation: Drawbacks of sequential i.e. array representation is that it is completely static representation….. We can not increase or decrease size of array at runtime.. In sequential representation, elements are stored in adjacent memory locations where as in dynamic representation(linked list), every node is holding address of next node…

57
Linked list: Linked list is a very common data structure often used to store similar data in memory. This memory is randomly selected by the compiler. The order of the elements is maintained by explicit links between them.

58
Data Link Node

59
Structure of linked list : typedef struct listNode *listpointer; typedef struct { int data; listPointer link; } listNode;

60
Types of linked list Singly linked list Doubly linked list Circular linked list

61
ADVANTAGES OF LINKED REPRESENTATION OVER SEQUENTIAL REPRESENTATION 1)In sequential representation the memory is allocated sequentially whereas in linked representation memory is allocated randomly. 2)In sequential representation we does not require address of next element where as in linked representation to access list elements in the correct order,with each element we store the address of the next element in that list… etc.

62
3) In array, element are stored in adjacent memory locations whereas linked list every node allocates the memory wherever available, linked list is also called scattered family. 4) In linked list, every node knows where the next node is present in memory with the help of link, it is not possible in array…

63
Polynomial using linked list

64
Structure for node of polynomial: typedef struct polyNode *polyPointer typedef struct { int coef; int expon; polyPointer link; } polyNode; polyPointer a, b;

65
Sparse Matrix Sparse matrix is that matrix which has more number of zero’s or can also be said as that matrix which consist of less number of non- zero numbers.

66
REPRESENTATION OF SPARSE MATRIX ELEMENT NODE HEADER NODE

67
Doubly linked list :

68
Structure for doubly linked list typedef struct node *nodePointer; typedef struct { nodePointer llink; element data; nodePointer rlink; } node;

69
Circular linked list:

71
Trees: A tree is a finite set of one or more node such that, 1: there is a specially designated node called root. 2: the remaining nodes are partitioned into n>=0 disjoint sets T 0, T 1,T 2,………. T n-1 where T 0, T 1,T 2,……… T n-1 Are called the subtrees of the root

72
typedef struct node { int data ; struct node* left; struct node* right; } node; 55

73
Representation of tree: List representation Left child right sibling representation Representation as degree two tree.

74
Basic terminology Root – The top node in a tree. Parent – The converse notion of child. Siblings – Nodes with the same parent. Descendant – a node reachable by repeated proceeding from parent to child. Ancestor – a node reachable by repeated proceeding from child to parent.

75
Leaf – a node with no children. Internal node – a node with at least one child. External node – a node with no children. Degree – number of sub trees of a node. Edge – connection between one node to another. Path – a sequence of nodes and edges connecting a node with a descendant.

76
Level – The level of a node is defined by 1 + the number of connections between the node and the root. Height – The height of a node is the number of edges on the longest downward path between the node and a leaf. Forest – A forest is a set of n ≥ 0 disjoint trees.

78
Binary Search Tree In computer science, a binary search tree (BST) or ordered binary tree is a node-based binary tree data structure which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.

80
Representation of binary tree: Linked list representation. Array representation.

81
Difference between binary tree and binary search tree Binary Tree In short, a binary tree is a tree where each node has up to two leaves. In a binary tree, a left child node and a right child node contain values which can be either greater, less, or equal to parent node. Binary Search Tree In binary search tree, the left child contains nodes with values less than the parent node and where the right child only contains nodes with values greater than the parent node. There must be no duplicate nodes.

82
Traversal

83
Graphs A graph consists of two sets v and e where, v is finite, non-empty set of vertices and e is set of pairs of vertices.. The pairs of vertices are called edges.

84
Graphs can be of two types: Undirected graph Directed graph

85
Representation of Graphs : Adjacency matrix Adjacency lists Adjacency multilists Weighted list

86
Priority queue: A priority queue is collection of element such that each element has an associated priority. There are two types of priority queue, 1: Single ended priority queue 2: Double ended priority queue

87
Heaps : Binomial heap 1. Max Binomial heap 2. Min binomial heap Fibonacci heap 1. Max Fibonacci heap 2. Min Fibonacci heap Pairing heap 1. Max Pairing heap 2.Min pairing heap

88
AVL Tree AVL tree definition – a binary tree in which the maximum difference in the height of any node’s right and left sub- trees is 1 (called the balance factor) balance factor = height(right) – height(left) AVL trees are usually not perfectly balanced – however, the biggest difference in any two branch lengths will be no more than one level

89
AVL Tree 0 00 0 0 00 1 AVL Tree

91
Definition: Red-Black Tree Balanced trees: Red-black trees Each node must have exactly two children. For each child that is lacking, you create a fake black one. 10 5 13 7 9 6 ← needs two fake children ← needs one fake child needs two fake children →

92
Definition: Red-Black Tree Balanced trees: Red-black trees Each node must have exactly two children. For each child that is lacking, you create a fake black one. 10 5 13 7 9 6

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google