Docsity

Data Structures: Arrays, Stacks, Linked Lists, and Binary Trees, Schemes and Mind Maps of Data Structures and Algorithms

Partial preview of the text

Download Data Structures: Arrays, Stacks, Linked Lists, and Binary Trees and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!

ALAGAPPA UNIVERSITY

[Accredited with ’A+’ Grade by NAAC (CGPA:3.64) in the Third Cycle and Graded as Category–I University by MHRD-UGC]

KARAIKUDI – 630 003

DIRECTORATE OF DISTANCE EDUCATION

Diploma in Computer Applications

DATA STRUCTURE AND

ALGORITHMS

II - Semester

DATA STRUCTURES AND ALGORITHMS

Diploma in Computer Applications

ALAGAPPA UNIVERSITY [Accredited with ‘A+’ Grade by NAAC (CGPA:3.64) in the Third Cycle and Graded as Category–I University by MHRD-UGC] (A State University Established by the Government of Tamil Nadu) KARAIKUDI – 630 003 Directorate of Distance Education Diploma in Computer Applications

II - Semester

DATA STRUCTURES AND ALGORITHMS

SYLLABI-BOOK MAPPING TABLE

Data structures and Algorithms

Syllabi Mapping in

Book

Unit I :

Introduction to Data Structure : Types of Data Structure , Primitive

data types - Algorithms – Time and space Complexity of algorithms.

Unit II:

Arrays: Array initialization, Definition of Array, Characteristic of

Array ,One- dimensional Array, Two-dimensional array and Multi-

dimensional array

Unit III:

Stack : Stack related terms, Operations on a stack - Representation

of Stack: Implementation of a stack – application of Stack.

Expression Evaluation Polish notation. Queues: Operations on

queue Circular Queue, Representation of Queues, Application of

Queues.

Unit IV:

List: Merging lists, Linked list, Single linked list, Double Linked

List, Header - Linked list - Operation on Linked List : Insertion and

Deletion of linked list - Traversal: Traversing a linked list ,

Representation of linked list.

Unit V:

Trees: Binary Trees, Types of Binary trees, Binary Tree

Representation - Binary Tree operations / Applications : Traversing

Binary Trees, Binary Search tree - Operations on Binary Tree:

Insertion and Deletion operations, Hashing Techniques.

Unit VI:

Searching Techniques : Introduction, Searching, Types of searching,

Linear Search, Binary search technique.

CONTENTS
BLOCK I : INTRODUCTION
UNIT -I: INTRODUCTION TO DATA STRUCTURE 1-

1.1 Introduction 1.2 Objectives 1.3 Types of data structures 1.3.1 Linear Data Structures 1.3.2 Non-Linear Data Structures 1.4 Algorithms 1.4.1 Time and space complexity of algorithms 1.5 Check your progress questions 1.6 Answers to check your progress questions 1.7 Summary 1.8 Key Words 1.9 Self-Assessment Questions and Exercises 1.10 Further Readings UNIT -II: ARRAYS^ 11- 2.1 Introduction 2.2 Objectives 2.3 Arrays 2.3.1 Definition 2.3.2 Initialization 2.3.3 Characteristic 2.4 Types of Array 2.4.1 Single-Dimensional Array 2.4.2 Multi-Dimensional Array 2.5 Check your progress 2.6 Answers to check your progress questions 2.7 Summary 2.8 Key Words 2.9 Self-Assessment Questions and Exercises 2.10 Further Readings BLOCK II : LINEAR DATA STRUCTURE UNIT -III: STACK 19- 3.0 Introduction 3.1 Objectives 3.2 Stacks 3.2.1 Implementation 3.3 Stack Related Terms 3.4 Operations on Stack 3.5 Check your progress questions 3.6 Answers to check your progress questions 3.7 Summary 3.8 Key Words 3.9 Self-Assessment Questions and Exercises 3.10 Further Readings

