Module in CC 204: Data Structures & Algorithm Grace Lhyn R. Legaste Notes to the Students This module is a part of the series of Modules for flexible learning spearheaded by the Office of the Director of Instruction and the Center for Teaching Excellence, West Visayas State University. This is meant for the course CC 204 – Data Structures & Algorithm A computer program is a collection of instructions to perform a specific task. For this, a computer program may need to store data, retrieve data, and perform computations on the data. This module have the following set of lessons or topics: Unit I. Introduction to Data Structures Unit II. Recursion/Backtracking Unit III. Linked Lists Unit IV. Stacks Unit V: Queues Unit VI: Trees Unit VII: Priority Queues & Heaps Unit VIII: Sorting The learning outcomes for CC 204, specified below are unpacked by the specific objectives of each lessons. Generally, at the end of this module, you would have: 1. Design implement, test and debug a program, based on a given specification that uses and implements abstract data types (stacks queues, priority queues, sets, maps 2. Argue strengths and weaknesses among multiple implementations for a problem (i.e., on the aspects of iterative vs. recursive solutions and on the aspects of abstraction, encapsulation and information hiding). Before you begin learning what the module is about, please be familiar with some icons to guide you through this instructional tool. The lesson will then unfold in this sequence. How much do you know? This is pre-test to check your knowledge on this subject. Activate Prior Knowledge. In here, you will do an activity that you already know and is related to the lesson. Acquire New Knowledge. This is where the lesson is presented. It may have several topics as stipulated in the specific objectives. Apply your Knowledge. In this part, you will practice what you have learned. Assess your Knowledge. You will be tested here and you will be able to know the gaps in your understanding in this lesson. If you are not satisfied with the feedback, you may then go back to some points that you may have missed. How well did you do? shows the feedback that comes after assessment. It can also be found in every break exercise within the lesson What I learned in this Unit sums up what you would learn on this unit is about. References list down the resources and links from which the content of the lesson was based from. These may take the form of books, internet, sites, blogs, videos, photographs, animation. How Much Do You Know? Identify the following statements below and write your answers on the space provided. 1. What is the size of a byte variable? a) 8 bit b) 16 bit c) 32 bit d) 64 bit 2. Which of the following is a primitive data type? a) bool b) int c) String d) void 3. Which of the following is NOT a primitive data type? a) float b) double c) String d) int 4. Which is a proper syntax in declaring and initializing a variable? a) char value; b) Char value = ‘a’; c) char value = aaa; d) char value = ‘G’; 5. What would be the output of the following code? Declare and initialize a variable with the following value (observe proper syntax): 34.56 OOP -12 B Hello 921718486500 Unit I. Introduction to Data Structures a) b) c) d) e) f) g) h) Variable, Data types, Data Structures Characteristics of Data Structures What is an Algorithm Analysis of Algorithms Asymptotic Notation Big O Notation Basic Terminologies Arrays Objectives 1. 2. 3. 4. Defined Variable, Data types and data Structures Identified the characteristics of Data Structures Applied data types in creating a simple program Performed how to create a simple program Acquire New Knowledge Lesson Proper DATA STRUCTURES & ALGORITHMS - OVERVIEW This chapter explains the basic terms related to data structure. Data Structures are the programmatic way of storing data so that data can be used efficiently. Almost every enterprise application uses various types of data structures in one or the other way. This tutorial will give you a great understanding on Data Structures needed to understand the complexity of enterprise level applications and need of algorithms, and data structures. Data Structure is a systematic way to organize data in order to use it efficiently. Following terms are the foundation terms of a data structure. Interface − Each data structure has an interface. Interface represents the set of operations that a data structure supports. An interface only provides the list of supported operations, type of parameters they can accept and return type of these operations. Implementation − Implementation provides the internal representation of a data structure. Implementation also provides the definition of the algorithms used in the operations of the data structure. Characteristics of a Data Structure Correctness − Data structure implementation should implement its interface correctly. Time Complexity − Running time or the execution time of operations of data structure must be as small as possible. Space Complexity − Memory usage of a data structure operation should be as little as possible. Need for Data Structure As applications are getting complex and data rich, there are three common problems that applications face now-a-days. Data Search − Consider an inventory of 1 million(106) items of a store. If the application is to search an item, it has to search an item in 1 million(106) items every time slowing down the search. As data grows, search will become slower. Processor speed − Processor speed although being very high, falls limited if the data grows to billion records. Multiple requests − As thousands of users can search data simultaneously on a web server, even the fast server fails while searching the data. To solve the above-mentioned problems, data structures come to rescue. Data can be organized in a data structure in such a way that all items may not be required to be searched, and the required data can be searched almost instantly. Execution Time Cases There are three cases which are usually used to compare various data structure's execution time in a relative manner. Worst Case − This is the scenario where a particular data structure operation takes maximum time it can take. If an operation's worst case time is ƒnn then this operation will not take more than ƒnn time where ƒnn represents function of n. Average Case − This is the scenario depicting the average execution time of an operation of a data structure. If an operation takes ƒnn time in execution, then m operations will take mƒnn time. Best Case − This is the scenario depicting the least possible execution time of an operation of a data structure. If an operation takes ƒnn time in execution, then the actual operation may take time as the random number which would be maximum as ƒnn. Basic Terminology Data − Data are values or set of values. Data Item − Data item refers to single unit of values. Group Items − Data items that are divided into sub items are called as Group Items. Elementary Items − Data items that cannot be divided are called as Elementary Items. Attribute and Entity − An entity is that which contains certain attributes or properties, which may be assigned values. Entity Set − Entities of similar attributes form an entity set. Field − Field is a single elementary unit of information representing an attribute of an entity. Record − Record is a collection of field values of a given entity. File − File is a collection of records of the entities in a given entity set. Variables A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. A program should be written with "Symbolic" notation, such that a statement is always true symbolically. For example if I want to know the average of two grades, EX. "average = (grade_1 + grade_2) / 2.0;" Data Definition Data Definition defines a particular data with the following characteristics. Atomic − Definition should define a single concept. Traceable − Definition should be able to be mapped to some data element. Accurate − Definition should be unambiguous. Clear and Concise − Definition should be understandable. Data Object Data Object represents an object having a data. Data object represents a container for data values, a place where data values may be stored and later retrieved. Data objects can be: • 1) at program execution- Programmer-defined -(example variables, constant, arrays, files etc) • 2) not directly accessible to programmer- system defined- run time storage, stacks, file buffers, free space lists. Data Type Data type is a way to classify various types of data such as integer, string, etc. which determines the values that can be used with the corresponding type of data, the type of operations that can be performed on the corresponding type of data. There are two data types − Primitive data types User defined data types Primitive data types Data types that are defined by system are called primitive data types. The primitive data types provided by many programming languages are: int, float, char, double, bool, etc. The number of bits allocated for each primitive data type depends on the programming languages, the compiler and the operating system. For the same primitive data type, different languages may use different sizes. Depending on the size of the data types, the total available values (domain) will also change. Primitive data types are those whose variables allows us to store only one value but they never allows us to store multiple values of same type. This is a data type whose variable can hold maximum one value at a time. User Defined Data Type A user-defined data type (UDT) is a data type that derived from an existing data type. If the system-defined data types are not enough, then most programming languages allow the users to define their own data types, called user – defined data types User defined data types are those which are developed by programmers by making use of appropriate features of the language. User defined data types related variables allows us to store multiple values either of same type or different type or both. Data Structures Data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. A data structure is a special format for organizing and storing data. General data structure types include arrays, files, linked lists, stacks, queues, trees, graphs and so on. Depending on the organization of the elements, data structures are classified into two types: Linear data structures: Elements are accessed in a sequential order but it is not compulsory to store all elements sequentially. Examples: Linked Lists, Stacks and Queues. Non – linear data structures: Elements of this data structure are tored/accessed in a non-linear order. Examples: Trees and graphs. Abstract Data Type • ADT is a theoretical concept in computer science, used in the design and analysis of algorithms, data structures, and software systems, and do not correspond to specific features of computer languages • ADT is a type (or class) for objects whose behavior is defined by a set of value and a set of operations. An abstract data type (ADT) is composed of • A collection of data e.g. Person : Name Address Birthdate Age Etc. • A set of operations on that data Add New Person Update Person Remove Person Search Person • Specifications of an ADT indicate what the ADT operations do (but not how to implement them) • Implementation of an ADT includes choosing a particular data structure DATA STRUCTURES & ALGORITHMS BASICS Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. Algorithms are generally created independent of underlying languages, i.e. an algorithm can be implemented in more than one programming language. From the data structure point of view, following are some important categories of algorithms − Search − Algorithm to search an item in a data structure. Sort − Algorithm to sort items in a certain order. Insert − Algorithm to insert item in a data structure. Update − Algorithm to update an existing item in a data structure. Delete − Algorithm to delete an existing item from a data structure. Characteristics of an Algorithm Not all procedures can be called an algorithm. An algorithm should have the following characteristics − Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning. Input − An algorithm should have 0 or more well-defined inputs. Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output. Finiteness − Algorithms must terminate after a finite number of steps. Feasibility − Should be feasible with the available resources. Independent − An algorithm should have step-by-step directions, which should be independent of any programming code. How to Write an Algorithm? There are no well-defined standards for writing algorithms. Rather, it is problem and resource dependent. Algorithms are never written to support a particular programming code. As we know that all programming languages share basic code constructs like loops (do, for, while), flow-control (if-else), etc. These common constructs can be used to write an algorithm. We write algorithms in a step-by-step manner, but it is not always the case. Algorithm writing is a process and is executed after the problem domain is well-defined. That is, we should know the problem domain, for which we are designing a solution. Example Let's try to learn algorithm-writing by using an example. Problem − Design an algorithm to add two numbers and display the result. Step 1 − START Step 2 − declare three integers a, b & c Step 3 − define values of a & b Step 4 − add values of a & b Step 5 − store output of step 4 to c Step 6 − print c Step 7 − STOP Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be written as − Step 1 − START ADD Step 2 − get values of a & b Step 3 − c ← a + b Step 4 − display c Step 5 − STOP In design and analysis of algorithms, usually the second method is used to describe an algorithm. It makes it easy for the analyst to analyze the algorithm ignoring all unwanted definitions. He can observe what operations are being used and how the process is flowing. Writing step numbers, is optional. We design an algorithm to get a solution of a given problem. A problem can be solved in more than one ways. Hence, many solution algorithms can be derived for a given problem. The next step is to analyze those proposed solution algorithms and implement the best suitable solution. Algorithm Analysis Efficiency of an algorithm can be analyzed at two different stages, before implementation and after implementation. They are the following − A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an algorithm is measured by assuming that all other factors, for example, processor speed, are constant and have no effect on the implementation. A Posterior Analysis − This is an empirical analysis of an algorithm. The selected algorithm is implemented using programming language. This is then executed on target computer machine. In this analysis, actual statistics like running time and space required, are collected. We shall learn about a priori algorithm analysis. Algorithm analysis deals with the execution or running time of various operations involved. The running time of an operation can be defined as the number of computer instructions executed per operation. Algorithm Complexity Suppose X is an algorithm and n is the size of input data, the time and space used by the algorithm X are the two main factors, which decide the efficiency of X. Time Factor − Time is measured by counting the number of key operations such as comparisons in the sorting algorithm. Space Factor − Space is measured by counting the maximum memory space required by the algorithm. The complexity of an algorithm f(n) gives the running time and/or the storage space required by the algorithm in terms of n as the size of input data. Space Complexity Space complexity of an algorithm represents the amount of memory space required by the algorithm in its life cycle. The space required by an algorithm is equal to the sum of the following two components − A fixed part that is a space required to store certain data and variables, that are independent of the size of the problem. For example, simple variables and constants used, program size, etc. A variable part is a space required by variables, whose size depends on the size of the problem. For example, dynamic memory allocation, recursion stack space, etc. Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I) is the variable part of the algorithm, which depends on instance characteristic I. Following is a simple example that tries to explain the concept − Algorithm: SUM(A, B) Step 1 - START Step 2 - C ← A + B + 10 Step 3 - Stop Here we have three variables A, B, and C and one constant. Hence S(P) = 1 + 3. Now, space depends on data types of given variables and constant types and it will be multiplied accordingly. Time Complexity Time complexity of an algorithm represents the amount of time required by the algorithm to run to completion. Time requirements can be defined as a numerical function T(n), where T(n) can be measured as the number of steps, provided each step consumes constant time. For example, addition of two n-bit integers takes n steps. Consequently, the total computational time is T(n) = c ∗ n, where c is the time taken for the addition of two bits. Here, we observe that T(n) grows linearly as the input size increases. DATA STRUCTURES & ASYMPTOMATIC 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. Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to work in a constant time. Other than the "input" all other factors are considered constant. Asymptotic analysis refers to computing the running time of any operation in mathematical units of computation. For example, the running time of one operation is computed as f(n) and may be for another operation it is computed as g(n2). This means the first operation running time will increase linearly with the increase in n and the running time of the second operation will increase exponentially when n increases. Similarly, the running time of both operations will be nearly the same if n is significantly small. 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 Oh Notation, Ο The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time. It measures the worst case time complexity or the longest amount of time an algorithm can possibly take to complete. For example, for a function f(n) Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. } Omega Notation, Ω The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. It measures the best case time complexity or the best amount of time an algorithm can possibly take to complete. For example, for a function f(n) Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. } Theta Notation, θ The notation θ(n) is the formal way to express both the lower bound and the upper bound of an algorithm's running time. It is represented as follows − θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. } Common Asymptotic Notations Following is a list of some common asymptotic notations − constant − Ο(1) logarithmic − Ο(log n) linear − Ο(n) n log n − Ο(n log n) quadratic − Ο(n2) cubic − Ο(n3) polynomial − nΟ(1) exponential − 2Ο(n) DATA STRUCTURES & ALGORITHMS - ARRAY Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array. Element − Each item stored in an array is called an element. Index − Each location of an element in an array has a numerical index, which is used to identify the element. Array Representation Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration. Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration. As per the above illustration, following are the important points to be considered. Index starts with 0. Array length is 10 which means it can store 10 elements. Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9. Array in JAVA Java provides a data structure, the array, which 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. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables. Declaring Array Variables To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable − Syntax dataType[] arrayRefVar; or dataType arrayRefVar[]; Example // preferred way. // works but not preferred way. The following code snippets are examples of this syntax − double[] myList; or double myList[]; // preferred way. // works but not preferred way. Creating Arrays You can create an array by using the new operator with the following syntax − Syntax arrayRefVar = new dataType[arraySize]; The above statement does two things − It creates an array using new dataType[arraySize]. It assigns the reference of the newly created array to the variable arrayRefVar. Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below − dataType[] arrayRefVar = new dataType[arraySize]; Alternatively you can create arrays as follows − dataType[] arrayRefVar = {value0, value1, ..., valuek}; The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arrayRefVar.length-1. Example Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList − double[] myList = new double[10]; Following picture represents array myList. Here, myList holds ten double values and the indices are from 0 to 9. Processing Arrays When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known. Example Here is a complete example showing how to create, initialize, and process arrays − public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (int i = 0; i < myList.length; i++) { System.out.println(myList[i] + " "); } // Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is " + total); } } // Finding the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } System.out.println("Max is " + max); This will produce the following result − Output 1.9 2.9 3.4 3.5 Total is 11.7 Max is 3.5 The foreach Loops JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable. Example The following code displays all the elements in the array myList − public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; } } // Print all the array elements for (double element: myList) { System.out.println(element); } This will produce the following result − Output 1.9 2.9 3.4 3.5 Passing Arrays to Methods Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array − Example public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2 − Example printArray(new int[]{3, 1, 2, 6, 4, 2}); Returning an Array from a Method A method may also return an array. For example, the following method returns an array that is the reversal of another array − Example public static int[] reverse(int[] list) { int[] result = new int[list.length]; } for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; Basic Operations Following are the basic operations supported by an array. Traverse − print all the array elements one by one. Insertion − Adds an element at the given index. Deletion − Deletes an element at the given index. Search − Searches an element using the given index or by the value. Update − Updates an element at the given index. Traverse Operation This operation is to traverse through the elements of an array. Example Following program traverses and prints the elements of an array: public class ArrayDemo { public static void main(String[] args) { int[] la = {1, 3, 5, 7, 8}; int item = 10, k = 3, n = 5; int i = 0, j = n; System.out.println("The original array elements are :"); for(i = 0; i < n; i++) { System.out.println("LA[" + i + "] = " + la[i] + " "); } } } When we compile and execute the above program, it produces the following result − Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 Insertion Operation Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array. Here, we see a practical implementation of insertion operation, where we add data at the end of the array − Example Following is the implementation of the above algorithm − public class ArrayDemo { public static void int[] la = int item = int i = 0, main(String[] args) { {1, 3, 5, 7, 8}; 10, k = 3, n = 5; j = n; System.out.println("The original array elements are :"); for(i = 0; i < n; i++) { System.out.println("LA[" + i + "] = " + la[i] + " "); } n = n + 1; while(j >= k) { la[j + 1] = la[j]; j = j - 1; } la[k] = item; System.out.println("The array elements after insertion :"); } } for(i = 0; i < n; i++) { System.out.println("LA[" + i + "] = " + la[i] + " "); } When we compile and execute the above program, it produces the following result − Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after insertion : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 10 LA[4] = 7 LA[5] = 8 For other variations of array insertion operation click here Deletion Operation Deletion refers to removing an existing element from the array and re-organizing all elements of an array. Algorithm Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA. 1. 2. 3. 4. 5. 6. 7. Start Set J = K Repeat steps 4 and 5 while J < N Set LA[J] = LA[J + 1] Set J = J+1 Set N = N-1 Stop Example Following is the implementation of the above algorithm − public class ArrayDemo { public static void main() { int[] la = {1, 3, 5, 7, 8}; int k = 3, n = 5; int j; System.out.println("The original array elements are :"); for(int i = 0; i < n; i++) { System.out.println("LA[" + i + "] = " + la[i] + " "); } j = k; while(j < n) { la[j - 1] = la[j]; j = j + 1; } n = n - 1; System.out.println("The array elements after deletion :"); } } for(int i = 0; i < n; i++) { System.out.println("LA[" + i + "] = " + la[i] + " "); } When we compile and execute the above program, it produces the following result − Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after deletion : LA[0] = 1 LA[1] = 3 LA[2] = 7 LA[3] = 8 Search Operation You can perform a search for an array element based on its value or its index. Algorithm Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search. 1. 2. 3. 4. 5. 6. 7. Start Set J = 0 Repeat steps 4 and 5 while J < N IF LA[J] is equal ITEM THEN GOTO STEP 6 Set J = J +1 PRINT J, ITEM Stop Example Following is the implementation of the above algorithm − public class ArrayDemo { public static void int[] la = int item = int i = 0, main() { {1, 3, 5, 7, 8}; 5, n = 5; j = 0; System.out.println("The original array elements are :"); for(i = 0; i < n; i++) { System.out.println("LA[" + i + "] = " + la[i] + " "); } while(j < n) { if(la[j] == item) { break; } } (j + 1)); } } j = j + 1; System.out.println("Found element " + item + " at position " + When we compile and execute the above program, it produces the following result − Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 Found element 5 at position 3 Update Operation Update operation refers to updating an existing element from the array at a given index. Algorithm Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth position of LA. 1. Start 2. Set LA[K-1] = ITEM 3. Stop Example Following is the implementation of the above algorithm − public class ArrayDemo { public static void main() { int[] la = {1, 3, 5, 7, 8}; int k = 3, n = 5, item = 10; int j; System.out.println("The original array elements are :"); for(int i = 0; i < n; i++) { System.out.println("LA[" + i + "] = " + la[i] + " "); } la[k - 1] = item; System.out.println("The array elements after updation :"); } } for(int i = 0; i < n; i++) { System.out.println("LA[" + i + "] = " + la[i] + " "); } When we compile and execute the above program, it produces the following result − Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after updation : LA[0] = 1 LA[1] = 3 LA[2] = 10 LA[3] = 7 LA[4] = 8 Apply Your Knowledge In this part, you will practice what you have learned. Design an algorithm to display the following: 1. Multiplication Table: Allow the user to input a number and display the multiplication table of the given number (from 1-10), as shown in the image. 2. A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount. Assess your knowledge. Answer each of the following multiple choice questions by ENCIRCLING the letter that corresponds to the BEST response. 1. Which of these best describes an array? a) A data structure that shows a hierarchical behaviour b) Container of objects of similar types c) Arrays are immutable once initialised d) Array is not a data structure 2. How do you instantiate an array in Java? a) int arr[] = new int(3); b) int arr[]; c) int arr[] = new int[3]; d) int arr() = new int(3); 3. Which of the following is a correct way to declare a multidimensional array in Java? a) int[] arr; b) int arr[[]]; c) int[][]arr; d) int[[]] arr; 4. What is the output of the following piece of code? public class array { public static void main(String args[]) { int []arr = {1,2,3,4,5}; System.out.println(arr[2]); System.out.println(arr[4]); } } a) 3 and 5 b) 5 and 3 c) 2 and 4 d) 4 and 2 5. Which of the following concepts make extensive use of arrays? a) Binary trees b) Scheduling of processes c) Caching d) Spatial locality 6. What are the advantages of arrays? a) Objects of mixed data types can be stored b) Elements in an array cannot be sorted c) Index of first element of an array is 1 d) Easier to store elements of same data type 7. What are the disadvantages of arrays? a) Data structure like queue or stack cannot be implemented b) There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size c) Index value of an array can be negative d) Elements are sequentially accessed 8. Assuming int is of 4bytes, what is the size of int arr[15];? a) 15 b) 19 c) 11 d) 60 9. In general, the index of the first element in an array is __________ a) 0 b) -1 c) 2 d) 1 10. Elements in an array are accessed _____________ a) randomly b) sequentially c) exponentially d) logarithmically Unit II. Recursion/Backtracking 1. 2. 3. 4. 5. 6. 7. 8. Introduction Recursion Recursion & Memory Recursion Vs Iteration Ex.of algorithms of recursion What is Backtracking Ex.algorithms of Backtracking Backtracking Problems & Solutions Objectives 1. Defined Recursion 2. Discussed the Recursion, Backtracking and code blocks using programming languages 3. Performed recursion algorithms Acquire New Knowledge Lesson Proper DATA STRUCTURES & RECURSION What is Recursion? Recursion is a widely used phenomenon in computer science used to solve complex problems by breaking them down into simpler ones. Recursion is a process by which a function calls itself directly or indirectly. The corresponding function is called as recursive function. Using recursive algorithms, certain problems can be solved quite easily. complex What is a base condition in recursion? In a recursive function, the solution to the base case is provided and the solution of the bigger problem is expressed in terms of smaller problems. int fact(int n) { if (n < = 1) // base case return 1; else return n*fact(n-1); } (value2); The role of the base condition is to stop a recursive function from executing endlessly – once a prespecified base condition is met, the function knows it’s time to exit. How a particular problem is solved using recursion? The idea is to represent a problem in terms of one or more smaller problems, and add one or more base conditions that stop the recursion. For example, we compute factorial n if we know factorial of (n1). The base case for factorial would be n = 0. We return 1 when n = 0. Why Stack Overflow error occurs in recursion? If the base case is not reached or not defined, then the stack overflow problem may arise. Let us take an example to understand this. int fact(int n) { // wrong base case (it may cause // stack overflow). if (n == 100) return 1; } else return n*fact(n-1); If fact(10) is called, it will call fact(9), fact(8), fact(7) and so on but the number will never reach 100. So, the base case is not reached. If the memory is exhausted by these functions on the stack, it will cause a stack overflow error. What is the difference between direct and indirect recursion? A function fun is called direct recursive if it calls the same function fun. A function fun is called indirect recursive if it calls another function say fun_new and fun_new calls fun directly or indirectly. Difference between direct and indirect recursion has been illustrated in Table 1. // An example of direct recursion void directRecFun() { // Some code.... directRecFun(); } // Some code... // An example of indirect recursion void indirectRecFun1() { // Some code... indirectRecFun2(); // Some code... } void indirectRecFun2() { // Some code... indirectRecFun1(); } // Some code... What is difference between tailed and non-tailed recursion? A recursive function is tail recursive when recursive call is the last thing executed by the function. How memory is allocated to different function calls in recursion? When any function is called from main(), the memory is allocated to it on the stack. A recursive function calls itself, the memory for a called function is allocated on top of memory allocated to calling function and different copy of local variables is created for each function call. When the base case is reached, the function returns its value to the function by whom it is called and memory is de-allocated and the process continues. Let us take the example how recursion works by taking a simple function. // A Java program to demonstrate working of // recursion class GFG { static void printFun(int test) { } } if (test < 1) return; else { System.out.printf("%d ", test); printFun(test - 1); // statement 2 System.out.printf("%d ", test); return; } public static void main(String[] args) { int test = 3; printFun(test); } Output : 3 2 1 1 2 3 When printFun(3) is called from main(), memory is allocated to printFun(3) and a local variable test is initialized to 3 and statement 1 to 4 are pushed on the stack as shown in below diagram. It first prints ‘3’. In statement 2, printFun(2) is called and memory is allocated to printFun(2) and a local variable test is initialized to 2 and statement 1 to 4 are pushed in the stack. Similarly, printFun(2) calls printFun(1) and printFun(1) calls printFun(0). printFun(0) goes to if statement and it return to printFun(1). Remaining statements of printFun(1) are executed and it returns to printFun(2) and so on. In the output, value from 3 to 1 are printed and then 1 to 3 are printed. The memory stack has been shown in below diagram. Problem 1: Write a program and recurrence relation to find the Fibonacci series of n where n>2 Mathematical Equation: n if n == 0, n == 1; fib(n) = fib(n-1) + fib(n-2) otherwise; Recurrence Relation: T(n) = T(n-1) + T(n-2) + O(1) Recursive program: Input: n = 5 Output: Fibonacci series of 5 numbers is : 0 1 1 2 3 Implementation: public class fibonacci { //method to calculate fibonacci series static int fibonacci(int n) { if (n <= 1) { return n; } return fibonacci(n-1) + fibonacci(n-2); } public static void main(String[] args) { int number = 5; //print first 5 numbers of fibonacci series System.out.println ("Fibonacci Series: First 5 numbers:"); for (int i = 1; i <= number; i++) { System.out.print(fibonacci(i) + " "); } } } Working: Problem 2: Write a program and recurrence relation to find the Factorial of n where n>2 . Mathematical Equation: 1 if n == 0 or n == 1; f(n) = n*f(n-1) if n> 1; Recurrence Relation: T(n) = 1 for n = 0 T(n) = 1 + T(n-1) for n > 0 Recursive program: Input: n = 5 Output: factorial of 5 is: 120 Implementation: class FactorialExample2{ static int factorial(int n){ if (n == 0) return 1; else return(n * factorial(n-1)); } public static void main(String args[]){ int i,fact=1; int number=5;//It is the number to calculate factorial fact = factorial(number); System.out.println("Factorial of "+number+" is: "+fact); } } Working: Diagram of factorial Recursion function for user input 5. Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is call iterative when there is a loop (or repetition). Example: Program to find the factorial of a number // Java program to find factorial of given number class GFG { // ----- Recursion ----// method to find factorial of given number static int factorialUsingRecursion(int n) { if (n == 0) return 1; } // recursion call return n * factorialUsingRecursion(n - 1); // ----- Iteration ----// Method to find the factorial of a given number static int factorialUsingIteration(int n) { int res = 1, i; // using iteration for (i = 2; i <= n; i++) res *= i; } return res; // Driver method public static void main(String[] args) { int num = 5; System.out.println("Factorial of " + num + " using Recursion is: " + factorialUsingRecursion(5)); } } System.out.println("Factorial of " + num + " using Iteration is: " + factorialUsingIteration(5)); Output: Factorial of 5 using Recursion is: 120 Factorial of 5 using Iteration is: 120 Below are the detailed example to illustrate the difference between the two: 1. Time Complexity: Finding the Time complexity of Recursion is more difficult than that of Iteration. Recursion: Time complexity of recursion can be found by finding the value of the nth recursive call in terms of the previous calls. Thus, finding the destination case in terms of the base case, and solving in terms of the base case gives us an idea of the time complexity of recursive equations. Please see Solving Recurrences for more details. Iteration: Time complexity of iteration can be found by finding the number of cycles being repeated inside the loop. 2. Usage: Usage of either of these techniques is a trade-off between time complexity and size of code. If time complexity is the point of focus, and number of recursive calls would be large, it is better to use iteration. However, if time complexity is not an issue and shortness of code is, recursion would be the way to go. Recursion: Recursion involves calling the same function again, and hence, has a very small length of code. However, as we saw in the analysis, the time complexity of recursion can get to be exponential when there are a considerable number of recursive calls. Hence, usage of recursion is advantageous in shorter code, but higher time complexity. Iteration: Iteration is repetition of a block of code. This involves a larger size of code, but the time complexity is generally lesser than it is for recursion. 3. Overhead: Recursion has a large amount of Overhead as compared to Iteration. Recursion: Recursion has the overhead of repeated function calls, that is due to repetitive calling of the same function, the time complexity of the code increases manifold. Iteration: Iteration does not involve any such overhead. 4. Infinite Repetition: Infinite Repetition in recursion can lead to CPU crash but in iteration, it will stop when memory is exhausted. Recursion: In Recursion, Infinite recursive calls may occur due to some mistake in specifying the base condition, which on never becoming false, keeps calling the function, which may lead to system CPU crash. Iteration: Infinite iteration due to mistake in iterator assignment or increment, or in the terminating condition, will lead to infinite loops, which may or may not lead to system errors, but will surely stop program execution any further. PROPERTY Definition Application Termination Usage Code Size Time Complexity RECURSION Function calls itself. For functions. Through base case, where there will be no function call. Used when code size needs to be small, and time complexity is not an issue. Smaller code size Very high(generally exponential) time complexity. ITERATION A set of instructions repeatedly executed. For loops. When the termination condition for the iterator ceases to be satisfied. Used when time complexity needs to be balanced against an expanded code size. Larger Code Size. Relatively lower time complexity(generally polynomial-logarithmic). DATA STRUCTURES & BACKTRACKING The Backtracking is an algorithmic-method to solve a problem with an additional way. It uses a recursive approach to explain the problems. We can say that the backtracking is needed to find all possible combination to solve an optimization problem. Backtracking is a systematic way of trying out different sequences of decisions until we find one that "works." In the following Figure: o Each non-leaf node in a tree is a parent of one or more other nodes (its children) o Each node in the tree, other than the root, has exactly one parent Generally, however, we draw our trees downward, with the root at the top. A tree is composed of nodes. Backtracking can understand of as searching a tree for a particular "goal" leaf node. Backtracking is undoubtedly quite simple - we "explore" each node, as follows: To "explore" node N: 1. If N is a goal node, return "success" 2. If N is a leaf node, return "failure" 3. For each child C of N, Explore C If C was successful, return "success" 4. Return "failure" Backtracking algorithm determines the solution by systematically searching the solution space for the given problem. Backtracking is a depth-first search with any bounding function. All solution using backtracking is needed to satisfy a complex set of constraints. The constraints may be explicit or implicit. Explicit Constraint is ruled, which restrict each vector element to be chosen from the given set. Implicit Constraint is ruled, which determine which each of the tuples in the solution space, actually satisfy the criterion function. Apply Your Knowledge In this part, you will practice what you have learned. Predict the output of following program. import java.io.*; class GFG { static void fun(int x) { if(x > 0) { fun(--x); System.out.print(x + " "); fun(--x); } } } public static void main (String[] args) { int a = 4; fun(a); } Assess your knowledge. Answer each of the following multiple choice questions by ENCIRCLING the letter that corresponds to the BEST response. 1. What is Recursion in Java? a) Recursion is a class b) Recursion is a process of defining a method that calls other methods repeatedly c) Recursion is a process of defining a method that calls itself repeatedly d) Recursion is a process of defining a method that calls other methods which in turn call again this method 2. Which of these data types is used by operating system to manage the Recursion in Java? a) Array b) Stack c) Queue d) Tree 3. Which of these will happen if recursive method does not have a base case? a) An infinite loop occurs b) System stops the program after some time c) After 1000000 calls it will be automatically stopped d) None of the mentioned 4. Which of these is not a correct statement? class recursion { int func (int n) { int result; result = func (n - 1); return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.func(12)); } } a) A recursive method must have a base case b) Recursion always uses stack c) Recursive methods are faster that programmers written loop to call the function repeatedly using a stack d) Recursion is managed by Java Runtime environment 5. Which of these packages contains the exception Stack Overflow in Java? a) java.lang b) java.util c) java.io d) java.system 6. What will be the output of the following Java program? class recursion { int func (int n) { int result; result = func (n - 1); return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.func(12)); } } a) 0 b) 1 c) Compilation Error d) Runtime Error 7. What will be the output of the following Java program? class recursion { int func (int n) { int result; if (n == 1) return 1; result = func (n - 1); return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.func(5)); } } a) 0 b) 1 c) 120 d) None of the mentioned 8. What will be the output of the following Java program? class recursion { int fact(int n) { int result; if (n == 1) return 1; result = fact(n - 1) * n; return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.fact(5)); } } a) 24 b) 30 c) 120 d) 720 9. What will be the output of the following Java program? class recursion { int fact(int n) { int result; if (n == 1) return 1; result = fact(n - 1) * n; return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.fact(1)); } } a) 1 b) 30 c) 120 d) Runtime Error 10. What will be the output of the following Java program? class recursion { int fact(int n) { int result; if (n == 1) return 1; result = fact(n - 1) * n; return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.fact(6)); } } a) 1 b) 30 c) 120 d) 720 Unit III. Linked Lists 1. 2. 3. 4. 5. 6. What is Linked list Linked List ADT Why Linked List Arrays Overview Comparison of linked list w/Arrays & Dynamic Arrays Singly Linked List 7. Doubly Linked List 8. Circular Linked List 9. Unrolled linked list Acquire New Knowledge Lesson Proper DATA STRUCTURES & LINKED LIST Linked List Representation Linked list can be visualized as a chain of nodes, where every node points to the next node. As per the above illustration, following are the important points to be considered. Linked List contains a link element called first. Each link carries a data field(s) and a link field called next. Each link is linked with its next link using its next link. Last link carries a link as null to mark the end of the list. Types of Linked List Following are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Basic Operations Following are the basic operations supported by a list. Insertion − Adds an element at the beginning of the list. Deletion − Deletes an element at the beginning of the list. Display − Displays the complete list. Search − Searches an element using the given key. Delete − Deletes an element using the given key. Insertion Operation Adding a new node in linked list is a more than one step activity. We shall learn this with diagrams here. First, create a node using the same structure and find the location where it has to be inserted. Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode). Then point B.next to C − NewNode.next −> RightNode; It should look like this − Now, the next node at the left should point to the new node. LeftNode.next −> NewNode; This will put the new node in the middle of the two. The new list should look like this − Similar steps should be taken if the node is being inserted at the beginning of the list. While inserting it at the end, the second last node of the list should point to the new node and the new node will point to NULL. Deletion Operation Deletion is also a more than one step process. We shall learn with pictorial representation. First, locate the target node to be removed, by using searching algorithms. The left (previous) node of the target node now should point to the next node of the target node − LeftNode.next −> TargetNode.next; This will remove the link that was pointing to the target node. Now, using the following code, we will remove what the target node is pointing at. TargetNode.next −> NULL; We need to use the deleted node. We can keep that in memory otherwise we can simply deallocate memory and wipe off the target node completely. Reverse Operation This operation is a thorough one. We need to make the last node to be pointed by the head node and reverse the whole linked list. First, we traverse to the end of the list. It should be pointing to NULL. Now, we shall make it point to its previous node − We have to make sure that the last node is not the last node. So we'll have some temp node, which looks like the head node pointing to the last node. Now, we shall make all left side nodes point to their previous nodes one by one. Except the node (first node) pointed by the head node, all nodes should point to their predecessor, making them their new successor. The first node will point to NULL. We'll make the head node point to the new first node by using the temp node. DATA STRUCTURES & DOUBLY LIST Doubly Linked List Representation Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. Prev − Each link of a linked list contains a link to the previous link called Prev. LinkedList − A Linked List contains the connection link to the first link called First and to the last link called Last. Doubly Linked List Representation As per the above illustration, following are the important points to be considered. Doubly Linked List contains a link element called first and last. Each link carries a data field(s) and two link fields called next and prev. Each link is linked with its next link using its next link. Each link is linked with its previous link using its previous link. The last link carries a link as null to mark the end of the list. Basic Operations Following are the basic operations supported by a list. Insertion − Adds an element at the beginning of the list. Deletion − Deletes an element at the beginning of the list. Insert Last − Adds an element at the end of the list. Delete Last − Deletes an element from the end of the list. Insert After − Adds an element after an item of the list. Delete − Deletes an element from the list using the key. Display forward − Displays the complete list in a forward manner. Display backward − Displays the complete list in a backward manner. DATA STRUCTURES & CIRCULAR LIST Circular Linked List Representation Circular Linked List is a variation of Linked list in which the first element points to the last element and the last element points to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list. Singly Linked List as Circular In singly linked list, the next pointer of the last node points to the first node. Doubly Linked List as Circular In doubly linked list, the next pointer of the last node points to the first node and the previous pointer of the first node points to the last node making the circular in both directions. As per the above illustration, following are the important points to be considered. The last link's next points to the first link of the list in both cases of singly as well as doubly linked list. The first link's previous points to the last of the list in case of doubly linked list. Basic Operations Following are the important operations supported by a circular list. insert − Inserts an element at the start of the list. delete − Deletes an element from the start of the list. display − Displays the list. Insertion Operation Following code demonstrates the insertion operation in a circular linked list based on single linked list. Example insertFirst(data): Begin create a new node node -> data := data if the list is empty, then head := node next of node = head else temp := head while next of temp is not head, do temp := next of temp done next of node := head next of temp := node head := node end if End Deletion Operation Following code demonstrates the deletion operation in a circular linked list based on single linked list. deleteFirst(): Begin if head is null, then it is Underflow and return else if next of head = head, then head := null deallocate head else ptr := head while next of ptr is not head, do ptr := next of ptr next of ptr = next of head deallocate head head := next of ptr end if End Display List Operation Following code demonstrates the display list operation in a circular linked list. display(): Begin if head is null, then Nothing to print and return else ptr := head while next of ptr is not head, do display data of ptr ptr := next of ptr display data of ptr end if End DATA STRUCTURES & LINKED LIST IN JAVA Example: The following implementation demonstrates how to create and use a linked list. import java.util.*; public class Test { public static void main(String args[]) { // Creating object of the // class linked list LinkedList<String> ll = new LinkedList<String>(); // Adding elements to the linked list ll.add("A"); ll.add("B"); ll.addLast("C"); ll.addFirst("D"); ll.add(2, "E"); System.out.println(ll); ll.remove("B"); ll.remove(3); ll.removeFirst(); ll.removeLast(); } } System.out.println(ll); Output: [D, A, E, B, C] [A] Performing Various Operations on ArrayList Let’s see how to perform some basics operations on the LinkedList. 1. Adding Elements: In order to add an element to an ArrayList, we can use the add() method. This method is overloaded to perform multiple operations based on different parameters. They are: add(Object): This method is used to add an element at the end of the LinkedList. add(int index, Object): This method is used to add an element at a specific index in the LinkedList. // Java program to add elements // to a LinkedList import java.util.*; public class GFG { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<>(); ll.add("Geeks"); ll.add("Geeks"); ll.add(1, "For"); } } System.out.println(ll); Output: [Geeks, For, Geeks] 2. Changing Elements: After adding the elements, if we wish to change the element, it can be done using the set() method. Since a LinkedList is indexed, the element which we wish to change is referenced by the index of the element. Therefore, this method takes an index and the updated element which needs to be inserted at that index. // Java program to change elements // in a LinkedList import java.util.*; public class GFG { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<>(); ll.add("Geeks"); ll.add("Geeks"); ll.add(1, "Geeks"); System.out.println("Initial LinkedList " + ll); ll.set(1, "For"); } } System.out.println("Updated LinkedList " + ll); Output: Initial LinkedList [Geeks, Geeks, Geeks] Updated LinkedList [Geeks, For, Geeks] 3. Removing Elements: In order to remove an element from a LinkedList, we can use the remove() method. This method is overloaded to perform multiple operations based on different parameters. They are: remove(Object): This method is used to simply remove an object from the LinkedList. If there are multiple such objects, then the first occurrence of the object is removed. remove(int index): Since a LinkedList is indexed, this method takes an integer value which simply removes the element present at that specific index in the LinkedList. After removing the element, all the elements are moved to the left to fill the space and the indices of the objects are updated. // Java program to remove elements // in a LinkedList import java.util.*; public class GFG { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<>(); ll.add("Geeks"); ll.add("Geeks"); ll.add(1, "For"); System.out.println( "Initial LinkedList " + ll); ll.remove(1); System.out.println( "After the Index Removal " + ll); ll.remove("Geeks"); } } System.out.println( "After the Object Removal " + ll); Output: Initial LinkedList [Geeks, For, Geeks] After the Index Removal [Geeks, Geeks] After the Object Removal [Geeks] 4. Iterating the LinkedList: There are multiple ways to iterate through the LinkedList. The most famous ways are by using the basic for loop in combination with a get() method to get the element at a specific index and the advanced for loop. // Java program to iterate the elements // in an LinkedList import java.util.*; public class GFG { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<>(); ll.add("Geeks"); ll.add("Geeks"); ll.add(1, "For"); // Using the Get method and the // for loop for (int i = 0; i < ll.size(); i++) { } System.out.print(ll.get(i) + " "); System.out.println(); } } // Using the for each loop for (String str : ll) System.out.print(str + " "); Output: Geeks For Geeks Geeks For Geeks In the above illustration, AbstractList, CopyOnWriteArrayList and the AbstractSequentialList are the classes which implement the list interface. A separate functionality is implemented in each of the mentioned classes. They are: 1. AbstractList: This class is used to implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement only the get() and the size() methods. 2. CopyOnWriteArrayList: This class implements the list interface. It is an enhanced version of ArrayList in which all the modifications(add, set, remove, etc.) are implemented by making a fresh copy of the list. 3. AbstractSequentialList: This class implements the Collection interface and the AbstractCollection class. This class is used to implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement only the get() and the size() methods. How LinkedList work Internally? Since a LinkedList acts as a dynamic array and we do not have to specify the size while creating it, the size of the list automatically increases when we dynamically add and remove items. And also, the elements are not stored in a continuous fashion. Therefore, there is no need to increase the size. Internally, the LinkedList is implemented using the doubly linked list data structure. The main difference between a normal linked list and a doubly LinkedList is that a doubly linked list contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in the singly linked list. Constructors in the LinkedList In order to create a LinkedList, we need to create an object of the LinkedList class. The LinkedList class consists of various constructors that allow the possible creation of the list. The following are the constructors available in this class: 1. LinkedList(): This constructor is used to create an empty linked list. If we wish to create an empty LinkedList with the name ll, then, it can be created as: LinkedList ll = new LinkedList(); 2. LinkedList(Collection C): This constructor is used to create an ordered list which contains all the elements of a specified collection, as returned by the collection’s iterator. If we wish to create a linkedlist with the name ll, then, it can be created as: LinkedList ll = new LinkedList(C); Methods for Java LinkedList METHOD add(int index, E element) add(E e) addAll(int index, Collection<E> c) addAll(Collection<E> c) addFirst(E e) addLast(E e) DESCRIPTION This method Inserts the specified element at the specified position in this list. This method Appends the specified element to the end of this list. This method Inserts all of the elements in the specified collection into this list, starting at the specified position. This method Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator. This method Inserts the specified element at the beginning of this list. This method Appends the specified element to the end of this list. clear() clone() contains(Object o) descendingIterator() element() get(int index) getFirst() getLast() indexOf(Object o) lastIndexOf(Object o) listIterator(int index) offer(E e) offerFirst(E e) offerLast(E e) peek() peekFirst() peekLast() poll() pollFirst() pollLast() pop() push(E e) remove() remove(int index) remove(Object o) removeFirst() removeFirstOccurrence(Object o) removeLast() removeLastOccurrence(Object o) set(int index, E element) This method removes all of the elements from this list. This method returns a shallow copy of this LinkedList. This method returns true if this list contains the specified element. This method returns an iterator over the elements in this deque in reverse sequential order. This method retrieves, but does not remove, the head (first element) of this list. This method returns the element at the specified position in this list. This method returns the first element in this list. This method returns the last element in this list. This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. This method returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. This method Adds the specified element as the tail (last element) of this list. This method Inserts the specified element at the front of this list. This method Inserts the specified element at the end of this list. This method retrieves, but does not remove, the head (first element) of this list. This method retrieves, but does not remove, the first element of this list, or returns null if this list is empty. This method retrieves, but does not remove, the last element of this list, or returns null if this list is empty. This method retrieves and removes the head (first element) of this list. This method retrieves and removes the first element of this list, or returns null if this list is empty. This method retrieves and removes the last element of this list, or returns null if this list is empty. This method Pops an element from the stack represented by this list. This method Pushes an element onto the stack represented by this list. This method retrieves and removes the head (first element) of this list. This method removes the element at the specified position in this list. This method removes the first occurrence of the specified element from this list, if it is present. This method removes and returns the first element from this list. This method removes the first occurrence of the specified element in this list (when traversing the list from head to tail). This method removes and returns the last element from this list. This method removes the last occurrence of the specified element in this list (when traversing the list from head to tail). This method replaces the element at the specified position in this list with the specified element. size() spliterator() toArray() toArray(T[] a) toString() This method returns the number of elements in this list. This method Creates a late-binding and fail-fast Spliterator over the elements in this list. This method returns an array containing all of the elements in this list in proper sequence (from first to last element). This method returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. This method returns a String containing all of the elements in this list in proper sequence (from first to last element), each element is separated by commas and the String is enclosed in square brackets. Apply Your Knowledge In this part, you will practice what you have learned. What is the output of following function for start pointing to first node of following linked list? 1->2->3->4->5->6 void fun(struct node* start) { if(start == NULL) return; printf("%d ", start->data); if(start->next != NULL ) fun(start->next->next); printf("%d ", start->data); } Assess your knowledge. Answer each of the following multiple choice questions by ENCIRCLING the letter that corresponds to the BEST response. 1. Which of the following is not a disadvantage to the usage of array? a) Fixed size b) There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size c) Insertion based on position d) Accessing elements at specified positions 2. What is the time complexity of inserting at the end in dynamic arrays? a) O(1) b) O(n) c) O(logn) d) Either O(1) or O(n) 3. What is the time complexity to count the number of elements in the linked list? a) O(1) b) O(n) c) O(logn) d) O(n2) 4. Which of the following performs deletion of the last element in the list? Given below is the Node class. class Node { protected Node next; protected Object ele; Node(Object e,Node n) { ele = e; next = n; } public void setNext(Node n) { next = n; } public void setEle(Object e) { ele = e; } public Node getNext() { return next; } public Object getEle() { return ele; } } class SLL { Node head; int size; SLL() { } } size = 0; a) public Node removeLast() { if(size == 0) return null; Node cur; Node temp; cur = head; while(cur.getNext() != null) { temp = cur; cur = cur.getNext(); } temp.setNext(null); size--; return cur; } b) public void removeLast() { if(size == 0) return null; Node cur; Node temp; cur = head; while(cur != null) { temp = cur; cur = cur.getNext(); } temp.setNext(null); return cur; } c) public void removeLast() { if(size == 0) return null; Node cur; Node temp; cur = head; while(cur != null) { cur = cur.getNext(); temp = cur; } temp.setNext(null); return cur; } d) public void removeLast() { if(size == 0) return null; Node cur; Node temp; cur = head; while(cur.getNext() != null) { cur = cur.getNext(); temp = cur; } temp.setNext(null); return cur; } 5. What is the functionality of the following code? public void function(Node node) { if(size == 0) head = node; else { Node temp,cur; for(cur = head; (temp = cur.getNext())!=null; cur = temp); cur.setNext(node); } size++; } a) Inserting a node at the beginning of the list b) Deleting a node at the beginning of the list c) Inserting a node at the end of the list d) Deleting a node at the end of the list 6. What is the space complexity for deleting a linked list? a) O(1) b) O(n) c) Either O(1) or O(n) d) O(logn) 7. How would you delete a node in the singly linked list? The position to be deleted is given. a) public void delete(int pos) { if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { } } Node temp = head; for(int i=1; i<pos; i++) { temp = temp.getNext(); } temp.setNext(temp.getNext().getNext()); size--; b) public void delete(int pos) { if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { Node temp = head; for(int i=1; i<pos; i++) { temp = temp.getNext(); } temp.setNext(temp.getNext()); } size--; } c) public void delete(int pos) { if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { Node temp = head; for(int i=1; i<pos; i++) { temp = temp.getNext().getNext(); } temp.setNext(temp.getNext().getNext()); } size--; } d) public void delete(int pos) { } if(pos < 0) pos = 0; if(pos > size) pos = size; if( size == 0) return; if(pos == 0) head = head.getNext(); else { Node temp = head; for(int i=0; i<pos; i++) { temp = temp.getNext(); } temp.setNext(temp.getNext().getNext()); } size--; 8. Which of these is not an application of linked list? a) To implement file systems b) For separate chaining in hash-tables c) To implement non-binary trees d) Random Access of elements 9. Which of the following piece of code has the functionality of counting the number of elements in the list? a) public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { size++; cur = cur.getNext(); } return size; } b) public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { cur = cur.getNext(); size++; } return size; } c) public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { size++; cur = cur.getNext(); } } d) public int length(Node head) { int size = 0; Node cur = head; while(cur!=null) { size++; cur = cur.getNext().getNext(); } return size; } 10. What is the functionality of the following piece of code? public int function(int data) { Node temp = head; int var = 0; while(temp != null) { if(temp.getData() == data) { return var; } var = var+1; temp = temp.getNext(); } return Integer.MIN_VALUE; } a) Find and delete a given element in the list b) Find and return the given element in the list c) Find and return the position of the given element in the list d) Find and insert a new element in the list Unit IV. Stacks 1.What is stacked 2. How stacked are used 3. Stack ADT 4.Applications 5. Implementation Objectives 1. Introduced the concept of stacks and familiarized its functions 2. Performed simple implementations of stacked in the program Acquire New Knowledge Lesson Proper DATA STRUCTURES & STACK A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc. A real-world stack allows operations at one end only. For example, we can place or remove a card or plate from the top of the stack only. Likewise, Stack ADT allows all data operations at one end only. At any given time, we can only access the top element of a stack. This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation. Stack Representation The following diagram depicts a stack and its operations − A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to implement stack using arrays, which makes it a fixed size stack implementation. Basic Operations Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations − push() − Pushing (storing) an element on the stack. pop() − Removing (accessing) an element from the stack. When data is PUSHed onto stack. To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks − peek() − get the top data element of the stack, without removing it. isFull() − check if stack is full. isEmpty() − check if stack is empty. At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always represents the top of the stack, hence named top. The top pointer provides top value of the stack without actually removing it. First we should learn about procedures to support stack functions − peek() Algorithm of peek() function − begin procedure peek return stack[top] end procedure Implementation of peek() function in Java programming language − Example public static int peek() { return stack[top]; } isfull() Algorithm of isfull() function − begin procedure isfull if top equals to MAXSIZE return true else return false endif end procedure Implementation of isfull() function in Java programming language − Example public static boolean isfull() { if(top == maxsize) { return true; } else { return false; } } isempty() Algorithm of isempty() function − begin procedure isempty if top less than 1 return true else return false endif end procedure Implementation of isempty() function in Java programming language is slightly different. We initialize top at -1, as the index in array starts from 0. So we check if the top is below zero or -1 to determine if the stack is empty. Here's the code − Example public static boolean isempty() { if(top == -1) { return true; } else { return false; } } Push Operation The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps − Step 1 − Checks if the stack is full. Step 2 − If the stack is full, produces an error and exit. Step 3 − If the stack is not full, increments top to point next empty space. Step 4 − Adds data element to the stack location, where top is pointing. Step 5 − Returns success. If the linked list is used to implement the stack, then in step 3, we need to allocate space dynamically. Algorithm for PUSH Operation A simple algorithm for Push operation can be derived as follows − begin procedure push: stack, data if stack is full return null endif top ← top + 1 stack[top] ← data end procedure Implementation of this algorithm in Java, is very easy. See the following code − Example } public static int push(int data) { if(!isfull()) { top = top + 1; stack[top] = data; } else { System.out.println("Could not insert data, Stack is full."); } return 0; Pop Operation Accessing the content while removing it from the stack, is known as a Pop Operation. In an array implementation of pop() operation, the data element is not actually removed, instead top is decremented to a lower position in the stack to point to the next value. But in linked-list implementation, pop() actually removes data element and deallocates memory space. A Pop operation may involve the following steps − Step 1 − Checks if the stack is empty. Step 2 − If the stack is empty, produces an error and exit. Step 3 − If the stack is not empty, accesses the data element at which top is pointing. Step 4 − Decreases the value of top by 1. Step 5 − Returns success. Algorithm for Pop Operation A simple algorithm for Pop operation can be derived as follows − begin procedure pop: stack if stack is empty return null endif data ← stack[top] top ← top - 1 return data end procedure Implementation of this algorithm in Java, is as follows − Example public static int pop() { int data; } if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { System.out.println("Could not retrieve data, Stack is empty."); } return 0; Complete Program Implementation public class Stack { public static int maxsize = 8; public static int[] stack = new int[8]; public static int top = -1; public static boolean isempty() { if(top == -1) { return true; } else { return false; } } public static boolean isfull() { if(top == maxsize) { return true; } else { return false; } } public static int peek() { return stack[top]; } public static int pop() { int data; if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { System.out.println("Could not retrieve data, Stack is empty."); } } return 0; public static int push(int data) { if(!isfull()) { top = top + 1; stack[top] = data; } else { System.out.println("Could not insert data, Stack is full."); } return 0; } public static void main(String[] args) { // push items on to the stack push(3); push(5); push(9); push(1); push(12); push(15); System.out.println("Element at top of the stack: " + peek()); System.out.println("Elements: "); // print stack data while(!isempty()) { int data = pop(); System.out.println(data); } } } System.out.println("Stack full: " + (isfull() ? "true" : "false")); System.out.println("Stack empty: " + (isempty() ? "true" : "false")); If we compile and run the above program, it will produce the following result − Output Element at top of the stack: 15 Elements: 15 12 1 9 5 3 Stack full: false Stack empty: true Apply Your Knowledge In this part, you will practice what you have learned. What is the output of following function for start pointing to first node of following linked list? The seven elements A, B, C, D, E, F and G are pushed onto a stack in reverse order, i.e., starting from G. The stack is popped five times and each element is inserted into a queue.Two elements are deleted from the queue and pushed back onto the stack. Now, one element is popped from the stack. The popped item is ________. If the sequence of operations – push (1), push (2), pop, push (1), push (2), pop, pop, pop, push (2), pop are performed on a stack, the sequence of popped out values_______________. 22112 Assess your knowledge. Answer each of the following multiple choice questions by ENCIRCLING the letter that corresponds to the BEST response. 1. Process of inserting an element in stack is called ____________ a) Create b) Push c) Evaluation d) Pop 2. Process of removing an element from stack is called __________ a) Create b) Push c) Evaluation d) Pop 3. In a stack, if a user tries to remove an element from empty stack it is called _________ a) Underflow b) Empty collection c) Overflow d) Garbage Collection 4. Pushing an element into stack already having five elements and stack size of 5, then stack becomes a) Overflow b) Crash c) Underflow d) User flow . This results in overwriting memory, code and loss of unsaved work on the computer. 5. Entries in a stack are “ordered”. What is the meaning of this statement? a) A collection of stacks is sortable b) Stack entries may be compared with the ‘<‘ operation c) The entries are stored in a linked list d) There is a Sequential entry that is one by one 6. Which of the following applications may use a stack? a) A parentheses balancing program b) Tracking of local variables at run time c) Compiler Syntax Analyzer d) Data Transfer between two asynchronous process 7. Consider the usual algorithm for determining whether a sequence of parentheses is balanced. The maximum number of parentheses that appear on the stack AT ANY ONE TIME when the algorithm analyzes: (()(())(())) are: a) 1 b) 2 c) 3 d) 4 or more 8. Consider the usual algorithm for determining whether a sequence of parentheses is balanced. Suppose that you run the algorithm on a sequence that contains 2 left parentheses and 3 right parentheses (in some order). The maximum number of parentheses that appear on the stack AT ANY ONE TIME during the computation? a) 1 b) 2 c) 3 d) 4 or more 9. What is the value of the postfix expression 6 3 2 4 + – *? a) 1 b) 40 c) 74 d) -18 10. Here is an infix expression: 4 + 3*(6*3-12). Suppose that we are using the usual stack algorithm to convert the expression from infix to postfix notation. The maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression? a) 1 b) 2 c) 3 d) 4 Unit V: Queues 1. 2. 3. 4. 5. 6. What is a queue How are queues used Queues ADT Exceptions Applications Problems & Solutions Objectives 1. Defined what its queue and its purpose 2. Performed simple implementations of queue. Acquire New Knowledge Lesson Proper DATA STRUCTURES & QUEUES Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first. A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops. Queue Representation As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure − As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array. Basic Operations Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues − enqueue() − add (store) an item to the queue. dequeue() − remove (access) an item from the queue. Few more functions are required to make the above-mentioned queue operation efficient. These are − peek() − Gets the element at the front of the queue without removing it. isfull() − Checks if the queue is full. isempty() − Checks if the queue is empty. In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer. Let's first learn about supportive functions of a queue − peek() This function helps to see the data at the front of the queue. The algorithm of peek() function is as follows − Algorithm begin procedure peek return queue[front] end procedure Implementation of peek() function in Java programming language − Example public statio int peek() { return queue[front]; } isfull() As we are using single dimension array to implement queue, we just check for the rear pointer to reach at MAXSIZE to determine that the queue is full. In case we maintain the queue in a circular linked-list, the algorithm will differ. Algorithm of isfull() function − Algorithm begin procedure isfull if rear equals to MAXSIZE return true else return false endif end procedure Implementation of isfull() function in Java programming language − Example public statio bool isfull() { if(rear == MAXSIZE - 1) return true; else return false; } isempty() Algorithm of isempty() function − Algorithm begin procedure isempty if front is less than MIN return true else return false endif OR front is greater than rear end procedure If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty. Here's the C programming code − Example public statio bool isempty() { if(front < 0 || front > rear) return true; else return false; } Enqueue Operation Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than that of stacks. The following steps should be taken to enqueue (insert) data into a queue − Step 1 − Check if the queue is full. Step 2 − If the queue is full, produce overflow error and exit. Step 3 − If the queue is not full, increment rear pointer to point the next empty space. Step 4 − Add data element to the queue location, where the rear is pointing. Step 5 − return success. Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen situations. Algorithm for enqueue operation procedure enqueue(data) if queue is full return overflow endif rear ← rear + 1 queue[rear] ← data return true end procedure Implementation of enqueue() in Java programming language − Example public statio int enqueue(int data) if(isfull()) return 0; rear = rear + 1; queue[rear] = data; return 1; end procedure Dequeue Operation Accessing data from the queue is a process of two tasks − access the data where front is pointing and remove the data after access. The following steps are taken to perform dequeue operation − Step 1 − Check if the queue is empty. Step 2 − If the queue is empty, produce underflow error and exit. Step 3 − If the queue is not empty, access the data where front is pointing. Step 4 − Increment front pointer to point to the next available data element. Step 5 − Return success. Algorithm for dequeue operation procedure dequeue if queue is empty return underflow end if data = queue[front] front ← front + 1 return true end procedure Implementation of dequeue() in C programming language − Example public static int dequeue() { int data; if(!isempty()) { return 0; } else { data = queue[front]; front = front + 1; return data; } } Complete Program Implementation public class Queue { public final static int MAX = 6; public static int[] intArray = new int[MAX]; public static int front = 0; public static int rear = -1; public static int itemCount = 0; public static int peek() { return intArray[front]; } public static boolean isEmpty() { return itemCount == 0; } public static boolean isFull() { return itemCount == MAX; } public static int size() { return itemCount; } public static void insert(int data) { if(!isFull()) { if(rear == MAX - 1) { rear = -1; } intArray[++rear] = data; itemCount++; } } public static int removeData() { int data = intArray[front++]; if(front == MAX) { front = 0; } itemCount--; return data; } public static void main(String[] args) { insert(3); insert(5); insert(9); insert(1); insert(12); insert(15); if(isFull()) { System.out.println("Queue is full!"); } int num = removeData(); System.out.println("Element removed: " + num); insert(16); insert(17); insert(18); System.out.println("Element at front: " + peek()); System.out.println("----------------------"); System.out.println("index : 5 4 3 2 1 0"); System.out.println("----------------------"); System.out.print("Queue: "); while(!isEmpty()) { int n = removeData(); System.out.print(n + " "); } } } If we compile and run the above program, it will produce the following result − Output Queue is full! Element removed: 3 Element at front: 5 ---------------------index : 5 4 3 2 1 0 ---------------------Queue: 5 9 1 12 15 16 Apply Your Knowledge In this part, you will practice what you have learned. What is the difference between a queue and a stack? If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed?______________ Suppose we have a circular array implementation of the queue class, with ten items in the queue stored at data[2] through data[11]. The CAPACITY is 42. Where does the push member function place the new entry in the array? _______________ Assess your knowledge. Answer each of the following multiple choice questions by ENCIRCLING the letter that corresponds to the BEST response. 1. A linear list of elements in which deletion can be done from one end (front) and insertion can take place only at the other end (rear) is known as a ? a) Queue b) Stack c) Tree d) Linked list 2. The data structure required for Breadth First Traversal on a graph is? a) Stack b) Array c) Queue d) Tree 3. A queue follows __________ a) FIFO (First In First Out) principle b) LIFO (Last In First Out) principle c) Ordered array d) Linear tree 4. Circular Queue is also known as ________ a) Ring Buffer b) Square Buffer c) Rectangle Buffer d) Curve Buffer 5. If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a time, in what order will they be removed? a) ABCD b) DCBA c) DCAB d) ABDC 6. A data structure in which elements can be inserted or deleted at/from both the ends but not in the middle is? a) Queue b) Circular queue c) Dequeue d) Priority queue 7. A normal queue, if implemented using an array of size MAX_SIZE, gets full when a) Rear = MAX_SIZE – 1 b) Front = (rear + 1)mod MAX_SIZE c) Front = rear + 1 d) Rear = front 8. Queues serve major role in ______________ a) Simulation of recursion b) Simulation of arbitrary linked list c) Simulation of limited resource allocation d) Simulation of heap sort 9. Which of the following is not the type of queue? a) Ordinary queue b) Single ended queue c) Circular queue d) Priority queue 10. Which of the following properties is associated with a queue? a) First In Last Out b) First In First Out c) Last In First Out d) Last In Last Out Unit VI: Trees 1. 2. 3. 4. 5. What is a tree Binary Trees Types of Binary Trees Properties of Binary Trees Binary Tree Traversals Objectives 1. Familiarized how trees are used to implement the file system of several popular operating systems. 2. How trees can be used to evaluate arithmetic expressions. 3. Demonstrated how to use trees to support searching operations in O(log n) average time, and how to refine these ideas to obtain O(log n) worstcase bounds. Acquire New Knowledge Lesson Proper DATA STRUCTURES & TREES Tree represents the nodes connected by edges. We will discuss binary tree or binary search tree specifically. Binary Tree is a special datastructure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children. A binary tree has the benefits of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or deletion operation are as fast as in linked list. Important Terms Following are the important terms with respect to tree. Path − Path refers to the sequence of nodes along the edges of a tree. Root − The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node. Parent − Any node except the root node has one edge upward to a node called parent. Child − The node below a given node connected by its edge downward is called its child node. Leaf − The node which does not have any child node is called the leaf node. Subtree − Subtree represents the descendants of a node. Visiting − Visiting refers to checking the value of a node when control is on the node. Traversing − Traversing means passing through nodes in a specific order. Levels − Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on. keys − Key represents a value of a node based on which a search operation is to be carried out for a node. Binary Search Tree Representation Binary Search tree exhibits a special behavior. A node's left child must have a value less than its parent's value and the node's right child must have a value greater than its parent value. We're going to implement tree using node object and connecting them through references. Tree Node The code to write a tree node would be similar to what is given below. It has a data part and references to its left and right child nodes. struct node { int data; struct node *leftChild; struct node *rightChild; }; In a tree, all nodes share common construct. BST Basic Operations The basic operations that can be performed on a binary search tree data structure, are the following − Insert − Inserts an element in a tree/create a tree. Search − Searches an element in a tree. Preorder Traversal − Traverses a tree in a pre-order manner. Inorder Traversal − Traverses a tree in an in-order manner. Postorder Traversal − Traverses a tree in a post-order manner. We shall learn creating (inserting into) a tree structure and searching a data item in a tree in this chapter. We shall learn about tree traversing methods in the coming chapter. Insert Operation The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left subtree and insert the data. Otherwise, search for the empty location in the right subtree and insert the data. Algorithm If root is NULL then create root node return If root exists then compare the data with node.data while until insertion position is located If data is greater than node.data goto right subtree else goto left subtree endwhile insert data end If Search Operation Whenever an element is to be searched, start searching from the root node, then if the data is less than the key value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow the same algorithm for each node. Algorithm If root.data is equal to search.data return root else while data not found If data is greater than node.data goto right subtree else goto left subtree If data found return node endwhile return data not found end if The implementation of this algorithm should look like this. struct node* search(int data) { struct node *current = root; printf("Visiting elements: "); while(current->data != data) { if(current != NULL) printf("%d ",current->data); //go to left tree if(current->data > data) { current = current->leftChild; } //else go to right tree else { } current = current->rightChild; //not found if(current == NULL) { return NULL; } } } return current; Binary Trees and Properties In this section we will see some important properties of one binary tree data structure. Suppose we have a binary tree like this. Some properties are − The maximum number of nodes at level ‘l’ will be 2l−12l−1 . Here level is the number of nodes on path from root to the node, including the root itself. We are considering the level of root is 1. Maximum number of nodes present in binary tree of height h is 2h−12h−1 . Here height is the max number of nodes on root to leaf path. Here we are considering height of a tree with one node is 1. In a binary tree with n nodes, minimum possible height or minimum number of levels arelog2⟮n+1⟯log2⟮n+1⟯ . If we consider that the height of leaf node is considered as 0, then the formula will be log2⟮n+1⟯−1log2⟮n+1⟯−1 A binary tree with ‘L’ leaves has at least log2L+1log2L+1 number of levels If a binary tree has 0 or 2 children, then number of leaf nodes are always one more than nodes with two children. Tree Traversal Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly access a node in a tree. There are three ways which we use to traverse a tree − In-order Traversal Pre-order Traversal Post-order Traversal Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the values it contains. In-order Traversal In this traversal method, the left subtree is visited first, then the root and later the right subtree. We should always remember that every node may represent a subtree itself. If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order. We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order. The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be − D→B→E→A→F→C→G Algorithm Until all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − Visit root node. Step 3 − Recursively traverse right subtree. Pre-order Traversal In this traversal method, the root node is visited first, then the left subtree and finally the right subtree. We start from A, and following pre-order traversal, we first visit A itself and then move to its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of pre-order traversal of this tree will be − A→B→D→E→C→F→G Algorithm Until all nodes are traversed − Step 1 − Visit root node. Step 2 − Recursively traverse left subtree. Step 3 − Recursively traverse right subtree. Post-order Traversal In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node. We start from A, and following Post-order traversal, we first visit the left subtree B. B is also traversed post-order. The process goes on until all the nodes are visited. The output of postorder traversal of this tree will be − D→E→B→F→G→C→A Algorithm Until all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − Recursively traverse right subtree. Step 3 − Visit root node. Apply Your Knowledge In this part, you will practice what you have learned. Draw a full binary tree with at least 6 nodes. Draw a binary taxonomy tree that can be used for these four animals: Rabbit, Horse, Whale, Snake. Assess your knowledge. Answer each of the following multiple choice questions by ENCIRCLING the letter that corresponds to the BEST response. 1. The number of edges from the root to the node is called __________ of the tree. a) Height b) Depth c) Length d) Width 2. The number of edges from the node to the deepest leaf is called _________ of the tree. a) Height b) Depth c) Length d) Width 3. What is a full binary tree? a) Each node has exactly zero or two children b) Each node has exactly two children c) All the leaves are at the same level d) Each node has exactly one or two children 4. What is a complete binary tree? a) Each node has exactly zero or two children b) A binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from right to left c) A binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right d) A tree In which all nodes have degree 2 5. What is the average case time complexity for finding the height of the binary tree? a) h = O(loglogn) b) h = O(nlogn) c) h = O(n) d) h = O(log n) 6. Which of the following is not an advantage of trees? a) Hierarchical structure b) Faster search c) Router algorithms d) Undo/Redo operations in a notepad 7. In a full binary tree if number of internal nodes is I, then number of leaves L are? a) L = 2*I b) L = I + 1 c) L = I – 1 d) L = 2*I – 1 8. Construct a binary tree by using postorder and inorder sequences given below. Inorder: N, M, P, O, Q Postorder: N, P, Q, O, M a) b) c) d) 9. Construct a binary search tree by using postorder sequence given below. Postorder: 2, 4, 3, 7, 9, 8, 5. a) b) c) d) 10. Construct a binary tree using inorder and level order traversal given below. Inorder Traversal: 3, 4, 2, 1, 5, 8, 9 Level Order Traversal: 1, 4, 5, 9, 8, 2, 3 a) b) c) d) Unit VII: Priority Queues & Heaps 1. 2. 3. 4. 5. 6. 7. What is a Priority Queue Priority queue ADT Priority Queue applications Priority queue implementation Heaps & Binary heaps Binary Heaps Heapsort Objectives 1. Understand the functions of the priority queue ADT. 2. Described the use of priority queues. 3. Advanced implementations of priority queues. Acquire New Knowledge Lesson Proper DATA STRUCTURES & PRIORITY QUEUE A priority queue is a special type of queue in which each element is associated with a priority and is served according to its priority. If elements with the same priority occur, they are served according to their order in the queue. Generally, the value of the element itself is considered for assigning the priority. For example, The element with the highest value is considered as the highest priority element. However, in other cases, we can assume the element with the lowest value as the highest priority element. In other cases, we can set priorities according to our needs. Element Removing Highest Priority Difference between Priority Queue and Normal Queue In a queue, the first-in-first-out rule is implemented whereas, in a priority queue, the values are removed on the basis of priority. The element with the highest priority is removed first. Implementation of Priority Queue Priority queue can be implemented using an array, a linked list, a heap data structure, or a binary search tree. Among these data structures, heap data structure provides an efficient implementation of priority queues. Hence, we will be using the heap data structure to implement the priority queue in this tutorial. A max-heap is implement is in the following operations. If you want to learn more about it, please visit max-heap and mean-heap. A comparative analysis of different implementations of priority queue is given below. Operations Linked List Binary Heap Binary Search Tree peek O(1) O(1) O(1) insert O(n) O(log n) O(log n) delete O(1) O(log n) O(log n) Priority Queue Operations Basic operations of a priority queue are inserting, removing, and peeking elements. Before studying the priority queue, please refer to the heap data structure for a better understanding of binary heap as it is used to implement the priority queue in this article. 1. Inserting an Element from the Priority Queue Inserting an element into a priority queue (max-heap) is done by the following steps. Insert the new element at the end of the tree. Insert an element at the end of the queue Heapify the tree. Heapify after insertion Algorithm for insertion of an element into priority queue (max-heap) If there is no node, create a newNode. else (a node is already present) insert the newNode at the end (last node from left to right.) heapify the array For Min Heap, the above algorithm is modified so that parentNode is always smaller than newNode . 2. Deleting an Element from the Priority Queue Deleting an element from a priority queue (max-heap) is done as follows: Select the element to be deleted . Select the element to be deleted Swap it with the last element. Swap with the last leaf node element Remove the last element. Remove the last element leaf Heapify the tree. Heapify the priority queue Algorithm for deletion of an element in the priority queue (max-heap) If nodeToBeDeleted is the leafNode remove the node Else swap nodeToBeDeleted with the lastLeafNode remove noteToBeDeleted heapify the array For Min Heap, the above algorithm is modified so that the both childNodes are smaller than currentNode . 3. Peeking from the Priority Queue (Find max/min) Peek operation returns the maximum element from Max Heap or minimum element from Min Heap without deleting the node. For both Max heap and Min Heap return rootNode 4. Extract-Max/Min from the Priority Queue Extract-Max returns the node with maximum value after removing it from a Max Heap whereas Extract-Min returns the node with minimum value after removing it from Min Heap. Priority Queue Implementations // Priority Queue implementation in Java import java.util.ArrayList; class Heap { // Function to heapify the tree void heapify(ArrayList<Integer> hT, int i) { int size = hT.size(); // Find the largest among root, left child and right child int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < size && hT.get(l) > hT.get(largest)) largest = l; if (r < size && hT.get(r) > hT.get(largest)) largest = r; // Swap and continue heapifying if root is not largest if (largest != i) { int temp = hT.get(largest); hT.set(largest, hT.get(i)); hT.set(i, temp); } } heapify(hT, largest); // Function to insert an element into the tree void insert(ArrayList<Integer> hT, int newNum) { int size = hT.size(); if (size == 0) { hT.add(newNum); } else { hT.add(newNum); for (int i = size / 2 - 1; i >= 0; i--) { heapify(hT, i); } } } // Function to delete an element from the tree void deleteNode(ArrayList<Integer> hT, int num) { int size = hT.size(); int i; for (i = 0; i < size; i++) { if (num == hT.get(i)) break; } int temp = hT.get(i); hT.set(i, hT.get(size - 1)); hT.set(size - 1, temp); } hT.remove(size - 1); for (int j = size / 2 - 1; j >= 0; j--) { heapify(hT, j); } // Print the tree void printArray(ArrayList<Integer> array, int size) { for (Integer i : array) { System.out.print(i + " "); } System.out.println(); } // Driver code public static void main(String args[]) { ArrayList<Integer> array = new ArrayList<Integer>(); int size = array.size(); Heap h = new Heap(); h.insert(array, 3); h.insert(array, 4); h.insert(array, 9); h.insert(array, 5); h.insert(array, 2); System.out.println("Max-Heap array: "); h.printArray(array, size); } } h.deleteNode(array, 4); System.out.println("After deleting an element: "); h.printArray(array, size); Priority Queue Applications Some of the applications of a priority queue are: Dijkstra's algorithm for implementing stack for load balancing and interrupt handling in an operating system for data compression in Huffman code Apply Your Knowledge In this part, you will practice what you have learned. A Priority-Queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of the heap is given below: 10, 8, 5, 3, 2 Two new elements ”1‘ and ”7‘ are inserted in the heap in that order. The level-order traversal of the heap after the insertion of the elements is: Assess your knowledge. Answer each of the following multiple choice questions by ENCIRCLING the letter that corresponds to the BEST response. 1. With what data structure can a priority queue be implemented? a) Array b) List c) Heap d) Tree 2. Which of the following is not an application of priority queue? a) Huffman codes b) Interrupt handling in operating system c) Undo operation in text editors d) Bayesian spam filter 3. Select the appropriate code that inserts elements into the list based on the given key value. (head and trail are dummy nodes to mark the end and beginning of the list, they do not contain any priority or element) a) public void insert_key(int key,Object item) { if(key<0) { Systerm.our.println("invalid"); System.exit(0); } else { Node temp = new Node(key,item,null); if(count == 0) { head.setNext(temp); temp.setNext(trail); } else { Node dup = head.getNext(); Node cur = head; while((key>dup.getKey()) && (dup!=trail)) { dup = dup.getNext(); cur = cur.getNext(); } cur.setNext(temp); temp.setNext(dup); } count++; } } b) public void insert_key(int key,Object item) { if(key<0) { Systerm.our.println("invalid"); System.exit(0); } else { Node temp = new Node(key,item,null); if(count == 0) { head.setNext(temp); temp.setNext(trail); } else { Node dup = head.getNext(); Node cur = dup; while((key>dup.getKey()) && (dup!=trail)) { dup = dup.getNext(); cur = cur.getNext(); } cur.setNext(temp); temp.setNext(dup); } count++; } } c) public void insert_key(int key,Object item) { if(key<0) { Systerm.our.println("invalid"); System.exit(0); } else { Node temp = new Node(key,item,null); if(count == 0) { head.setNext(temp); temp.setNext(trail); } else { Node dup = head.getNext(); Node cur = head; while((key>dup.getKey()) && (dup!=trail)) { dup = dup.getNext(); cur = cur.getNext(); } cur.setNext(dup); temp.setNext(cur); } } } count++; d) public void insert_key(int key,Object item) { if(key<0) { Systerm.our.println("invalid"); System.exit(0); } else { Node temp = new Node(key,item,null); if(count == 0) { head.setNext(temp); temp.setNext(trail); } else { Node dup = head.getNext(); Node cur = head; while((key>dup.getKey()) && (dup!=trail)) { dup = cur cur = cur.getNext(); } cur.setNext(dup); temp.setNext(cur); } count++; } } 4. What is the time complexity to insert a node based on key in a priority queue? a) O(nlogn) b) O(logn) c) O(n) d) O(n2) 5. What is the functionality of the following piece of code? public Object delete_key() { if(count == 0) { System.out.println("Q is empty"); System.exit(0); } else { Node cur = head.getNext(); Node dup = cur.getNext(); Object e = cur.getEle(); head.setNext(dup); count--; return e; } } a) Delete the second element in the list b) Return but not delete the second element in the list c) Delete the first element in the list d) Return but not delete the first element in the list 6. What is not a disadvantage of priority scheduling in operating systems? a) A low priority process might have to wait indefinitely for the CPU b) If the system crashes, the low priority systems may be lost permanently c) Interrupt handling d) Indefinite blocking 7. Which of the following is not an advantage of priority queue? a) Easy to implement b) Processes with different priority can be efficiently handled c) Applications with differing requirements d) Easy to delete elements in any case 8. What is the time complexity to insert a node based on position in a priority queue? a) O(nlogn) b) O(logn) c) O(n) d) O(n2) Unit VIII: Sorting 1. What is Sorting 2. Why Sorting is necessary 3. Classification of Sorting algorithms 4. Bubble Sort 5. Selection Sort 6. Insertion Sort 7. Shell Sort 8.Merge Sort 9. Heap Sort 10.Quick sort 11. Tree Sort Objectives 1. Identified types of sorting and their functions 2. Created a program using different types of sorting Acquire New Knowledge Lesson Proper DATA STRUCTURES & SORTING OVERVIEW Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a particular order. Most common orders are in numerical or lexicographical order. The importance of sorting lies in the fact that data searching can be optimized to a very high level, if data is stored in a sorted manner. Sorting is also used to represent data in more readable formats. Following are some of the examples of sorting in real-life scenarios − Telephone Directory − The telephone directory stores the telephone numbers of people sorted by their names, so that the names can be searched easily. Dictionary − The dictionary stores words in an alphabetical order so that searching of any word becomes easy. In-place Sorting and Not-in-place Sorting Sorting algorithms may require some extra space for comparison and temporary storage of few data elements. These algorithms do not require any extra space and sorting is said to happen in-place, or for example, within the array itself. This is called in-place sorting. Bubble sort is an example of in-place sorting. However, in some sorting algorithms, the program requires space which is more than or equal to the elements being sorted. Sorting which uses equal or more space is called not-in-place sorting. Merge-sort is an example of not-in-place sorting. Stable and Not Stable Sorting If a sorting algorithm, after sorting the contents, does not change the sequence of similar content in which they appear, it is called stable sorting. If a sorting algorithm, after sorting the contents, changes the sequence of similar content in which they appear, it is called unstable sorting. Stability of an algorithm matters when we wish to maintain the sequence of original elements, like in a tuple for example. Adaptive and Non-Adaptive Sorting Algorithm A sorting algorithm is said to be adaptive, if it takes advantage of already 'sorted' elements in the list that is to be sorted. That is, while sorting if the source list has some element already sorted, adaptive algorithms will take this into account and will try not to re-order them. A non-adaptive algorithm is one which does not take into account the elements which are already sorted. They try to force every single element to be re-ordered to confirm their sortedness. Important Terms Some terms are generally coined while discussing sorting techniques, here is a brief introduction to them − Increasing Order A sequence of values is said to be in increasing order, if the successive element is greater than the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next element is greater than the previous element. Decreasing Order A sequence of values is said to be in decreasing order, if the successive element is less than the current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is less than the previous element. Non-Increasing Order A sequence of values is said to be in non-increasing order, if the successive element is less than or equal to its previous element in the sequence. This order occurs when the sequence contains duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every next element is less than or equal to (in case of 3) but not greater than any previous element. Non-Decreasing Order A sequence of values is said to be in non-decreasing order, if the successive element is greater than or equal to its previous element in the sequence. This order occurs when the sequence contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order, as every next element is greater than or equal to (in case of 3) but not less than the previous one. DATA STRUCTURES & BUBBLE SORT Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items. How Bubble Sort Works? We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and precise. Bubble sort starts with very first two elements, comparing them to check which one is greater. In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27. We find that 27 is smaller than 33 and these two values must be swapped. The new array should look like this − Next we compare 33 and 35. We find that both are in already sorted positions. Then we move to the next two values, 35 and 10. We know then that 10 is smaller 35. Hence they are not sorted. We swap these values. We find that we have reached the end of the array. After one iteration, the array should look like this − To be precise, we are now showing how an array should look like after each iteration. After the second iteration, it should look like this − Notice that after each iteration, at least one value moves at the end. And when there's no swap required, bubble sorts learns that an array is completely sorted. Now we should look into some practical aspects of bubble sort. Algorithm We assume list is an array of n elements. We further assume that swap function swaps the values of the given array elements. begin BubbleSort(list) for all elements of list if list[i] > list[i+1] swap(list[i], list[i+1]) end if end for return list end BubbleSort Pseudocode We observe in algorithm that Bubble Sort compares each pair of array element unless the whole array is completely sorted in an ascending order. This may cause a few complexity issues like what if the array needs no more swapping as all the elements are already ascending. To ease-out the issue, we use one flag variable swapped which will help us see if any swap has happened or not. If no swap has occurred, i.e. the array requires no more processing to be sorted, it will come out of the loop. Pseudocode of BubbleSort algorithm can be written as follows − procedure bubbleSort( list : array of items ) loop = list.count; for i = 0 to loop-1 do: swapped = false for j = 0 to loop-1 do: /* compare the adjacent elements */ if list[j] > list[j+1] then /* swap them */ swap( list[j], list[j+1] ) swapped = true end if end for /*if no number was swapped that means array is sorted now, break the loop.*/ if(not swapped) then break end if end for end procedure return list Implementation One more issue we did not address in our original algorithm and its improvised pseudocode, is that, after every iteration the highest values settles down at the end of the array. Hence, the next iteration need not include already sorted elements. For this purpose, in our implementation, we restrict the inner loop to avoid already sorted values. import com.mtsystems.coot.IntContainer; public class BubbleSort { public final static int MAX = 10; public static IntContainer list = IntContainer.fromPartialData(MAX, 1, 8, 4, 6, 0, 3, 5, 2, 7, 9); public static void display() { System.out.print("["); // navigate through all items for(int i = 0; i < MAX; i++) { System.out.print(list.get(i) + " "); } System.out.println("]"); } public static void bubbleSort() { int temp; boolean swapped = false; // loop through numbers falling ahead for(int i = 0; i < MAX - 1; i++) { swapped = false; for(int j = 0; j < MAX - 1 - i; j++) { System.out.print(" Items compared: [ " + list.get(j) + ", " + list.get(j + 1) + " ] "); // check if next number is lesser than current no // swap the numbers. // (Bubble up the highest number) if(list.get(j) > list.get(j + 1)) { temp = list.get(j); list.set(j, list.get(j + 1)); list.set(j + 1, temp); swapped = true; System.out.println(" => swapped [" + list.get(j) + ", " + list.get(j + 1) + "]"); } else { System.out.println(" => not swapped"); } } // if no number was swapped that means // array is sorted now, break the loop. if(!swapped) { break; } System.out.print("Iteration " + (i + 1) + "#: "); display(); } } public static void main() { System.out.print("Input Array: "); display(); System.out.println(); } } bubbleSort(); System.out.print("\nOutput Array: "); display(); If we compile and run the above program, it will produce the following result − Output Input Array: [1 8 4 6 0 3 5 2 7 9 ] Items compared: [ 1, 8 ] => not swapped Items compared: [ 8, 4 ] => swapped [4, Items compared: [ 8, 6 ] => swapped [6, Items compared: [ 8, 0 ] => swapped [0, Items compared: [ 8, 3 ] => swapped [3, Items compared: [ 8, 5 ] => swapped [5, Items compared: [ 8, 2 ] => swapped [2, Items compared: [ 8, 7 ] => swapped [7, Items compared: [ 8, 9 ] => not swapped Iteration 1#: [1 4 6 0 3 5 2 7 8 9 ] Items compared: [ 1, 4 ] => not swapped Items compared: [ 4, 6 ] => not swapped Items compared: [ 6, 0 ] => swapped [0, Items compared: [ 6, 3 ] => swapped [3, Items compared: [ 6, 5 ] => swapped [5, Items compared: [ 6, 2 ] => swapped [2, Items compared: [ 6, 7 ] => not swapped Items compared: [ 7, 8 ] => not swapped Iteration 2#: [1 4 0 3 5 2 6 7 8 9 ] Items compared: [ 1, 4 ] => not swapped Items compared: [ 4, 0 ] => swapped [0, Items compared: [ 4, 3 ] => swapped [3, Items compared: [ 4, 5 ] => not swapped Items compared: [ 5, 2 ] => swapped [2, Items compared: [ 5, 6 ] => not swapped Items compared: [ 6, 7 ] => not swapped Iteration 3#: [1 0 3 4 2 5 6 7 8 9 ] Items compared: [ 1, 0 ] => swapped [0, Items compared: [ 1, 3 ] => not swapped Items compared: [ 3, 4 ] => not swapped Items compared: [ 4, 2 ] => swapped [2, Items compared: [ 4, 5 ] => not swapped Items compared: [ 5, 6 ] => not swapped Iteration 4#: [0 1 3 2 4 5 6 7 8 9 ] Items compared: [ 0, 1 ] => not swapped Items compared: [ 1, 3 ] => not swapped Items compared: [ 3, 2 ] => swapped [2, Items compared: [ 3, 4 ] => not swapped Items compared: [ 4, 5 ] => not swapped Iteration 5#: [0 1 2 3 4 5 6 7 8 9 ] Items compared: [ 0, 1 ] => not swapped Items compared: [ 1, 2 ] => not swapped Items compared: [ 2, 3 ] => not swapped Items compared: [ 3, 4 ] => not swapped Output Array: [0 1 2 3 4 5 6 7 8 9 ] 8] 8] 8] 8] 8] 8] 8] 6] 6] 6] 6] 4] 4] 5] 1] 4] 3] DATA STRUCTURES & INSERTION SORT This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort. The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2), where n is the number of items. How Insertion Sort Works? We take an unsorted array for our example. Insertion sort compares the first two elements. It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list. Insertion sort moves ahead and compares 33 with 27. And finds that 33 is not in the correct position. It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted after swapping. By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10. These values are not in a sorted order. So we swap them. However, swapping makes 27 and 10 unsorted. Hence, we swap them too. Again we find 14 and 10 in an unsorted order. We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items. This process goes on until all the unsorted values are covered in a sorted sub-list. Now we shall see some programming aspects of insertion sort. Algorithm Now we have a bigger picture of how this sorting technique works, so we can derive simple steps by which we can achieve insertion sort. Step Step Step Step than 1 − 2 − 3 − 4 − the If it is the first element, it is already sorted. return 1; Pick next element Compare with all elements in the sorted sub-list Shift all the elements in the sorted sub-list that is greater value to be sorted Step 5 − Insert the value Step 6 − Repeat until list is sorted Pseudocode procedure insertionSort( A : array of items ) int holePosition int valueToInsert for i = 1 to length(A) inclusive do: /* select value to be inserted */ valueToInsert = A[i] holePosition = i /*locate hole position for the element to be inserted */ while holePosition > 0 and A[holePosition-1] > valueToInsert do: A[holePosition] = A[holePosition-1] holePosition = holePosition -1 end while /* insert the number at hole position */ A[holePosition] = valueToInsert end for end procedure This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it is to be inserted there. Hence the name insertion sort. Implementation import com.mtsystems.coot.IntContainer; public class InsertionSort { public final static int MAX = 7; public static IntContainer intArray = IntContainer.fromPartialData(MAX, 4, 6, 3, 2, 1, 9, 7); public static void printline(int count) { for(int i = 0; i < count - 1; i++) { System.out.print("="); } System.out.println("="); } public static void display() { System.out.print("["); for(int i = 0; i < MAX; i++) { } System.out.print(intArray.get(i) + " "); } System.out.println("]"); public static void insertionSort() { int valueToInsert; int holePosition; for(int i = 1; i < MAX; i++) { valueToInsert = intArray.get(i); holePosition = i; 1) > valueToInsert) { while(holePosition > 0 && intArray.get(holePosition - intArray.set(holePosition, intArray.get(holePosition - 1)); holePosition--; System.out.println(" item moved : " + intArray.get(holePosition)); } if(holePosition != i) { System.out.println(" item inserted : " + valueToInsert + ", at position : " + holePosition); } } } intArray.set(holePosition, valueToInsert); } System.out.print("Iteration " + i + "#:"); display(); public static void main() { System.out.print("Input Array: "); display(); printline(50); insertionSort(); System.out.print("Output Array: "); display(); printline(50); } If we compile and run the above program, it will produce the following result − Output Input Array: [4 6 3 2 1 9 7 ] ================================================== Iteration 1#:[4 6 3 2 1 9 7 ] item moved : 6 item moved : 4 item inserted : 3, at position : 0 Iteration 2#:[3 4 6 2 1 9 7 ] item moved : 6 item moved : 4 item moved : 3 item inserted : 2, at position : 0 Iteration 3#:[2 3 4 6 1 9 7 ] item moved : 6 item moved : 4 item moved : 3 item moved : 2 item inserted : 1, at position : 0 Iteration 4#:[1 2 3 4 6 9 7 ] Iteration 5#:[1 2 3 4 6 9 7 ] item moved : 9 item inserted : 7, at position : 5 Iteration 6#:[1 2 3 4 6 7 9 ] Output Array: [1 2 3 4 6 7 9 ] ================================================== DATA STRUCTURES & QUICK SORT Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value. Quicksort partitions an array and then calls itself recursively twice to sort the two resulting subarrays. This algorithm is quite efficient for large-sized data sets as its average and worstcase complexity are O(nLogn) and image.png(n2), respectively. Partition in Quick Sort Following animated representation explains how to find the pivot value in an array. The pivot value divides the list into two parts. And recursively, we find the pivot for each sublists until all lists contains only one element. Quick Sort Pivot Algorithm Based on our understanding of partitioning in quick sort, we will now try to write an algorithm for it, which is as follows. Step 1 − Choose the highest index value has pivot Step 2 − Take two variables to point left and right of the list excluding pivot Step 3 − left points to the low index Step 4 − right points to the high Step 5 − while value at left is less than pivot move right Step 6 − while value at right is greater than pivot move left Step 7 − if both step 5 and step 6 does not match swap left and right Step 8 − if left ≥ right, the point where they met is new pivot Quick Sort Pivot Pseudocode The pseudocode for the above algorithm can be derived as − function partitionFunc(left, right, pivot) leftPointer = left rightPointer = right - 1 while True do while A[++leftPointer] < pivot do //do-nothing end while while rightPointer > 0 && A[--rightPointer] > pivot do //do-nothing end while if leftPointer >= rightPointer break else swap leftPointer,rightPointer end if end while swap leftPointer,right return leftPointer end function Quick Sort Algorithm Using pivot algorithm recursively, we end up with smaller possible partitions. Each partition is then processed for quick sort. We define recursive algorithm for quicksort as follows − Step 1 − Make the right-most index value pivot Step 2 − partition the array using pivot value Step 3 − quicksort left partition recursively Step 4 − quicksort right partition recursively Quick Sort Pseudocode To get more into it, let see the pseudocode for quick sort algorithm − procedure quickSort(left, right) if right-left <= 0 return else pivot = A[right] partition = partitionFunc(left, right, pivot) quickSort(left,partition-1) quickSort(partition+1,right) end if end procedure DATA STRUCTURES & SELECTION SORT Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparisonbased algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right. This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2), where n is the number of items. How Selection Sort Works? Consider the following depicted array as an example. For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14 is stored presently, we search the whole list and find that 10 is the lowest value. So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list, appears in the first position of the sorted list. For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner. We find that 14 is the second lowest value in the list and it should appear at the second place. We swap these values. After two iterations, two least values are positioned at the beginning in a sorted manner. The same process is applied to the rest of the items in the array. Following is a pictorial depiction of the entire sorting process − Now, let us learn some programming aspects of selection sort. Algorithm Step Step Step Step Step 1 2 3 4 5 − − − − − Set MIN to location 0 Search the minimum element in the list Swap with value at location MIN Increment MIN to point to next element Repeat until list is sorted Pseudocode procedure selection sort list : array of items n : size of list for i = 1 to n - 1 /* set current element as minimum*/ min = i /* check the element to be minimum */ for j = i+1 to n if list[j] < list[min] then min = j; end if end for /* swap the minimum element with the current element*/ if indexMin != i then swap list[min] and list[i] end if end for end procedure DATA STRUCTURES & SHELL SORT Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to the far left. This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts the less widely spaced elements. This spacing is termed as interval. This interval is calculated based on Knuth's formula as − Knuth's Formula h = h * 3 + 1 where − h is interval with initial value 1 This algorithm is quite efficient for medium-sized data sets as its average and worst-case complexity of this algorithm depends on the gap sequence the best known is Ο(n), where n is the number of items. And the worst case space complexity is O(n). How Shell Sort Works? Let us consider the following example to have an idea of how shell sort works. We take the same array we have used in our previous examples. For our example and ease of understanding, we take the interval of 4. Make a virtual sub-list of all values located at the interval of 4 positions. Here these values are {35, 14}, {33, 19}, {42, 27} and {10, 44} We compare values in each sub-list and swap them (if necessary) in the original array. After this step, the new array should look like this − Then, we take interval of 1 and this gap generates two sub-lists - {14, 27, 35, 42}, {19, 10, 33, 44} We compare and swap the values, if required, in the original array. After this step, the array should look like this − Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sort to sort the array. Following is the step-by-step depiction − We see that it required only four swaps to sort the rest of the array. Algorithm Following is the algorithm for shell sort. Step Step Step Step 1 2 3 3 − − − − Initialize the value of h Divide the list into smaller sub-list of equal interval h Sort these sub-lists using insertion sort Repeat until complete list is sorted Pseudocode Following is the pseudocode for shell sort. procedure shellSort() A : array of items /* calculate interval*/ while interval < A.length /3 do: interval = interval * 3 + 1 end while while interval > 0 do: for outer = interval; outer < A.length; outer ++ do: /* select value to be inserted */ valueToInsert = A[outer] inner = outer; /*shift element towards right*/ while inner > interval -1 && A[inner - interval] >= valueToInsert do: A[inner] = A[inner - interval] inner = inner - interval end while /* insert the number at hole position */ A[inner] = valueToInsert end for /* calculate interval*/ interval = (interval -1) /3; end while end procedure DATA STRUCTURES & MERGE SORT Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms. Merge sort first divides the array into equal halves and then combines them in a sorted manner. How Merge Sort Works? To understand merge sort, we take an unsorted array as the following − We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4. This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves. We further divide these arrays and we achieve atomic value which can no more be divided. Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists. We first compare the element for each list and then combine them into another list in a sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are placed sequentially. In the next iteration of the combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in a sorted order. After the final merging, the list should look like this − Now we should learn some programming aspects of merge sorting. Algorithm Merge sort keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is sorted. Then, merge sort combines the smaller sorted lists keeping the new list sorted too. Step 1 − if it is only one element in the list it is already sorted, return. Step 2 − divide the list recursively into two halves until it can no more be divided. Step 3 − merge the smaller lists into new list in sorted order. Pseudocode We shall now see the pseudocodes for merge sort functions. As our algorithms point out two main functions − divide & merge. Merge sort works with recursion and we shall see our implementation in the same way. procedure mergesort( var a as array ) if ( n == 1 ) return a var l1 as array = a[0] ... a[n/2] var l2 as array = a[n/2+1] ... a[n] l1 = mergesort( l1 ) l2 = mergesort( l2 ) return merge( l1, l2 ) end procedure procedure merge( var a as array, var b as array ) var c as array while ( a and b have elements ) if ( a[0] > b[0] ) add b[0] to the end of c remove b[0] from b else add a[0] to the end of c remove a[0] from a end if end while while ( a has elements ) add a[0] to the end of c remove a[0] from a end while while ( b has elements ) add b[0] to the end of c remove b[0] from b end while return c end procedure DATA STRUCTURES & HEAP SORT Heap sort is performed on the heap data structure. We know that heap is a complete binary tree. Heap tree can be of two types. Min-heap or max heap. For min heap the root element is minimum and for max heap the root is maximum. After forming a heap, we can delete an element from the root and send the last element to the root. After these swapping procedure, we need to re-heap the whole array. By deleting elements from root we can sort the whole array. The complexity of Heap Sort Technique Time Complexity: O(n log n) Space Complexity: O(1) Input and Output Input: A list of unsorted data: 30 8 99 11 24 39 Output: Array before Sorting: 30 8 99 11 24 39 Array after Sorting: 8 11 24 30 39 99 Algorithm heapify(array, size) Input − An array of data, and the total number in the array Output − The max heap using an array element Begin for i := 1 to size do node := i par := floor (node / 2) while par >= 1 do if array[par] < array[node] then swap array[par] with array[node] node := par par := floor (node / 2) done done End DATA STRUCTURES & TREE SORT Tree sort is a sorting algorithm that is based on Binary Search Tree data structure. It first creates a binary search tree from the elements of the input list or array and then performs an inorder traversal on the created binary search tree to get the elements in sorted order. Algorithm: Step 1: Take the elements input in an array. Step 2: Create a Binary search tree by inserting data items from the array into the binary search tree. Step 3: Perform in-order traversal on the tree to get the elements in sorted order. Apply Your Knowledge In this part, you will practice what you have learned. Design an algorithm that can sort N numbers using any sort method. Select only one (1) method. Assess your knowledge. Answer each of the following multiple choice questions by ENCIRCLING the letter that corresponds to the BEST response. 1. Which of the following is not an advantage of optimized bubble sort over other sorting techniques in case of sorted elements? a) It is faster b) Consumes less memory c) Detects whether the input is already sorted d) Consumes less time 2. The given array is arr = {1, 2, 4, 3}. Bubble sort is used to sort the array elements. How many iterations will be done to sort the array? a) 4 b) 2 c) 1 d) 0 3. The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and selection sort respectively are, a) 5 and 4 b) 4 and 5 c) 2 and 4 d) 2 and 5 4. The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable)The number of iterations in selection sort and bubble sort respectively are, a) 5 and 4 b) 1 and 4 c) 0 and 4 d) 4 and 1 5. Which of the following algorithm implementations is similar to that of an insertion sort? a) Binary heap b) Quick sort c) Merge sort d) Radix sort 6. Merge sort uses which of the following technique to implement sorting? a) backtracking b) greedy algorithm c) divide and conquer d) dynamic programming 7. What is the other name for a shell sort algorithm? a) Diminishing increment sort b) Diminishing decrement sort c) Insertion sort d) Selection sort 8. How many sub arrays does the quick sort algorithm divide the entire array into? a) one b) two c) three d) four 9. How many arrays are required to perform deletion operation in a heap? a) 1 b) 2 c) 3 d) 4 10. Which of the following is a comparison based sort? a) tree sort b) radix sort c) counting sort d) pigeonhole sort References Here’s the list of the resources and links from which the content of the lesson was based from. Karumanchi,N.,(2017) Data Structures & Algorithms Made easy, 5th edition https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm https://www.w3schools.in/data-structures-tutorial/intro/