0% found this document useful (0 votes)
16 views37 pages

CQ20232 Midterm Revision

Uploaded by

Linh Phan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views37 pages

CQ20232 Midterm Revision

Uploaded by

Linh Phan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Midterm Revision

CSC10004 - Data Structures and Algorithms

Hoang-Quan Tran

Department of Knowledge Engineering


Faculty of Information Technology, VNUHCM - University of Science

Winter 2024
Table of Contents

1 Linked List (Singly & Doubly)

2 Stack & Queue


Converting between bases
Reverse Polish Notation

3 Trees
Heap
Binary Search Tree
Balanced BST – AVL Tree

thquan (fit@hcmus) Midterm Revision Winter 2024 2 / 34


Linked List (Singly & Doubly)
Linked List

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.

Note: Please revise the Linked List section in the Programming


Techniques (CSC10002) lecture notes.

thquan (fit@hcmus) Midterm Revision Winter 2024 3 / 34


Linked List
Further (coding) questions

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)

1 Design the structure of a node in a singly linked list to store this


information.
2 Write a function to print out receipts before a fixed date
(dd/MM/YYYY) from a given linked list.

thquan (fit@hcmus) Midterm Revision Winter 2024 4 / 34


Stack & Queue
Stack and Queue

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.

thquan (fit@hcmus) Midterm Revision Winter 2024 5 / 34


Converting between bases
Key ideas

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)

thquan (fit@hcmus) Midterm Revision Winter 2024 6 / 34


Reverse Polish Notation
Key ideas

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.

thquan (fit@hcmus) Midterm Revision Winter 2024 7 / 34


Reverse Polish Notation
The algorithm

Key ideas – Converting infix to RPN


Maintaining a stack to store open brackets and operators.
Operands: put to the result.
Open brackets "(" and operators: push to stack.
Closing brackets ")": pop the stack to result until an open bracket is
met.

Important
Remember to add a pair of brackets to the original expression!

thquan (fit@hcmus) Midterm Revision Winter 2024 8 / 34


Reverse Polish Notation
Practice

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 / - ×

thquan (fit@hcmus) Midterm Revision Winter 2024 9 / 34


Trees
Binary Tree
Terminology

Definition (Binary Tree)


A Binary Tree is a tree where each node has at most 2 child nodes.

Variants of Binary Tree


This course covers 3 types of Binary Tree:
Full: A node has either 2 or no child nodes.
Complete: All nodes at the last level are filled from left to right.
Perfect: The last level has no empty slots.

thquan (fit@hcmus) Midterm Revision Winter 2024 10 / 34


Binary Tree
More Terminology

Definition (Height – in this course)


The height of a binary tree is the total number of nodes on the longest
path from the root to a leaf node.

Do not confuse DSA with math!


Question
What is the height of a general binary tree with n nodes in the best and
worst cases?
Answer: depends on who are you talking to. In this course: blog2 nc + 1
and n, respectively.

thquan (fit@hcmus) Midterm Revision Winter 2024 11 / 34


Binary Tree
Further questions

Question
What is the maximum number of nodes at the i th level of a binary tree?

Answer: 2i−1 (if you’re counting levels from 1).


Question
How many nodes are there in a perfect binary tree with h levels?

Answer: 2h − 1.
Question
What is the height of a node?

Answer: The height of the subtree with that node as its root.

thquan (fit@hcmus) Midterm Revision Winter 2024 12 / 34


Heap
Heap Terminology

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: Is a heap a full/perfect binary tree? Answer: Not always.


Important
Heaps can be built using arrays.
If a parent node’s index is i, its two child nodes are 2i + 1 and 2i + 2,
respectively.
Also, there are no relationships between sibling nodes.

thquan (fit@hcmus) Midterm Revision Winter 2024 13 / 34


Heap
Heap construction (Heapify)

Key ideas – To build a max-heap


To build a max-heap from the array A consists of n items,
1 Start at index b n2 c − 1 backward to 0.
2 Swap Ai with the larger of its two children (A2i+1 , A2i+2 ).

Question
Why should we start at i = b n2 c − 1?

Answer: The rest of the nodes are leaves.

thquan (fit@hcmus) Midterm Revision Winter 2024 14 / 34


Heap
Practice

Question
Consider the following array:

A = [38, 57, 65, 51, 79, 6, 47, 36, 88, 81]

From the array A, build


1 a max-heap?
2 a min-heap?

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

thquan (fit@hcmus) Midterm Revision Winter 2024 15 / 34


Heap
Further questions

Question
What is the height of a heap (both min and max-heap)?

Answer: Always blog2 nc + 1. Why?


in the best case, the heap is a complete binary tree with 1 node at
the last level;
in the worst case, the heap is a perfect binary tree with all nodes at
the i th level filled.

thquan (fit@hcmus) Midterm Revision Winter 2024 16 / 34


