470 likes | 643 Views
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.
E N D
Stacks and Queues, Binary Search Trees,and Big-Oh CS61BL Spring 2011
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 • 2 → 7 → 15 → 12 → 6 → 9 → 5 → 11 → 4
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).
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
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.
Stacks • First in LAST out • Methods: • peek() • push(Object o) // add • pop() // next
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
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).
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
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!
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.
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.
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.
Static helper for insert Already in the Tree Should be to the left of potentialParent Should be to the right of potentialParent
Timing & Asymptotic Costs(Connecting the statement counts you did to the Big Idea)
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
Which one is fastest? it depends
“Woah! One of these is WAY better after this point. Let’s call that point N”
Formal definition if and only if for all n > N
Important Big-Oh Sets Subset of
Which is fastest after some value N? WAIT – who cares? These are all proportional! Sometimes we do care, but for simplicity we ignore constants
Formal definition if and only if for all n > N
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.
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
Maximally Balanced? NO NULL
A Better Tree Implementation(don’t use the 0 index) Left Child at 2n Right Child at 2n + 1
How many elements are in a balanced tree? Assume h = height What is h in terms of n?
// Take the log of both sides // Remember: