1 / 47

Stacks and Queues, Binary Search Trees, and Big-Oh

Stacks and Queues, Binary Search Trees, and Big-Oh. CS61BL Spring 2011. Tree Traversals with Stacks and Queues. Queue. Queue. First in first out Methods: hasNext() enqueue(Object o) // add dequeue() // next. Level Order: Breadth First Uses a queue One depth at a time.

nituna
Télécharger la présentation

Stacks and Queues, Binary Search Trees, and Big-Oh

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. Stacks and Queues, Binary Search Trees,and Big-Oh CS61BL Spring 2011

  2. Tree Traversals with Stacks and Queues

  3. Queue

  4. Queue • First in first out • Methods: • hasNext() • enqueue(Object o) // add • dequeue() // next

  5. Level Order: Breadth First • Uses a queue • One depth at a time • 2 → 7 → 15 → 12 → 6 → 9 → 5 → 11 → 4

  6. Level Order Traversal Steps Add the root node to the queue. Continue until the queue is empty. Dequeue a node from the front of the list. Visit it. Enqueue its children (in order from left to right).

  7. DEMO: Queue Based Level Order Traversal

  8. DEMO: Queue Based Level Order Traversal Add the root node to the queue. Continue until the queue is empty. Dequeue a node from the front of the list. Visit it. Enqueue its children

  9. Summary Notes:Level-Order (Breadth-First) Traversal • Traverse all nodes at depth 0, then depth 1… • Unlike the other traversals, this algorithm is not naturally recursive • Use a queue, which initially contains only the root. Then repeat the following steps: • Dequeue a node from the front of the list. • Visit it. • Enqueue its children (in order from left to right). Continue until the queue is empty.

  10. Stacks

  11. Stacks • First in LAST out • Methods: • peek() • push(Object o) // add • pop() // next

  12. DEMO: Stack Based Depth First Traversal The only difference between Breadth First Search (Queue) and Depth First Search (Stack) is the Data Structure! Add the root node to the stack. Continue until the stack is empty. Pop a node from the top of the stack. Visit it. Push its children

  13. Depth First Traversal • Preorder: visit node, traverse its children • Pre: Start with “me” • Postorder: traverse children, visit node. • Post: Be a good “host” • Inorder: traverse first child, visit node, traverse second child (binary trees only).

  14. Summary Notes:Depth First Traversal • This algorithm is naturally recursive (using the call stack) • Depending upon the order you get different depth first traversals. • If you use an explicit stack: • Add the root node to the stack. Continue until the stack is empty. • Pop a node from the top of the stack. • Visit it. • Push its children

  15. Binary Search Trees* (BSTs)*BST Remove is next lecture

  16. Binary Search TreeBST

  17. Binary Search Tree Invariants • For every node • All nodes in the left sub-tree are less • All nodes in the right sub-tree are greater • Every operation must maintain these invariants!

  18. Summary Notes: Binary Search Tree (BST) • Binary Search Tree is a binary tree. • Generally, each node of the Binary Search Tree contains an <Key, Value> pair called an Entry. • The key can be the same as the value. • Binary Search Tree satisfies the following property, called the binary search tree invariant: For any node X (not just the root): • every key in the leftsubtree of X is less than or equal to X's key, and • every key in the rightsubtree of X is greater than or equal to X's key. • A key equal to the parent's key can go into either subtree.

  19. BST Nodes need Comparable values

  20. obj1.compareTo(obj2) obj1.isBiggerThan(obj2) negative if obj1 is less than obj2, positive if obj1 is greater than obj2, and zero if the two objects have equal values.

  21. Binary Search TreeInsert

  22. Inserting • insert() starts by following the same path through the tree as find(). • When it reaches a null reference, replace the null with a reference to a new node <Key, Value> . • Duplicate keys are allowed. • If insert() finds a node that already has the key, it puts it the new entry in the left subtree of the older one. • We could just as easily choose the right subtree; it doesn't matter.

  23. Insert into a BST

  24. Static helper for insert Already in the Tree Should be to the left of potentialParent Should be to the right of potentialParent

  25. Timing & Asymptotic Costs(Connecting the statement counts you did to the Big Idea)

  26. Asymptotic Cost We want to express the speed of an algorithm independently of a specific implementation on a specific machine. We examine the cost of the algorithms for large input sets i.e. the asymptotic cost. In later classes (CS70/CS170) you’ll do this in more detail

  27. Which one is fastest? it depends

  28. “Woah! One of these is WAY better after this point. Let’s call that point N”

  29. Formal definition if and only if for all n > N

  30. Which is fastest after some big value N?

  31. Important Big-Oh Sets Subset of

  32. Which is fastest after some value N? WAIT – who cares? These are all proportional! Sometimes we do care, but for simplicity we ignore constants

  33. Formal definition if and only if for all n > N

  34. Simplifying stuff is important

  35. What is the running time to do pair-wise comparison of all student solutions? Number of pairs e.g. (n-5) and 5 Sum of each pair General Form n is the number of students that submit the proj.

  36. What is the running time to do pair-wise comparison of all student solutions and every student solution with every old solution? TWO variables! This complicates simplifying expressions! n is the number of students that submit the proj m is the number of previous semester solutions

  37. “Maximally Balanced”

  38. Maximally Balanced? YES

  39. Maximally Balanced? NO NULL

  40. Maximally Balanced? YES

  41. Another way to implement trees!

  42. Maximally Balanced Trees – WHY?

  43. A Better Tree Implementation(don’t use the 0 index) Left Child at 2n Right Child at 2n + 1

  44. How many elements are in a balanced tree? Assume h = height What is h in terms of n?

  45. // Take the log of both sides // Remember:

  46. What is the run-time for find in this Binary Search Tree?

  47. What is the run-time for find in this tree?

More Related