300 likes | 390 Views
Week 6 - Friday. CS221. Last time. What did we talk about last time? Exam 1 Merge sort. Questions?. Assignment 3. Recursion. Project 2. Infix to Postfix Converter. Master Theorem. Master Theorem. Has a great name…
E N D
Week 6 - Friday CS221
Last time • What did we talk about last time? • Exam 1 • Merge sort
Assignment 3 Recursion
Project 2 Infix to Postfix Converter
Master Theorem • Has a great name… • Allows us to determine the Big Oh running time of many recursive functions that would otherwise be difficult to determine
Basic form that recursion must take where • a is the number of recursive calls made • b is how much the quantity of data is divided by each recursive call • f(n) is the non-recursive work done at each step
Case 1 • If for some constant , then
Case 2 • If for some constant , then
Case 3 • If for some constant , and if for some constant and sufficiently large , then
Stupid Sort algorithm (recursive) • Base case: List has size less than 3 • Recursive case: • Recursively sort the first 2/3 of the list • Recursively sort the second 2/3 of the list • Recursively sort the first 2/3 of the list again
Stupid Sort • We need to know logba • a = 3 • b = 3/2 = 1.5 • Because I’m a nice guy, I’ll tell you that the log1.5 3 is about 2.7
Binary Search • We know that binary search takes O(log n) time • Can we use the Master Theorem to check that?
What is a tree? • A tree is a data structure built out of nodes with children • A general tree node can have any non-negative number of children • Every child has exactly one parent node • There are no loops in a tree • A tree expressions a hierarchy or a similar relationship
Terminology • The root is the top of the tree, the node which has no parents • A leaf of a tree is a node that has no children • An inner node is a node that does have children • An edge or a link connects a node to its children • The level of a node is the length of the path from the root to the node plus 1 • Note that some definitions leave off the plus 1 • The height of the tree is the greatest level of any node • A subtree is a node in a tree and all of its children
A tree 1 Root Inner Nodes 2 3 4 Leaves 5 6 7
Binary tree • A binary tree is a tree such that each node has two or fewer children • The two children of a node are generally called the left child and the right child, respectively
Binary tree 1 2 3 4 5 6
Binary tree terminology • Full binary tree: every node other than the leaves has two children • Perfect binary tree: a full binary tree where all leaves are at the same depth • Complete binary tree: every level, except possibly the last, is completely filled, with all nodes to the left • Balanced binary tree: the depths of all the leaves differ by at most 1
Binary search tree (BST) • A binary search tree is binary tree with three properties: • The left subtree of the root only contains nodes with keys less than the root’s key • The right subtree of the root only contains nodes with keys greater than the root’s key • Both the left and the right subtrees are also binary search trees
BST 4 2 5 1 3 6
Next time… • More on binary trees
Reminders • Finish Assignment 3 • Due tonight! • Keep working on Project 2 • Due next Friday • No class on Monday!