CQ20232 Midterm Revision
CQ20232 Midterm Revision
Hoang-Quan Tran
Winter 2024
Table of Contents
3 Trees
Heap
Binary Search Tree
Balanced BST – AVL Tree
Key ideas
The main difference between Linked Lists and Arrays:
Arrays are consecutive memory blocks, so it is difficult to insert new
elements in the middle.
Linked Lists are not consecutive memory blocks, so it is difficult to
randomly access elements in the middle.
Question
A convenience store wants to develop a system for logging customer
receipts. A receipt contains the following information:
Customer ID (integer)
Total amount of items bought (integer)
Total price (double)
Receipt creation date (dd/MM/YYYY - string)
Key ideas
The operation:
Stack: last-in-first-out (LIFO)
Queue: first-in-first-out (FIFO)
The purpose:
Stack: DFS, converting between bases, ..etc.
Queue: BFS, simulating a queue, ..etc.
Implementation: Both can be implemented with array and linked list.
Note: Please revise the Stack and Queue section in the Programming
Techniques (CSC10002) lecture notes.
Key ideas
Maintaining a stack to store converted terms.
Question
Write a program to convert a number n (in decimal representation) to
1 Binary (base 2)
2 Octal (base 8)
3 Hexadecimal (base 16)
Important
[Keywords to know what is Polish/Reverse Polish Notation]
Polish Notation is prefix notation, where the operators (mathematical
signs) stand before the operands (numbers, variables).
In contrast with Polish Notation: postfix notation = Reverse Polish
Notation – operators stand behind the operands.
Important
Remember to add a pair of brackets to the original expression!
Question
Consider the following infix mathematical expression:
g
P = ((a + b) × (c − d)) × (e × f ) −
h
Convert P to
1 Prefix (Polish) notation?
2 Postfix (Reverse Polish) notation?
Answer:
1 PN: × × + a b - c d - × e f / g h
2 RPN: a b + c d - × e f × g h / - ×
Question
What is the maximum number of nodes at the i th level of a binary tree?
Answer: 2h − 1.
Question
What is the height of a node?
Answer: The height of the subtree with that node as its root.
Definition (Heap)
A heap is a complete binary tree, where a parent node has a bigger
(max-heap) or smaller (min-heap) value than its children.
Question
Why should we start at i = b n2 c − 1?
Question
Consider the following array:
Answer:
1 Max-heap: 88, 81, 65, 57, 79, 6, 47, 36, 51, 38
2 Min-heap: 6, 36, 38, 51, 79, 65, 47, 57, 88, 81
Question
What is the height of a heap (both min and max-heap)?
A D
B C E F
On this tree:
The green node is the in-order predecessor of the root node R.
The orange node is the in-order successor of the root node R.
Question
Consider the following array:
A = [32, 50, 46, 74, 30, 26, 77, 92, 95, 72]
32
30 50
26 46 74
72 77
92
95
46 46 50
30 50 30 50 30 77
26 74 26 77 26 72 92
72 77 72 92 95
92 95
95
Question
Can we build different BSTs from the same set of values?
Answer: Yes – by using different inserting orders.
Question
Can 2 different BSTs built from the same set of values have the same
traversal order?
Answer: Yes – by using inorder traversal.
Question
BST traversal by level.
Question
Check if a binary tree is a valid BST.
Question
Check if a binary tree is a full/complete/perfect BST.
Question
Find the nearest node in a BST: given a value k, find a node that is a
ceiling of k (i.e. the smallest node in the BST that is larger than k).
Question
Consider the array
35
11 57
2 19 38 97
8 36 53
Removing 11:
35
8 57
2 19 38 97
36 53
Removing 36:
35
8 57
2 19 38 97
53
Removing 2:
35
8 57
19 38 97
53
Removing 19:
38
35 57
8 53 97
Question
Is an AVL tree a full/complete/perfect tree?
To sum up:
Review all lectures from the first week.
Do not confuse the terms used in math and DSA.
Practice more (!) – most important.
During the test
Think carefully before finalize your answers.