CMPT125 Lecture22
CMPT125 Lecture22
Lecture 22
Emails
• I think I replied to all emails about lab exam and assignments
• If not, please let me know
foo(int n)
for i=1…n
for j=1…i
for k=1…i
for l=1…i
print(j+k+l)
Big-O notation
Q: T(n) = T(n/2) + n, and T(1)=1. Prove that T(n) = O(n)
bar(8) bar(n)
12345678 if n==0
return
1234 for i = 1..n
print i
12 bar(n/2)
1
Big-O notation
Consider the following function.
int f (int n) {
int sum = 0, i, j;
for (i=0; i<n; i++)
for (j=1; j<i; j=j*2)
sum += 1;
return sum;
}
Prove that the running time is O(n log(n))
Remark: the O(n log(n)) in the tight answer: Look at the last n/2 iterations: i=n/2…n-1
In each them the inner loop makes > log(n/2) = log(n)-1>log(n)/2 steps
sum>(n/2)*(log(n)/2) > n log(n)/4
Big-O notation
Consider the following function.
int f5 (int n) {
int sum = 0, i, j;
for (i=1; i<n; i=i*2)
for (j=0; j<i; j++)
sum += 1;
return sum;
}
Prove that the running time is O(n)
Proof: For each outer iteration we have i inner iterations.
- Recursion - T(n-1)
- For loop – definitely less than n. But what is the correct answer?
After k iterations sum=1+2+3…+k~k2/2
and we stop when sum>=n k2/2>=n k>=sqrt(2n)
Merge sort
Q: Consider the MergeSort we saw in class with O(n) time merge procedure.
What is the running time of MergeSort on a sorted array of length n?
Use big-O notation to state your answer.
Can make it O(n) on sorted arrays by checking first if the input is sorted in O(n) time
1. L = get_level_k(root->left, k-1)
2. R = get_level_k(root->right , k-1)
3. Return L+R
Get level k of a binary tree
Write an algorithm that gets a Binary Tree a number k and returns all values
at level k. What is the running time of your algorithm?
BreadthFirstSearch(root):
q = create queue of nodes 10
Print 10
q.enqueue(root)
5 21
while q is not empty:
Print 5
21 1 7
node = q.dequeue() Print 21
1 7 16 25
printf(node->value) Print 1
if (node->left != NULL) 7 16 25
Print 7
q.enqueue(node->left)
16 25
Print 16
if (node->right != NULL) 25
Print 25
q.enqueue(node->right)
Depth of a tree
Compute depth iteratively. (Hint: use BFS)
BreadthFirstSearch(root):
q = create queue of nodes
Modify the queue to hold pairs
q.enqueue(root)
while q is not empty: (node, depth)
node = q.dequeue() - In the beginning add (root,0)
printf(node->value)
if (node->left != NULL)
- In each iteration get (node, i)
q.enqueue(node->left)
And add (node->child, i+1)
if (node->right != NULL)
q.enqueue(node->right)
Binary Search Trees
• Example 1:
• A binary search tree can be balanced
Binary Search Trees
Example 2:
binary search tree can be
very skinny / unbalanced
Not a Binary Search Tree
Not a Binary Search Tree
Remove node with no children
Remove (BST_t* tree, BTnode_t* node):
if (tree-> root == node) 4
tree->root = NULL;
else 2 6
if (node ->parent->left == node)
set_left_child(node->parent, NULL)
1 3 5 7
else // node ->parent->right == node
set_right_child(node->parent, NULL)
free(node)
remove( 3 )
Remove node with one child
Remove (BST_t* tree, BTnode_t* node): 4
child = getChild(node); 3 5
if (tree->root == node)
7
root = child; 2
else 6
1
if (node->parent->left == node) remove( 3 )
set_left_child(node->parent, child);
remove( 7` )
else // node->parent->right == node
set_right_child(node->parent, child);
free(node)
Remove node with two children
remove( 4 )
Remove (BST_t* tree, BTnode_t* node)
next = find successor of node in the tree; 4
// next has ≤1 child
node->value = next->value; 2 6
Successor of 4 is 5
BST 1
Create a Binary Search Tree from the following list of insertions:
List: 4, 2, 8, 1, 3 , 6, 5, 9, 10, 7
4
5 7 10
Queues
Write the states of the queue for this execution
1. queue_t* q = queue_create(); []
2. enqueue(q, 1); <- [1] <-
3. enqueue(q, 2); [1,2]
4. enqueue(q, 3); [1,2,3]
5. enqueue(q, 4); [1,2,3,4]
6. dequeue(q); [2,3,4]
7. enqueue(q, 5); [2,3,4,5]
8. enqueue(q, 1); [2,3,4,5,1]
9. dequeue(q); [3,4,5,1]
10. enqueue(q, 1); [3,4,5,1,1]
11. enqueue(q, 3); [3,4,5,1,1,3]
12. dequeue(q); [4,5,1,1,3]
Questions?
Comments?