Presented by : Preeti Banswal. What is data structure? A data structure is a way of organizing data that considers not only the items stored, but also.

Slides:



Advertisements
Similar presentations
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.
Advertisements

©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Advanced Data Structures
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS Data Structures Chapter 4 Lists.
Important Problem Types and Fundamental Data Structures
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Applications of Data-Structure
INTRODUCTION TO DATA STRUCTURES. INTRODUCTION A data structure is nothing but an arrangement of data either in computer's memory or on the disk storage.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
Starting at Binary Trees
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Binary Tree.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Data Structure and Algorithms
LINKED LISTS.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C, 2e
Queues.
ADT description Implementations
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Top 50 Data Structures Interview Questions
Multiway Search Trees Data may not fit into main memory
Data Structure Interview Question and Answers
12 C Data Structures.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Data Structures Interview / VIVA Questions and Answers
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Data Structure Interview
Chapter 8: Data Abstractions
CSCE 210 Data Structures and Algorithms
Chapter 1.
Trees and Binary Trees.
Introduction to Data Structures
DATA STRUCTURE.
Important Problem Types and Fundamental Data Structures
(1 - 2) Introduction to C Data Structures & Abstract Data Types
LINEAR DATA STRUCTURES
Presentation transcript:

Presented by : Preeti Banswal

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.

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

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

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

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

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

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

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

 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.

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.

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.

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

 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.

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.

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

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)

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.

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

One dimensional array

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];

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.

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 ;

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; } ;

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;

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

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

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];

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).

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.

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;

Stack empty condition : top = MAX_STACK_SIZE - 1

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”); }

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"); }

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

 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

ENQUEUE BACKFRONT DEQUEUE

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;

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

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; }

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

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

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.

Expression using stack:

? ? ?

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

Circular queue:

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….

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…

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.

Data Link Node

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

Types of linked list  Singly linked list  Doubly linked list  Circular linked list

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.

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…

Polynomial using linked list

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

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.

REPRESENTATION OF SPARSE MATRIX ELEMENT NODE HEADER NODE

Doubly linked list :

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

Circular linked list:

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

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

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

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.

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.

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.

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.

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

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.

Traversal

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.

Graphs can be of two types: Undirected graph Directed graph

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

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

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

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

AVL Tree AVL Tree

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 ← needs two fake children ← needs one fake child needs two fake children →

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