Laboratory Manual Data Structures and Files Second Year - Information Technology Teaching Scheme Examination Scheme Theory : —— Term Work: 25 Marks Practical : 04 Hrs/Week Practical : 50 Marks Oral : —— Prepared By Prof. S. S. Kare Department of Information Technology Vidya Pratishthan’s College of Engineering Baramati – 413133, Dist- Pune (M.S.) INDIA December 2012 Table of Contents 1 Polynomial Using Circular Linked-List 1.1 Problem Statement . . . . . . . . . . . . 1.2 Pre Lab . . . . . . . . . . . . . . . . . . 1.3 Hardware and Software Requirement . . 1.4 Theory . . . . . . . . . . . . . . . . . . . 1.5 Procedure . . . . . . . . . . . . . . . . . 1.6 Post Lab . . . . . . . . . . . . . . . . . . 1.7 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 2 2 4 5 2 Stack as an ADT 2.1 Problem Statement 2.2 Pre Lab . . . . . . 2.3 Theory . . . . . . . 2.4 Procedure . . . . . 2.5 Post Lab . . . . . . 2.6 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 6 6 6 7 9 9 3 Priority Queue 3.1 Problem Statement 3.2 Pre Lab . . . . . . 3.3 Theory . . . . . . . 3.4 Procedure . . . . . 3.5 Post Lab . . . . . . 3.6 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 10 10 10 11 12 12 4 Expression Tree and Traversals 4.1 Problem Statement . . . . . . . 4.2 Pre Lab . . . . . . . . . . . . . 4.3 Theory . . . . . . . . . . . . . . 4.4 Procedure . . . . . . . . . . . . 4.5 Post Lab . . . . . . . . . . . . . 4.6 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 13 13 13 16 21 21 5 Binary search Tree 5.1 Problem Statement 5.2 Pre Lab . . . . . . 5.3 Theory . . . . . . . 5.4 Procedure . . . . . 5.5 Post Lab . . . . . . 5.6 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 22 22 22 23 27 27 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i TABLE OF CONTENTS TABLE OF CONTENTS 6 GRAPH WITH ADJACENCY LIST AND 6.1 Problem Statement . . . . . . . . . . . . . . 6.2 Pre Lab . . . . . . . . . . . . . . . . . . . . 6.3 Theory . . . . . . . . . . . . . . . . . . . . . 6.4 Procedure . . . . . . . . . . . . . . . . . . . 6.5 Post Lab . . . . . . . . . . . . . . . . . . . . 6.6 Viva Questions . . . . . . . . . . . . . . . . BFS & . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 28 28 28 33 36 36 7 Shortest path using adjacency matrix 7.1 Problem Statement . . . . . . . . . . . 7.2 Pre Lab . . . . . . . . . . . . . . . . . 7.3 Theory . . . . . . . . . . . . . . . . . . 7.4 Procedure . . . . . . . . . . . . . . . . 7.5 Post Lab . . . . . . . . . . . . . . . . . 7.6 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 37 37 37 40 42 42 8 IMPLEMENT HUFFMAN’S 8.1 Problem Statement . . . . . 8.2 Pre Lab . . . . . . . . . . . 8.3 Theory . . . . . . . . . . . . 8.4 Viva Questions . . . . . . . ALGORITHM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 43 43 43 50 9 Hash Table 9.1 Problem Statement 9.2 Pre Lab . . . . . . 9.3 Theory . . . . . . . 9.4 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 51 51 51 57 10 SEQUENTIAL FILE ORGANIZATION. 10.1 Problem Statement . . . . . . . . . . . . . 10.2 Pre Lab . . . . . . . . . . . . . . . . . . . 10.3 Theory . . . . . . . . . . . . . . . . . . . . 10.4 Viva Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 58 58 58 67 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References 68 ii List of Tables iii List of Figures 1.1 1.2 Circular list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Poly in Circular list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 2.1 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4.1 4.2 4.3 4.4 Binary tree traversals Node . . . . . . . . . . Expression Tree . . . . Representation Tree in . . . . 14 15 16 16 5.1 Binary Search tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 6.1 6.2 6.3 6.4 Graph-G Adjacency Adjacency Adjacency . . . . 29 30 31 31 7.1 7.2 Graph-G3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Graph-G4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 39 8.1 8.2 8.3 8.4 8.5 T6 . . . . . . HT . . . . . HT1 . . . . . Huffman Tree Prefix Code . . . . . . 44 44 45 46 47 9.1 9.2 T1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . T2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 54 . . . . . . . Matrix-G . List Of G1 List Of G1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Assignment 1 Polynomial Using Circular Linked-List 1.1 Problem Statement • Write a menu driven program to perform display, addition, multiplication and evaluation on the polynomial represented in CLL • Sort while creating polynomial in the decreasing order of exponent. 1.2 Pre Lab • Concept of Linked-List • Concepts Of structures 1.3 Hardware and Software Requirement • Hardware requirement : – Intel pentium III/M 1.4 GHz, or Pentium IV 1.4 GHz minimum – Microsoft Windows XP/2000 – 512 MB RAM (1 GB+ recommended) – 900 MB free hard disk space • Software Requirement 1 1.4. THEORY Polynomial Using Circular Linked-List – Turbo c 1.4 Theory Circular list (CLL): A linked list is called Circular linked list if the last node is linked to the first node of the list. Figure 1.1: Circular list Structure used for polynomial representation using CLL is, typedef struct node { int coeff; int power ; struct node *next; }node; Representing a polynomial using CLL: A polynomial can be represented in an array or in a linked list by simply storing the coefficient and V V exponent of each term. For example: 3x 14+2x 7+1 can be represented using CLL as below. Figure 1.2: Poly in Circular list 1.5 Procedure I. CREAT(): Lab Manual - Data Structures and Files 2 VPCOE, Baramati 1.5. PROCEDURE Polynomial Using Circular Linked-List 1. Enter the no. of terms in the poly i.e. n 2. Scan the terms as a tuple of (power, coeff) 3. Insert the n terms using INSERT() function head = INSERT(head,power1,coeff1) 4. Return head II.INSERT(node *head, int power1,float coeff1) 1. P stores adds of node created. P= (node*) malloc(sizeof(node)) p→ power = power1 p → coeff = coeff1 p → next = NULL 2. Place the node at proper position (a) If Linked list is empty head = P head → next = head return head (b) if (power1 < head→ power) ..... Lowest power inserted at the end P→ next = head→ next head → next = P head = p return head (c) If (power1==head→ power) ..... add coefficients head→ coeff = head→ coeff + coeff1 return head (d) q = head while(q→ next!=head && power1 < = q→ next→ power) q=q→ next ....locate the position for insertion if (P→ power == q→ power) q→ coeff = q→ coeff + coeff1 else p→ next = q→ next q→ next = P return head Lab Manual - Data Structures and Files 3 VPCOE, Baramati 1.6. POST LAB Polynomial Using Circular Linked-List 3. stop. III. Addition ( node*P1,node*P2) 1. P = P1→ next head = insert(head,p→ power,P→ coeff) //Insert the first polynomial 2. P = P2→ next head = insert(head,P→ power,P→ coeff) //insert the second polynomial 3. Return head. IV.Mult(node*p1,node*p2) 1. head1→ poly1 head2→ poly2 head2 = p2→ next 2. for every term of second polynomial do till end of second list head1 = p1→ next multiply with every term of first polynomial do till end of second list power2 = head1→ power+head2→ power coeff2 = head1→ coeff*head2→ coeff head=insert(head,power2,coeff2) head2 = head2→next head1 = head1→ next 3. Return head. V. Eval (node*head, foat x) 1. P = head→ next 2. do till the end of list, val = val + p→ coeff * pow(x,p→ power) 3. return val 1.6 Post Lab Addition, Multiplication and Evaluation of polynomial using circular linked list. Lab Manual - Data Structures and Files 4 VPCOE, Baramati 1.7. VIVA QUESTIONS 1.7 Polynomial Using Circular Linked-List Viva Questions 1. What is Circular Linked-List? 2. What is a Difference between CLL, DLL and SLL? 3. What are the advantages of CLL over SLL? Lab Manual - Data Structures and Files 5 VPCOE, Baramati Assignment 2 Stack as an ADT 2.1 Problem Statement Write a program for conversion of: • infix to its postfix form • infix to its prefix form • Evaluation of postfix expression operators supported ±,?,÷,%,,̂(,) operands supported – all single character operands 2.2 Pre Lab • Concept of Structure • Linked List • Array 2.3 Theory Definition and Representation of stack.: Stack is an LIFO data structure in which we can insert elements from one end and also remove element from the same end.This end is called as a top end. 6 2.4. PROCEDURE Stack as an ADT Representation of the stack is as follows: (If we consider stack of Integer elements by using array Figure 2.1: Stack In stack elements are kept together on on the top of other and top indicate position of the topmost element on the stack .Now if we want to remove element 16 is removed first . What is ADT for stack: Representing any data structure as an ADT means considering variables and operations performing on those variables in a single logical block. Consider stack variable and different operations on it like Push,Pop,Display etc. If we consider it in a single block them it is an ADT for the stack. 2.4 Procedure I:Conersion of infix Expression to postfix form 1. S:Stack 2. while(more tokens) x←nexttoken 3. if (x is an operand) print x 4. elseif(x==( ) push(x) Lab Manual - Data Structures and Files 7 VPCOE, Baramati 2.4. PROCEDURE Stack as an ADT 5. elseif(x is )) while(x is not ( ) print(pop(s)) 6. else while(precedence(x)≤ precedence(top(s)) print(pop(s)) push(s,x) 7. while(!empty(s)) print(pop(s)) II: Conversion of infix expression to prefix form 1. Reverse the infix expression 2. Make every ’(’as ’)’ and vice versa 3. convert the expression to postfix form 4. reverse the postfix expression III:Evaluation of postfix expression 1. x:tokentype operand1,operand2:operand-type operator : operator-type s:Stack 2. initialise(s) x=nexttoken 3. while(x) if(x is operator) operand2=pop(s) operand1=pop(s) push(s,eval(x,operand1,operand2)) 4. else push(s,x) x=nexttoken() Lab Manual - Data Structures and Files 8 VPCOE, Baramati 2.5. POST LAB 2.5 Stack as an ADT Post Lab • stack as an ADT • stack for expression conversion and expression evaluation. 2.6 Viva Questions 1. Why infix expression is converted into prefix or postfix form ? 2. Explain Infix to Prefix conversion? 3. Explain infix to postfix conversion? 4. Evaluate given postfix expression? Lab Manual - Data Structures and Files 9 VPCOE, Baramati Assignment 3 Priority Queue 3.1 Problem Statement Write a program to Implement the priority queue to cater services to different kinds of patients as: • Serious (top priority), • non-serious (medium priority), • General Checkup (Leastpriority). 3.2 Pre Lab • Concept of Linked-List • Concepts of Structures 3.3 Theory Concept of queue: • It is FIFO(First In First Out ) structure. • Element which is inserted first will be taken out first. • Queue is having 2 ends front and rear. • Front end is used for deletion and rear is for insertion. Priority Queue: 10 3.4. PROCEDURE Priority Queue • Queue in which elements are stored according to priorities (highest to lowest). • Front element is always highest in priority and will get service first. Priority Queue using linked list: Here we are implementing queue by using linked list for hospital Scenario where single node will store record of 1 patient. So , struct for a single node is Struct patient { char name[20]; int priority; strcuct patient *next; } Using priority queue in given scenario: Consider there are 3 types of patients 1-High priority patient 2-Medium priority patient. 3- General patient. 3.4 Procedure I. void insert(node **h,char name[], int priority) 1. node *p,*q; p=(node*)malloc(sizeof(node)); strcpy(p→name,name); p→priority=priority; p→next=NULL; 2. if(*h==NULL)//if the queue is empty *h=p; 3. else if(priority > (*h)→ priority)//highest priority element p→ next=*h; *h=p; 4. else //locate the point of insertion q=*h; while(q→ next =NULL && priority¡=q→ next→ priority) Lab Manual - Data Structures and Files 11 VPCOE, Baramati 3.5. POST LAB Priority Queue q=q→ next; //insert p→ next=q→ next; q→ next=p; 5. stop. II. node *Delete(node **h) 1. node *p; p=*h; 2. *h=p→ next; 3. return(p); III. void display(node *h) 1. while(h!=NULL) 2. print h→ name,h→priority 3. h=h→next; 3.5 Post Lab Priority queue without violating basic FIFO principle of queue. 3.6 Viva Questions 1. What is Priority queue? 2. What is Circular Linked-List? 3. What is a Difference between CLL, DLL and SLL? 4. What are the advantages of CLL over SLL? Lab Manual - Data Structures and Files 12 VPCOE, Baramati Assignment 4 Expression Tree and Traversals 4.1 Problem Statement Write a program for 1. Accept postfix expression & construct a expression tree 2. perform preorder,inorder and post order recursive traversals and 3. preorder, inorder and postorder non recursive traversals. 4.2 Pre Lab • Concepts of Linked-List • Concepts of Structures • Concept of tree 4.3 Theory Tree Traversal :- This is a systematic way of ordering the nodes of a tree. There are basically six traversals LDR,LRD,RLD,RDL,DLR,DRL according to binary tree structure.But considering left child should meet first then right child,There are three popular schemes (many other schemes are possible):LDR,LRD,DLR. These are as fallows: 1. Preorder(DLR) 2. Inorder(LDR) 3. Postorder(LRD) 13 4.3. THEORY Expression Tree and Traversals All these are recursive ways of listing or visiting all the nodes (each node exactly once) Binary Tree Traversal: 1. Preorder Visit root, visit left subtree in preorder, visit right subtree in preorder 2. Postorder Visit left subtree in postorder, right subtree in postorder, then the root 3. Inorder : Visit left subtree in inorder, then the root, then the right subtree in inorder Figure 4.5 shows several binary trees and the traversal sequences in preorder, inorder, and postorder. We can construct a binary tree uniquely from preorder and inorder or postorder and inorder but not preorder and postorder. Figure 4.1: Binary tree traversals Arithmatic Expression:There are three forms in which arithmetic expressions can be written. Infix :(operand1 ) operator (operand2) Prefix :- operator (operand1 ) (operand2) Lab Manual - Data Structures and Files 14 VPCOE, Baramati 4.3. THEORY Expression Tree and Traversals Postfix:- :(operand1 ) (operand2) operator For example: Infix:-A/B^C+D*E-A*C Prefix:- -+/A ^BC*DE*AC Postfix:-ABC^/DE*+AC*We can construct the expression tree by assuming root is always operator and leaf nodes are operands.so we can construct the binary tree for the given infix,prefix and postfix expression by using external stack.Once we construct the binary tree ,we can obtain prefix ,infix and postfix forms of arithmetic expression by applying preorder, inorder and postorder algorithms respectively. Data Structures for Binary Trees 1. Arrays, especially suited for complete and full binary trees 2. A linked list for Pointer-based implementation. Array representation can be used to represent complete binary tree otherwise memory utilization is very inefficient for the trees which are not having both the childs whereas in linked representation memory utilization can be done very efficiently by creating the following node structure for binary tree: Figure 4.2: Node Lchild:-it contains address of a root node of a left binary tree, Rchild:- it contains address of a root node of a right binary tree. Data :-it contains the data item or information at node. For example:-consider following binary tree for postfix arithmetic expression AB+*CDStructure of treenode :- typedef struct treenode { char data; struct treenode *left; struct treenode *right; }treenode; Structure of Stack:Lab Manual - Data Structures and Files 15 VPCOE, Baramati 4.4. PROCEDURE Expression Tree and Traversals Figure 4.3: Expression Tree Figure 4.4: Representation Tree in Linked List typedef struct stack { treenode * data[30]; int top; }stack; 4.4 Procedure I. treenode*Create(): 1. char a[50]; int i; treenode *p,*q,*root; stack s; Lab Manual - Data Structures and Files 16 VPCOE, Baramati 4.4. PROCEDURE Expression Tree and Traversals init(&s); 2. Enter a postfix expression gets(a); 3. scan each term a[i] of the postfix expression 4. if term a[i] is operand p=(treenode*)malloc(sizeof(treenode)); p→left=NULL; p→right=NULL; p→data=a[i]; push(&s,p); 5. else tern a[i] is operator q=pop(&s); p=pop(&s); root=(treenode*)malloc(sizeof(treenode)); root→left=p; root→right=q; root→data=a[i]; push(&s,root); 6. root=pop(&s); return(root); 7. stop. II. Preorder( treenode * T) 1. if(T!=NULL) 2. print (T→data); 3. Call recursively preorder(T→left); 4. Call recursively preorder(T→right); III.Inorder(treenode* T) 1. if T is not NULL Lab Manual - Data Structures and Files 17 VPCOE, Baramati 4.4. PROCEDURE Expression Tree and Traversals 2. call inorder(T→left) 3. print T→data 4. Call inorder (T→right) IV.Postorder(treenode *T) 1. if T is not NULL 2. Call postorder(T→left) 3. call postorder(T→right); 4. print T→data V. Non-rec-preorder(treenode *T) 1. stack s; init(&s); 2. if(T==NULL) return; 3. while(T!=NULL) print(T→data); push(&s,T); T=T→left; 4. while(!empty(&s)) T=pop(&s); T=T→right; 5. while(T!=NULL) print(T→data); push(&s,T); T=T→left; 6. stop. VI. Non-rec-inorder(treenode *T) 1. stack s; init(&s); Lab Manual - Data Structures and Files 18 VPCOE, Baramati 4.4. PROCEDURE Expression Tree and Traversals 2. if(T==NULL) return; 3. while(T!=NULL) push(&s,T); T=T→left; 4. while(!empty(&s)) T=pop(&s); print(T→data); T=T→right; 5. while(T!=NULL) push(&s,T); T=T→left; 6. stop. VII.Non-rec-postorder(treenode *T) 1. stack s,s1; treenode *flag; init(&s); init(&s1); 2. if(T==NULL) return; 3. while(T!=NULL) push(&s,T); push(&s1,NULL); T=T→left; 4. while(!empty(&s)) T=pop(&s); flag=pop(&s1); if(flag!=NULL) print(T→data); else push(&s,T); push(&s1,(treenode*)1); T=T→right; Lab Manual - Data Structures and Files 19 VPCOE, Baramati 4.4. PROCEDURE Expression Tree and Traversals 5. while(T!=NULL) push(&s,T); push(&s1,NULL); T=T→left; 6. stop. Lab Manual - Data Structures and Files 20 VPCOE, Baramati 4.5. POST LAB 4.5 Expression Tree and Traversals Post Lab • structure of binary tree. • How and Why they are used for search? • how they can be traversed? 4.6 Viva Questions 1. What is skew Tree? 2. What are right and left spin binary trees? 3. What is the concept of Traversal in trees? How it is helpful? 4. What is height and breadth of binary tree? Lab Manual - Data Structures and Files 21 VPCOE, Baramati Assignment 5 Binary search Tree 5.1 Problem Statement 5.2 Pre Lab • Concepts Binary tree • Concepts tree traversals • Concepts of stack & queue data structures 5.3 Theory BINARY SEARCH TREE DEF: A binary tree in which the nodes are labeled with elements of an ordered dynamic set and the following BST property is satisfied: all elements stored in the left subtree of any node x are less than the element stored at x and all elements stored in the right subtree of x are greater than the element at x. An Example: Figure A) shows a binary search tree. Notice that this tree is obtained by inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an empty tree. Analysis of Algorithms : 1) Time Complexity :Time complexity for construction of BST from input integers using create function is O(nlogn),since for every integer to be added in BST,there will be logn comparisons and number of nodes to be added are n. Average case complexity of Search, Insert, and Delete Operations is O(log n), where n is the number of nodes in the tree. If we repeatedly insert a sorted sequence of values to form a BST, we obtain a 22 5.4. PROCEDURE Binary search Tree Figure 5.1: Binary Search tree completely skewed BST. The height of such a tree is n - 1 if the tree has n nodes. Thus, the worst case complexity of searching or inserting an element into a BST having n nodes is O(n). 2) Space Complexity:No additional space required for the insertion,deletion and search of a node in BST.But for traversing BST in inorder additional space equal to height of BST necessary. APPLICATIONS: 1. A prominent data structure used in many systems programming applications for representing and managing dynamic sets. 2. For easy Traversals. It can be used in games such as tic tac toe, chess. 3. To Find spanning trees. 4. In searching particular record in database. 5.4 Procedure ALGORITHMS: I.BSTnode *insert(BSTnode *T,char *x) 1. length=strlen(x); // acquire memory for the new node Lab Manual - Data Structures and Files 23 VPCOE, Baramati 5.4. PROCEDURE Binary search Tree r=(BSTnode*)malloc(sizeof(BSTnode)); r→data=(char*)malloc(length+1); strcpy(r→data,x); r→left=NULL; r→right=NULL; 2. if(T==NULL) return(r); // find the leaf node for insertion 3. p=T; while(p!=NULL) q=p; if(strcmp(x,p→data)¿0) p=p→right; else p=p→left; 4. if(strcmp(x,q→data)¿0) q→right=r; // x as right child of q else q→left=r; //x as left child of q return(T); 5. stop. II. inorder(BSTnode *T) 1. if(T!=NULL) inorder(T→left); print(T→data); inorder(T→right); 2. stop. III. BSTnode *mirror(BSTnode *T) 1. if(T!=NULL) p=(BSTnode*)malloc(sizeof(BSTnode)); length=strlen(T→data); p→data=(char*)malloc(length+1); strcpy(p→data,T→data); Lab Manual - Data Structures and Files 24 VPCOE, Baramati 5.4. PROCEDURE Binary search Tree p→left=mirror(T→right); p→right=mirror(T→left); return(p); 2. else return(NULL); 3. stop. IV.BSTnode *find(BSTnode *root,char *x) 1. while(root!=NULL) if(strcmp(x,root→data)==0) return(root); 2. if(strcmp(x,root→data)¿0) root=root→right; 3. else root=root→left; 4. return(NULL); 5. stop. V.leafnodes(BSTnode *T) 1. if(T!=NULL) if(T→eft==NULL && T→right==NULL) print(T→data); 2. else leafnodes(T→left); leafnodes(T→right); 3. stop. VI.BSTnode *delet(BSTnode *T,char *x) 1. if(T==NULL) print(Element not found :; return(T); Lab Manual - Data Structures and Files 25 VPCOE, Baramati 5.4. PROCEDURE Binary search Tree 2. if(strcmp(x,T→data)¡0) // delete in left subtree T→left=delet(T→left,x); return(T); 3. if(strcmp(x,T→data)¿0) // delete in right subtree T→right=delet(T→right,x); return(T); 4. // element is found if(T→left==NULL && T→right==NULL) // a leaf node temp=T; free(temp); return(NULL); 5. if(T→left==NULL) temp=T; T=T→right; free(temp); return(T); 6. if(T→right==NULL) temp=T; T=T→left; free(temp); return(T); 7. // node with two children //go to the inorder successor of the node temp=T→right; 8. while(temp→left !=NULL) temp=temp→left; T→data=temp→data; T→right=delet(T→right,temp→data); return(T); 9. stop. Lab Manual - Data Structures and Files 26 VPCOE, Baramati 5.5. POST LAB Binary search Tree VII. int height(BSTnode *T) 1. if(T==NULL) return(0); 2. if(T→left==NULL && T→right==NULL) return(0); return(max(height(T→left)+1,height(T→right)+1)); 3. stop. VIII. level-wise(BSTnode *T) 1. init(&q); enqueue(&q,T); 2. while(!empty(&q)) /*insert children of nodes at current level into q1*/ init(&q1) goto newline 3. while(!empty(&q)) T=dequeue(&q) print(T→data) 4. if(T→left !=NULL) enqueue(&q1,T→left) 5. if(T→right !=NULL) enqueue(&q1,T→right); q=q1; /*next level is the current level */ 6. stop. 5.5 Post Lab • Display the resultant binary search tree after performing search, creation, insertion and deletion of node in binary search tree. 5.6 Viva Questions 1. What is binary search tree and its application. 2. What are disadvantages of using binary search tree in large database? Lab Manual - Data Structures and Files 27 VPCOE, Baramati Assignment 6 GRAPH WITH ADJACENCY LIST AND BFS & DFS 6.1 Problem Statement Write a program to represent a given graph using adjacency list and perform DFS and BFS. 6.2 Pre Lab • Linked-List • Adjacency Matrix • Adjacency List 6.3 Theory GRAPH: A graph G consist of two sets V & E where, V is finite non-empty set of vertices & E is set of pair of vertices called edges. V(G) represent set of edges in graph G. We can write G=(V,E) to represent a graph. For example: Graph G1, (1,2) , (2,1) In above graph G=(V,E) V=1,2,3,4,5,6,7,8 E=(1,2),(1,3),(2,4),(2,5),(3,6),(4,5),(5,8),(6,8),(7,8) GRAPH PRESENTATION : Commonly Used representation to represent a graph on computer are • Adjacency Matrix 28 6.3. THEORY GRAPH WITH ADJACENCY LIST AND BFS & DFS Figure 6.1: Graph-G • Adjacency List ADJACENCY MATRIX REPRESENTATION OF A GRAPH Let G=(V,E) be a graph with n vertices n≤1. The adjacency matrix of graph G is 2-dimensional n*n array say A[m:n],with property that A(i,j)=1 iff edge (vi,vj) is in E(G) for undirected graph. A (i, j)=0 if there is no edge in graph G. The adjacency matrix for undirected graph G is : Lab Manual - Data Structures and Files 29 VPCOE, Baramati 6.3. THEORY GRAPH WITH ADJACENCY LIST AND BFS & DFS Figure 6.2: Adjacency Matrix-G ADJACENCY LIST REPRESENTATION OF A GRAPH: In this representation the n row of adjacency matrix re represented as n linked lists. There is one list for each vertex in a graph. The nodes In list i represent the vertices that are adjacent to vertex i. Adjacency List for undirected graph G1: Lab Manual - Data Structures and Files 30 VPCOE, Baramati 6.3. THEORY GRAPH WITH ADJACENCY LIST AND BFS & DFS Figure 6.3: Adjacency List Of G1 Figure 6.4: Adjacency List Of G1 TRAVERSAL OF A GRAPH: Let G=(V,E) be an undirected graph and a vertex V in V(G), we are interested in visiting all vertices connected to V. There are two ways to do this: • Depth First Search • Breadth First Search 1.DEPTH FIRST SEARCH: In depth first search, we follow a path inthe graph as deeply as we can go. When there are no adjacent, on visited nodes, then we proceed backwards(backtrack) , wherein we can repeat the process. Hence we need to examine the depths of the graph first. We can maintain a stack to keep track of the visited Lab Manual - Data Structures and Files 31 VPCOE, Baramati 6.3. THEORY GRAPH WITH ADJACENCY LIST AND BFS & DFS nodes, so that it can help in backtracking. For eg. For the given graph G1, DFS traversal is: 1 2 4 8 5 6 7 3 1. BREADTH FIRST SEARCH: In this method, instead of going deep into the graph, we examine the nodes across the breadth of the graph, before going to the next level. In DFS, we visit a node and then push into the stack. The next node we visit is an adjacent node, unvisited one. If no such node exist, we backtrack to the most recently visited node which has unprocessed neighbor. Hence the stack structure, LIFO was useful. For BFS, we use queue. In this method, we pick a node to visit first and place its unprocessed adjacent nodes in queue. Repeatedly take a node from the front of the queue. If the node is unvisited, then we visit the node and then place all its neighbor in the queue. For eg. For the given graph G1, BFS traversal is: 1 2 3 4 5 6 7 8 9 0 TEST CASE : 1. Display the total number of comprisond required to construct the graph in computer memory. 2. Display the result properly. 3. For ech traversal (DFS & BFS) display the following: 1. No. of times function called. 2. Total no. of comparisons 3. Maximum depth of stack used in recursion. Finally conclude on space and time complexity for the construction of graph and DFS and BFS traversal of the graph. Analysis of Algorithms : 1) Time Complexity : For the construction of an undirected graph with n vertices using adjacency matrix is O(n2 ). For the construction of n undirected graph with n vertices & e edges using adjacency list is O(n+e). For DFS and BFS traversals of an undirected graph with n vertices using adjacency matrix is O(n2 ). For adjacency list O(e). 2) Space Complexity : Additional space required for DFS and BFS to store the intermediate element in stack and queue respectively while traversing the graph. Lab Manual - Data Structures and Files 32 VPCOE, Baramati 6.4. PROCEDURE GRAPH WITH ADJACENCY LIST AND BFS & DFS APPLICATIONS: 1. Representation of electrical circuits. 2. Representation of telephone and computer networks. 3. Finding spanning tree of connected graphs. 6.4 Procedure Data structures used are: typedef struct Q { int data[MAX]; int R,F; }Q; typedef struct node { struct node *next; int vertex; }node; int visited[MAX]; node *G[20]; int n; Algorithms: I. BFS(int v) 1. Q q; node p; q.R=q.F=-1; //initialise for(i=0;i¡n;i++) visited[i]=0; 2. enqueue(&q,v) print(v) visited[v]=1 Lab Manual - Data Structures and Files 33 VPCOE, Baramati 6.4. PROCEDURE GRAPH WITH ADJACENCY LIST AND BFS & DFS 3. while(!empty(&q)) v=dequeue(&q); //insert all unvisited,adjacent vertices of v into queue for(p=G[v];p!=NULL;p=p→next) w=p→vertex; 4. if(visited[w]==0) enqueue(&q,w); visited[w]=1 print(w); 5. stop. II. DFS(int i) 1. node *p; print(i); p=G[i]; visited[i]=1; 2. while(p!=NULL) i=p→vertex; 3. if(!visited[i]) DFS(i); p=p→next; 4. stop. III. insert(int vi,int vj) 1. node *p,*q; //acquire memory for the new node q=(node *)malloc(sizeof(node)); q→vertex=vj; q→next=NULL; //insert the node in the linked list for the vertex no. vi 2. if(G[vi]==NULL) G[vi]=q; 3. else // go to the end of linked list p=G[vi]; Lab Manual - Data Structures and Files 34 VPCOE, Baramati 6.4. PROCEDURE GRAPH WITH ADJACENCY LIST AND BFS & DFS while(p→next!=NULL) p=p→next; p→next=q; 4. stop. Lab Manual - Data Structures and Files 35 VPCOE, Baramati 6.5. POST LAB 6.5 GRAPH WITH ADJACENCY LIST AND BFS & DFS Post Lab • Graph using adjacency list • Graph using adjacency matrix • Graph Traversala 6.6 Viva Questions 1. What is a spanning tree and minimum spanning tree? 2. What is done in the algorithm of DFS? 3. What is done in the algorithm of BFS? Lab Manual - Data Structures and Files 36 VPCOE, Baramati Assignment 7 Shortest path using adjacency matrix 7.1 Problem Statement Write a program to represent the given graph using adjacency matrix and find the shortest path using Dijkstra’s Algorithm. 7.2 Pre Lab 1. Linked-List. 2. Dijksta’s Algorithm 7.3 Theory PROBLEM DEFINITION: GRAPH REPRESENTATION using ADJACENCY MATRIX : We can use a one dimensional array to store the values of nodes. For example consider the graph below: Here the one dimensional array will have the data stored as: Node list: [a b c d]. It means that node 1 has value a, node 2 has value b, node 3 has value c, and node 4 has value d. But to show the connectivity between the nodes we need a two dimensional matrix. This matrix will have the entry 1, if there is an edge between the two nodes, otherwise zero. Thus if an entry of 37 7.3. THEORY Shortest path using adjacency matrix Figure 7.1: Graph-G3 matrix at (i, j) is 1, it denotes that there is an edge between the nodes i and j. This matrix is known as Adjacency matrix. In undirected graph, this matrix will be symmetric. Since, if we say that there is an edge between nodes I and j, it also implies that there is an edge between j and i. For example, consider the graph shown above. The adjacency matrix for the graph will be: 0 0 1 1 0 1 0 0 0 0 1 1 1 1 1 0 * Consider another example as given below: Now the above graph is a directed graph and so there will be some changes in it’s adjacency matrix representation. So the representation of the above graph using adjacency matrix is : 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 0 0 0 0 0 PROBLEM SPECIFICATION:- 1) Data structure to be used: Structures int cost[MAX][MAX],distance[MAX],pred[MAX]; Lab Manual - Data Structures and Files 38 VPCOE, Baramati 7.3. THEORY Shortest path using adjacency matrix Figure 7.2: Graph-G4 int visited[MAX],count,mindistance,nextnode,i,j; pred[] 2) Concepts to be used : arrays 1. Function to create adj. vertices and cost matrix for a graph. 2. Function to display the vertex wise adj. vertices and resp. costs. 3. Function to generate the shortest paths from given source vertex to all the other vertices using Dijkstra’s Algorithm. 3) INPUT: 1. No. of vertices. 2. The adjacent vertices and their weights. 4) OUTPUT: Display vertex wise adj. vertices and cost of the associated edges for the constructed graph. Analysis of Algorithms : Lab Manual - Data Structures and Files 39 VPCOE, Baramati 7.4. PROCEDURE Shortest path using adjacency matrix 1) Time Complexity :- For ’n’ vertex digraph the frequency count for each step of the algorithm are: Step Freq. count Remark 1 1 Declaration of arrays. 2 n+1 For loop no. of comparisons. 3 1 Putting starting vertex in S. 4 n - 1 While loop condition is checked. 5 (n-2)(n+1) Choosing vertex u from distance array. 6 n - 2 Putting chosen vertex u in sets. 7 (n-2)(n+1) Changing shortest path lengths in distance array after putting the vertex u in sets. Total freq. = (n-2) (2n+4) + n + 3 Count Time complexity of Dijkstra’s algorithm: O(n2) 2) Space Complexity:- Space required for the array adj-ver and cost of O(n2) size for n vertex graph. TEST CASE : 1. Enter a graph having more than 1 shortest path. 2. Enter a graph having isolated vertices. 3. Enter a graph with only one vertex. 4. Print the shortest path in graphical form. APPLICATIONS: 1. Finding minimum distances between cities, countries etc. 2. Used in electrical circuits. 3. Used to find out minimum paths in graphs. 7.4 Procedure Algorithm dijkstra(int G[MAX][MAX],int n, int startnode) 1. int cost[MAX][MAX],distance[MAX],pred[MAX] int visited[MAX],count,mindistance,nextnode,i,j /*pred[] stores the predecessor of each node count gives the number of nodes seen so far*/ Lab Manual - Data Structures and Files 40 VPCOE, Baramati 7.4. PROCEDURE Shortest path using adjacency matrix 2. //create the cost matrix for(i=0;i¡n;i++) for(j=0;j¡n;j++) if(G[i][j]==0) cost[i][j]=INFINITY; else cost[i][j]=G[i][j]; 3. //initialize for(i=0;i¡n;i++) distance[i]=cost[startnode][i]; pred[i]=startnode;visited[i]=0; 4. distance[startnode]=0; visited[startnode]=1; count=1; 5. while(count¡n-1) mindistance=INFINITY ; // nextnode is the node at minimum distance for(i=0;i¡n;i++) if(distance[i] ¡ mindistance && !visited[i]) mindistance=distance[i]; nextnode=i; 6. check if a better path exist through nextnode visited[nextnode]=1; for(i=0;i¡n;i++) if(!visited[i]) if(mindistance+cost[nextnode][i]¡distance[i] distance[i]=mindistance+cost[nextnode][i]; pred[i]=nextnode; count++ 7. print the path and distance of each node for(i=0;i¡n;i++) if(i!=startnode) print(i,distance[i]); Lab Manual - Data Structures and Files 41 VPCOE, Baramati 7.5. POST LAB Shortest path using adjacency matrix print(i); j=i; 8. do j=pred[j]; print(”¡- ”,j); while(j!=startnode); 9. stop. 7.5 Post Lab 1. Graph representation using adjacency matrix. 2. Adjacency matrix representation. 7.6 Viva Questions 1. How is the adjacency matrix representation of a graph done ? 2. What is the concept of Dijkstra’s algorithm ? Lab Manual - Data Structures and Files 42 VPCOE, Baramati Assignment 8 IMPLEMENT HUFFMAN’S ALGORITHM 8.1 Problem Statement Write a program to create a Huffman tree and find the prefix codes for given alphabets. 8.2 Pre Lab Prerequisites of the assignments. 8.3 Theory Huffman algorithm: Solution to determine a binary tree with minimum weighted external path length was given by D. Huffman. Suppose there are N nodes in a tree then every time we have to select two nodes of smallest weight, such that this will break any ties and construct the new tree with these two nodes and create a new tree with these nodes as sub trees. This should be repeated for N-1 times so at end we get a single tree The algorithm Huffman makes use of list L of external nodes of an extended binary tree. List L contains the external nodes containing their individual weight. During course of algorithm, for any tree in L root node T an depth greater than weight (T) is sum of weights of all external nodes in T. The algorithm least determines a tree in L with minimum weight and removes it from L. Step 1: Sort the frequencies. 10,15,20,30 Step 2: Add the two smallest frequencies.Here 10 and 15 are the two smallest frequencies ,then create 43 8.3. THEORY IMPLEMENT HUFFMAN’S ALGORITHM Figure 8.1: T6 a root by adding 10 and 15 Our Array becomes 25,20,30. Figure 8.2: HT Step 3: Again take two smallest frequencies from array and create root node by adding them. Step 4: Again take two smallest frequencies from array and create root node by adding them. Step 5: Now assign the codes for left as 0 and to right as 1 and generate prefix code for each element in list. PROBLEM SPECIFICATION:1) Data structure to be used: Linked list // stucture of tree node typedef struct treenode { float freq; Lab Manual - Data Structures and Files 44 VPCOE, Baramati 8.3. THEORY IMPLEMENT HUFFMAN’S ALGORITHM Figure 8.3: HT1 char data; struct treenode *left,*right; }treenode; /* structure of node of linked list */ typedef struct node { treenode *data; //address of tree struct node *next; }node; 2) Concepts to be used: 1. Doubly linked list. 2. Array of pointers. 3) INPUT: n integer values as weight of nodes. 4) OUTPUT: Lab Manual - Data Structures and Files 45 VPCOE, Baramati 8.3. THEORY IMPLEMENT HUFFMAN’S ALGORITHM Figure 8.4: Huffman Tree Display prefix code for the given weights. ALGORITHM: ALGORITHM: treenode *create(): 1. treenode *p,*t1,*t2; node *head; int n,i; char x; float probability; head=NULL; //empty linked list 2. Enter No. of alphabets n for(i=0;i¡n;i++) flushall(); Enter alphabet x Lab Manual - Data Structures and Files 46 VPCOE, Baramati 8.3. THEORY IMPLEMENT HUFFMAN’S ALGORITHM Figure 8.5: Prefix Code Enter frequency f /* create a new tree and insert it in the priority linked list */ p=(treenode*)malloc(sizeof(treenode)); p→left=p→right=NULL; p→data=x; p→freq=probability; head=insert(head,p); 3. /* create the final tree by merging of two trees of smaller weights (n-1) merges will be required*/ for(i=1;i¡n;i++) t1=head→data; //first tree 4. stop. 2. Algorithm for create function: n = (NODE *)malloc(sizeof(NODE)); strcpy(n -> prefix,"\0"); n -> left = NULL; n -> right = NULL; 3. Algorithm for huff-tree: Lab Manual - Data Structures and Files 47 VPCOE, Baramati 8.3. THEORY IMPLEMENT HUFFMAN’S ALGORITHM for(i=0;i<n-1;i++) if(root == NULL) { p = create(); p -> data = huff[i] -> data; q = create(); q -> data = huff[i+1] -> data; temp = create(); temp -> data = p-> data + q -> data; temp -> left = p; temp -> right = q; } else { temp = create(); temp -> left = huff[i]; temp -> right = huff[i+1]; temp -> data = huff[i] -> data + huff[i+1] -> data; } root = temp; huff[i] = NULL; huff[i+1] = NULL; insert(root); 4. Algorithm for insert node: for(i=1;i<n;i++) { if(huff[i] != NULL) { if(huff[i] -> data >= t -> data) if(huff[i-1] == NULL) huff[i-1] = t; } else { Lab Manual - Data Structures and Files 48 VPCOE, Baramati 8.3. THEORY IMPLEMENT HUFFMAN’S ALGORITHM j = i-1; for(i=0;i<=j;i++) huff[i] = huff[i+1]; huff[j] = t; } break; } else if(huff[i] -> data < t -> data && i == n-1) { for(i=0;i<n-1;i++) huff[i] = huff[i+1]; huff[n-1] = t; break; } 5. Algorithm for form-code: if(t != NULL) { if(t -> left != NULL) { strcat(t -> left -> prefix,q -> prefix); strcat(t -> left -> prefix,"0"); form_code(t -> left); } if(t -> right != NULL) { strcat(t -> right -> prefix, q -> prefix); strcat(t -> right -> prefix,"1"); form_code(t -> right); } if(t -> left == NULL && t -> right == NULL) { printf("\n\n%d \t -> \t %s", t -> data,t -> prefix); } } } Lab Manual - Data Structures and Files 49 VPCOE, Baramati 8.4. VIVA QUESTIONS 8.4 IMPLEMENT HUFFMAN’S ALGORITHM Viva Questions 1. What is Huffman tree ? 2. Which data structure is used to create Huffman tree ? 3. What are the application of Huffman tree ? Lab Manual - Data Structures and Files 50 VPCOE, Baramati Assignment 9 Hash Table 9.1 Problem Statement To create & maintain a hash table using 1. Linear probing without replacing 2. Linear probing with or without replacing 9.2 Pre Lab 1. Concept of Array 9.3 Theory HASHING: The address or location of an identifier ’x’ is obtained by computing some arithmetic function ’f of x’ i.e. f(x) gives the address of x in hash table. HASH ADDRESS: The computed address using f(x) is called as hash address or home address of ’x’. HSDH TABLE: The memory table available to maintain the table of identifiers is assumed to be sequential. This memory is referred to as hash table. COLLISION: A collision occurs when two non-identical identifiers hash to same address. 51 9.3. THEORY Hash Table WHAT IS THE NEED OF HASHING? We know that we can store a given set of identifiers in sorted order in an array and then we can apply either binary search or Fibonacci search or use search bst algorithm if they are stored as a complete binary tree in array to search any identifier in the table. The time complexity in all binary trees in array to search any identifier in the table is O(log(n)). But if we store these identifiers in hash table using some hash function ,then time required to insert / delete / search any identifier is O(1). The time complexity in all these cases is O(log n) further to insert / delete an identifier from a table is O(n). CHARACTERISTICS OF GOOD HASHING FUNCTION: 1. A good hash function avoids collision ideally or keeps collision at minimum. 2. A good hash function tends to spread key evenly in hash table. 3. A good hash function is easy to compute. SOME OF POPULAR HASH FUNCTIONS: 1. Division Method: L+(K mod M)+1 2. Mid Square Method: In this method key value is squared & the address or position in hash table or file. The no. of digits selected is equal to the no. of digits in maximum value of address or position no. in hash table or file. COLLISION RESOLUTION: In practice unless we know something about keys chosen, we can not guaranty that there will not be collision. In some application, we’ve some about the keys then we can exploit it to reduce the likely hood of collision but we can not eliminate it. Hence we must find out the way to resolve this problem. Hence we must find out the way aimis to place the keys into different position. This needs to scan through table or file to fin=d an empty slot where we can place when ever collision occurs and at the time we must have mechanism to find that key when ever needed. Open addressing: Is this synonyms are identified by some method and some of predictable slots are examined or probed until an empty slot is found to place the synonym in slot. a. Linear probing b. Chaining A. Linear probing without replacement: In linear probing without replacement when a synonym is identified, the table or file will be forward from natural address to record or entry until an empty slot is found. The record is then inserted into the empty slot. Natural address of an identifier or record is the location given by hash function operating on identifier or key of record. In this when a synonym is identified, the table or file will be searched forward from the natural address to the record or entry until empty slot is not found, if a new identifier and the identifier stored Lab Manual - Data Structures and Files 52 VPCOE, Baramati 9.3. THEORY Hash Table Figure 9.1: T1 at natural address. Eg. Let h(x) = x mod 10 and the keys of records or identifiers to be stored in hash table. 0,1,4,8,65,25,36,16,16,9,49 C. Chaining Without Replacement : In chaining without replacement is similar to linear probing without replacement. Only difference is in a record a link is maintained to point to next synonym. Eg. h(x)=x mod 10 & the keys of records or identifiers to be stored in hash table. 0,1,4,81,25,36,16,26,9,49 1) Time complexity : 1. Chaining with replacement gives better results in searching or locating a record but insertion/deletion of a record will take more time. 2. With low loading density for all methods time complexity is O(1). 3. With high loading density time complexity for linear probing without replacement methods will vary from O(1) to O(n) in worst case. 2) Space complexity : Lab Manual - Data Structures and Files 53 VPCOE, Baramati 9.3. THEORY Hash Table Figure 9.2: T2 Additional space is required to store link address in record in case of chaining with or without replacement ALGORITHM: I. insert( int data[],int flag[],int chain[],int x): 1. int i=0,j,start; start=h(x); /*hashed location*/ /*locate the beginning of the chain*/ 2. while(flag[start] && i<SIZE) { if(data[start]%SIZE==x%SIZE) break; i++; start=(start+1)%SIZE; Lab Manual - Data Structures and Files 54 VPCOE, Baramati 9.3. THEORY Hash Table } 3. if(i==SIZE) { print(hash table is full); return; } 4. /*go to the end of chain */ while(chain[start]!=-1) start=chain[start]; 5. /*locate an empty place for the current data */ j=start; while(flag[j] && i<SIZE) { j=(j+1)%SIZE; i=i+1; } 6. if(i==SIZE) { print(hash table is full); return; } 7. data[j]=x; /*store the data */ flag[j]=TRUE; if(j!=start) chain[start]=j; /*set the chain */ 8. stop. II. Search(int data[],int flag[],int chain[],int x): 1. int i=0,j; j=h(x); /*hashed location*/ 2. /*locate beginning of the chain*/ Lab Manual - Data Structures and Files 55 VPCOE, Baramati 9.3. THEORY Hash Table while(i<SIZE && flag[j] && data[j]%SIZE !=x%SIZE) { i++; j=(j+1)%SIZE; } 3. if(!flag[j] || i==SIZE) return(-1); 4. /*locate the element in the chain */ while(j!=-1) { if(data[j]==x) return(j); j=chain[j]; } 5. return(-1); 6. stop. \end{verbatm} {\bf print(int data[],int flag[],int chain[]:} \begin{verbatim} 1. int i; for(i=0;i<SIZE;i++) 2. if(flag[i]) print(i,data[i],chain[i]); 3. else print(i,chain[i]); 4. stop. Lab Manual - Data Structures and Files 56 VPCOE, Baramati 9.4. VIVA QUESTIONS 9.4 Hash Table Viva Questions 1. What is hashing?. 2. Explain the difference between linear and non linear probing ? 3. What are the different hashing techniques? Lab Manual - Data Structures and Files 57 VPCOE, Baramati Assignment 10 SEQUENTIAL FILE ORGANIZATION. 10.1 Problem Statement To perform sequential file organization in order to implement student database. 10.2 Pre Lab Concept of Structure 10.3 Theory FILE: A file is defined as a collection of records where each record consists of one or more associate fields or data items. For example the records in an employee file may contain the following fields : Employee number : 001 Name of employee : Nandan Nilekani Address : Infosys Technologies, techno-park, M.G Road, Bangalore. Highest qualification : M.E(IT) Designation : CEO Sex : Male Marital status : Married 58 10.3. THEORY SEQUENTIAL FILE ORGANIZATION. Why do we need a file ? Because of limitations of computer main memory which is volatile and also because we cannot store thousands of records of file in main memory. FILE ORGANIZATION: The method to store the data on secondary storage & to support subsequent operations like retrieval, insertion/append a record to file, deletion of a record from file & modification of a record is called as file organization. There are 3 types of file organization: 1. Sequential 2. Index sequential 3. Direct or random Sequential file organization: The sequential file is organization by writing the records in a serial order in which records arrive. Thus the first record in a file is the oldest one & the last one is the most recent one. We access these records from file in same order in which they are written to a file. The serial order does not correspond to any sequence. The records may be stored serially in random order or in sorted order. But in practice the sequential files are ordered in same sequence. Primitive operations on sequential files: 1. Open: This operation permits the access to the file & sets pointer immediately before the first record. 2. Read next: This operation sets the current pointer to the next record in the file & returns that record to the user. If there is no record present then an end of file (eof) condition is set. 3. Write: This operation writes the record from temporary storage to the file at position pointed by current pointer. 4. Close: This operation terminates the access to the file releases memory locations used for maintaining the file. 5. Valid: Returns true if the previous operation on a file was successful otherwise returns false. 6. EOF: Returns true if current pointer is beyond the last record of file otherwise returns false. Lab Manual - Data Structures and Files 59 VPCOE, Baramati 10.3. THEORY SEQUENTIAL FILE ORGANIZATION. 7. Append: This operation positions the current pointer to the next of last record in a file & writes there the new record & current pointer is at the end of record written. 8. Update: This operation is applicable to the sequential files stored on disk. It writes back the current record pointed by current pointer. C implementation of primitive operations of sequential files: 1. Open: ’C’ implementations for opening for a file are file fp; Fp=fopen (filename) The various modes of opening a file are : ’r’ : opens a file for READ only. Current pointer fp is placed at the beginning of the file. ’w’ : opens a file for WRITE only. It is assumed that the file does not exist with that name \& new f ’a’ : opens a file in APPEND mode i.e, a new records can be added at the end of existing file. ’r+’ : opens a file for READ \& UPDATE i.e current record can be modified & rewritten in a file. File ’w+’ : opens a file for READ \& WRITE. It is assumed that file does not exist with name exists and ne ’a+’ : open a file for READ \& APPEND ,i.e new records can be added in the existing file \& it does n 2. Close: ’C’ implementation for closing a file : fclose(filename pointer) Value of EOF=0on successful closing of a file 1on error 3. Read: Fread( \&structure , sizeof (structure) , 1 , fp ) : to read a structure from a file. Fscanf(): to read data items from file as per format. Fget(): to read a character from a file. 4. Write: fwrite( \&structure , sizeof(structure) , 1 , fp ) : to write a structure on a file. fprintf() : to write the data items on file as per the format. 5. Update: Lab Manual - Data Structures and Files 60 VPCOE, Baramati 10.3. THEORY SEQUENTIAL FILE ORGANIZATION. To rewrite a structure on a file : fwrite( \&structure , sizeof(structure) , 1 , fp ) Advantages of sequential files : 1. simple organization. 2. Minimum disk space overheads. 3. Allows block spanning. 4. Optimal if the application accesses all the records in each run. 5. Most versatile format compatible with ANSI magnetic tape format. 6. No restriction on type of storage media. 7. Sequential files are portable. 8. Easy to store variable length records. Disadvantages of sequential files : 1. Not suitable for interactive online applications. 2. Records can be added only at the end of a file. SPECIFICATIONS: 1. Data structure to be used: FILE *fp; char fname[size]; struct bollywood { char film[50],hero[50],heroine[50],song[50],director[50]; int year; }bolly; 2. Concepts to be used: Lab Manual - Data Structures and Files 61 VPCOE, Baramati 10.3. THEORY SEQUENTIAL FILE ORGANIZATION. 1. Functions to create a sequential file. 2. Functions to add/append a record for a given roll number. 3. Function to search/display record for a given roll number. 4. Function to update a record for a given roll number. 5. Function to display all records of a file. 3. Inputs: 1. Create a sequential file containing student database for any number of students 2. Add one more student data to the existing sequential file. 3. Display the student record for a given roll number. 4. Update student data for a given roll number in the sequential file. 4. Output: 1. Display all student records contained in sequential file after creation of initial student database. 2. Display all student records contained in a sequential file after adding one more record to it. 3. Search and display the student record for a given updated roll number in the existing sequential file. 5. Testing: Test your program for the following cases: a. Display the total number of records read from file for each option. b. Display the results as given in sample o/p above. ANALYSIS OF ALGORITHM : 1. Time complexity : For searching a record , appending a file & creating a file , time complexity is O(n). 2. Space complexity: Additional space is required to store one structure. ALGORITHM : I. creat(): char ch; printf("\n\n\tENTR FILE NAME :: "); scanf("%s",fname); fp=fopen(fname,"w"); Lab Manual - Data Structures and Files 62 VPCOE, Baramati 10.3. THEORY SEQUENTIAL FILE ORGANIZATION. fseek(fp,0,SEEK_SET); if(fp==NULL) { printf("\n File not created.... "); return; } else { do { clrscr(); printf("\n\n\tENTER THE RECORD"); printf("\n\n\tENTER THE YEAR::"); scanf("%d",&bolly.year); printf("\n\nENTER THE NAME OF THE BEST FILM::"); scanf("%s",bolly.film); printf("\n\nENTER THE NAME OF THE BEST HERO::"); scanf("%s",bolly.hero); printf("\n\nENTER THE NAME OF THE BEST HEROINE::"); scanf("%s",bolly.heroine); printf("\n\nENTER THE SONG OF THE YEAR::"); scanf("%s",bolly.song); printf("\n\nENTER THE DIRECTOR OF THE YEAR::"); scanf("%s",bolly.director); fwrite(&bolly,sizeof(bolly),1,fp); printf("\nInsert another record?(y/n)"); flushall(); scanf("%c",&ch); }while(ch==’y’||ch==’Y’); } fclose(fp); } void II. append(): char ch; clrscr(); Lab Manual - Data Structures and Files 63 VPCOE, Baramati 10.3. THEORY SEQUENTIAL FILE ORGANIZATION. printf("\n\n\tENTR FILE NAME ==> "); scanf("%s",fname); fp=fopen(fname,"a"); clrscr(); printf("\n\t\t\t\tRECORD BOOK\n\n\t\t\tBOLLYWOOD INFORMATION "); do { printf("\n\n\tENTER THE RECORD"); printf("\n\n\tENTER THE YEAR::"); scanf("%d",&bolly.year); printf("\n\nENTER THE NAME OF THE BEST FILM::"); scanf("%s",bolly.film); printf("\n\nENTER THE NAME OF THE BEST HERO::"); scanf("%s",bolly.hero); printf("\n\nENTER THE NAME OF THE BEST HEROINE::"); scanf("%s",bolly.heroine); printf("\n\nENTER THE SONG OF THE YEAR::"); scanf("%s",bolly.song); printf("\n\nENTER THE DIRECTOR OF THE YEAR::"); scanf("%s",bolly.director); fwrite(&bolly,sizeof(bolly),1,fp); printf("\n Insert another record?(y/n)"); flushall(); scanf("%c",&ch); }while(ch==’y’||ch==’Y’); fclose(fp); } III.void display(): printf("\n ENTR FILE NAME ==> "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) { printf("\nFile can’t opened.."); Lab Manual - Data Structures and Files 64 VPCOE, Baramati 10.3. THEORY SEQUENTIAL FILE ORGANIZATION. return; } fread(&bolly,sizeof(bolly),1,fp); printf("\n\nYEAR\tFILM\tHERO\tHEROINE\tSONG\tDIRECTOR"); while(!feof(fp)) { printf("\n%d\t%s\t%s\t%s \t%s\t%s",bolly.year,bolly.film,bolly.hero,bolly.heroine,bolly.song,bolly.di fread(&bolly,sizeof(bolly),1,fp); } fclose(fp); IV.void dis(): printf("\n%d\t%s\t%s\t%s \t%s\t%s",bolly.year,bolly.film,bolly.hero,bolly.heroine,bolly.song,bolly.d V.void search(): int recno; int flag=0; printf("\n ENTR FILE NAME ==> "); scanf("%s",fname); fp=fopen(fname,"r"); fseek(fp,0,SEEK_SET); printf("\nENTER THE YEAR::"); scanf("%d",&recno); fread(&bolly,sizeof(bolly),1,fp); while(!feof(fp) && !flag) { if(bolly.year==recno) { flag=1; break; } fread(&bolly,sizeof(bolly),1,fp); } if(flag==1) { Lab Manual - Data Structures and Files 65 VPCOE, Baramati 10.3. THEORY printf("\n\t\t Record SEQUENTIAL FILE ORGANIZATION. found.."); // dis(); printf("\n%d\t%s\t%s\t%s \t%s\t%s",bolly.year,bolly.film,bolly.hero,bolly.heroine,bolly.song,b fclose(fp); } else { printf("\n\t\t Record Not found.."); fclose(fp); } VI.void modify(): FILE *ftemp; int recno; printf("\n ENTER FILE NAME ==> "); scanf("%s",&fname); printf("\n ENTER THE YEAR U WANT TO MODIFY ==> "); scanf("%d",&recno); fp=fopen(fname,"r"); fseek(fp,0,SEEK_SET); ftemp=fopen("temp.txt","w"); while(!feof(fp)) { fread(&bolly,sizeof(bolly),1,fp); if(recno==bolly.year) { printf("\n\n\tENTER THE YEAR::"); scanf("%d",&bolly.year); printf("\n\nENTER THE NAME OF THE BEST FILM::"); scanf("%s",bolly.film); printf("\n\nENTER THE NAME OF THE BEST HERO::"); scanf("%s",bolly.hero); printf("\n\nENTER THE NAME OF THE BEST HEROINE::"); scanf("%s",bolly.heroine); printf("\n\nENTER THE SONG OF THE YEAR::"); scanf("%s",bolly.song); Lab Manual - Data Structures and Files 66 VPCOE, Baramati 10.4. VIVA QUESTIONS SEQUENTIAL FILE ORGANIZATION. printf("\n\nENTER THE DIRECTOR OF THE YEAR::"); scanf("%s",bolly.director); // fwrite(&bolly,sizeof(bolly),1,fp); } fwrite(&bolly,sizeof(bolly),1,ftemp); fread(&bolly,sizeof(bolly),1,fp); } fclose(fp); fclose(ftemp); if(remove(fname)==0) printf("\n\t\tRECORD MODIFIED..."); rename("temp.txt",fname); } 10.4 Viva Questions 1. What are the primitive operations on sequential files ? 2. Explain the various functions used in file handling. 3. List the pros & cons of sequential file organization. Lab Manual - Data Structures and Files 67 VPCOE, Baramati References [1] Author 1, ”Title of Reference 1”, Publisher, Year. [2] Author 2, ”Title of Reference 2”, Publisher, Year. 68