7.7 Key Words 7.8 Self-Assessment Questions and Exercises 7.9 Further Readings UNIT - VIII: TRAVERSAL 67 - 71 8.1 Introduction 8.2 Objectives 8.3 Traversing Linked Lists 8.4 Representation of Linked List 8.5 Check Your Progress Questions 8.6 Answers to Check Your Progress Questions 8.7 Summary 8.8 Key Words 8.9 Self-Assessment Questions and Exercises 8.10 Further Readings BLOCK : III NON-LINEAR DATA STRUCTURE UNIT - IX: TREES 72 - 81 9.1 Introduction 9.2 Objectives 9.3 Binary Trees 9.4 Types of Binary Tree 9.5 Binary Tree Representations 9.6 Check Your Progress Questions 9.7 Answers to Check Your Progress Questions 9.8 Summary 9.9 Key Words 9.10 Self-Assessment Questions and Exercises 9.11 Further Readings UNIT - X: BINARY TREE OPERATIONS / APPLICATIONS 82 - 87 10.1 Introduction 10.2 Objectives 10.3 Traversing Binary trees 10.4 Binary Search Tree 10.5 Traversing a Binary Search Tree 10.6 Check Your Progress Questions 10.7 Answers to Check Your Progress Questions 10.8 Summary 10.9 Key Words 10.10Self-Assessment Questions and Exercises 10.11 Further Readings UNIT - XI: OPERATIONS ON BINARY TREE 88 - 105 11.1 Introduction 11.2 Objectives 11.3 Insertion and Deletion Operations 11.4 Hashing Techniques 11.5 Check Your Progress Questions 11.6 Answers to Check Your Progress Questions

11.7 Summary 11.8 Key Words 11.9 Self-Assessment Questions and Exercises 11.10 Further Readings BLOCK IV : SEARCHING TECHNIQUES UNIT - XII: SEARCHING 106 - 111 12.1 Introduction 12.2 Objectives 12.3 Searching 12.4 Linear Search 12.5 Binary Search 12.13 Check Your Progress Questions 12.6 Answers to Check Your Progress Questions 12.7 Summary 12.8 Key Words 12.9 Self-Assessment Questions and Exercises 12.10 Further Readings BLOCK V : SORTING TECHNIQUES UNIT - XIII: SORTING 112 - 120 13.1 Introduction 13.2 Objectives 13.3 Definition 13.4 Bubble Sort 13.5 Insertion Sort 13.6 Radix Sort 13.7 Check Your Progress Questions 13.8 Answers to Check Your Progress Questions 13.9 Summary 13.10 Key Words 13.11 Self-Assessment Questions and Exercises 13.12 Further Readings UNIT - XIV: OTHER SORTING TECHNIQUES 121 - 128 14.1 Introduction 14.2 Objectives 14.3 Selection Sort 14.4 Quick Sort 14.5 Tree Sort 14.6 Check Your Progress Questions 14.7 Answers to Check Your Progress Questions 14.8 Summary 14.9 Key Words 14.10 Self-Assessment Questions and Exercises 14.11 Further Readings

2 Introduction to Data Structure NOTES Self instructional materials Data is in an unorganized form, i.e. it is randomly collected facts and figures which are processed to draw conclusions. On the other hand, when the data is organized, it becomes information, which presents data in a better way and gives meaning to it.

1.2 Objectives

After going through the unit you will be able to;  Discuss Primitive data types  Understand composite data types  Explain Linear and non-linear data structure

 Analyze abstract data types

1. 3 Types of Data Structures

Primitive Data Structures Primitive Data Structures are the basic data structures that directly operate upon the machine instructions. They have different representations on different computers. For example, integer, character, and string are all primitive data types. Programmers can use these data types when creating variables in their programs. Non-primitive Data Structures Non-primitive data structures are more complicated data structures and are derived from primitive data structures. 1.3.1 Linear Data Structures A data structure is called linear if all of its elements are arranged in the linear order. In linear data structures, the elements are stored in non- hierarchical way where each element has the successors and predecessors except the first and last element.

