Week 04 Lab Solution
Binary Search Trees
Sample Solution
bstSumEvens()
int bstSumEvens(struct node *t) {
if (t == NULL) {
return 0;
}
int sumEvens = bstSumEvens(t->left) + bstSumEvens(t->right);
if (t->value % 2 == 0) {
return t->value + sumEvens;
} else {
return sumEvens;
}
}
bstNumLeaves()
int bstNumLeaves(struct node *t) {
if (t == NULL) {
return 0;
} else if (t->left == NULL && t->right == NULL) {
return 1;
} else {
return bstNumLeaves(t->left) + bstNumLeaves(t->right);
}
}
Worst-case time complexity: \( O(n) \)
Explanation: The function is called at most \(2n\) times, and the number of operations performed within each function call (ignoring recursion) is constant, so the worst-case time complexity is \(O(n)\).
bstRange()
int bstRange(struct node *t) {
if (t == NULL) {
return -1;
}
struct node *currMin = t;
while (currMin->left != NULL) {
currMin = currMin->left;
}
struct node *currMax = t;
while (currMax->right != NULL) {
currMax = currMax->right;
}
return currMax->value - currMin->value;
}
Worst-case time complexity: \( O(h) \)
Explanation: Each of the two while loops goes down one branch of the tree, and the length of a branch is at most \( h \), where \( h \) is the height of the tree. Therefore, the worst-case time complexity is \( O(h) \).
bstDeleteLeaves()
struct node *bstDeleteLeaves(struct node *t) {
if (t == NULL) {
return NULL;
} else if (t->left == NULL && t->right == NULL) {
free(t);
return NULL;
} else {
t->left = bstDeleteLeaves(t->left);
t->right = bstDeleteLeaves(t->right);
return t;
}
}
bstClosest()
int bstClosest(struct node *t, int value) {
assert(t != NULL);
int closestInSubtree = t->value;
if (t->value < value && t->right != NULL) {
closestInSubtree = bstClosest(t->right, value);
} else if (t->value > value && t->left != NULL) {
closestInSubtree = bstClosest(t->left, value);
} else {
return t->value;
}
if (abs(t->value - value) < abs(closestInSubtree - value)) {
return t->value;
} else {
return closestInSubtree;
}
}
bstLevelOrder()
void bstLevelOrder(struct node *t) {
if (t == NULL) {
return;
}
Queue q = QueueNew();
QueueEnqueue(q, t);
while (!QueueIsEmpty(q)) {
struct node *n = QueueDequeue(q);
showBstNode(n);
if (n->left != NULL) {
QueueEnqueue(q, n->left);
}
if (n->right != NULL) {
QueueEnqueue(q, n->right);
}
}
QueueFree(q);
}
Worst-case time complexity: \( O(n) \)
Explanation: According to Queue.h, QueueNew is \(O(1)\), and we also see that QueueEnqueue and QueueDequeue are \( O(1) \) in the worst case, so the body of the while loop is \( O(1) \). The loop runs exactly \(n\) times, since every node is enqueued once and one node is dequeued every iteration, so the loop is \(O(n)\) in the worst case. QueueFree is \(O(1)\) since the queue is empty when it is called. Therefore, the worst-case time complexity is \( O(n) \).
bstTrim()
struct node *bstTrim(struct node *t, int depth) {
if (t == NULL) {
return NULL;
}
t->left = bstTrim(t->left, depth - 1);
t->right = bstTrim(t->right, depth - 1);
if (depth < 0) {
free(t);
return NULL;
} else {
return t;
}
}
numCommonValues()
int numCommonValues(struct node *t1, struct node *t2) {
if (t1 == NULL) {
return 0;
}
int numCommon = numCommonValues(t1->left, t2) +
numCommonValues(t1->right, t2);
if (bstFind(t2, t1->value)) {
return 1 + numCommon;
} else {
return numCommon;
}
}
Worst-case time complexity: \( O(n_1 h_2) \)
Explanation: For each of the \(n_1\) values in t1, the function calls bstFind to check if it is in t2. bstFind is \(O(h_2)\) in the worst case since it goes down one branch of t2 to find a given value. Therefore, the worst-case time complexity of numCommonValues is \(O(n_1 h_2)\).