Dsa22 13
Dsa22 13
Unit Review
Main Content of the Lecture
q The main topics covered by this unit
q Review of Assignments
q Information about the final examination
v Examination format and rules
v Sample questions
if (treePtr->leftChildPtr == NULL) {
treePtr->leftChildPtr = node;
} else {
TreeNode walker = treePtr->leftChildPtr; Assuming total
while (walker->rightChildPtr != NULL) number of items
walker = walker->rightChildPtr; stored in the
walker->rightChildPtr = node; general tree or
} // assuming the maximal number of AVL is n.
} // children of a node is m and m << n
Example of Complexity Analysis
void processNode(TreeNode *&treePtr, int role, int count) {
//A buyer buys the item
int thisKey = (treePtr->data).key;
Item dataOut;
itemList.AVL_Retrieve(thisKey, dataOut);//O(log n)
if (dataOut.buyASellingItem()) {
if (dataOut.getQuantity() > 1) {
int q = dataOut.getQuantity() - 1;
generatTree->data.setQuantity(q);
dataOut.setQuantity(q);
itemList.AVL_update(dataOut); //O(log n)
} else {
itemList.AVL_Delete(thisKey); //O(log n)
deleteNode(thisKey); //O(1)
}
}
} Overall O(log n)
Topics covered by the unit
§ Linear data structures and searching algorithms (lecture 2 & 3)
§ Basic operations of any linear lists: Insertion, deletion, search,
traversal
§ Basic components of a linked list: head and nodes. Each node
contains data and link.
§ vector: array-based linear data structure in STL
§ list: linked list based linear data structure in STL
§ Sequential search and binary search
Review of assignments
§ The purpose of assignment 1 (Cephalopod game) is to:
§ Learn to use STL data structures to solve real-world problems.
§ Learn to design relatively highly complicated algorithms
§ Learn to design greedy algorithms
§ Learn to design randomized algorithms
§ Learn to design relatively complicated recursive algorithms
§ The purpose of assignment 2 (Catalog sale) is to:
§ Learn to create customised data structures to solve real-world
problems.
§ Be aware of efficiency of implementation and learn how to
improve efficiency.
§ Train ourselves to be a professional programmer
18
Review of assignments
§ Data structures involved in Assignment 1: vector, queue and priority
queue.
§ Data structures involved in Assignment 2: AVL tree and general
tree.
§ Typical algorithms involved in the assignments: sequential search,
heuristic search, randomization, recursion, AVL tree operations,
general tree operations, dynamic programming, ...
Reading materials
§ Lecture notes
§ Textbook
§ Example code
§ Your solution to tutorial questions
§ Your solution to practical tasks
§ Assignment specifications and your solutions
20
Which of the following edges cannot be in the spanning tree generated from a
breadth first traversal starting from A?
(a). (E, A)
(b). (E, B)
(c). (E, C)
(d). (E, D)
28
Which of the following edges cannot be in the spanning tree generated from a
breadth first traversal starting from A?
(a). (E, A)
(b). (E, B)
(c). (E, C)
(d). (E, D)
29
Example questions
What is the output of the following program segment?
priority_queue<int> intQueue;
int x, y;
x = 2;
y = 3;
intQueue.push(x);
intQueue.push(y);
x = intQueue.top(); (a). x=2, y=3
(b). x=4, y=6
intQueue.pop();
(c). x=3, y=6
intQueue.push(x-2); (d). x=6, y=3
intQueue.push(x);
intQueue.push(y+3);
y = intQueue.top();
intQueue.pop();
cout<<"x = "<< x << ", ";
cout<<"y = "<< y << endl;
31
Example questions
Show the left and right children of 32, and the parent
of 11 in the following array-represented heap:
40, 27, 32, 15, 14, 20, 25, 11
Example questions
Show the left and right children of 32 and the parent
of 11 in the following array represented heap:
40, 27, 32, 15, 14, 20, 25, 11
40
32
27
15 14 20 25
Task 1. Write a program that reads words from the given text
file and store the occurrence frequency of each word in an AVL
tree.
Task 2. Print the AVL tree, showing the key, level and balance
factor of each node.
Task 3. List all the words by traversing the AVL tree in pre-
order
Task 4. List all the words in the order of frequency
Task 5. Assume that a text file contains n words. Analyse the
complexity of your algorithm for Task 1 and Task 4.
Example of programming tasks
struct Data { int main() {
string key; AvlTree<Data, string> tree;
int info; ifstream fin(“article.txt”);
}; string word;
while (fin >> word) { Any better solution?
Data item;
item.key = word;
item.frequency = 1;
if(tree.AVL_Retrieve(word, item)) {
tree.AVL_Delete(word);
Item.info++;
}
tree.AVL_Insert(item);
}
fin.close();
return 0;
}
Permitted Resources
§ Paper and pen
§ Textbooks, lecture notes, your solution of tutorials, practicals and
assignments, your noted related to the subject
§ Subject related reference books in Computer Science
§ Subject related local files in your computer
§ Search in the Internet
§ C++ IDEs
§ C++ Compilers
§ Text editors
40
Scholarship opportunities
Scholarship opportunities