Download presentation
Presentation is loading. Please wait.
Published byLynne Welch Modified over 8 years ago
1
CS32 Discussion Section 1B Week 10 TA: Hao Yu (Cody)
2
Reminder & Agenda Review: Go through all topics! Practice problems Final exam Tomorrow! TV TOKYO Communications Corporation, LINE stickers
3
Constructor/Deconstructor Type Default constructor Copy constructor Deconstructor Note Ordering! Scope of variables Allocated memory ( new ) must be freed ( delete )
4
Linked Lists Type Single linked lists Doubly linked lists Circularly linked lists (dummy node?) Operations Insert/search/delete Consider all cases! (head/middle/tail/empty/full) Application stack/queue/heap/hash …
5
Inheritance Concept The process of deriving a new class using another class as a base Note What can you see in the base/derived class? The order of constructors/deconstructors Overriding vs. overloading Polymorphism and virtual functions Abstract base class
6
Recursion Design guideline – Leap of faith 1.Find the base cases What are the corner cases? e.g. empty array What’s the terminal condition? e.g. n=0, length=1 2.Decompose the problem What’s the small problem you solve in one iteration? 3.Finish the small problem Note Any recursion and iterative implementation can be transformed to each other Practice and practice!
7
Template Concept Let compile generate multiple versions of functions for you Note Type conversion Specialized version first Legal implementation types template class Pair { public: Pair(); Pair(T first, U second); void setFirst(T value); void setSecond(U value); int getFirst() const; int getSecond() const; private: T m_first; U m_second; };
8
Standard Template Library Common used data structures vector (array) list (doubly linked list) set (binary search tree) map stack queue Iterator Note: Time complexity Insert/search/delete
9
Time Complexity
10
Sorting Selection sort 1.Find the minimum value 2.Swap the minimum value and the current value 3.Repeat until sorted Insertion sort 1.Pick one value 2.Insert it to the right position 3.Repeat until sorted Bubble sort 1.Compare two consecutive values and swap if necessary 2.Repeat until sorted Merge sort 1.Keep splitting the array until the length is 1 2.Merge Quick sort 1.Pick a pivot and move smaller values to the left, larger values to the right 2.Repeat step 1 to all subarrays
11
Binary Trees Another data structure! Usually implemented using linked list class Node { Node(int val) { value = val; left = nullptr; right = nullptr; } int value; Node *left, *right; }; class BinaryTree { BinaryTree() { m_root = nullptr; } ~BinaryTree() { freeTree(m_root); } void preOrder(Node *node); void inOrder(Node *node); void postOrder(Node *node); void levelOrder(Node *node); int numNode(Node *node); int numLeafNode(Node *node); int numNonLeafNode(Node *node); int height(Node *node); void freeTree(Node *node); Node *m_root; };
12
Heaps Properties A complete binary tree A node value must greater/less than its children Implementations Linked-list Array Operations Find max/min Insert/Delete Application Heapsort O(nlogn)
13
Hash Tables Closed vs. Open hash table Efficiency: the load factor Hash function Fast and less collisions Time complexity Usually O(1) but depends
14
Practice – Chang’s Problem #3
15
Practice – Chang’s Problem #4 X X O O X O O
16
Practice – Chang’s Problem #6 X O O O O
17
Practice – Chang’s Problem #13
18
A B CD EF G HI J
19
Practice – Choi’s Problem #7, #8 UCL N A Left right center G E Center left right C NL U A GE
20
Practice – Choi’s Problem #11 15 1014 79811 4356 14 1011 7986 435 14 1211 71086 4359
21
Practice – Choi’s Problem #13 1.Only +, -, *, / 2.Precedence: * = / > + = - 3.Number range: 0 - 9
22
Practice – Choi’s Problem #13 5 + 6 + 56 5 + 6 x 4 + 5 5 + 6 - 4 + 56 - 4 6 x 4 5 + 6 x 4 + 3 + 5x 64 + 3 Possible situations: Two operators 1.P(op1) = P(op2) 2.P(op1) < P(op2) 3.P(op1) > P(op2)
23
Practice – Choi’s Problem #13 Node *addOp(Node *root, char op, char digit) { if (isDigit(root->val)) { // First operator Node *digitNode = new Node(digit, NULL, NULL); Node *newRoot = new Node(op, root, digitNode); return newRoot; } else if (isLower(root->val, op)) { // P(op1) < P(op2) root->right = addOp(root->right, op, digit); return root; } else { // P(op1) >= P(op2) Node *digitNode = new Node(digit, NULL, NULL); Node *newRoot = new Node(op, root, digitNode); return newRoot; } If empty tree (only has a digit) else if P(op1) < P(op2) else if P(op1) >= P(op2)
24
Practice – Choi’s Problem #13 int evaluate(Node *root) { if (isDigit(root->val)) return root->val - '0'; switch (root->val) { case '+': return evaluate(root->left) + evaulate(root->right); case '-': return evaluate(root->left) - evaulate(root->right); case '*': return evaluate(root->left) * evaulate(root->right); case '/': return evaluate(root->left) / evaulate(root->right); } }
25
Finally… Review all slides (lectures, discussions) Practice released problem sets The devil is in the detail Congratulations! Good luck TV TOKYO Communications Corporation, LINE stickers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.