Binary Search Tree
Terminology

Definition (Binary Search Tree – BST)


A Binary Search Tree (BST) is a binary tree where at each node,
its left descendents (child nodes) have smaller value than it,
its right descendants have bigger values than it.
its left and right subtrees are both BSTs.

Therefore, you will have


in the worst case – a singly linked list.
in the best case – a balanced BST.

thquan (fit@hcmus) Midterm Revision Winter 2024 17 / 34


Binary Search Tree
Inserting/Deleting a node

Key ideas – Insertion


1 Traverse to the correct position.
2 Insert at that position.

Key ideas – Deletion


1 Traverse to the position.
2 Delete and replace: if the node has
No children: just remove it.
One child: replace the node with its only child.
Two children: replace the node with either its predecessor or
successor.

thquan (fit@hcmus) Midterm Revision Winter 2024 18 / 34


Binary Search Tree
Predecessor and Successor

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.

thquan (fit@hcmus) Midterm Revision Winter 2024 19 / 34


Binary Search Tree
Practice

Question
Consider the following array:

A = [32, 50, 46, 74, 30, 26, 77, 92, 95, 72]

1 Build a BST T by inserting elements from A one by one.


2 What is the height of T ?
3 What is the preorder, inorder, postorder traversal of T ?
4 Removing 32, 74, 46 from T , respectively. If a node has 2 children,
the replacement node will be its successor.

thquan (fit@hcmus) Midterm Revision Winter 2024 20 / 34


Binary Search Tree
Practice – Answer

Question 1’s final tree:

32

30 50

26 46 74

72 77

92

95

thquan (fit@hcmus) Midterm Revision Winter 2024 21 / 34


Binary Search Tree
Practice – Answer

2 Final tree height: 6 (blue nodes).


3 Traversal orders:
NLR: 32, 30, 26, 50, 46, 74, 72, 77, 92, 95
LNR: 26, 30, 32, 46, 50, 72, 74, 77, 92, 95
LRN: 26, 30, 46, 72, 95, 92, 77, 74, 50, 32

thquan (fit@hcmus) Midterm Revision Winter 2024 22 / 34


Binary Search Tree
Practice – Answer

Question 3’s trees (from left to right, respectively):

46 46 50

30 50 30 50 30 77

26 74 26 77 26 72 92

72 77 72 92 95

92 95

95

thquan (fit@hcmus) Midterm Revision Winter 2024 23 / 34


Binary Search Tree
Further questions

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.

thquan (fit@hcmus) Midterm Revision Winter 2024 24 / 34


Binary Search Tree
Further (coding) questions

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).

thquan (fit@hcmus) Midterm Revision Winter 2024 25 / 34


AVL Tree
Terminology

Definition (AVL Tree)


An AVL Tree is a BST where at each node, the difference of the heights
of its subtrees is at most 1.

Key ideas – Insertion & Deletion


The same as a BST, but remember to re-balance the tree after each
step. There are 4 types of imbalance:
left-left.
left-right.
right-left.
right-right.

thquan (fit@hcmus) Midterm Revision Winter 2024 26 / 34


AVL Tree
Practice

Question
Consider the array

A = [2, 19, 35, 97, 57, 36, 11, 38, 53, 8]

1 Build an AVL tree by inserting elements from A one by one.


2 Removing 11, 36, 2, 19 (respectively) from the tree. If a node has 2
children, the replacement node will be its successor.

thquan (fit@hcmus) Midterm Revision Winter 2024 27 / 34


AVL Tree
Practice – Answer

Question 1’s final tree:

35

11 57

2 19 38 97

8 36 53

thquan (fit@hcmus) Midterm Revision Winter 2024 28 / 34


AVL Tree
Practice – Answer

Removing 11:

35

8 57

2 19 38 97

36 53

thquan (fit@hcmus) Midterm Revision Winter 2024 29 / 34


AVL Tree
Practice – Answer

Removing 36:

35

8 57

2 19 38 97

53

thquan (fit@hcmus) Midterm Revision Winter 2024 30 / 34


AVL Tree
Practice – Answer

Removing 2:

35

8 57

19 38 97

53

thquan (fit@hcmus) Midterm Revision Winter 2024 31 / 34


AVL Tree
Practice – Answer

Removing 19:

38

35 57

8 53 97

thquan (fit@hcmus) Midterm Revision Winter 2024 32 / 34


AVL Tree
Further questions

Question
Is an AVL tree a full/complete/perfect tree?

Answer: Not always.


Question
What is the height of an AVL tree with n nodes in the best/worst case?

Answer: log2 n and ≈ 1.44 log2 n, respectively.

thquan (fit@hcmus) Midterm Revision Winter 2024 33 / 34


Final words

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.

thquan (fit@hcmus) Midterm Revision Winter 2024 34 / 34

You might also like