You are on page 1of 4

School of Computer Science, University of Windsor

COMP 2540: Data Structures and Algorithms


Term: Summer 2022
Instructor: Dr. Asish Mukhopadhyay

Lab 8
Posted: 10 July, 2022
Due: 11:59 pm, 16 July, 2022

Instructions:

ˆ You are expected to finish the lab by the end of the posted date. Submissions beyond the due
date will earn a penalty of n ∗ 25%, where n = submissionDay − dueDay. Thus if the lab is
due Tuesday and you submit on Wednesday, this will be considered a day late.
ˆ Whether or not you finish your work during the lab hour you will have to upload your work on
BLACKBOARD for record-keeping and grading before the beginning of the next lab. Create
a script file as follows:

1. script LabName.txt
2. cat LabName.c
3. cat input.txt
4. cc labName.c
5. ./a.out < input.txt
6. ls -l
7. exit (DO NOT FORGET THIS STEP!!)

ˆ There will be no make-up for missed labs. If you have missed a lab for truly extenuating
circumstances (like illness or family emergency) I will consider allowing you to make a late
submission. However, I need to be informed by email about this on the day of the missed lab.
The email should include your name and SID.

Problems:

1. Consider the problem of deciding if there exists an integer i such that A[i] = i in a strictly
increasing array A[0..n − 1] of n integers.

1
Clearly, we can check for the existence of such an integer i by a sequential search through the
array. This takes O(n) time in the worst case (Why ?).
The idea of solving the problem by binary search in O(log n) time (in the worst case) is based
on the observation that B[i] = A[i] − i, i = 0, 1, .., n − 1 is a non-decreasing sequence of
integers (Can you see why ?). All that needs to be done now is to use binary search to check
if 0 occurs in the array B[0..n − 1].
Implement the above algorithm in C. Take the size n of the array as input from the user,
create a strictly increasing random array of integers of this size and search for an i in this
array such that A[i] = i. (5 points)
2. Implement in C an algorithm that traverses a binary tree, T , in preorder and computes (a)
the number of leaves in T (b) the number of nodes in T with only one child and (c) the
number of nodes in T that contain exactly two children.
This problem requires that you first build a binary tree (in fact, a binary search tree). Do
so by repeated insertion of a key into an initially empty binary tree, following insertion rules
that make the binary tree a binary search tree. Please look up the slides of Lec 10 for the
definition of a binary search tree.
For this problem, you also need to review the topics of structures and pointers from your
COMP 1410 course material. (5 points)

To aid you further in completing the two problems, here is a framework that you might want to
follow to write complete and correct programs:

//includes

//function prototype

int listSearch(int a[], int , int );

int main(void){
int listSize,....

// read in the size of list to be created and allocate store

int *list = (int *)calloc(listSize, sizeof(int));

// populate array

// The first element of the list has to be set carefully to avoid generating
// lists that do not have an i such that list[i] = i.
// So, you might try to assign a value to the first element of your list this way:

srand(time(NULL));
list[0] = pow(-1, rand())* (rand()% 50);

2
// populate the rest of the list so that it is strictly increasing

// modify list by subtracting i from each A[i], as discussed above

// call listSearch with this modified list with x = 0

} // end of main

int listSearch(int a[], int n, int x){// search for x in a sorted list of n elements

// write code for binary search

// return -1 if x not found


} // end of listSearch

This is a suggested framework for the second problem.

#includes

#define SIZE 10 // this defines the number of keys in your binary search tree

struct tnode {
int key;
struct tnode *left;
struct tnode *right;
};

//global variables
int numberOfFullNodes = 0; // full nodes have two children
int numberOfPartialNodes = 0; // partial nodes have one chile
int numberOfLeaves = 0; // leaf nodes have no chilfren

//function prototypes
struct tnode *addtree(struct tnode *, int );
struct tnode *talloc(void);
void preOrderCount(struct tnode *);

int main(void)
{

// create a file, say "inputList", and populate it with it positive integer values
// these will be key-vlues to be inserted into an initially empty binary tree

3
// get key-values from the file created and insert into binary tree

fPtr = fopen("inputList", "r");


for (i = 0; i < SIZE ; i++){
fscanf(fPtr, "%d", &number);
// fPtr is the file pointer

root = addtree(root, number);


}

//call functiion preOrderCount

//print numberOfFullNodes,numberOfPartialNodes, numberOfLeaves;


}

//function to add a key to a binary search tree


struct tnode *addtree(struct tnode *p, int x){

return p;
}
// end struct tnode

//function to visit the binary search tree in preorder and obtain the counts
void preOrderCount(struct tnode *t){

}// end preOrderCount

//function
/*talloc : make a tnode */
struct tnode *talloc(void)

{
return (struct tnode *)malloc(sizeof(struct tnode));
}

You might also like