3 [ T y p e t h e s i d e b a r c o n t e n t. A s i d e b a r i s a s t a n d a Introduction to Data Structure NOTES Self instructional materials Types of Linear Data Structures are given below:  Arrays: An array is a collection of similar type of data items and each data item is called an element of the array. The data type of the element may be any valid data type like char, int, float or double. The elements of array share the same variable name but each one carries a different index number known as subscript. The array can be one dimensional, two dimensional or multidimensional.  Linked List: Linked list is a linear data structure which is used to maintain a list in the memory. It can be seen as the collection of nodes stored at non-contiguous memory locations. Each node of the list contains a pointer to its adjacent node.  Stack: Stack is a linear list in which insertion and deletions are allowed only at one end, called top. A stack is an abstract data type (ADT), can be implemented in most of the programming languages. It is named as stack because it behaves like a real- world stack, for example: - piles of plates or deck of cards etc.  Queue: Queue is a linear list in which elements can be inserted only at one end called rear and deleted only at the other end called front .It is an abstract data structure, similar to stack. Queue is opened at both end therefore it follows First-In-First- Out (FIFO) methodology for storing the data items. 1.3.2 Non Linear Data Structures This data structure does not form a sequence i.e. each item or element is connected with two or more other items in a non-linear arrangement. The data elements are not arranged in sequential structure. Types of Non Linear Data Structures are given below:  Trees: Trees are multilevel data structures with a hierarchical relationship among its elements known as nodes. The bottommost nodes in the hierarchy are called leaf node while the topmost node is called root node. Tree data structure is based on the parent-child relationship among the nodes. Each node contains pointers that points to the child node. Each node in the tree can have more than one children except the leaf nodes whereas each node can have at most one parent except the root node. Trees can be classified into many categories which will be discussed later.

 Graphs: Graphs can be defined as the pictorial representation

of the set of elements (represented by vertices) connected by the links known as edges. A graph is different from tree in the sense that a graph can have cycle while the tree cannot have the one.

Primitive Data Types

5 [ T y p e t h e s i d e b a r c o n t e n t. A s i d e b a r i s a s t a n d a Introduction to Data Structure NOTES Self instructional materialsDelete: Algorithm developed for deleting the existing element from the data structure.  Insert: Algorithm developed for inserting an item inside a data structure.  Update: Algorithm developed for updating the existing element inside a data structure.

1.4.1 Time and space complexity of algorithms

Sometimes, there are more than one way to solve a problem. We need to learn how to compare the performance different algorithms and choose the best one to solve a particular problem. While analysing an algorithm, we mostly consider time complexity and space complexity. Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. Similarly, Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input. The performance of algorithm is measured on the basis of following properties:  Time complexity: It is a way of representing the amount of time needed by a program to run to the completion.  Space complexity: It is the amount of memory space required by an algorithm, during a course of its execution. Space complexity is required in situations when limited memory is available and for the multi user system. Each algorithm must have:  Specification: Description of the computational procedure.  Pre-conditions: The condition(s) on input.  Body of the Algorithm: A sequence of clear and unambiguous instructions.  Post-conditions: The condition(s) on output. Example: Design an algorithm to multiply the two numbers x and y and display the result in z. o Step 1 START o Step 2 declare three integers x, y & z o Step 3 define values of x & y o Step 4 multiply values of x & y o Step 5 store the output of step 4 in z o Step 6 print z o Step 7 STOP

6 Introduction to Data Structure NOTES Self instructional materials

Asymptotic analysis

Asymptotic analysis of an algorithm refers to defining the mathematical foundation/framing of its run-time performance. Using asymptotic analysis, we can very well conclude the best case, average case, and worst case scenario of an algorithm. Usually, the time required by an algorithm falls under three types  Best Case − Minimum time required for program execution.  Average Case − Average time required for program execution.  Worst Case − Maximum time required for program execution.

Asymptotic Notations

Following are the commonly used asymptotic notations to calculate the running time complexity of an algorithm.  Ο Notation  Ω Notation  θ Notation Big O Notation The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. It is used for estimating the rate of function growth. It was introduced by Paul Bachmann in 1894. f(n) is O(g(n)) if there exist positive numbers c and N such that f(n)≤ c.g(n), ∀𝑛 ≥ 𝑁 i.e., f is not larger than c.g(n) for sufficiently large ns. g(n) is an upper- bound on the value of f(n), where g(n) is the given function and f(n) is the analysing function. Ω Notation Just as Big O notation provides an asymptotic upper bound on a function, Ω notation provides an asymptotic lower bound.

8 Introduction to Data Structure NOTES Self instructional materials

  1. What is data structure?
  2. What is time and space complexity?

1.6 Check your progress questions

  1. A data structure is a specialized format for organizing, processing, retrieving and storing data.
  2. Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. Similarly, Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input.

1.7 Summary

 Each programming language provides various data types and each data type is represented differently within the computer’s memory.  The memory requirement of a data type determines the permissible range of values for that data type.  The data types can be classified into several categories, including primitive data types and composite data types.  The data types provided by a programming language are known as primitive data types or in-built data types.  In addition to primitive and composite data types, programming languages allow the user to define new data types (or user-defined data types) as per his requirements.  Generally, handling small problems is much easier than handling comparatively larger problems.  The size of each module is kept as small as possible and if required, other modules are invoked from it.  Second, a well-designed modular program has modules independent of each other’s, implementation, which will make the program easily modifiable.  An abstract data type (ADT) is an extension of a modular design in a way that the set of operations of an ADT are defined at a formal, logical level, and nowhere in ADT’s definition, it is mentioned how these operations are implemented.  The basic idea of ADT is that the implementation of the set of operations are written once in the program and the part of program which needs to perform an operation on ADT accomplishes this by invoking the required operation.  If there is a need to change the implementation details of an ADT, the change will be completely transparent to the programs using it.  The logical or mathematical model used to organize the data in main memory is called a data structure.  These features should be kept in mind while choosing a data structure for a particular situation.  The choice of a data structure depends on its simplicity and effectiveness in processing of data.  Data structures are divided into two categories, namely, linear data structure and non-linear data structure.

9 [ T y p e t h e s i d e b a r c o n t e n t. A s i d e b a r i s a s t a n d a Introduction to Data Structure NOTES Self instructional materials  A linear data structure is one in which its elements form a sequence. It means each element in the structure has a unique predecessor and a unique successor.  A finite collection of homogenous elements is termed as an array.  The elements of an array are always stored in a contiguous memory locations irrespective of the array size.  A stack is a linear list of data elements in which the addition of a new element or the deletion of an element occurs only at one end.  A queue is a linear data structure in which the addition or insertion of a new element occurs at one end, called ‘Rear’, and deletion of an element occurs at other end, called ‘Front’.  A tree consists of multiple nodes, with each node containing zero, one or more pointers to other nodes called child nodes.

1.8 Keywords

Traversing: It means accessing all the data elements one by one to process all or some of them.  Substitution method : In this method, a reasonable guess for the solution is made and it is proved through mathematical induction.  Recursion tree: In this method, recurrences are represented as a tree whose nodes indicate the cost that is incurred at the various levels of recursion.  Searching: It is the process of finding the location of a given data element in the data structure.  Insertion : It means adding a new data element in the data structure. A new element can be inserted anywhere in the structure, such as in the beginning, in the end, or in the middle.  Deletion: It means removing any existing data element from the data structure.  Sorting: It is the process of arranging all the elements of a data structure in a logical order such as ascending or descending order.  Merging: It is the process of combining the elements of two sorted data structures into a single sorted data structure.

1.9 Self-Assessment Questions and Exercises

Short-Answer Questions

  1. Write a short note on abstract data types.
  2. Write some points of differences between linear and non-linear data structures.
  3. What are the different operations on data structures?
  4. What are algorithms?
  5. What are the various asymptotic notations?

11 [ T y p e t h e s i d e b a r c o n t e n t. A s i d e b a r i s a s t a n d a Arrays NOTES Self instructional materials UNIT - II ARRAYS Structure 2.1 Introduction 2.2 Objectives 2.3 Arrays 2.3.1 Definition 2.3.2 Initialization 2.3.3 Characteristic 2.4 Types of Array 2.4.1 Single-Dimensional Array 2.4.2 Multi-Dimensional Array 2.5 Check your progress 2.6 Answers to check your progress questions 2.7 Summary 2.8 Key Words 2.9 Self-Assessment Questions and Exercises 2.10 Further Readings

2.1 Introduction

Array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

2.2 Objectives

After going through this unit, the learner will be able to understand  Array initialization and characterization  Types of Arrays

12 Self instructional materials Arrays NOTES

2.3 Arrays

An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer, float, character or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched. int no[3]={10,20,30}; float no[3]={1.5,2.7,8.9};

2.3.1 Definition

An array is a collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier.

2.3.2 Initialization

An array can be initialized in two ways.  By declaring and initializing it simultaneously. int arr[] = { 10, 20, 30, 40 }  By declaring it first and initializing it later during run time.

2.3.3 Characteristics

 An array holds elements that have the same data type  Array elements are stored in subsequent memory locations  Two-dimensional array elements are stored row by row in subsequent memory locations  Array name represents the address of the starting element  Array size should be mentioned in the declaration.  Array size must be a constant expression and not a variable.

2.4 Types of Arrays

 One dimensional (1-D) arrays or Linear arrays.  Multi-dimensional arrays.  Two dimensional (2-D) arrays or Matrix arrays.  Three dimensional arrays.

2.4.1 Single-Dimensional Arrays