DATA STRUCTURE.

Slides:



Advertisements
Similar presentations
CS252: Systems Programming Ninghui Li Program Interview Questions.
Advertisements

The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
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.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
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,
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Advanced Sorting 7 2  9 4   2   4   7
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
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
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
12 C Data Structures.
Priority Queues and Heaps
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Binary Search Tree (BST)
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Data Structures Interview / VIVA Questions and Answers
Week 11 - Friday CS221.
Hashing Exercises.
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Data Structure Interview
Teach A level Computing: Algorithms and Data Structures
ITEC 2620M Introduction to Data Structures
Priority Queues (Heaps)
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
structures and their relationships." - Linus Torvalds
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
structures and their relationships." - Linus Torvalds
i206: Lecture 14: Heaps, Graphs intro.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
Introduction to Data Structures
A Robust Data Structure
Lecture 12 CS203 1.
CS6045: Advanced Algorithms
Advanced Implementation of Tables
Introduction to Data Structures
Priority Queues (Heaps)
Important Problem Types and Fundamental Data Structures
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
B-Trees.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
structures and their relationships." - Linus Torvalds
LINEAR DATA STRUCTURES
Presentation transcript:

DATA STRUCTURE

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.

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.

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

Example Program

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}

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.

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.

Write an algorithm to add two numbers entered by user.

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.

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.

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.

Representation of Stack

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.

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

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.

Representation of Circular Queue:

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.

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:

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.

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.

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.

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

Representation of Quick Sort

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.

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.

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.

How selection sort works?

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.

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.

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.

Inorder Traversal: Preorder Traversal: Postorder Traversal:

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

An Example Tree that is NOT an AVL Tree

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)

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.