Professional Documents
Culture Documents
Solution (1/2)
struct record {
char name[50+1];
int id;
int code;
int grade;
};
struct at {
struct record element;
struct at *next
};
typedef struct at atom;
Solution (2/2)
int removeNegative(atom **head){
int removed = 0;
atom *tmp;
while(*head) {
if ((*head)->element.grade == 1) {
removed++;
tmp = *head;
*head = (*head)->next;
free(tmp);
} else {
head = &((*head)->next);
}
}
return removed;
}
Write the function that reverses the order of elements in the doubly
linked list. The prototype of the function is:
void reverseList(atom **head, atom **tail);
Solution
void reverseList(atom **head, atom **tail){
atom *aux,*aux1;
aux = *head;
*head = *tail;
*tail = aux;
while(aux != NULL){
aux1 = aux->next;
aux->next = aux->prev;
aux->prev = aux1;
aux = aux->prev;
}
}
Trees
Tree containing data about products (name and price) is given with the
following structures:
typedef struct {
char name[20];
float price;
} Element;
struct n{
Element element;
struct n *left, *right;
};
typedef struct n node;
Trees (1)
Write the function that will change the given tree into its mirror copy
(for each node replace its left and right child). The prototype of the
function is:
node *mirrorTree (node *root);
Solution
node *mirrorTree(node *root){
if (root){
node *tmp;
tmp = root->left;
root->left = mirrorTree(root->right);
root->right = mirrorTree(tmp);
}
return root;
}
Trees (2)
A binary search tree is sorted according to the price. Write the function
with the prototype:
int search(node *root, float price);
The function returns 1 if the tree contains the product with the
searched price, otherwise it returns 0.
Solution
int search(node *root, float price){
if (root){
if (price == root->element.price)
return 1;
else if (price < root->element.price)
return search(root->left, price);
else
return search(root->right, price);
}
return 0;
}
Heap (1)
Write the function with the prototype
int isHeap(node *root);
The function returns 1 if the complete binary tree (with the given root)
is heap, otherwise it returns 0.
Solution
int isHeap(node *root){
if (root == NULL)
return 1;
if ((root->left!=NULL) &&
(root>left->element > root->element))
return 0;
Heap(1)
Assuming a heap is created for the following input sequence:
5, 6, 7, 7, 6, 8, 5, 9, using the algorithm with O(n) running time in the
worst case, illustrate all the steps of the creation process:
5
Solution:
6
7
7
6
6
5
7
6
8
6
8
6
8
6
9
6
7
5
6
5
8
6
8
6
Heap (2)
Assuming a heap is created for the following input sequence:
27, 49, 35, 19, 87, 53, 67, 29, using the algorithm with O(n) running
time in the worst case, illustrate all the steps of the creation process:
Solution:
27
49 35
19 87 53 67
29
27
49 35
29 87 53 67
19
27
49 67
29 87 53 35
19
27
87 67
29 49 53 35
19
87
27 67
29 49 53 35
19
Heap:
87
49 67
29 27 53 35
19
Heap (3)
Assuming a heap is created for the following input sequence:
15, 23, 7, 11, 40, 5, 90, using the algorithm with O(nlog2n) runnning
time in the worst case, illustrate all the steps of the creation process:
Solution:
15
23
15
23
15
23
23
15
15
11
23
15
11
40
40
40
23
11
23
15
11
15
Heap (3)
40
23
11
15
90
90
23
11
40
15
Heap (4)
The given heap is stored in the array:
93
82
47
31
64
23
17
27
Solution (1/8)
Initial heap:
93
93 82 47 31 64 23 17 27
82
31
47
64
23
17
27
Replace the element from the top of the heap with the last element from the unsorted array part:
27
82
47
27 82 47 31 64 23 17 93
31
64
23
17
Solution 2/8
Adjust the heap:
82
64
82 64 47 31 27 23 17 93
47
31
27
23
17
Replace the element from the top of the heap with the last element from the unsorted array part:
17
64
31
47
27
23
17 64 47 31 27 23 82 93
Solution 3/8
Adjust the heap:
64
31
64 31 47 17 27 23 82 93
47
17
27
23
Replace the element from the top of the heap with the last element from the unsorted array part:
23
31
17
47
27
23 31 47 17 27 64 82 93
Solution 4/8
Adjust the heap:
47
31
47 31 23 17 27 64 82 93
23
17
27
Replace the element from the top of the heap with the last element from the unsorted array part:
27
31
17
23
27 31 23 17 47 64 82 93
Solution 5/8
Adjust the heap:
31
31 27 23 17 47 64 82 93
27
23
17
Replace the element from the top of the heap with the last element from the unsorted array part:
17
17 27 23 31 47 64 82 93
27
23
Solution 6/8
Adjust the heap:
27
27 17 23 31 47 64 82 93
17
23
Replace the element from the top of the heap with the last element from the unsorted array part:
23
23 17 27 31 47 64 82 93
17
Solution 7/8
Adjust the heap:
23
23 17 27 31 47 64 82 93
17
Replace the element from the top of the heap with the last element from the unsorted array part:
17
17 23 27 31 47 64 82 93
Solution 8/8
Sorted array:
17
23
27
31
47
64
82
93