cseengineers2k13.files.wordpress.com€¦  · web viewa singly-linked list is ideally suited to...

76
DS Through C Laboratory Manual II YEAR B.TECH (CSE, IT) Department of Computer Science & Engineering

Upload: others

Post on 12-Feb-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

DS Through CLaboratory Manual

II YEAR B.TECH(CSE, IT)

Department of Computer Science & Engineering

DS Through CLaboratory ManualII YEAR B.TECH(CSE, IT)

Department of Computer Science & Engineering

Document No:

MLRIT/CSE/LAB MANUAL/DSC

VERSION 1.1.3

Date of Issue

30 JUNE 2012

Date of Revision

JUNE 2013

Compiled by

Mrs L.Lakshmi

(Assistant Professor)

Verified by

Mr. Sheikh Gouse

Authorized by

HOD(CSE)

PREFACE

DS Through C is one of the important subjects included in the second year curriculum by JNTU, Hyderabad. In addition to theory subject also includes DS Through C as lab practical’s using C language.

The DS Through C constitutes step-by-step procedure encompassing understanding of the problem, developing logic to solve the problem in the form of Algorithms and converting logic into source code using C language.

The students must grasp three aspects while doing DS Through C in the lab as given below.

a. Understanding grammar of C language to enable writing correct program from syntax point of view.

b. Developing the logic to find solution to a given problem from semantic point of view.

c. Importance of space and time complexity to enable writing a program which meets requirements of memory constraints and time constraints.

Students will be in a position to grasp the above three aspects while doing lab practical’s as defined in the manual through four steps i.e design and code, compile, linkage and run and test.

This manual is a collective effort of the faculty teaching second year DS Through C subject.

This manual will need constant up gradation based on the student feedback and change in the syllabus.

HOD (IT)HOD (CSE) DEAN PRINCIPAL

Steps in Program Development

Program development is a multi step process. The steps are 1) Understand the problem 2) Develop a solution 3) Write the program and test it.

1. Understand the problem: When you are assigned to develop a program for solving a problem, you are given the programming requirements. Study these requirements carefully resolve all doubts and ensure that you have understood it fully as per the user requirements.

2. Develop a solution: Once you have understood the problem, you have to develop the solution in terms of some programming language. The tools that help in this are:

a) Algorithm / Pseudo code

b) Flowchart

c) Programming Language, like C etc.,

d) Test cases

a) Algorithm

Definition: A method of representing the step-by-step logical procedure for solving a problem in natural language (like English, etc.) is called as an Algorithm.

Algorithm can also be defined as an ordered sequence of well-defined and effective operations that, when executed, will always produce a result and eventually terminate in a finite amount of time.

On the whole an algorithm is a recipe for finding a right answer to a problem by breaking it down into simple steps.

Properties an Algorithm should possess:

a. Generality: The algorithm must be complete in itself so that it can also be used to solve all problems of a specific type for given input data.

b. Input / Output: Each algorithm can take zero, one or more input data and must produce one or more output values.

c. Optimization: Unnecessary steps should be eliminated so as to make the algorithm to terminate in finite number of steps.

d. Effectiveness: Each step must be effective in the sense that it should be primitive (easily convertible into program statement) and can be performed exactly in a finite amount of time.

e. Definiteness: Each step of the algorithm, should be precisely and unambiguously stated.

b) Flowchart:

Definition: It is a diagrammatic representation of an algorithm. It is constructed using different types of symbols.

Standard symbols used in drawing flowcharts:

Oval

Terminal

Start/stop/begin/end

Parallelogram

Input/output

Making data available for processing(input) or recording of the processed information(output)

Document

Printout

Show data output in the form of document

Rectangle

Process

Any processing to be performed. A process changes or moves data. An assignment operation

Diamond

Decision

Decision or switching type of operation

Circle

Connector

Used to connect different parts of flowchart

Arrow

Flow

Joins two symbols and also represents flow of execution

Bracket with broken line

Annotation

Descriptive comments or explanation

Double sided rectangle

Predefined process

Modules or subroutines specified elsewhere

Programming and Testing:

A computer program is the sequence of instructions written in a computer language according to the algorithm and computer follows these in carrying out its computations.

The process of writing a program is called programming.

Steps involved in Computer Programming:

· Problem Understanding

· Problem Definition

· Program writing

· Error analysis

· Validation & Verification

· Documentation & Maintenance

LAB CODE

1. Student should report to the concerned as per the time table.

2. Students who turn up late to the labs will in no case be permitted to the program schedule for the day.

3. After completion of the program, certification of the concerned staff in-charge in the observation book is necessary.

4. Student should bring a note book of 100 pages and should enter the readings/observations into the note book while performing the experiment.

5. The record of observations along with the detailed experimental Algorithm of the experiment in the immediate last session should be submitted and certified staff member in-charge

6. Not more than 3 students in a group are permitted to perform the experiment on the set.

7. The group-wise division made in the beginning should be adhered to and no mix up of students among different groups will be permitted.

8. The components required pertaining to the experiment should be collected from stores in-charge after duly filling in the requisition form.

9. When the experiment is completed, should disconnect the setup made by them, and should return all the components / instruments taken for the purpose.

10. Any damage of the equipment or burn –out components will be viewed seriously either by putting penalty or by dismissing the total group of students from the lab for the semester / year.

11. Students should be present in the labs for total scheduled duration.

12. Students are required to prepare thoroughly to perform the experiment before coming to laboratory.

13. Algorithm sheets/data sheets provided to student’s groups should be maintained neatly and to be returned after the experiment.

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY

HYDERABAD

II Year B.Tech. CSE - I SemL T/P/DC

0 -/3/- 2

DATA STRUCTURES LAB

Objectives:

To write and execute programs in C to solve problems using data structures such as arrays, linked lists, stacks, queues, trees, graphs, hash tables and search trees.

To write and execute write programs in C to implement various sorting and searching methods.

Recommended Systems/Software Requirements:

· Intel based desktop PC with minimum of 166 MHZ or faster processor with at least 64 MB RAM and 100MB free disk space.

· C compiler.

Week1: Write a C program that uses functions to perform the following:

a) Create a singly linked list of integers.

b) Delete a given integer from the above linked list.

c) Display the contents of the above list after deletion.

Week2: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of integers.

b) Delete a given integer from the above doubly linked list.

c) Display the contents of the above list after deletion.

Week3: Write a C program that uses stack operations to convert a given infix expression into its postfix Equivalent, Implement the stack using an array.

Week 4:Write C programs to implement a double ended queue ADT using i)array and ii)doubly linked list respectively.

Week 5:Write a C program that uses functions to perform the following:

a) Create a binary search tree of characters.

b) Traverse the above Binary search tree recursively in Postorder.

Week 6:Write a C program that uses functions to perform the following:

a) Create a binary search tree of integers.

b) Traverse the above Binary search tree non recursively in inorder.

Week 7: Write C programs for implementing the following sorting methods to arrange a list of integers in Ascending order :

a) Insertion sort b) Merge sort

Week 8:Write C programs for implementing the following sorting methods to arrange a list of integers in ascending order:

a) Quick sort b) Selection sort

Week 9: i) Write a C program to perform the following operation:

a)Insertion into a B-tree.

ii) Write a C program for implementing Heap sort algorithm for sorting a given list of integers in ascending order.

Week 10: Write a C program to implement all the functions of a dictionary (ADT) using hashing.

Week 11: Write a C program for implementing Knuth-Morris- Pratt pattern matching algorithm.

Week 12: Write C programs for implementing the following graph traversal algorithms:

a)Depth first traversal b)Breadth first traversal

TEXT BOOKS:

1. C and Data Structures, Third Edition, P.Padmanabham, BS Publications.

2. C and Data Structures, Prof. P.S.Deshpande and Prof. O.G. Kakde, Dreamtech Press.

3. Data structures using C, A.K.Sharma, 2nd edition, Pearson.

