Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 9: Review. cis 335 Fall 2001 Barry Cohen Big O Complexity n Complexity of problem, complexity of algorithm (Sum (n)) n Intuition: worst case rate.

Similar presentations


Presentation on theme: "Class 9: Review. cis 335 Fall 2001 Barry Cohen Big O Complexity n Complexity of problem, complexity of algorithm (Sum (n)) n Intuition: worst case rate."— Presentation transcript:

1 Class 9: Review

2 cis 335 Fall 2001 Barry Cohen Big O Complexity n Complexity of problem, complexity of algorithm (Sum (n)) n Intuition: worst case rate of growth as problem gets big n Informally: a guarantee of when you’ll get an answer n Definition: A is O(f(n)) is exist constants k and n 0 s.t. A takes n 0

3 cis 335 Fall 2001 Barry Cohen Analyzing algorithms n Code or pseudocode n Identify the measure of complexity (time, space, unit operation) n Count the number of maximum number of executions of each line n It’s OK to overestimate. Why?

4 cis 335 Fall 2001 Barry Cohen Find the Big-O of a function n Drop all but the fastest growing term in the growth function n Drop any constant multiplier for the fastest growing term n O(f(n))+O(g(n))=O(f(n)+g(n) O(n 2 ) + O(n) = O(n 2 + n) = O(n 2 ) n What if the input has multiple aspects?

5 cis 335 Fall 2001 Barry Cohen Sorting n Selection: find the right item for each position n Insertion: find the right position for each item n Mergesort: recursively divide in half, merge and recombine n Quicksort: recursively split around pivot value

6 cis 335 Fall 2001 Barry Cohen Binary trees n Build from nodes and their descendants n Empty tree T has no nodes n Else T can be partitioned into: - a root - a left subtree - a right subtree n (Exactly what does partitioned mean?)

7 cis 335 Fall 2001 Barry Cohen Binary search tree n Each node has a value n Each node has value greater than its left child n Each node has a value less than its right child

8 cis 335 Fall 2001 Barry Cohen Binary tree ops n createBT() n destroyBT() n bool isEmpty() n btType getRootData() n setRootData(item:btType) n attachLeft(item:btType) n attachRight(item:btType) n attachLeftTree(item:bt) n attachRightTree(item:bt) n bt detachLeftTree() n bt detachRightTree()

9 cis 335 Fall 2001 Barry Cohen Preorder traversal Preorder(binTree:BinaryTree) if (binTree not empty) { visit root preorder(left subtree) preorder(right subtree) }

10 cis 335 Fall 2001 Barry Cohen Inorder traversal Inorder(binTree:BinaryTree) if (binTree not empty) { inorder(left subtree) visit root inorder(right subtree) }

11 cis 335 Fall 2001 Barry Cohen Postorder traversal Postorder(binTree:BinaryTr ee) if (binTree not empty) { postorder(left subtree) postorder(right subtree) visit root }


Download ppt "Class 9: Review. cis 335 Fall 2001 Barry Cohen Big O Complexity n Complexity of problem, complexity of algorithm (Sum (n)) n Intuition: worst case rate."

Similar presentations


Ads by Google