This preview shows page 1. Sign up to view the full content.
Unformatted text preview: CS32 Discussion Sec.on 1B Week 8 TA: Brian Choi Reminder
• Homework 4 – Template class/func.ons • Just follow the rules – Recursion – Big
O analysis Quick Review of Big
O
• N = size of input some code for
loop (runs N .mes) { You need to see the
function and analyze for
loop (runs N .mes) { the runtime of this
function: suppose it’s O some func.on call } (N)
} while
loop (runs N .mes) { print a character O(1)
} O(N3)
O(N2) O(N) O(N3) + O(N) = O(N3) Sor.ng Algorithms
• We switch gears and discuss some well known sor.ng algorithms. Selec.on Sort
4 3 1 5 2 1 3 4 5 2 1 2 4 5 3 1 2 3 5 4 1 2 3 4 5 • Find the smallest item in the unsorted por.on, and place it in front. • What is the running .me (complexity) of this algorithm? Inser.on Sort
4 3 1 5 2 3 4 1 5 2 1 3 4 5 2 1 3 4 5 2 1 2 3 4 5 • Pick one from the unsorted part, and place it in the “right” posi.on in the sorted part. • Best case? • Avg. case? • Worst case? Inser.on Sort
4 3 1 5 2 3 4 1 5 2 1 3 4 5 2 1 3 4 5 2 1 2 3 4 5 • Pick one from the unsorted part, and place it in the “right” posi.on in the sorted part. • Best case? O(n) • Avg. case? O(n2) • Worst case? O(n2) Merge Sort
3 7 6 5 8 2 1 4 3 7 6 5 3 7 3 7 6 5 6 5 8 2 1 4 8 2 8 2 1 4 1 4 Keep splitting Merge Sort
3 7 3 7 6 5 5 6 3 5 6 7 8 2 2 8 1 4 1 4 1 2 4 8 1 2 3 4 5 6 7 8 Merge Merge Sort: Running Time?
3 7 6 5 8 2 1 4 O(n) 3 7 5 6 2 8 1 4 O(n) 3 5 6 7 1 2 4 8 O(n) 1 2 3 4 5 6 7 8 O(log n) O(n)O(log n) = O(n log n) General Sor.ng: Running Time
• O(n log n) is faster than O(n2) – merge sort is more eﬃcient than selec.on sort or inser.on sort. • O(n log n) is the best average complexity that a general (comparison) sor.ng algorithm can get (assuming you know nothing about the data set). • If more informa.on about the data set is provided, it’s possible to sort things almost linearly. Quick Sort
4 3 1 5 2 3 1 2 4 5 3 1 2 1 2 3 1 3 5 • Pick a pivot, and move numbers that are less than the pivot to front, and ones that are greater than the pivot to end. • On average, O(n log n) • Depending on how you pick your pivots, it can be as bad as O(n2) Quick Ques.ons
• Given an unsorted array of n items, what is the best you can do to search an item, if you are to run this search only once? • Given an unsorted array of n items, what is the best you can do to search an item, if you are to run such search 100 .mes? (assume: n >> 100) • Given an unsorted array of n items, what is the best you can do to search an item, if you are to run this search n .mes? Tree: Deﬁni.ons
root
node
X No loop! link (edge) parent children
siblings
leaves
height H subtree Bound on # of edges
How many edges should there be in a tree of N nodes? H Binary Trees
No node has more than 2 children (lei child + right child). Binary Trees
How many nodes can a binary tree of height h have? (one with max. # of nodes == full binary tree) Tree is a data structure!
• For every data structure we need to know: – how to insert a node, – how to remove a node, – search for a node • and (for tree only) – how to traverse the tree Tree is a data structure!
• For every data structure we need to know: – how to insert a node, – how to remove a node, – search for a node • and (for tree only) – how to traverse the tree struct Node!
{!
ItemType val;!
Node* left;!
Node* right;!
};! Three Methods of Traversal
void preorder(Node *node)!
{!
cout << node>val << “ “;!
preorder(node>left);!
preorder(node>right);!
}! void inorder(Node *node)!
{!
inorder(node>left);!
cout << node>val << “ “;!
inorder(node>right);!
}! void postorder(Node *node)!
{!
postorder(node>left);!
postorder(node>right);!
cout << node>val << “ “;!
}!
Note: NULL check omitted – every function should have if (node == NULL) return;! Binary Search Tree
• At all nodes: – All nodes in the lei subtree have smaller values than the current node’s value – All nodes in the right subtree have larger values than the current node’s value • Which traversal method should you use to: – print values in the increasing order? – print values in the decreasing order? Insert
void insert(Node* &node, ItemType newVal)!
{! } ! Insert
void insert(Node* &node, ItemType newVal)!
{!
if (node == NULL)!
node = new Node(newVal);!
if (node>val > newVal)!
insert(node>left, newVal);!
else!
insert(node>right, newVal);!
}! Assume Node(val) sets left and right pointers of the new node to NULL. Insert
• Average .me complexity? – as many steps as the height of the tree – full tree: N = 2h+1
1 ≈ 2h+1 nodes – h ≈ log2 N
1 – Roughly, it takes O(log N). Search
Node* search(Node *node, ItemType value)!
{! } ! Search
Node* search(Node *node, ItemType value)!
{!
if (node == NULL)!
return NULL;!
if (node>val == value)!
return node;!
else if (node>val > value)!
return search(node>left, value);!
else!
return search(node>right, value);!
}! Removal
• A limle tricky! • Case
by
case analysis – Case 1: the node is a leaf (easy) – Case 2: the node has one child – Case 3: the node has two children Case 3
copy Use inorder traversal to identify these nodes treeHeight
int treeHeight(Node *node)!
{! } ! treeHeight
int treeHeight(Node *node)!
{!
if (node == NULL)!
return 1;!
int leftHeight = treeHeight(node>left);!
int rightHeight = treeHeight(node>right);!
if (leftHeight > rightHeight)!
return leftHeight + 1;!
else!
return rightHeight + 1;!
}! ...
View
Full
Document
This note was uploaded on 02/09/2012 for the course CS 32 taught by Professor Davidsmallberg during the Spring '08 term at UCLA.
 Spring '08
 DavidSmallBerg
 Recursion

Click to edit the document details