24
Final Review Dr. Yingwu Zhu

Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Embed Size (px)

Citation preview

Page 1: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Final Review

Dr. Yingwu Zhu

Page 2: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Goals

• Use appropriate data structures to solve real-world problems– E.g., use stack to implement non-recursive BST

traversal, – use queue to implement BST level traversal, – use stack to implement non-recursive quicksort– use heaps to do heapsort, and priority queues

• Use appropriate algorithms to slove real-world problems– Search algorithms– Sorting algorithms

Page 3: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Goals

• Use Big-Oh notation to evaluate algorithm efficiency

• Understand ADTs including BST, Heap, Priority Queue, AVL trees

• Understand hashing • Understand sorting algorithms

Page 4: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

ADTs

• Tree terminologies

• BST

• AVL Trees

• Heap

• Priority Queue

Page 5: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Trees

• Binary trees

• Complete trees

• Balanced trees

• Level

• Height

Page 6: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

BST

• Definition• Recursive ADT• Implementing a BST (recursive and non-

recursive)– Search– Traversals (in-order, pre-order, post-order)– Insertion– Deletion– Other operations: height, level, …

Page 7: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

BST

• T(n) = ?• Is BST balanced?• Lopsidedness problem!• BST AVL trees

Page 8: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

AVL Trees

• Definition• Four rotation techniques

– Single rotations– Double rotations

• Key to perform rotation: identify the nearest ancestor with BF of +2 or -2 for the inserted item

• Two steps in double rotations– Rotate child and grandchild nodes of the ancestor– Rotate the ancestor and the new child node

Page 9: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Heap

• Defintion

• Recusive data structure

• Semiheap

• What data structures are good to implement a heap? Why?

• Parent-child relationships

Page 10: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Heap

• Implementation– Insertion– Deletion– removeMax– Other operations?

• Two basic operations– Percolate down– Percolate up

Page 11: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Priority Queue

• Definition

• Using different ADTs to implement priority queue– Unsorted lists– Sorted lists– BST– Heap

• Why heap is a good choice?

Page 12: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Hashing

• Why need hashing?

• Definition of hash function?

• Problem of hashing: collision

Page 13: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Hashing

• Collision resolution techniques– Open addressing

• Linear probing• Quadratic probing• Double hashing

– Chaining

Page 14: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Hashing

• Three strategies to improve hashing performance

– Increase hash table capacity– Use a good hash function (how to evaluate a

hash function?)– Use a good collision resolution technique

Page 15: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Algorithm Efficiency

• Big-Oh notation definition T(n)

• Non-recursive algorithms– The most executed instruction

• Recursive algorithms: telescoping principal– Anchor case– Inductive step

Page 16: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Sort

• Selection sort, insertion sort, bubble sort

• Heapsort

• Quicksort

• Mergesort

Page 17: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Selection Sort

• How does it work?

• T(n) = ?

Page 18: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Insertion Sort

• How does it work?

• T(n) = ?

• Recursive and Non-recursive algorithms

Page 19: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Bubble Sort

• How does it work?

• How does it detect partially sort sublist to improve performance

• T(n) = ?

• Best case performance

• Worst case performance

Page 20: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Quicksort

• How does it work?– Devide and conquer

• Basic operation– Split based on pivot

• T(n) = ? best case and worst case?

Page 21: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Quicksort

• How to improve performance– Median-of-three rule in pivot choice– Short sublists are handle first in recursive alg.– Non-recursive– Other solutions

Page 22: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Mergesort

• Internal and external algorithm

• Basic operation: split and merge

• Divide-and-conquer

• T(n) = ?

Page 23: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

Heapsort

• Heapify process

• How does heapsort work?– Exploits heap property– T(n) = ?

Page 24: Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,

About Final Exam

• Must >= 75 to pass

• Multiple choices

• Short answers

• Coding

• Reminder: do not loose points in basic concept questions!

• Good luck!