4. Data Structures using C, R.Thareja, Oxford University Press.

5. C and Data Structures, N.B.Venkateswarlu and E.V.Prasad,S.Chand.

6. C Programming and Data Structures, P.Radha Krishna, Hi-Tech Publishers.

INDEX

SNO

Experiment No

Name of the Experiment

Page No

1

Objective , Outcomes, Hardware Software Requirements

11

2

1

Single Linked List

12-18

3

2

Double Linked List

19-27

4

3

Infix to Postfix Conversion

28-34

5

4

Deque using Arrays and linked list

35-42

6

5

Binary Search tree Using recursion

43-49

7

6

Binary Search tree Using non recursion

50-56

8

7

9

8

10

9

11

10

12

11

13

12

Objective

· To write and execute programs in C to solve problems using data structures such as arrays, linked lists, stacks, queues, trees, graphs, hash tables and search trees.

· To write and execute write programs in C to implement various sorting and searching methods.

Outcome

· Ability to identify the appropriate data structure for given problem.

· Graduate able to design and analyze the time and space complexity of algorithm or program.

· Ability to effectively use compilers includes library functions, debuggers and trouble shooting.

Hardware and Software required

1. Intel based desktop PC

2. Minimum of 166 MHZ or faster processor

3. At least 64 MB RAM

4. At least 100 MB free disk space

5. C compiler and STL Recommended

Experiment No:1

Experiment as given in the JNTUH curriculum.

Week1: Write a C program that uses functions to perform the following:

a) Create a singly linked list of integers.

b) Delete a given integer from the above linked list.

c) Display the contents of the above list after deletion

AIM: To Implement singly linked list of integers in data structure using functions.

THEORY:

Singly Linked List

Generally a Linked List means "Singly Linked List". It is a chain of records known as Nodes. Each node has at least two members, one of which points to the next Node in the list and the other holds the data.

Figure 1: Singly Linked List

Basically Single Linked Lists are uni-directional as they can only point to the next Node in the list but not to the previous. We use below structure for a Node in our example.

struct Node  

{  

  int Data;  

  struct Node *Next;  

};   

Variable Data holds the data in the Node (It can be a pointer variable pointing to the dynamically allocated memory) while Next holds the address to the next Node in the list.

Figure 2: Node in a Singly Linked List

Head is a pointer variable of type struct Node which acts as the Head to the list. Initially we set 'Head' as NULL which means list is empty.

Basic Operations on a Singly Linked List

· Traversing a List

· Inserting a Node in the List

· Deleting a Node from the List

.

Inserting a Node in Singly Linked List 1.Inserting a Node at the beginning of the ListInserting a Node at the End of the ListInserting a Node at position 'p' in the List

Deletion of a Node from a Singly Linked ListDeleting First Node in Singly Linked ListDeleting Last Node in the Singly Linked List Deleting Node from position 'p' in the List

ALGORITHM:

Initialize the first and last nodes with NULL values

struct node *first=NULL,*last=NULL,*next,*prev,*cur;

1. Algorithm creating a new node:

Step 1: if the list is empty then first==NULL

Step 2: Create a new node

cur=(struct node*) malloc (sizeof (struct node));

Step 3: Read the content of node

Step 4: Assign new node link to NULL

cur->link=NULL

Step 5: Assign new node to first & last node

first=cur

last=cur

Step 6: If the list is not empty call insert function

insert ()

Step 7 : Stop

2. Algorithm for Inserting a new node:

Step 1 : Initialize count c to 1

Step 2 : Create inserting node

cur=(struct node*)malloc(sizeof (struct node));

Step 3: Read the content of node

Step 4: Read the position of insertion

Step 5: Inserting in first position

Check if the pos=1 and first!=NULL

cur->link=first;

first=cur;

Step 6: Inserting in a given position

next=first;

repeat the steps a to c until c < pos

a. prev=next;

b. next=prev->link;

c. c++;

cur->link=prev->link;

prev->link=cur;

Step 7 : Stop

3. Algorithm for Deleting a node:

Step 1 : Initialize count c to 1

Step 2 : Read the position for deletion

Step 3 : Check if first=NULL

print list is empty

Step 4 : If the list contains single element

Check if pos=1 and first->link=NULL

print deleted element is first->data

Step 5 : Assign first to NULL

first=NULL;

Step 6 : If the list contains more than one element and to delete first element

if pos=1 and first->link!=NULL

cur=first;

first=first->link;

cur->link=NULL;

print deleted element is cur->data

free(cur)

Step 7 : If the list contains more than one element and to delete an element at given position

next=first;

repeat the steps a to c until c < pos

a. cur=next;

b. next=next->link;

c. c++;

cur->link=next->link;

next->link=NULL;

print deleted element is next->data

free(next);

Step 8 : Stop

4. Algorithm for Displaying a node:

Step1 : Check if first node is NULL

print list is empty

Step2: If first node is not NULL then

cur=first;

repeat the steps a to b until cur!=NULL

a . print cur->data

b . cur=cur->link;

Step3 : Stop

INPUT & OUTPUTInsert() : 1 2 3 4 5Display () : 1->2->3->4->5Delete() : 3Display() : 1->2->4->5

PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK1

P1: Write a C program that uses functions to perform the following:

a) Create a singly linked list of even integers between 1 to 100.

b) Delete the nodes which are divisible by 5 from the above linked list.

c) Display the first five contents of the above list after deletion.

Test Case 1: 12 10 15

Result : 12->10

Number not in range or not even

Test Case 2: 10 12 24 34 40 44 66 68

Result:

display: 10 12 24 34 40 44 66 68

delete:10 40

display:12 24 34 44 66 68

P2: Write a C program that uses functions to perform the following:

a) Create a singly linked list of odd integers between 1 to 50.

b) Delete the nodes which are multiples of 3 from the above linked list.

c) Display the last five contents of the above list after deletion.

Test Case 1: 9 11 51

Result : 9->11

Number not in range or not odd

Test Case 2: 9-->11-->13-->17-->19-->23-->25-->

Result:

display: 9-->11-->13-->17-->19-->23-->25-->

deleted: 9

display: 25-> 23-> 19-> 17->13

P3: Write a C program that uses functions to perform the following:

a) Create a singly linked list of prime integers between 10 to 100.

b) Delete the nodes which are less than 30 from the above linked list.

c) Display the nodes from the given position(read position).

Test Case 1: 11->37>41>43>7

Result: 11->37>41>43>

Number not in range or not prime

Test Case 2: 11->37>41>43

Delete: 37>41>43

Display: position 2

41->43

P4: Write a C program that uses functions to perform the following:

a) Create a singly linked list of integers divisible by 5 between 100 to 1000.

b) Delete the nodes which are greater than 500 from the above linked list.

c) Display the nodes from the beginning of list to the given position(read position).

Test Case 1: 600-->700-->500-->400-->300-->24

Result: 600-->700-->500-->400-->300

the number is not divisible by 5 or not in range

Test Case 2: 600-->700-->500-->400-->300

Display :position 3

600-->700-->500

Deleted:600 700

P5: Write a C program that uses functions to perform the following:

a) Create a singly linked list of integers which are multiple of 3 between 1 to 100.

b) Delete the nodes which are multiple of 5 from the above linked list.

c) Display the nodes between 50 to 100 from the above given list.

Test Case 1: 9-->3-->6-->75-->15-->123

Result: the number is not multiple of 3 or not in range

Test Case 2: 9-->3-->6-->75-->15-->123

Display:75->

Delete: 75 15

9-->3-->6

P6: Write a C program that uses functions to perform the following:

a) Create a singly linked list of integers less than 100.

b) Delete the nodes which are greater than 50 from the above linked list.

c) Display the nodes between 75 to 100 from the above given list.

Test Case 1: 45-->23-->56-->89-->99-->

Result: 45-->23-->56-->89-->99--> 123

the number is not less than 100

Test Case 2: 45-->23-->56-->89-->99-->

Display: 89-->99-->

Delete: 56 89 99

45-->23

P7: Write a C program that uses functions to perform the following:

a) Create a singly linked list of integers greater than 100.

b) Delete the nodes which are less than 500 from the above linked list.

c) Display the above given list in ascending order.

Test Case 1: 600-->200-->300-->500-->600-->900-->90

Result: the number is not greater than 100

Test Case 2: 600-->200-->300-->500-->600-->900

Display: data in ascending order is 200 300 500 600 600 900

Delete: 200 300

600-->500-->600-->900

P8: Write a C program that uses functions to perform the following:

a) Create a singly linked list of integers between 100 to 1000.

b) Delete the nodes which are multiple of 10 from the above linked list.

c) Display the nodes which are multiples of 3 from the above given list.

Test Case 1: 345-->315-->310-->200-->100-->1000-->1001

Result: the number is not in range 100-1000

Test Case 2: 345-->315-->310-->200-->100-->1000

Display: 345-->315

Delete: 310 200 100 1000

345-->315

P9: Write a C program that uses functions to perform the following:

a) Create a singly linked list of perfect integers between 1 to 1000.

b) Delete a node from a given position(read position)from the above linked list.

c) Display the above given list in reverse order.

Test Case 1: 6-->28-->496-->455

Result: the number is not in range 1-1000 or not perfect number

Test Case 2: 6-->28-->496

Display: 496 28 6

Delete: position 1

28->6

P10: Write a C program that uses functions to perform the following:

a) Create a singly linked list of squares of integers between 1 to 50.

b) Delete the previous node from a given position(read position)from the above linked list.

c) Display the above given list in descending order.

Test Case 1: 16-->9-->4-->254964

Result:The number not in range 1-50

Test Case 2: 16-->9-->4-->254964

Display: 49 25 16 9 4

Delete: pos 1

Previous element not exist

Pos=2

942549

APPLICATIONS:

Various applications of Singly Linked List are:

A singly-linked list is ideally suited to stacks (last in, first out). The list maintains a link with the head node in the list and each node points to the next node in the list (the last node points to NULL). When a new node is added it is added to the head of the list. Since enqueue and dequeue both occur at the head of the list, processing is always in constant time [O(1)].

VIVA VOICE QUESTIONS

1. The linked list structure is defined as

a. struct nodeb. nodec. struct noded. node

{{{{

int item;int item;int item;int item;

struct node *next;struct node *next;struct node *next;node next;

};};};};

2.Single linked list uses _ _ _ _ _ _ no. of pointers

a. Zerob. onec. Twod. Three

3. No.of pointers to be manipulated in a linked list to insert an item in the middle _ _ _ __ _ _ _

a. Twob. Threec. Oned. Zero

4. Linked lists are not suitable for data structures of which one of the following problem?

a. insertion sortb. Binary searchc. radix sortd. polynomial manipulation problem

5. The linked list field(s) are

a. datab. pointerc. pointer to next noded. data and pointer to next node

Experiment No:2

Experiment as given in the JNTUH curriculum.

Week2: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of integers.

b) Delete a given integer from the above doubly linked list.

c) Display the contents of the above list after deletion.

AIM: To implement doubled linked list of integers in data structure using functions.

THEORY:

A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.

Following is representation of a DLL node in C language.

/* Node of a doubly linked list */

struct node

{

  int data;

  struct node *next; // Pointer to next node in DLL

  struct node *prev; // Pointer to previous node in DLL 

};

Following are advantages/disadvantages of doubly linked list over singly linked list.

Advantages over singly linked list

1) A DLL can be traversed in both forward and backward direction.2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given.In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.

Disadvantages over singly linked list

1) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though.

2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.

InsertionA node can be added in four ways1) At the front of the DLL2) After a given node.3) At the end of the DLL4) Before a given node.

1.Insertion at the front of list

2.Insertion in the middle of the list

3.Insertion at the end of the list

Deletion

Write a function to delete a given node in a doubly linked list.

(a) Original Doubly Linked List

(a) After deletion of head node

(a) After deletion of middle node

(a) After deletion of last node

ALGORITHM :

Initialize the first and last nodes with NULL values

struct node *first=NULL,*last=NULL,*next,*prev,*cur;

1. Algorithm creating a new node:

Step 1: if the list is empty then first==NULL

Step 2: Create a new node

cur=(struct node*) malloc (sizeof (struct node));

Step 3: Read the content of node

Step 4: Assign new node left and right links to NULL

cur->left=NULL;

cur->right=NULL;

Step 5: Assign new node to first & last node

first=cur

last=cur

Step 6: If the list is not empty call insert function

insert ()

Step 7 : Stop

2. Algorithm for Inserting a new node:

Step 1 : Initialize count c to 1

Step 2 : Create inserting node

cur=(struct node*)malloc(sizeof (struct node));

Step 3: Read the content of node

Step 4: Read the position of insertion

Step 5: Inserting in first position

Check if the pos=1 and first!=NULL

cur->right=first;

cur->left=NULL;

first=cur;

Step 6: Inserting in a given position

next=first;

repeat the steps a to c until c < pos

a. prev=next;

b. next=prev->right;

c. c++;

prev->right=cur;

cur->right=next;

Step 7 : Stop

3. Algorithm for Deleting a node:

Step 1 : Initialize count c to 1

Step 2 : Read the position for deletion

Step 3 : Check if first=NULL

print list is empty

Step 4 : If the list contains single element

Check if pos=1 and first->right=NULL

print deleted element is first->data

Step 5 : Assign first to NULL

first=NULL;

Step 6 : If the list contains more than one element and to delete first element

if pos=1 and first->right!=NULL

cur=first;

first=first->right;

cur->right=NULL;

print deleted element is cur->data

free(cur);

Step 7 : If the list contains more than one element and to delete an element at given position

next=first;

repeat the steps a to c until c < pos

a. cur=next;

b. next=next->right;

c. c++;

cur->right=next->right;

next->right=NULL;

next->left=NULL;

print deleted element is next->data

free(next);

Step 8 : Stop

4. Algorithm for Displaying a node:

Step1 : Check if first node is NULL

print list is empty

Step2: If first node is not NULL then

cur=first;

repeat the steps a to b until cur!=NULL

a . print cur->data

b . cur=cur->right;

Step3 : Stop

INPUT & OUTPUT:

Insert() : 10 20 30 40 50Display () : 10->20->30->40->50Delete() : 40Display() : 10->20->30->50

PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK-2

P1: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of even integers between 1 to 100.

b) Delete the nodes which are divisible by 5 from the above linked list.

c) Display the first five contents of the above list after deletion.

Test Case 1: 12 10 15

Result : 12->10

Number not in range or not even

Test Case 2: 10 12 24 34 40 44 66 68

Result:

display: 10 12 24 34 40 44 66 68

delete:10 40

display:12 24 34 44 66 68

P2: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of odd integers between 1 to 50.

b) Delete the nodes which are multiples of 3 from the above linked list.

c) Display the last five contents of the above list after deletion.

Test Case 1: 9 11 51

Result : 9->11

Number not in range or not odd

Test Case 2: 9-->11-->13-->17-->19-->23-->25-->

Result:

display: 9-->11-->13-->17-->19-->23-->25-->

deleted: 9

display: 25-> 23-> 19-> 17->13

P3: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of prime integers between 10 to 100.

b) Delete the nodes which are less than 30 from the above linked list.

c) Display the nodes from the given position(read position).

Test Case 1: 11->37>41>43>7

Result: 11->37>41>43>

Number not in range or not prime

Test Case 2: 11->37>41>43

Delete: 37>41>43

Display: position 2

41->43

P4: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of integers divisible by 5 between 100 to 1000.

b) Delete the nodes which are greater than 500 from the above linked list.

c) Display the nodes from the beginning of list to the given position(read position).

Test Case 1: 600-->700-->500-->400-->300-->24

Result: 600-->700-->500-->400-->300

the number is not divisible by 5 or not in range

Test Case 2: 600-->700-->500-->400-->300

Display :position 3

600-->700-->500

Deleted:600 700

P5: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of integers which are multiple of 3 between 1 to 100.

b) Delete the nodes which are multiple of 5 from the above linked list.

c) Display the nodes between 50 to 100 from the above given list.

Test Case 1: 9-->3-->6-->75-->15-->123

Result: the number is not multiple of 3 or not in range

Test Case 2: 9-->3-->6-->75-->15-->123

Display:75->

Delete: 75 15

9-->3-->6

P6: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of integers less than 100.

b) Delete the nodes which are greater than 50 from the above linked list.

c) Display the nodes between 75 to 100 from the above given list.

Test Case 1: 45-->23-->56-->89-->99-->

Result: 45-->23-->56-->89-->99--> 123

the number is not less than 100

Test Case 2: 45-->23-->56-->89-->99-->

Display: 89-->99-->

Delete: 56 89 99

45-->23

P7: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of integers greater than 100.

b) Delete the nodes which are less than 500 from the above linked list.

c) Display the above given list in ascending order.

Test Case 1: 600-->200-->300-->500-->600-->900-->90

Result: the number is not greater than 100

Test Case 2: 600-->200-->300-->500-->600-->900

Display: data in ascending order is 200 300 500 600 600 900

Delete: 200 300

600-->500-->600-->900

P8: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of integers between 100 to 1000.

b) Delete the nodes which are multiple of 10 from the above linked list.

c) Display the nodes which are multiples of 3 from the above given list.

Test Case 1: 345-->315-->310-->200-->100-->1000-->1001

Result: the number is not in range 100-1000

Test Case 2: 345-->315-->310-->200-->100-->1000

Display: 345-->315

Delete: 310 200 100 1000

345-->315

P9: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of perfect integers between 1 to 1000.

b) Delete a node from a given position(read position)from the above linked list.

c) Display the above given list in reverse order.

Test Case 1: 6-->28-->496-->455

Result: the number is not in range 1-1000 or not perfect number

Test Case 2: 6-->28-->496

Display: 496 28 6

Delete: position 1

28->6

P10: Write a C program that uses functions to perform the following:

a) Create a doubly linked list of squares of integers between 1 to 50.

b) Delete the previous node from a given position(read position)from the above linked list.

c) Display the above given list in descending order.

Test Case 1: 16-->9-->4-->254964

Result:The number not in range 1-50

Test Case 2: 16-->9-->4-->254964

Display: 49 25 16 9 4

Delete: pos 1

Previous element not exist

Pos=2

942549

APPLICATIONS:

1.Applications that have an MRU list (a linked list of file names)

2. The cache in your browser that allows you to hit the BACK button (a linked list of URLs)

3. Undo functionality in Photoshop or Word (a linked list of state)

4. A stack, hash table, and binary tree can be implemented using a doubly linked list.

5. A great way to represent a deck of cards in a game.

VIVA VOICE QUESTIONS

1.In a linked list with n nodes, the time taken to insert an element after an element pointed by

some pointer is

(A) 0 (1) (B) 0 (log n)

(C) 0 (n) (D) 0 (n 1og n)

Ans:A

2.A linear collection of data elements where the linear node is given by means of pointer is

called

(A) linked list (B) node list

(C) primitive list (D) None of these

Ans:A

3. Representation of data structure in memory is known as:

(A) recursive (B) abstract data type

(C) storage structure (D) file structure

Ans:B

Experiment No:3

Experiment as given in the JNTUH curriculum.

Week3: Write a C program that uses stack operations to convert a given infix expression into its postfix Equivalent, Implement the stack using an array.

AIM: To implement stack using array to convert a given infix expression into its postfix Equivalent in data structure using functions.

THEORY:

Infix To Postfix Conversion

Algorithm for converting Infix to Postfix:

· If the input is an operand, then place it into the output buffer.

· If the input is an operator, push it into the stack.

· If the operator in stack has equal or higher precedence than input operator, then pop the operator present in stack and add it to output buffer.

· If the input is an open brace, push it into the stack

· If the input is a close brace, pop elements in stack one by one until we encounter close brace.  Discard braces while writing to output buffer.

Infix Expression:  (a - b) / (c + d)Input: (Input is open brace.  So, place it into the stack.

|      ||  (   |           Output: ------

Input: aPlace the operand in output buffer.

|      ||  (   |          Output:  a ------

Input: -Place the operator into the stack.

|      ||  -   ||  (   |         Output:  a ------

Input: bPlace the operand in output buffer|       ||  -    ||  (    |         Output: ab -------

Input: )Pop '-' and '('

|       ||       ||       |         Output:  ab- -------

Input: /Push the operator into the stack.

|       ||       ||  /    |         Output:  ab- -------

Input: (Push open brace into the stack.

|       ||  (    ||  /    |         Output: ab- -------

Input: cPlace the operand in output buffer.

|       ||  (    ||  /    |         Output:  ab-c -------

Input: +Place the operator into the stack.

|  +   ||  (    ||  /    |         Output:  ab-c -------

Input: dPlace the operand in output buffer.

|  +    ||  (    ||  /    |         Output:  ab-cd -------

Input: )Pop all elements until we get open brace.

|       ||       ||  /    |         Output:  ab-cd+ ------

No more input to parse.  So, pop all other elements from the stack

|       ||       ||       |         Output:  ab-cd+/ ------

ALGORITHM:

1. Algorithm for postfix conversion:

void postfix(char inf[15])

Step 1: repeat the steps a to c until inf[i]!='\0'

a.check if infix expression is opening brace push it onto stack

check if(inf[i]='(')

push(inf[i])

b. check if infix expression is alphabetic character add it to postfix expression

if(isalpha(inf[i]))

pf[j++]=inf[i]

c. check if infix expression is closing brace

if(inf[i]==')')

repeat 1 to 2 until stack[top]!=opening brace

1.pf[j++]=pop()

2.x=pop()

Otherwise

repeat 1 to 2 until prec(stack[top])>=prec(inf[i])

1.pf[j++]=pop()

2.push(inf[i])

Step 2:repeat the steps a to b until top!=-1

a.pf[j++]=pop()

b.pf[j]='\0'

step 3: stop

2. Algorithm for push operation on stack:

Step 1:increment top by 1

Step 2: stack[++top]=y

Step 3:stop

3. Algorithm for pop operation on stack:

Step 1:return stack[top]

Step 2: decrement top by 1

Step 3:stop

4. Algorithm for finding precedence of operators :

Step 1:check if (c=='*' || c=='/' || c=='%')

return(5)

step 2:check if (c=='+' || c=='-')

return(3)

Step 3:if(c=='(')

return(1)

step 4:stop

5. Algorithm for main function :

Step 1:start

Step 2: read infix expression

Step 3:call postfix(infix)

Step 4:print postfix expression result

Step 5:stop

INPUT: a+b*c

OUTPUT: abc*+

PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK 3

P1: Write a C program that uses stack operations to convert a given infix expression having + and - operators into its postfix Equivalent, Implement the stack using an array. (ex: a+b-c)

Test case 1: (a+b-c)

Result: ab+c-

Test case 2: a+(b-c)

Result: abc-+

P2: Write a C program that uses stack operations to convert a given infix expression having + and * operators into its postfix Equivalent, Implement the stack using an array. (ex: a+b*c)

Test case 1: (a+b*c)

Result: abc*+

Test case 2: (a+b)*c

Result: ab+c*

P3: Write a C program that uses stack operations to convert a given infix expression having *,% and / operators into its postfix Equivalent, Implement the stack using an array.

(ex: a*b%d/c)

Test case 1: (a*b%d/c)

Result: ab*d%c/

Test case 2: (a*(b%d)/c)

Result: abd%*c/

P4: Write a C program that uses stack operations to convert a given infix expression having /

and % operators into its postfix Equivalent, Implement the stack using an array. (ex: a/b%c)

Test case 1: (a/b%c)

Result: ab/c%

Test case 2: a/(b%c)

Result: abc%/

P5: Write a C program that uses stack operations to convert a given infix expression having && and + operators into its postfix Equivalent, Implement the stack using an array. (ex: a+b && c)

Test case 1: (a+b & c)

Result: abc&+

Test case 2: (a+b)&c

Result: ab+c&

P6: Write a C program that uses stack operations to convert a given infix expression having ^ and * operators into its postfix Equivalent, Implement the stack using an array.

(ex: a^b*c )

Test case 1: (a^b * c)

Result: abc*^

Test case 2: (a^b)*c

Result: ab^c*

P7: Write a C program that uses stack operations to convert a given infix expression having < and + operators into its postfix Equivalent, Implement the stack using an array.

(ex: a < b + c)

Test case 1: (a

Result: abc+<

Test case 2: (a

Result: ab

P8: Write a C program that uses stack operations to convert a given infix expression having ! and + operators into its postfix Equivalent, Implement the stack using an array.

(ex: !a + b + c)

Test case 1: (!a+b+ c)

Result: a!b+c+

Test case 2: !a+(b+c)

Result: a!bc++

P9: Write a C program that uses stack operations to convert a given infix expression having << and * operators into its postfix Equivalent, Implement the stack using an array.

(ex: a & b * c)

Test case 1: (a&b* c)

Result: ab&c*

Test case 2: a&(b*c)

Result: abc*&

P10: Write a C program that uses stack operations to convert a given infix expression having == and / operators into its postfix Equivalent, Implement the stack using an array. (ex: a = b / c)

Test case 1: (a=b/ c)

Result: abc/=

Test case 2: (a=b)/c)

Result: ab=c/

APPLICATIONS:

1. infix notation is easy to read for humans, whereas pre-/postfix notation is easier to parse for a machine. The big advantage in pre-/postfix notation is that there never arise any questions like operator precedence.

2. Postfix notation, also known as RPN, is very easy to process left-to-right. An operand is pushed onto a stack; an operator pops its operand(s) from the stack and pushes the result. Little or no parsing is necessary. It's used by Forth and by some calculators (HP calculators are noted for using RPN).

VIVA VOICE QUESTIONS:

1. The equivalent of (a+b↑c↑d)*(e+f/d) in the post fix notation is

a. ab+c↑d↑e &fd/b. abcd↑+↑efd/+*c. abcdefd/+*↑↑+d. abcd↑↑+efd/+*

2. The infix form of the postfix expression ABC-/D*E+ is

a. A/B-C*D+Eb. A-B/C*D+Ec. (A-B)/C*D+Ed. A/(B-C)*D+E

3. The postfix expression for the infix expression A/B*C+D*E is

a. AB/C*DE*+b. ABC/*DE+*c. ABCD/*E+*d. ABC*/D*E+

4. The prefix expression for the infix expressionA/B*C+D*E is

a. AB/C*DE*+b. +*/ABC*DEc. +*AB/C*DEd. /+ABCDE

5. Suffix expression is

a. Infixb. postfixc. prefixd. post & prefix

6. polish expression is

a. infixb. postfixc. prefixd. post & prefix

Experiment No:4

Experiment as given in the JNTUH curriculum.

Week 4 : Write C programs to implement a double ended queue ADT of integers using

i) Array and ii) Doubly linked list respectively.

AIM: To implement a double ended queue ADT of integers using array and doubly linked list in data structure using functions.

THEORY:

A double-ended queue is an abstract data type similar to an simple queue, it allows you to insert and delete from both sides means items can be added or deleted from the front or rear end.

Dequeue is also called as double ended queue and it allows user to perform insertion and deletion from the front and rear position.  And it can be easily implemented using doubly linked list.  On creating dequeue, we need to add two special nodes at the ends of the doubly linked list(head and tail). And these two nodes needs to be linked between each other(initially).

          head                                                     tail      ---------------------------                --------------------------------     |         |        |        ---|---------->|           |           |            |     | NULL |   0   |            |              |           |   0      |  NULL   |     |         |        |            |<----------|---       |           |            |     ----------------------------                -------------------------------

So, the header node goes before the first node of the queue and the trailer node goes after the last node in the queue.  To do insertion at the front position, place the new node next to the head.  And to do insertion at the rear position, place the new node before the tail.  Similarly, dequeue operation can also be performed at the front and rear positions.

Operations

1. Enter New Element From Right

2. Enter New Element From Left

3. Remove Element From Right

4. Remove Element From Left

4a) AIM: To implement a double ended queue ADT of integers using array

ALGORITHM:

1. Algorithm for insertion at rear end:

Step 1: check if(front==-1)

front++

rear++

read the element to insert a[rear]

count++;

Step 2: check if(rear>=SIZE-1)

print Insertion is not possible, overflow!!!

return

Step 3: Increment the rear by 1

read the element to insert a[rear]

count++;

Step 4:stop

2. Algorithm for insertion at front end:

Step 1: check if(front==-1)

front++

rear++

read the element to insert a[rear]

count++

Step 2: check if(rear>=SIZE-1)

print Insertion is not possible, overflow!!!

return

Step 3: Repeat the steps a to d until i>0

a. a[i]=a[i-1]

b. read the element to insert a[i]

c. count++

d. rear++;

Step 4:stop

3. Algorithm for deletion at rear end:

Step 1: check if (front==-1)

Print Deletion is not possible: Dequeue is empty

return

Step 2: otherwise

Print The deleted element is a[rear]

Check if(front==rear)

front=rear=-1

otherwise

rear=rear-1

Step 3:stop

4. Algorithm for deletion at front end:

1: check if (front==-1)

Print Deletion is not possible: Dequeue is empty

return

Step 2: otherwise

Print The deleted element is a[front]

Check if(front==rear)

front=rear=-1

otherwise

front=front-1

Step 3:stop

5.Algorithm for displaying the dequeue:

Step 1: check if (front==-1)

Print Dequeue is empty

return

Step 2: otherwise

Repeat step a until i<=rear (i=front)

Print a[i]

Step 3:stop

INPUT & OUTPUT:

enqueue: front 400 300 200 150

Enqueue rear: 200 500 600 700

Display: 400 300 200 150 200 500 600 700

Dequeue rear:700 600 500 200

Dequeue front:200 500 600 700

4b) AIM: To implement a double ended queue ADT of integers using Doubly linked list.

ALGORITHM:

Initialize the front and rear nodes with NULL values

struct node *front=NULL,*rear=NULL,*cur,*temp

1. Algorithm for insertion at rear end:

Step 1: Create a new node

cur=(struct node*) malloc (sizeof (struct node))

Step 2: Read the content of node (cur->data)

Step 3: Assign new node right link to NULL

cur->right=NULL

Step 4: Assign new node to front & rear node

Check if(rear==NULL&&front==NULL)

rear=front=cur

Step 5: otherwise

rear->right=cur

cur->left=rear

rear=cur

Step 6:stop

2. Algorithm for insertion at front end:

Step 1: Create a new node

cur=(struct node*) malloc (sizeof (struct node))

Step 2: Read the content of node (cur->data)

Step 3: Assign new node right link to NULL

cur->right=NULL

Step 4: Assign new node to front & rear node

Check if(rear==NULL&&front==NULL)

rear=front=cur

Step 5: otherwise

cur->right=front

front->left=cur

front=cur

Step 6:stop

3. Algorithm for deletion at front end:

Step 1: check if(front==NULL)

Print deQueue is empty

Step 2: otherwise

Check if(front==rear)

Print Deleted data is front->data

front=rear=NULL

Step 3: otherwise

temp=front;

print Deleted data is temp->data

front=front->right

front->left=NULL

free(temp)

Step 4: stop

4. Algorithm for deletion at rear end:

Step 1: check if(front==NULL)

Print deQueue is empty

Step 2: otherwise

Check if(front==rear)

Print Deleted data is rear->data

front=rear=NULL

Step 3: otherwise

temp=rear;

print Deleted data is temp->data

if(front==rear)

front=rear=NULL

rear=rear->left

rear->right=NULL

free(temp)

Step 4: stop

5.Algorithm for displaying the dequeue:

Step 1: check if(front==NULL)

Print deQueue is empty

Step 2: otherwise

temp=front;

repeat steps a-b until temp!= NULL

a. printf("<-%d ->",temp->data)

b. temp=temp->right;

Step 3: stop

INPUT & OUTPUT

: enqueue: front<-400 -><-300 -><-200 -><-150 ->

Enqueue rear: <-200 -><-500 -><-600 -><-700->

Display: <-400 -><-300 -><-200 -><-150 -><-200 -><-500 -><-600 -><-700->

Dequeue rear:700 600 500 200

Dequeue front:200 500 600 700

PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK 4

P1: Write C programs to implement a double ended queue ADT of even integers between 1 to 100 using array.

Test case 1: 12 10 8 2 4 6 7

Result: the number is not even or not in range

12 10 8 2 4 6 8

Result: Queue overflow

Test case 2: 12 10 8 2 4 6

Result: Delete rear 6 4 2

Delete front 12 10 8

P2: Write C programs to implement a double ended queue ADT of odd integers between 10 to 100 using doubly linked list.

Test case 1: 19 17 15 13 25 27 29 8

Result: the number is not even or not in range

19 17 15 13 25 27 29

Test case 2: 19 17 15 13 25 27 29

Result: Delete rear 29 27 25 13 15 17 19

Delete front dequeue empty

P3: Write C programs to implement a double ended queue ADT of prime integers between 1 to 50 using array.

Test case 1: 19 17 13 23 29 43 25

Result: the number is not prime or not in range

19 17 13 23 29 43 47

Result: Queue overflow

Test case 2: 19 17 13 23 29 43 25

Result: Delete rear 25 43 29

Delete front 19 17 13

P4: Write C programs to implement a double ended queue ADT of integers divisible by 5 between 100 to 1000 using doubly linked list

Test case 1: <-400 -><-300 -><-200 -><-150 -><-200 -><-500 -><-600 -><-700 ->20

Result: the number is not divisible by 5 or not in range 100 to 1000

Test case 2: :<-400 -><-300 -><-200 -><-150 -><-200 -><-500 -><-600 -><-700 ->

Result: Delete rear 700 600 500 200 150

Delete front 400 300 200

Queue underflow

P5: Write C programs to implement a double ended queue ADT of integers which are multiples of 10 between 1 to 1000 using array.

Test case 1: 600 500 400 100 200 300 1200

Result: the number is not multiple of 10 or not in range

600 500 400 100 200 300 700

Result: Queue overflow

Test case 2: 600 500 400 100 200 300

Result: Delete rear 300 200 100

Delete front 600 500 400

P6: Write C programs to implement a double ended queue ADT of integers less than 100 using doubly linked list.

Test case 1: <-36 -><--1 -><-42 -><-85 -><-100 -><-45 -><-56 ->101

Result: the number is greater than100

<-36 -><--1 -><-42 -><-85 -><-100 -><-45 -><-56 ->

Test case 2: <-36 -><--1 -><-42 -><-85 -><-100 -><-45 -><-56 ->

Result: Delete front 36 -1 42 85 100 45 56

Delete rear dequeue underflow

P7: Write C programs to implement a double ended queue ADT of integers greater than 500 using array.

Test case 1: 850 733 600 400 550 525 300

Result: the number is not less than 500

850 733 600 400 550 525 900

Result: Queue overflow

Test case 2: 850 733 600 400 550 525

Result: Delete rear 525 550 400 600 733 850

Delete front dequeue empty

P8: Write C programs to implement a double ended queue ADT of integers between 500 and 1000 using doubly linked list

Test case 1: <-700 -><-600 -><-500 -><-800 -><-900 -><-1000 ->1100

Result: the number is not between 500-1000

<-700 -><-600 -><-500 -><-800 -><-900 -><-1000 ->

Test case 2: <-700 -><-600 -><-500 -><-800 -><-900 -><-1000 ->

Result: Delete front 700 600

Delete rear 1000 900 800 500

dequeue empty

P9: Write C programs to implement a double ended queue ADT of perfect integers between 1 and 1000 using array.

Test case 1: 496 6 28 24

Result: the number is not perfect number or not in range 1-1000

496 6 28

Test case 2: 496 6 28

Result: Delete front 496 6 28

Delete rear queue empty

P10: Write C programs to implement a double ended queue ADT of squares of integers between 1 and 50 using doubly linked list.

Test case 1: <-9 -><-4 -><-1 -><-16 -><-25 -><-36 -><-49 ->64

Result: the number is not in range between 1-50

<-9 -><-4 -><-1 -><-16 -><-25 -><-36 -><-49 ->

Test case 2:delete rear 49 36 25 16 1

Delete front 9 4

Dequeue empty

APPLICATIONS:

1. When modeling any kind of real-world waiting line: entities (bits, people, cars, words, particles, whatever) arrive with a certain frequency to the end of the line and are serviced at a different frequency at the beginning of the line.

2. To implements task scheduling for several processors.

3. The steal-job scheduling algorithm is used by Intel's Threading Building Blocks (TBB) library for parallel programming uses deque.

VIVA VOICE QUESTIONS:

1. To simulate people waiting in a line, which data structure would you use?

a)Vectorb)DeQueuec)Stackd)Sete) List

