Professional Documents
Culture Documents
Handout: Data Structures With C: Version: DSC/Handout/0307/2.1 Date: 05-03-07
Handout: Data Structures With C: Version: DSC/Handout/0307/2.1 Date: 05-03-07
Handout: Data Structures With C: Version: DSC/Handout/0307/2.1 Date: 05-03-07
Cognizant 500 Glen Pointe Center West Teaneck, NJ 07666 Ph: 201-801-0233 www.cognizant.com
TABLE OF CONTENTS
Introduction ................................................................................................................................4 About this Document..................................................................................................................4 Target Audience.........................................................................................................................4 Objectives ..................................................................................................................................4 Pre-requisite ..............................................................................................................................4 Session 1: Introduction to Data Structure .................................................................................5 Learning Objectives ...................................................................................................................5 Overview....................................................................................................................................5 Summary ...................................................................................................................................9 Test your Understanding..........................................................................................................10 Session 2: Arrays ......................................................................................................................11 Learning Objectives .................................................................................................................11 Overview..................................................................................................................................11 Summary .................................................................................................................................20 Test your Understanding..........................................................................................................20 Session 4: Linked Lists .............................................................................................................21 Learning Objectives .................................................................................................................21 Linked lists ...............................................................................................................................21 Summary .................................................................................................................................32 Test your Understanding..........................................................................................................32 Session 6: Sorting and Searching ............................................................................................33 Learning Objectives .................................................................................................................33 Sorting .....................................................................................................................................33 Summary .................................................................................................................................43 Test your Understanding..........................................................................................................44 Session 8: Trees ........................................................................................................................45 Learning Objectives .................................................................................................................45 Overview: .................................................................................................................................45 Summary .................................................................................................................................56 Test your Understanding..........................................................................................................56
Page 2 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 3 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Introduction
Target Audience
In-Campus Trainees
Objectives
Acquire the basic knowledge on data structures Select the appropriate data structures for the application Analyze the complexity of the algorithm Apply data structures using data structures
Pre-requisite
The participants must have basic knowledge in writing programs using C.
Page 4 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Learning Objectives
After completing this chapter, you will be able to: Define a data structure List the types of data structures Identify how to analyze and select data structure for a particular application
Overview
Study of computer science involves study of organization, manipulation and utilization of data in a computer in order to improve the efficiency of the processor and memory. Data type and data structure Data can be represented in the form of binary digits in memory. A binary digit can be stored using the basic unit of data called bit. A bit can represent either a zero or a one. Data type A data type defines the specification of a set of data and the characteristics for that data. Data type is derived from the basic nature of data that are stored for processing rather from their implementation. Data Structure Data structure refers to the actual implementation of the data type and offers a way of storing data in an efficient manner. Any data structure is designed to organize data to suit a specific purpose so that it can be accessed and worked in appropriate ways both effectively and efficiently. In computer programming, a data structure may be selected or designed to store data for the purpose of working on it by various algorithms. The choice of a data structure begins from the choice of an abstract data type. Data structures are implemented using the data types, references and operations on them that are provided by a programming language. Example data structures include: Arrays Stacks Queues Linked Lists
Page 5 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Some of the linear structures are: Array: Linked-list: Stack: Queue: Priority queue: Traversal: Search: Insertion: Deletion: Sorting: Merging: Fixed-size Variable-size Add to top and remove from top Add to back and remove from front Add anywhere, remove the highest priority Travel through the data structure Traversal through the data structure for a given element Adding new elements to the data structure Removing an element from the data structure Arranging the elements in some type of order Combining two similar data structures into one
Hash table A hash table, or a hash map, is a data structure that associates keys with values. A function termed as Hash function is applied on the key to find the address of the record. Non linear structures A data structure is said to be non linear if its elements are not in a sequence. The elements in the data structure are not arranged in a linear manner; rather it has a branched structure. Some of the non linear structures are: Tree: Graph: Collection of nodes represented in hierarchical fashion Collection of nodes connected together through edges
Page 6 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 7 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Big O notation or Big Oh notation Big O notation (Big Oh notation) expresses the amount of time required by the algorithm to execute. It can be denoted using the symbol O. It is used in the analysis of the complexity of algorithms and is used to characterize a function's behavior for the extreme inputs in a simple way. The measurement of complexity for different scenarios is expressed as follows: For a method which executes in constant time period, the complexity is given by O(1) For a method which executes in linear time period, the complexity is given by O(N) For a method which executes in quadratic time period, the complexity can be given by O(N2) Determination of complexities Determining the complexity of an algorithm depends on the statements being used in the algorithm. For different types of statements the complexity is given below Sequence of statements Statement 1; Statement 2; . . . . . Statement n; // none of the statements are loops, all are independent statements Time period can be given by Total time = time (statement 1) + time (statement 2) + . + time (statement 3) If each statement is simple, then the time for each statement is constant, and hence the total time is also constant. This makes the complexity as O(1). Selection statement (if-then-else) if (condition) Sequence of statements 1; else Sequence of statements 2;
Page 8 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Summary
Study of data structure deals with the actual implementation of the data type and offers a way of storing data in an efficient manner. An Abstract Data Type (ADT) is a data type together with the operations, whose properties are specified independently of any particular implementation The different types of data structure available are: o o o o Linear Hash table Trees Graphs
A well-designed data structure allows a variety of critical operations to be performed, using as few resources, both execution time and memory space, as possible. Big O Notation can be made use of for the analysis of the complexity of algorithms.
Page 9 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
2. ParentChild relationship can be considered as a linear data structure a. True b. False Answers 1. c 2. b
Page 10 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Session 2: Arrays
Learning Objectives
After completing this chapter, you will be able to: Define arrays Use arrays as data structures
Overview
An array is a collection of individual values of the same data type stored in consequent memory locations. An array index (positioning in the array) usually starts from 0. We can even specify the value from which the index should start depending on the language we use. Here is an array of integers: myArray 13 0 5 1 12 2 3 3 6 4 Array values Array positions/Index
Referring to elements of the array The position of an element in an array is given by the index. The name of the array, followed by the index, is used to refer to a particular element: myArray[1] = 5; The above statement assigns the value 5 to the element at the position 1(second element) of the array, myArray. Using elements of an array Elements of the array can be used in the same way as variables of the same data type can be used. i.e. an element of an array of integers can be used anywhere an integer variable can be used. printf ('The fifth element of the array is %d', myArray[4]);
Page 11 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
for ( count = 0 ; count < 5 ; count++) { evens[count] = 2 * count; } The above piece of code will construct an array evens as given below 0 2 4 6 8 Array values
Array index
Multi Dimensional Arrays These are the arrays which has more than one dimension. For example, the following declaration in C creates a two-dimensional array of two rows and two columns: int myArray1[4,2]
The following declaration creates an array of three dimensions, 2, 2, and 3: int myArray2[4,2,3];
Initialization The following piece of code initializes the arrays myArray1 and myArray2 myArray1 = {(1, 2), (3, 4)} myArray2 = {(1, 2), (3, 4), (5, 6)} In a matrix form the above array can be represented as below myArray1 1 3 myArray2 1 3 5 2 4 6
2 4
Page 12 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Advantages and disadvantages of an array Advantages Array data structure is simple to use. Elements in an array are stored in contiguous memory locations and hence each element can be accessed directly using their index. Allocation and de-allocation of memory is done automatically by the computer. Disadvantages Elements in an array are stored in contiguous memory locations and hence array can not be stored if the available memory is non contiguous. i.e. if the size of the array is n bytes, then there should be n contiguous bytes available in memory. The array size is fixed and hence the size of the array can not be reduced or increased at run time based on the requirement. Stacks A stack is a homogeneous collection of items of any one type, arranged linearly with access at one end only, known as the top. This means that data can be added or removed from only the top. Formally this type of stack is called a Last In First Out (LIFO) stack. Data is added to the stack using the Push operation, and removed using the Pop operation.
In order to clarify the idea of a stack here is an example. Think of a number of plates kept in a cafeteria. When the plates are being stacked, they are added one on top of each other. It doesn't make much sense to put each plate on the bottom of the pile, as that would be far more work. Similarly, when a plate is taken, it is usually taken from the top of the stack. Stack consists of two parts: Storage space within stack that contains the elements of a stack. Top of stack that refers to the element pushed recently.
Page 13 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
A stack can be implemented either using an array or a linked list. Stack implementation using an array Top is an integer value, which contains the array index for the top of the stack. Each time data is pushed or popped, top is incremented or decremented accordingly, to keep track of the current top of the stack. By convention, an empty stack is indicated by setting top to be equal to -1. Stacks implemented as arrays are useful if a fixed amount of data is to be used. However, if the amount of data is not a fixed size or the amount of the data fluctuates widely during the stack's life time, then an array is a poor choice for implementing a stack. Any recursive call is implemented with the help of a stack by the computer. The size of the stack can not be predicted in recursion, and implementing the stack using array is a poor choice in this
Algorithm to implement the operations using array Push: if(top>=total_no_elements) return(1); else { printf("\n Enter the element \n"); scanf("%d",&stack[top]); top++; } // Error code
Page 14 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Pop:
This operation removes an item from Data type the stack. This operation returns the value of the Data type item at the top of the stack. This operation returns true if the stack Boolean is empty and false if it is not. This operation returns true if the stack Boolean is full and false if it is not.
Top:
Is Empty:
Is Full:
Page 15 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Types of queues Circular queue A circular queue is one in which the insertion of a new element is done at the very first location of the queue if the last location of the queue is full. i.e. circular queue is one in which the first element comes just after the last element. A circular queue overcomes the problem of unutilized space in linear queues implemented as arrays. A circular queue also have a Front and Rear to keep the track of elements to be deleted and inserted and therefore to maintain the unique characteristic of the queue. The assumptions made are: 1. 2. 3. 4. Front will always be pointing to the first element If Front=Rear, the queue is empty Each time a new element is inserted into the queue the Rear is incremented by one. Each time an element is deleted from the queue the value of Front is incremented by one
Page 16 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Q[0]
Q[1]
Q[4] Q[3]
Q[2]
Inserting and deleting elements Insertion and deletion of elements in a circular queue is the same as that in a linear queue except that whenever an element is deleted from the front of the queue, the rear pointer can be made to point to the vacant position and the element can be inserted there once the queue is full. Front
10
Q[4] Q[3]
20
Rear
Front
10
40
Rear
20 30
Before insertion
Page 17 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Front
Q[0]
10
40
Rear
20 30
After inserting two elements 30 and 40 Queue full Deletion in a circular queue Now Q[0] will be available in the queue for another insertion. Double Ended Queues Double ended queue is a homogeneous list of elements in which insertion and deletion operations are performed from both the ends. They are also called as deque. There are two types of deques Input-restricted deques and Output-restricted deques
The major operations involved are: Insertion of an element at the Rear end of the queue. Deletion of an element from the Front end of the queue Insertion of an element at the Front end of the queue Deletion of an element from the Rear end of the queue For an input-restricted deque, all the four operations mentioned above are valid. For an outputrestricted deque, all the above points except the fourth are valid. Priority Queue In priority queues, the items added to the queue have a priority associated with them which determines the order in which they exit the queue. Items with highest priority are removed first. A priority queue is an abstract data type supporting the following three operations: add an element to the queue with an associated priority remove the element from the queue that has the highest priority, and return it (optionally) peek at the element with highest priority without removing it The simplest way to implement a priority queue data type is to keep an associative array mapping each priority to a list of elements with that priority
Page 18 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 19 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Summary
An array is a collection of individual values of the same data type stored in adjacent memory locations A stack is a homogeneous collection of items of any one type, arranged linearly with access at one end only, known as the top. The two major operations available for a stack include push(adding an element) and pop(deleting an element) A collection of items in which only the earliest added item may be accessed. Basic operations are add (to the tail) or enqueue and delete (from the head) or dequeue. The major variations for queues are double ended queue, circular queue and priority queue
Page 20 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Learning Objectives
After completing this chapter, you will be able to: Define linked list Implement linked list operations in your program
Linked lists
A linked list can be viewed as a group of items, each of which points to the item in its neighbourhood. An item in a linked list is known as a node. A node contains a data part and one or two pointer part which contains the address of the neighbouring nodes in the list. Linked list is a data structure that supports dynamic memory allocation and hence it solves the problems of using an array.
Types of linked lists The different types of linked lists include: Singly linked lists Circular linked lists Doubly linked lists Simple/Singly Linked Lists In singly linked lists, each node contains a data part and an address part. The address part of the node points to the next node in the list. Node Structure of a linked list
Data part
Link part
An example of a singly linked list can be pictured as shown below. Note that each node is pictured as a box, while each pointer is drawn as an arrow. A NULL pointer is used to mark the end of the list.
Page 21 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
A head pointer to a list Possible Operations on a singly linked list Insertion: Elements are added at any position in a linked list by linking nodes. Deletion: Elements are deleted at any position in a linked list by altering the links of the adjacent nodes. Searching or Iterating through the list to display items. To insert or delete items from any position of the list, we need to traverse the list starting from its root till we get the item that we are looking for. Implementation of a singly linked list Creating a linked list A node in a linked list is usually a structure in C and can be declared as struct Node { int info; Node *next; }; //end struct A node is dynamically allocated as follows: Node *p; p = new Node; For creating the list, the following code can be used: do { Current_node = malloc (sizeof (node) ); Current_node->info=input_value; Current_node->next=NULL; if(root_node==NULL) root_node=Current_node; else
Page 22 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
The following figure illustrates how a node is inserted at the beginning of the list.
Page 23 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Deleting an element After getting the element to be removed, the following code can be used to remove the particular element. temp_node=root_node; if ( root_node != NULL ) if ( temp_node->info == input_element ) { root_node=root_node->next; return; } While ( temp_node != NULL input_element ) temp_node = temp_node->next; if ( temp->next != NULL ) { && temp_node->next->info !=
delete_node = temp_node->next; temp_node->next=delete_node->next; free ( delete_node ) ; } The following figures illustrate the deletion of an intermediate node and the deletion of the first node from the list.
Page 24 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
To display the elements of the list temp_node = root_node; while(temp_node != NULL) { printf("%d\t", temp_node->info); temp_node = temp_node->next; } The following figure illustrates the above piece of code.
Page 25 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Data
Next Link
Implementation of a doubly linked list Adding an element to the list To add the first node first_node->next = NULL; first_node->data = input_element; first_node->prev = NULL; To add a node at the position specified Temp_node = *first_node; for ( counter = 0 ; counter<position-1 ; counter++ ) { Temp_node = Temp_node->next; } new_node->next = temp_node->next; temp_node->next->new_node; new_node->prev = temp_node->next->prev; temp_node->next->prev = new_node;
Page 26 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Circular Linked Lists In a circularly-linked list, the first and final nodes are linked together. In another words, circularlylinked lists can be seen as having no beginning or end. To traverse a circular linked list, begin at any node and follow the list in either direction until you return to the original node. This type of list is most useful in cases where you have one object in a list and wish to see all other objects in the list. The pointer pointing to the whole list is usually called the end pointer. Singly-circularly-linked list In a singly-circularly-linked list, each node has one link, similar to an ordinary singly-linked list, except that the link of the last node points back to the first node. As in a singly-linked list, new nodes can only be efficiently inserted after a node we already have a reference to. For this reason, it's usual to retain a reference to only the last element in a singly-circularly-linked list, as this allows quick insertion at the beginning, and also allows access to the first node through the last node's next pointer. The following figure shows a singly circularly linked list.
10
20
30
40
Doubly-circularly-linked list In a doubly-circularly-linked list, each node has two links, similar to a doubly-linked list, except that the previous link of the first node points to the last node and the next link of the last node points to the first node. As in doubly-linked lists, insertions and removals can be done at any point with access to any nearby node.
Page 27 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
10
20
30
40
Circularly-linked list vs. linearly-linked list Circularly linked lists are useful to traverse an entire list starting at any point. In a linear linked list, it is required to know the head pointer to traverse the entire list. The linear linked list cannot be traversed completely with the help of an intermediate pointer. Access to any element in a doubly circularly linked list is much easier than in a linearly linked list since the particular element can be approached in two directions. For example to access an element present in the fourth node of a circularly linked list having five elements, it is enough to start from the last node and traverse the list in the reverse direction to get the value in the fourth node. Implementation of a circular linked list: Creating the list while (input_element != -999) { new_node=(struct node *) malloc (size); new_node->info=input_element; if ( root_node==NULL ) root_node=new_node; else ( *last_node )->next=new_node; (*last_node)=new_node; scanf("%d",&input_element); } if(root!=NULL) new->next=root; return root;
Page 28 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
&&
(temp_node->next
!=
Page 29 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Top
20
NULL
The list consists of three cells, each of which holds a data object and a link to another cell. A variable, top, holds the address of the first cell in the list. An empty stack looks like this: Top NULL Implementing stacks as linked lists provides a feasibility on the number of nodes by dynamically growing stacks, as a linked list is a dynamic data structure. The stack can grow or shrink as the program demands it to. Algorithm to implement stack operations using pointers: Push node=(struct stack*)malloc(sizeof(struct stack)); printf("\n\n Enter the data scanf("%d",&node->data); node->link=top; top=node; Pop if(top==NULL) return(1); else { printf("\n \n Item deleted is %d ",top->data); top=top->link; } //Error code ");
Page 30 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
A pictorial representation of a queue being implemented as a linked list is given below. The variable rear points to the last item in the queue.
Front
3
Rear
NULL
Algorithm to represent queue operations using pointers Inserting an element new_element->link = NULL; if (front==NULL) front = new_element; else rear->link = new_element; rear = new_element;
Page 31 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Summary
A linked list is a collection of elements called nodes, each of which contains a data portion and a pointer to the node following that one in the linear ordering of the list. A singly linked list is a dynamic data structure which can grow and shrink depending upon the operations made. It has a single pointer which points to the successive node in the list. A doubly linked list is one in which all nodes are linked together by multiple number of links which help in accessing both the successor node and the predecessor node from a given node position. It provides bi-directional traversing. A circular linked list is the one which has no end. i.e the link field of the last node does not point to NULL, rather it points back to the beginning of the linked list. Stacks and queues can be more efficiently implemented using pointers rather than by using arrays.
Answers 1. b 2. b
Page 32 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Learning Objectives
After completing this chapter, you will be able to: Explain the concepts of sorting and searching List the advantages of each technique List the limitations of each technique
Sorting
Sorting refers to ordering data in an increasing or decreasing fashion according to some linear relationship among the data items. Sorting can be done on names, numbers and records. Sorting reduces the For example, it is relatively easy to look up the phone number of a friend from a telephone dictionary because the names in the phone book have been sorted into alphabetical order. This example clearly illustrates one of the main reasons that sorting large quantities of information is desirable. That is, sorting greatly improves the efficiency of searching. If we were to open a phone book, and find that the names were not presented in any logical order, it would take an incredibly long time to look up someones phone number. Sorting can be performed using several methods, they are: Selection Sort. In this method, the successive elements are selected in order and are placed in their proper sorted positions. Insertion sort. In this method, sorting is done by inserting elements into an existing sorted list. Initially, the sorted list has only one element. Other elements are gradually added into the list in the proper position. Bubble Sort. In this method, the entire file will be passed through several times. Each pass will compare each element with its successor and putting the element in the proper position. Merge Sort. In this method, the elements are divided into partitions until each partition has sorted elements. Then, these partitions are merged and the elements are properly positioned to get a fully sorted list.
Page 33 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 34 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Each pass consists of comparing each element in the file with its successor (x[i] > x[i+1]) Swap the two elements if they are not in proper order. After each pass i, the largest element x[n-(i1)] is in its proper position within the sorted array. Bubble Sort - Algorithm bubble(int x[], int n) { int hold, j, pass; int switched = TRUE; for (pass = 0; pass < n - 1 && switched == TRUE; pass++) { switched = FALSE; for (j = 0; j < n-pass-1; j++) if (x[j] > x[j+1]) { switched = TRUE; /* swap x[j], x[j+1] */ hold = x[j]; x[j] = x[j+1]; x[j+1] = hold; } } } In the first pass, n-1 items have to be scanned. On the second pass, the second largest item will move to its correct position, and on the third pass (stopping at item n-3) the third largest will be in place. It is this gradual filtration, or bubbling of the larger items to the top end that gives this sorting technique its name. /* it stops if there is no swap in the pass */
Page 35 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 36 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Heap Sort In heap sort the file to be sorted is interpreted as a binary tree. The sorting technique is implemented using array, which is a sequential representation of binary tree. The positioning of a node is given as follows For a node at position i the parent is at position i/2, the left child is at position 2i and right child is at position 2i+1 ( 2i and 2i+1 <=n, otherwise children do not exist). Heap sort is a two stage method. In the first stage the tree representing the input data is converted into a heap. A heap can be defined as a complete binary tree with the property that the value of each node is at least as large as the value of its children nodes. This, in turn, gives the root of the tree as the largest key. In the second stage the output sequence is generated in decreasing order by outputting the root and restructuring the remaining tree into a heap.
Page 37 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
34
64
64
51
34
51
32
21
32
21
Input File Initial Heap In fig 6.3(a) given below, the first largest number 64 which was brought into root is interchanged with the last element 21 (index 6) in the tree. For easy identification of arranged elements the edge is removed from its parent. In fig 6.3(b) given below, the same procedure is followed to bring 51 to root and is interchanged with the element in index 5. The same step is followed in fig 6.3(c) and fig 6.3(d) to get a sorted file as given in fig 6.3(e)
51
34
32
34
32
21
21
64
51
64
6.3 (a)
6.3 (b)
Page 38 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
32
21
21
32
34
51
64
34
51
64
6.3 (c)
6.3 (d)
21
32
34
51
64
6.3 (e) Sorted File Algorithm 6.3.1: Heap Sort implementation Heap is an algorithm which sorts the given set of numbers using heap sort technique. Where n is the number of elements, a is the array representation of elements in the input binary tree. The heap algorithm 6.3.1 calls adjust algorithm 6.3.2 each time when heaping is needed. heap(a,n) { Int i,t; for(i=n/2;i>=1;i--) { adjust(a,i,n); } for(i=n;i>=2;i--) { t=a[i]; a[i]=a[1]; a[i]=t; adjust(a,1,i-1); } }
Page 39 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 40 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
The code searches for the element through a loop starting form 0 to n. The loop can terminate in one of two ways. If the index variable i reach the end of the list, the loop condition fails. If the current item in the list matches the key, the loop is terminated early with a break statement. Then the algorithm tests the index variable to see if it is less than that size (thus the loop was terminated early and the item was found), or not (and the item was not found). Example 6.4 Assume the element 45 is searched from a sequence of sorted elements 12, 18, 25, 36, 45, 48, 50. The Linear search starts from the first element 12, since the value to be searched is not 12 (value 45), the next element 18 is compared and is also not 45, by this way all the elements before 45 are compared and when the index is 5, the element 45 is compared with the search value and is equal, hence the element is found and the element position is 5. List 12 18 25 36 12 18 25 12 18 25 12 18 25 12 18 25 36 36 36 36 i 1 2 3 4 5 Result of comparison 12 <> 45 : false 18 <> 45 : false 25 <> 45 : false 36 <> 45 : false 45 = 45 : true
45 45 45 45 45
48 48 48 48 48
50 50 50 50 50
Page 41 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 42 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
36 36 36
45 45 45
48 48 48
50 50 50
Binary
Summary
Sorting is process of arranging elements either in ascending or descending order. This facilitates the searching faster. Bubble sorting is a sorting in which each element is compared with its adjacent elements and largest value is moved to last. Quick sorting is a sorting by partitioning. Instead of a single element a pair of elements are arrange in one swap. Heap sorting is a sorting by heaping the elements in a tree. It works with the same complexity in all its worst, best and average cases. In Linear search all the elements preceding the search element must be searched. In Binary search the middle element is compared and either the left are right part is only checked instead of all.
Page 43 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
2. Quick sort works better if the input elements are of a. Sorted order b. Jumbled order c. Reverse order d. All the above Answers 1. a 2. c
Page 44 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Session 8: Trees
Learning Objectives
After completing this chapter, you will be able to Describe a tree Explain how a tree can be represented internally Describe how a tree can be traversed
Overview:
The data structures discussed in the previous sessions like Lists, stacks, and queues, are all linear data structures. Tree is one of the several types of non-linear data structure. Tree is a collection of nodes represented in a hierarchical fashion, with a specially designated node called root. Except root all other nodes have parent in their higher hierarchy. A parent node of a particular node is the one which is in the higher hierarchy for a A node can have exactly one parent i.e. a node can be attached to exactly one node in its higher hierarchy. Example 8.1
Page 45 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
The line depicts the connectivity between two nodes Sequence of nodes connected Number of nodes involved in the path Length of the longest path from the root Length of the path to that node from the root Number of children connected from that node Degree of a node which has maximum degree
Some applications of trees are: representing family genealogy as the underlying structure in decision-making algorithms to represent priority queues (a special kind of tree called a heap) to provide fast access to information in a database (a special kind of tree called a btree) Binary Tree Binary tree is a finite set of nodes which either empty, or consist of a root and two disjoint binary trees, called the left and right sub-trees. In other words it can be defined as a tree in which all the nodes can have 2 as a maximum degree i.e. a node can have maximum two children. A binary tree differs from a general tree in the following aspects: A tree must have at least one node but a binary tree may be empty. A tree may have any number of sub-trees but a binary tree can have at most two.
Page 46 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Full Binary tree: A binary tree in which all its leaf nodes are in the same level is called a full binary tree. Example 8.3
Complete Binary tree A binary tree in which the array representation is contiguous without any null pointers in between is a complete binary tree.
Page 47 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
1 B
2 C
3 D
4 E
In a binary tree the maximum number of nodes at level i (level of the root node is 1) is equal to 2i-1 and the maximum number of nodes till level i is equal to 2i 1 Example 8.5 In example 8.2 Number of nodes at level 2 is 22-1 = 2 Number of nodes at level 3 is 23-1 = 4 Maximum number of nodes till level 2 is 2 -1 = 3 Skewed binary tree A binary tree is a skewed binary tree, if it has only left child (skewed left) or only right (skewed right) child for all its internal nodes.
2
Page 48 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Skewed left
Skewed right
Tree Representation
A binary tree can be represented in two ways and are 1. Array representation 2. Linked list representation Array representation The binary tree can be represented as we have discussed in the heap sort. Since a binary-tree node never has more than two children, a node can be represented with 3 fields as one field for the data in the node in remaining two fields for two child pointers.
Left child
Data
Right Child
Programming representation of node is as follows. Struct BinaryTreenode { Struct BinaryTreenode * leftChild; Char data; Struct BinaryTreenode * rightChild; };
Many algorithms pertaining to tree structures usually involve a process in which each node of the tree is visited, or processed, exactly once. Such a process is called a traversal.
Page 49 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Inorder traversal
Postorder traversal o Traverse the left subtree in postorder o Traverse the right subtree in postorder o Visit the root
Page 50 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Inorder traversal
:DBEAIHJFCG
Preorder traversal : A B D E C F H I J G Postorder traversal : D E B I J H F G C A Algorithms for the tree traversals Inorder traversal void inorder(struct btreenode *sr) { if(sr!=NULL) { inorder (sr->left); printf(%d\n, sr->data); inorder (sr ->right); } }
Page 51 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Postorder traversal void postorder(struct btreenode *sr) { if(sr!=NULL) { postorder(sr -> left); postorder (sr ->right); printf(d\n, sr->data); } }
Binary Search Tree (BST) BST is a binary tree which has the following properties. All elements stored in the left subtree of a node whose value is K have values less than K. All elements stored in the right subtree of a node whose value is K have values greater than or equal to K. That is, a nodes left child must have a key less than its parent, and a nodes right child must have a key greater or equal to its parent The left and right sub trees of a node is also a binary search tree
Page 52 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
63
47
71
54
67
84
79
91
Operations that can be performed on a BST are: Creation Insertion Deletion Searching Creation The first element in the list is made as the root of the node. The elements following first are placed in its left sub tree if they are less than the root and are placed in its right sub tree if they are greater than the root. In other words we can state that creation is a combination of search and insertion after the of root node. Searching The search is always carried from the root node, if the node to be searched is less than the root value then the left sub tree is searched. If the search value is greater than the node value then the right sub tree is searched. The search is continued till the search node is found or till the search is ended without any branch to proceed. Insertion Steps involved in inserting a node are Search for the node that has to be inserted (though it is not available) in the tree. If the search ended at a node x insert the new node as its left child if the new node is less than X, otherwise insert as its right child.
Page 53 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
63
47
71
54
67
84
15
79
91
15 is greater than 6 hence it is joined as its right child. Deletion The node which has to deleted is first searched from the root to find its position. The deletion operation is easier if the node which has to deleted is a leaf node. The link from its parent is disconnected in order to delete that node. If the node is a non leaf node the deletion is carried as below. If the non leaf node has a single sub tree then the child node is replaced in its place. If the non leaf node has both left and right sub tree then either the in order successor or the predecessor is replaced in its place.(i.e. the greatest left descendent or the smallest right descendent) Example 8.10 : Deleting 71 from example 8.9 The dotted line represents the search and the dotted circle represents the node to be deleted.
Page 54 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
63
47
71
54
67
84
15
79
91
63
63
47
67
47
79
54
84
54
67
84
15
79
91
15
91
Advantage of a BST Searching a node in a BST is faster, since either left or right sub tree is only searched from the root till the node is found instead of comparing all the nodes preceding it. Disadvantage of a BST The tree may be a skewed binary tree if the elements are either in ascending(skewed left) or in descending(skewed right) order, which lead to more levels.
Page 55 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Summary
Tree is collection of nodes arranged in hierarchical fashion Binary tree is tree with 2 as its maximum degree Tree can be represented either using an array or linked list Tree can be traversed in 3 ways Binary search tree is a binary tree in which a node can have all its left descendants as less than that and right as greater than that.
Page 56 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Learning Objectives
After completing this chapter you will be able to Define a balanced tree Identify how a balanced tree can be constructed from a Binary tree Define hashing List the advantages and disadvantages of Hashing
Overview:
Balanced trees are classified into two categories Height Balanced tree Weight Balanced tree AVL Tree An AVL tree is a height balanced Binary Search Tree. The number of null branches is more in a normal BST if the elements are almost in order, this leads to more levels and in turn need more space. This problem is solved by balancing the height whenever a node is inserted into an AVL tree. The re-balancing is recommended based on the balancing factor. Balancing factor Balancing factor of each node is calculated by finding the difference in levels between the left and right sub tree. Balancing factor of X = height of left sub tree of X - height of right sub tree of X If the balancing factor of all the nodes in the tree is within the range of -1 and 1, then the tree is already in balanced form, otherwise balancing is needed. AVL Tree Rotations As mentioned previously, an AVL Tree and the nodes it contains must meet strict balance requirements to maintain its O(log n) search capabilities. These balance restrictions are maintained using various rotation functions. Below is a diagrammatic overview of the four possible rotations that can be performed on an unbalanced AVL Tree, illustrating the before and after states of an AVL Tree requiring the rotation.
Page 57 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 58 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 59 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
-2 45 0 2 50 1 45 0 30 -1 45 -1 50 -1 55 0 63 30 -2 50
0 30
0 55
Page 60 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
-2 45 0 -1 45 0 30 0 55 -1 50 0 63 0 30 30 1 55 1 45
0 50 0 55
0 53
0 63
0 50
0 63
0 53
Deletion in AVL tree The deletion algorithm for AVL Trees is a little more complex, as there are several extra steps involved in the deletion of a node. If the node is not a leaf node (that is, it has at least one child), then the node must be swapped with either it's in-order successor or predecessor (based on availability). Once the node has been swapped we can delete it (and have its parent pick up any children it may have - bear in mind that it will only ever have at most one child). If a deletion node was originally a leaf node, then it can simply be removed. Now, as with the insertion algorithm, we traverse back up the path to the root node, checking the balance of all nodes along the path. If we encounter an unbalanced node we perform an appropriate rotation to balance the node. NB. Unlike the insertion algorithm, more than one rotation may be required after a delete operation, so in some cases we will have to continue back up the tree after a rotation.
Weight Balanced Trees Tree structures support various basic dynamic set operations including Search, Predecessor, Successor, Minimum, Maximum, Insert, and Delete in time proportional to the height of the tree. Ideally, a tree will be balanced and the height will be log n where n is the number of nodes in the tree. To ensure that the height of the tree is as small as possible and therefore provide the best running time, a balanced tree structure like a red-black tree, AVL tree, or b-tree must be used. When working with large sets of data, it is often not possible or desirable to maintain the entire structure in primary storage (RAM). Instead, a relatively small portion of the data structure is
Page 61 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
The worst case height is O(log n). Since the "branchiness" of a b-tree can be large compared to many other balanced tree structures, the base of the logarithm tends to be large; therefore, the number of nodes visited during a search tends to be smaller than required by other tree structures. Although this does not affect the asymptotic worst case height, b-trees tend to have smaller heights than other trees with the same asymptotic height. Operations on B-Trees The algorithms for the search, create, and insert operations are shown below. Note that these algorithms are single pass; in other words, they do not traverse back up the tree. Since b-trees
Page 62 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
B-Tree-Search(x, k) i <- 1 while i <= n[x] and k > keyi[x] do i <- i + 1 if i <= n[x] and k = keyi[x] then return (x, i) if leaf[x] then return NIL else Disk-Read(ci[x]) return B-Tree-Search(ci[x], k) The search operation on a b-tree is analogous to a search on a binary tree. Instead of choosing between a left and a right child as in a binary tree, a b-tree search must make an n-way choice. The correct child is chosen by performing a linear search of the values in the node. After finding the value greater than or equal to the desired value, the child pointer to the immediate left of that value is followed. If all values are less than the desired value, the rightmost child pointer is followed. Of course, the search can be terminated as soon as the desired node is found. Since the running time of the search operation depends upon the height of the tree, B-Tree-Search is O(logt n). B-Tree-Create(T) x <- Allocate-Node() leaf[x] <- TRUE n[x] <- 0 Disk-Write(x) root[T] <- x The B-Tree-Create operation creates an empty b-tree by allocating a new root node that has no keys and is a leaf node. Only the root node is permitted to have these properties; all other nodes must meet the criteria outlined previously. The B-Tree-Create operation runs in time O(1). B-Tree-Split-Child(x, i, y) z <- Allocate-Node() leaf[z] <- leaf[y]
Page 63 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 64 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 65 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
10
10
17
17
10
25
17
10
17
10
17
9 10
25
13
25
13 16
25
10 10 17 8 8 9 13 25 17
13 16
25
Page 66 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
10
15 17
13
16
25
10
15 17
13
16
22
25
10
15
22
13
17
25
Page 67 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
The possible operations in a symbol table are Search if a particular name is in table Retrieve attribute of that name Modify the name and attributes Insert a new name and attributes Delete a name and attributes Hashing techniques are used to search, insert, and delete the items (name & attributes). Unlike identifier comparisons to perform a search, hashing technique uses a formula called hash function h(x). The hashing technique can be classified into two types Static Hashing Dynamic Hashing Static Hashing: In Static hashing the identifiers are stored in a fixed sized table called the hash table. The table size cannot be altered in this hashing. Dynamic Hashing: In dynamic hashing the identifiers are stored in a dynamic sized table called the hash table. The table size can be altered in this hashing. The arithmetic function h(x) gives the address of x in the table. The address is named as hash address or home address. Overflow: A new key k1 is mapped or hashed into a full table. If the mapping results in a table which is already full, then it cannot be inserted into that table, this type of situation is called overflow.
Hash Collision: When two different keys are resulting in same address after a hash function is termed as collision. Suppose that two keys k1 and k2 are such that h(k1) equals h(k2). Then when a record with key k1 is entered into the table, it is inserted at position h(k1). But when k2 is hashed, because its hash key is the same as that of k2, an attempt may be made to insert the record into the same position where the record with key k1 is stored. Cleary, two records cannot occupy the same position. Such a situation is called a hash collision or hash clash. Hash collision can be avoided through rehashing and double hashing.
Page 68 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Folding The key x is divided in to several parts and are added together to get the final result of hashing. Two types of folding methods are available and are: Shift folding Folding at the boundaries In shift folding the parts are simply added together. Example: 74568392 74 + 56 + 83 + 92 305 Folding at the boundaries (Reverse Folding) Parts in even position are reversed and then the values are added together. Example: 74 + 56 + 83 + 92 74 + 65 + 83 + 29 => 242
Page 69 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Summary
Balanced Tree is a tree in which the number levels are minimized by balancing the height or weight. AVL tree is a height balanced tree, balancing is done through four possible rotations. B-tree is a weight balanced tree, balancing is done to maintain number of elements and sub trees in each node. Hashing is the process of calculating the address of the item using a mathematical formula instead of searching.
2) Which of the following is not a hashing method a) Mid-Square b) Radix c) Folding d) None of the above Answers 1) a 2) d
Page 70 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Learning Objectives
After completing this chapter, you will be able to: Represent the graph using array and Linked list Traverse the graph Calculate minimum cost spanning tree Calculate the shortest route from source to all other nodes
Graphs
Introduction Graph is a collection of nodes or vertices connected together through edges or arcs. Graphs are used to model electrical circuits, chemical compounds, highway maps, and so on. They are also used in the analysis of electrical circuits, finding the shortest route, project planning, linguistics, genetics, social science, and so forth. Graph Definitions and Notations A graph G is a pair, G = (V, E), where V is a finite nonempty set, called the set of vertices of G. E is called the set of edges. Let V(G) denote the set of vertices, and E(G) denote the set of edges of a graph G. If the elements of E(G) are ordered pairs, G is called a directed graph or digraph; otherwise, G is called an undirected graph. In an undirected graph, the pairs (u, v) and (v, u) represent the same edge. Let G be a graph. A graph H is called a sub-graph of G if V(H) V(G) and E(H) E(G); that is, every vertex of H is a vertex of G, and every edge in H is an edge in G. A graph can be shown pictorially. The vertices are drawn as circles, and a label inside the circle represents the vertex. In an undirected graph, the edges are drawn using lines. In a directed graph, the edges are drawn using arrows.
Page 71 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
(a) (b) Let G be an undirected graph. Let u and v be two vertices in G. Then u and v are called adjacent if there is an edge from one to the other; that is, (u, v) E. Let e = (u, v) be an edge in G. We then say that edge e is incident on the vertices u and v. An edge incident on a single vertex is called a loop. If two edges, e1 and e2, are associated with the same pair of vertices, then e1 and e2 are called parallel edges. A graph is called a simple graph if it has no loops and no parallel edges. There is a path from u to v if there is a sequence of vertices u1, u2, ..., un such that u = u1, un = v, and (ui, ui + 1) is an edge for all i = 1, 2, ..., n 1.Vertices u and v are called connected if there is a path from u to v. A simple path is a path in which all the vertices, except possibly the first and last vertices, are distinct. A cycle in G is a simple path in which the first and last vertices are the same. G is called connected if there is a path from any vertex to any other vertex. A maximal subset of connected vertices is called a component of G. Let G be a directed graph, and let u and v be two vertices in G. If there is an edge from u to v, that is, (u, v) E, then we say that u is adjacent to v and v is adjacent from u. The definitions of the paths and cycles in G are similar to those for undirected graphs. G is called strongly connected if any two vertices in G are connected. Graph Representation A graph can be represented in several ways. Two common ways: adjacency matrices and adjacency lists. Adjacency Matrix Let G be a graph with n vertices, where n > 0. Let V(G) = {v1, v2, ..., vn}.The adjacency matrix AG is a two dimensional matrix n x n matrix such that the (i, j)th entry of AG is 1 if there is an edge from vi to vj; otherwise, the (i, j)th entry is zero. Example 11.2: Adjacency Matrix for graphs 11.1 (a) and (b) A A 0 B 1 C 1 D 1 E 0 B 1 0 0 0 C 1 0 0 0 D 1 0 0 0 1 E 0 1 1 1 0 A B A 0 1 B 0 0 C 0 0 D 1 0 E 0 C 1 1 0 0 D 0 0 0 0 0 E 0 0 1 1 0
1 1 (a)
1 0 (b)
Page 72 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Operations on Graphs The operations commonly performed on a graph are as follows: Create the graph. That is, store the graph in computer memory using a particular graph representation. Clear the graph. This operation makes the graph empty. Determine whether the graph is empty. Traverse the graph. Print the graph. How a graph is represented in computer memory depends on the specific application. For illustration purposes, we use the adjacency list (linked list) representation of graphs. Therefore, for each vertex, v, the vertices adjacent to v (in a directed graph, also called the immediate successors) is stored in the linked list associated with v.
Page 73 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 74 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Shortest Path Algorithm Shortest path can be calculated only for the weighted graphs. The edges connecting two vertices can be assigned a nonnegative real number, called the weight of the edge. A graph with such weighted edges is called a weighted graph. Let G be a weighted graph. Let u and v be two vertices in G, and let P be a path in G from u to v. The weight of the path P is the sum of the weights of all the edges on the path P, which is also called the weight of v from u via P. Let G be a weighted graph representing a highway structure. Suppose that the weight of an edge represents the travel time. For example, to plan monthly business trips, a salesperson wants to find the shortest path (that is, the path with the smallest weight) from her or his city to every other city in the graph. Many such problems exist in which we want to find the shortest path from a given vertex, called the source, to every other vertex in the graph. This section describes the shortest path algorithm, also called the greedy algorithm, developed by Dijkstra. Shortest Path Given a vertex, say vertex (that is, a source), this section describes the shortest path algorithm. The general algorithm is: 1. Initialize the array smallestWeight so that smallestWeight[u] = weights[vertex, u]. 2. Set smallestWeight[vertex] = 0. 3. Find the vertex, v, that is closest to vertex for which the shortest path has not been determined. 4. Mark v as the (next) vertex for which the smallest weight is found.
Page 75 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
B 1 5 A
2 C
SOURCE
Direct Cost Select A-B Edge B C D Cost 1 2 3 Path A-B A-C A-B-D
Page 76 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Page 77 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
4 1 A
B 1 2 C E
2 D
1 A C
Page 78 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
B 1 1 A C E
1 A C E
2 D
Page 79 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
1 2
1 A C
2 E
Summary
Graph is a collection of nodes Connected together using edges. Graph can be traversed using DFS or BFS Shortest path for a vertex with other vertices can be calculated using Dijkstras algorithm. Spanning tree is an acyclic graph. Minimum cost spanning trees can be derived using Kruskals algorithm.
Page 80 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Glossary
Abstract data type Acyclic graph Adjacency list Adjacency matrix Adjacent Arc AVL Tree A formal, language-independent description of data elements, the relationships among then, and the operations that act upon them. A graph without any cycles. A method using linked list to represent a the edges of graph or network. A method that uses a matrix to represent the edges of a graph or network. Two nodes of a graph are adjacent if they are connected by an edge. The edge of a graph that establishes a directional orientation between its end point. A tree in which, for each node, the difference between the height of its left sub tree and the height of its right sub tree is at most one. For a node in a binary tree, the difference between the height of its left sub tree and the height of its right sub tree. A technique in which the time and the space requirements of an algorithm are estimated in order of magnitude terms, The process examining a middle value of a sorted array to see which half contains the value in question and continuing to halve until the value is located. A Binary tree with the ordering property. A tree in which each node has at most two sub trees. A visiting of all nodes in a graph, it proceeds from each node by first visiting all nodes adjacent to that node. An efficient, flexible index structure often used in DBMS on random access files. Rearranges elements of an array until they are in either ascending or descending order. In bucket hashing contiguous region of storage locations. Nodes pointed to by an element in a tree. A linked list in which the lost node of the list points to the first node in the list. Occurs when a collision resolution strategy causes keys that have a collision at an initial hashing position to be relocated to the same region within the storage space. Condition in which more than one key hashes to the same position with a given hashing function. A path of a graph which originates and terminates at the same node.
Binary search tree Binary tree Breadth first search B-tree Bubble sort Bucket Children Circular linked list Clustering
Collision Cycle
Page 81 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Digraph/Directed graph A Graph in which each edge establishes a directional orientation between its end points. Dijkstras algorithm Directed path An algorithm for finding the shortest path between two nodes in a graph. A sequence of directed edges from one node of a graph to another. Each pair of successive edges in the path contains a end point. A collision processing method in which a second hashing is used to determine a sequence of storage locations to examine until an available spot is found. A linked list in which each node has two pointers instead of one. One pointer points to the node preceding that node and the points to the node following that node in that list. Establishes a link between two nodes. A method of constructing a hashing function in cases where the key is not an integer value. The non numeric characters are removed and the remaining digits are combined to produce an integer value. A binary tree in which all the leaf nodes are in the same level. A structure composed of two sets of objects: a set of nodes and a set of edges. A density dependent search technique in the key for a given data item is transformed using a hash function to produce the address. Sort in which the array is treated like the array implementation of binary tree and the items are repeatedly manipulated to create a heap from which the root is removed and added to the sorted portion of the array. A technique for ensuring that an ordered binary tree remains as full as possible in form. The number of directed edges that terminate at the node. A binary tree traversal in which the order of traversal is left sub tree, node and right sub tree. An algorithm for finding the minimum spanning tree of a graph. In a tree a node that has no children. Number of edges in a path.
Double hashing
Edge Folding
Height balancing In-degree of a node In-order traversal Kruskals algorithm Leaf Length
Page 82 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
Minimum spanning tree A collection of edges connecting all of the nodes of a graph without any cycle. Node Out degree of node Overflow Parent Partition A structure storing a data item in a linked list, tree, graph. A number of directed edges that originate at the node. In linked collision processing the area in which keys that cause collisions are placed. In a tree the node that is pointing to its children. In quick sort the process of moving the pivot to the location where it belongs in the sorted array and arranging the remaining data items to the left of pivot if they are less than or equal to the pivot and to the right if they are greater than or equal to the pivot. A sequence of edges that connects two nodes in a graph. Item used to direct the partitioning of quick sort. A memory location containing the location of another data item. A binary tree traversal in which the nodes are traversed in the order of left sub tree, right sub tree and the node. A binary tree traversal in which the nodes are traversed in the order of node, left sub tree and the right sub tree. A queue in which the deletion is done on priority. A data structure in which the elements are added at one end and removed form the other end. Referred to as a FIFO. Relatively fast sorting technique that uses recursion and a partition algorithms. Method of handling a collision in which a sequence of new hashing function is applied to the key that caused the collision until an available location for that the key is found. Children of a same node. A data structure in which the elements are accessed from one end. Referred to as a LIFO. A collection of nodes arranged in a hierarchical fashion. A numeric value associated with an edge in a graph. Maintaining the number elements that can be handled in a single node of trees.
Path Pivot Pointer Post order traversal Preorder traversal Priority queue Queue Quick sort Rehashing
Page 83 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
References
Websites
http://www.macs.hw.ac.uk/~alison/ds98/ds98.html http://www.cs.auckland.ac.nz/software/AlgAnim/lists.html http://students.washington.edu/mukundn/courses/cse490b/ http://en.wikipedia.org/wiki/Data_structure http://www.cs.indiana.edu/classes/a202-sbog/notes/BigOh.html http://www.personal.psu.edu/faculty/j/h/jhm/f90/lectures/18.html http://cslibrary.stanford.edu/103/ http://ocw.mit.edu/NR/rdonlyres/Civil-and-Environmental-Engineering/1-00Spring2005/9EBF826C-7CC3-40C8-8FA6-FF579460CC3E/0/sptutorial10.pdf http://www.csc.liv.ac.uk/~frans/COMP101/AdditionalStuff/moreRecords.html http://www.cse.cuhk.edu.hk/~csc2100a/lecture/sort1.pdf http://www.cs.sunysb.edu/~skiena/214/lectures/lect16/lect16.html http://www.iimb.ernet.in www.ncsi.iisc.ernet.in www.highered.mcgraw-hill.com www.tech.purdue.edu www.indianmba.com www.iimb.ernet.in http://wps.prenhall.com
Books
Fundamentals of Data Structures, Ellis Horowitz, Sartaj Sahni, Glagotia Book Source, Computer Science Press Inc 1983 An Introduction to Data Structures with applications Jean-Paul Tremblay, Paul G Sorenson, II Edition, Tata McGraw-Hill Edition Introduction to Data Structures, Bhagat Singh, Thomas L Naps, Glagotia Book Source Data Structures using C and C++, Yedidyah Langson, Moshe J Augenstein, Aaron M Tenenbaum, Pearson Education Asia Introduction to Data Structures and Algorithms Analysis, Thomas L Naps, Second Edition, WEST publishing company, US
Page 84 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected
STUDENT NOTES:
Page 85 Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected