1 / 30

CS221

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…

Download Presentation

CS221

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 6 - Friday CS221

  2. Last time • What did we talk about last time? • Exam 1 • Merge sort

  3. Questions?

  4. Assignment 3 Recursion

  5. Project 2 Infix to Postfix Converter

  6. Master Theorem

  7. 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

  8. 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

  9. Case 1 • If for some constant , then

  10. Case 2 • If for some constant , then

  11. Case 3 • If for some constant , and if for some constant and sufficiently large , then

  12. Stupid Sort

  13. 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

  14. Let's code up Stupid Sort!

  15. But, how long does it take?

  16. 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

  17. Binary Search • We know that binary search takes O(log n) time • Can we use the Master Theorem to check that?

  18. Student Lecture: Binary Trees

  19. Trees

  20. 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

  21. 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

  22. A tree 1 Root Inner Nodes 2 3 4 Leaves 5 6 7

  23. 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

  24. Binary tree 1 2 3 4 5 6

  25. 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

  26. 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

  27. BST 4 2 5 1 3 6

  28. Upcoming

  29. Next time… • More on binary trees

  30. Reminders • Finish Assignment 3 • Due tonight! • Keep working on Project 2 • Due next Friday • No class on Monday!

More Related