2. To implement which data structure can be used?

a)array b)SLL c)DLL D)none

3.dequeue rear operation is similar to

a)push()b)pop()c)enqueue ()d)none

Experiment No:5

Experiment as given in the JNTUH curriculum.

Week 5:Write a C program that uses functions to perform the following:

a) Create a binary search tree of characters.

b) Traverse the above Binary search tree recursively in Postorder.

AIM: To implement a binary search tree of characters in data structure using functions.

THEORY:

A binary search tree is a tree where each node has a left and right child. Either child, or both children, may be missing. Figure 3-2 illustrates a binary search tree. Assuming k represents the value of a given node, then a binary search tree also has the following property: all children to the left of the node have values smaller than k, and all children to the right of the node have values larger than k. The top of a tree is known as the root, and the exposed nodes at the bottom are known as leaves.

Insertion in Binary Search Tree:

· Check whether root node is present or not(tree available or not).  If root is NULL, create root node.

· If the element to be inserted is less than the element present in the root node, traverse the left sub-tree recursively until we reach T->left/T->right is NULL and place the new node at T->left(key in new node < key in T)/T->right (key in new node > key in T).

· If the element to be inserted is greater than the element present in root node, traverse the right sub-tree recursively until we reach T->left/T->right is NULL and place the new node at T->left/T->right.

Binary search tree deletion

There are three different cases that needs to be considered for deleting a node from binary search tree.

case 1: Node with no children (or) leaf nodecase 2: Node with one childcase 3: Node with two children.

Binary search tree searching

Binary search tree is a tree in which the key value of left child is less than the parent node and the key value in right child is greater than the root.

· Check whether the root node is NULL or not.  If it's NULL, there is no tree available.  So, return NULL.

· If node T has the search element, then return that node.

· If the search element is less than the key in node T, recursively search the left sub tree of T.

· If the search element is greater than the key in node T, recursively search the right sub tree of T.

THEORY: Tree Traversals

Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Following are the generally used ways for traversing trees.

Tree Traversals:(a) Inorder(b) Preorder(c) Postorder

Inorder Traversal:

Algorithm Inorder(tree)

1. Traverse the left subtree, i.e., call Inorder(left-subtree) 2. Visit the root. 3. Traverse the right subtree, i.e., call Inorder(right-subtree)

Uses of Inorder

In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder itraversal s reversed, can be used.

Preorder Traversal:

Algorithm Preorder(tree) 1. Visit the root. 2. Traverse the left subtree, i.e., call Preorder(left-subtree) 3. Traverse the right subtree, i.e., call Preorder(right-subtree)

Uses of Preorder

Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree.

Postorder Traversal:

Algorithm Postorder(tree) 1. Traverse the left subtree, i.e., call Postorder(left-subtree) 2. Traverse the right subtree, i.e., call Postorder(right-subtree) 3. Visit the root.

Uses of Postorder

Postorder traversal is used to delete the tree. Postorder traversal is also useful to get the postfix expression of an expression tree.

ALGORITHM:

Step 1.declare a structure

struct BST

{

char data;

struct BST *left,*right;

}node;

Step 2: initialize binay search tree root to NULL

struct BST* root=NULL,*temp,*cur;

1. Algorithm for creating a binary search tree:

Step 1: Start

Step 2 :Declare c[10]

Step 3: Assign root to temp

temp=root;

Step 4 :Create a new node

cur=(struct BST*)malloc(sizeof(struct BST))

step 5: Read the content of node

Read c

Step 6: Initialize

cur->data=c[0]

assign left and right links to NULL

cur->left=NULL

cur->right=NULL

Step 7: Check the temp is NULL then assign cur to root

if(temp==NULL)

root=cur;

Step 8: otherwise

repeat the steps upto 9-11 until temp!=NULL

Step 9 : Check the cur->data less than temp->data

if((cur->data))<(temp->data))

Step 10: check the temp->left equal to NULL

if(temp->left==NULL)

temp->left=cur;

return

otherwise

temp=temp->left;

Step11: otherwise check temp right equal to null

if(temp->right==NULL)

temp->right=cur;

return;

otherwise

temp=temp->right;

Step 12: stop

2. Algorithm for post order traversal of a binary search tree:

Algortihm postorder(struct BST *temp)

Step 1: Start

Step 2: check the temp not equal to null

temp!=NULL

Step 3: call to postorder with temp->left

postorder(temp->left);

Step 4: call to postorder with temp->right

postorder(temp->right);

Step 5: print temp->data

Step 6: stop

INPUT & OUTPUT:

A B C

C B A

PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK 5

P1: Write a C program that uses functions to perform the following:

a) Create a binary search tree of capital alphabets.

b) Traverse the above Binary search tree recursively in Preorder.

Test case 1: A B C a

Result: A B C

lower case letters are not allowed

Test case 2: D C E B F

Result: D C E B F

P2: Write a C program that uses functions to perform the following:

a) Create a binary search tree of small case alphabets.

b) Traverse the above Binary search tree recursively in inorder.

Test case 1: a b c A

Result: a b c

upper case letters are not allowed

Test case 2: d c e b f

Result: b c d e f

P3: Write a C program that uses functions to perform the following:

a) Create a binary search tree of lower case vowels.

b) Traverse the above Binary search tree recursively in Postorder.

Test case 1: i o e u a x

Result: i o e u a

consonant letters are not allowed

Test case 2: i o e u a

Result: a e u o i

P4: Write a C program that uses functions to perform the following:

a) Create a binary search tree of consonants.

b) Traverse the above Binary search tree recursively in converse Postorder.

Test case 1: a

Result: vowels are not allowed

d c f b g

Test case 2: d c f b g

Result: g f b c d

P5: Write a C program that uses functions to perform the following:

a) Create a binary search tree of upper case vowels symbols.

b) Traverse the above Binary search tree recursively in converse Preorder.

Test case 1: a

Result: small case vowels are not allowed

I E O A U

Test case 2: I E O A U

Result: I O U E A

P6: Write a C program that uses functions to perform the following:

a) Create a binary search tree of capital alphabets and operators(+,*,/).

b) Traverse the above Binary search tree recursively in converse Inorder.

Test case 1: A + B - * a

Result: small case letters are not allowed

A + B - *

Test case 2: A + B - *

Result: B A - + *

P7: Write a C program that uses functions to perform the following:

a) Create a binary search tree of small alphabets and operators(%,-,*).

b) Traverse the above Binary search tree recursively in preorder.

Test case 1: a % - b * A

Result: Upper case letters are not allowed

a % - * b

Test case 2: a % - b *

Result: a % - * b

P8: Write a C program that uses functions to perform the following:

a) Create a binary search tree for a given infix expression.(a+b*c)

b) Traverse a specific node of the above Binary search tree recursively in Inorder.

Test case 1: a + b * c A

Result: Upper case letters are not allowed

Test case 2: a + b * c

Result: * + a b c

P9: Write a C program that uses functions to perform the following:

a) Create a binary search tree for a given prefix expression.(-a/bc)

b) Traverse a specific node of the above Binary search tree recursively in preorder.

Test case 1: - a / b c B

Result: Upper case letters are not allowed

Test case 2: - a / b c

Result: - a / b c

P10: Write a C program that uses functions to perform the following:

a) Create a binary search tree for a given postfix expression.(a*b% c)

b) Traverse a specific node of the above Binary search tree recursively in postorder.

Test case 1: a * b % c C

Result: Upper case letters are not allowed

Test case 2: a * b % c

Result: % * c b a

APPLICATIONS:

1. Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries.

2. Binary Space Partition - Used in almost every 3D video game to determine what objects need to be rendered.

3. Binary Tries - Used in almost every high-bandwidth router for storing router-tables.

4. Hash Trees - used in p2p programs and specialized image-signatures in which a hash needs to be verified, but the whole file is not available.

5. Heaps - Used in implementing efficient priority-queues, which in turn are used for scheduling processes in many operating systems, Quality-of-Service in routers, and A* (path-finding algorithm used in AI applications, including robotics and video games). Also used in heap-sort.

6. Huffman Coding Tree - used in compression algorithms, such as those used by the .jpeg and .mp3 file-formats.

.

VIVA VOICE QUESTIONS:

1.Every binary tree with n elements have _ no of edges

a)nb)n-1c)n-2d)n+1

2.Define inorder traversal

a)left,root,rightb)root,left,rightc)left,right,rootd)none

3. Define postorder traversal

a)left,root,rightb)root,left,rightc)left,right,rootd)none

4. Define converse postorder traversal

a)left,root,rightb)root,left,rightc)right,left,rootd)none

Experiment No:6

Experiment as given in the JNTUH curriculum.

Week 6:Write a C program that uses functions to perform the following:

a) Create a binary search tree of integers.

b) Traverse the above Binary search tree non recursively in inorder.

AIM: To implement a binary search tree of integers in data structure using functions.

THEORY

A binary search tree is a tree where each node has a left and right child. Either child, or both children, may be missing. Figure 3-2 illustrates a binary search tree. Assuming k represents the value of a given node, then a binary search tree also has the following property: all children to the left of the node have values smaller than k, and all children to the right of the node have values larger than k. The top of a tree is known as the root, and the exposed nodes at the bottom are known as leaves.

Insertion in Binary Search Tree:

· Check whether root node is present or not(tree available or not).  If root is NULL, create root node.

· If the element to be inserted is less than the element present in the root node, traverse the left sub-tree recursively until we reach T->left/T->right is NULL and place the new node at T->left(key in new node < key in T)/T->right (key in new node > key in T).

· If the element to be inserted is greater than the element present in root node, traverse the right sub-tree recursively until we reach T->left/T->right is NULL and place the new node at T->left/T->right.

Binary search tree deletion

There are three different cases that needs to be considered for deleting a node from binary search tree.

case 1: Node with no children (or) leaf nodecase 2: Node with one childcase 3: Node with two children.

Binary search tree searching

Binary search tree is a tree in which the key value of left child is less than the parent node and the key value in right child is greater than the root.

· Check whether the root node is NULL or not.  If it's NULL, there is no tree available.  So, return NULL.

· If node T has the search element, then return that node.

· If the search element is less than the key in node T, recursively search the left sub tree of T.

· If the search element is greater than the key in node T, recursively search the right sub tree of T.

THEORY: Tree Traversals

Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Following are the generally used ways for traversing trees.

Tree Traversals:(a) Inorder(b) Preorder(c) Postorder

Inorder Traversal:

Algorithm Inorder(tree)

1. Traverse the left subtree, i.e., call Inorder(left-subtree) 2. Visit the root. 3. Traverse the right subtree, i.e., call Inorder(right-subtree)

Uses of Inorder

In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder itraversal s reversed, can be used.

Preorder Traversal:

Algorithm Preorder(tree) 1. Visit the root. 2. Traverse the left subtree, i.e., call Preorder(left-subtree) 3. Traverse the right subtree, i.e., call Preorder(right-subtree)

Uses of Preorder

Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree.

Postorder Traversal:

Algorithm Postorder(tree) 1. Traverse the left subtree, i.e., call Postorder(left-subtree) 2. Traverse the right subtree, i.e., call Postorder(right-subtree) 3. Visit the root.

Uses of Postorder

Postorder traversal is used to delete the tree. Postorder traversal is also useful to get the postfix expression of an expression tree.

ALGORITHM

Step 1.declare a structure

struct BST

{

char data;

struct BST *left,*right;

}node;

Step 2: initialize binay search tree root to NULL

struct BST* root=NULL,*temp,*cur;

1. Algorithm for creating a binary search tree:

Step 1: Start

Step 2 :Declare c[10]

Step 3: Assign root to temp

temp=root;

Step 4 :Create a new node

cur=(struct BST*)malloc(sizeof(struct BST))

step 5: Read the content of node

read cur->data

Step 6: Assign left and right links to NULL

cur->left=NULL

cur->right=NULL

Step 7: Check the temp is NULL then assign cur to root

if(temp==NULL)

root=cur;

Step 8: otherwise

repeat the steps upto 9-11 until temp!=NULL

Step 9 : Check the cur->data less than temp->data

if((cur->data))<(temp->data))

Step 10: check the temp->left equal to NULL

if(temp->left==NULL)

temp->left=cur;

return

otherwise

temp=temp->left;

Step11: otherwise check temp right equal to null

if(temp->right==NULL)

temp->right=cur;

return;

otherwise

temp=temp->right;

Step 12: stop

2. Algorithm for In order traversal of a binary search tree:

void inorder(struct BST *r)

Function receives the root not node in r

Step 1: Initialize top=0;

Create a stack structure

struct BST *s[20],*pt=r;

initialize with stack [0] with NULL

Step 2: repeat the steps upto a-b until pt!=NULL

increment top by 1

a. s[++top] = pt;

b. pt = pt->left;

Step 3: when pt is NULL assign pt = s[top--];

Step 4: repeat the steps upto a-c until pt!=NULL

a. print pt->data);

b. check if(pt->right != NULL)

pt = pt->right;

repeat the steps upto 1-2 until pt!=NULL

1. s[++top] = pt;

2. pt = pt->left;

c. pt = s[top--];

Step 5: stop

INPUT : 2010305152535

OUTPUT: 5 10 15 2025 30 35

PROBLEM DEFINITIONS DIFFERENT FROM JNTU TOPICS FOR WEEK 6

P1: Write a C program that uses functions to perform the following:

a) Create a binary search tree of even integers from 1 to 1000

b) Traverse the above Binary search tree non recursively in Preorder.

Test case 1: 20 10 30 5

Result: the number is not even or not in range 1-1000

Test case 2: 20 10 30 6 12 24 36

Result: 20 10 60 12 30 24 36

P2: Write a C program that uses functions to perform the following:

a) Create a binary search tree of odd integers from 1 to 500.

b) Traverse the above Binary search tree non recursively in inorder.

Test case 1: 30 35

Result: the number is not odd or not in range 1-500

Test case 2: 35 25 45 17 27 37 47

Result: 17 25 27 35 37 45 47

P3: Write a C program that uses functions to perform the following:

a) Create a binary search tree of prime integers from 1 to 100

b) Traverse the above Binary search tree non recursively in Postorder.

Test case 1: 47 23 53 59 24

Result: the number is not prime or not in range 1-100

Test case 2: 47 43 53 23 59

Result: 23 43 59 53 47

P4: Write a C program that uses functions to perform the following:

a) Create a binary search tree of integers less than 500.

b) Traverse the above Binary search tree non recursively in converse Preorder.

Test case 1: 200 300 400 450 600

Result: the number is not in range 1-500

200 300 400 450

Test case 2: 300 200400 100 450

Result: 300 450 400 100 200

P5: Write a C program that uses functions to perform the following:

a) Create a binary search tree of integers greater than 200.

b) Traverse the above Binary search tree non recursively in converse Postorder.

Test case 1: 250 300 400 450 100

Result: the number is not greater than200

250 300 400 450

Test case 2: 300 250400 100 450

Result: 450 400 100 250 300

P6: Write a C program that uses functions to perform the following:

a) Create a binary search tree of integers between 200 and 500.

b) Traverse the above Binary search tree non recursively in converse Inorder.

Test case 1: 250 300 400 450 600

Result: the number is not in range 200-500

250 300 400 450

Test case 2: 300 250400 100 450

Result: 450 400 300 100 250

P7: Write a C program that uses functions to perform the following:

a) Create a binary search tree of integers which are divisible by 3 between 1-100

b) Traverse the above Binary search tree non recursively converse preorder.

Test case 1: 3 6 9 12 150

Result: the number is not in range 1-100

3 6 9 12

Test case 2: 30 15 45 6 18 42 48

Result: 30 45 48 42 15 18 6

P8: Write a C program that uses functions to perform the following:

a) Create a binary search tree of integers which are multiples of 6 between 1-100.

b) Traverse a specific node of the above Binary search tree non recursively in Inorder.

Test case 1: 6 12 18 24 30 36 5

Result: the number is not multiple of 5 or not in range 1-100

Test case 2: 6 12 18 24 30 36 40

Result: 6 12 18 24 30 36 40

P9: Write a C program that uses functions to perform the following:

a) Create a binary search tree of perfect integers between 1 and 100.

b) Traverse a specific node of the above Binary search tree non recursively in preorder.

Test case 1: 6 28 496 10

Result: the number is not perfect integer or not in range 1-1000

Test case 2: 6 28 496

Result: 6 28496

P10: Write a C program that uses functions to perform the following:

a) Create a binary search tree of square of integers between 1 and 50.

b) Traverse a specific node of the above Binary search tree non recursively in postorder.

Test case 1: 1 4 9 16 25 36 64

Result: the number is not in range 1-50

Test case 2: 1 4 9 16 25 36

Result: 36 25 16 9 4 1

APPLICATIONS:

1. Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries.

2. Binary Space Partition - Used in almost every 3D video game to determine what objects need to be rendered.

3. Binary Tries - Used in almost every high-bandwidth router for storing router-tables.

4. Hash Trees - used in p2p programs and specialized image-signatures in which a hash needs to be verified, but the whole file is not available.

5. Heaps - Used in implementing efficient priority-queues, which in turn are used for scheduling processes in many operating systems, Quality-of-Service in routers, and A* (path-finding algorithm used in AI applications, including robotics and video games). Also used in heap-sort.

6. Huffman Coding Tree - used in compression algorithms, such as those used by the .jpeg and .mp3 file-formats.

VIVA VOICE QUESTIONS:

1.Define postorder traversal

a)left,root,rightb)root,left,rightc)left,right,rootd)none

2. Define preorder traversal

a)left,root,rightb)root,left,rightc)left,right,rootd)none

3. Define converse Inorder traversal

a)right,root,leftb)root,right,leftc)right,left,rootd)none

3. Define converse preorder traversal

a)right,root,leftb)root,right,leftc)right,left,rootd)none

22 | Page