CS100-Trees 2 and Doubly Linked Lists.pptx

advertisement
Trees 2 and Doubly Linked Lists
As you arrive: Please snarf today’s code.
3 Topics
1.
2.
3.
4.
Some more on height
Tree Traversals
Doubly Linked Lists
Big O of recursive functions
Given a tree that’s height-balanced,
what is its height?
Number of nodes = 7
Height = 3
A. O(n)
B. O(n log n)
C. O(log n)
D. O(n2)
Number of nodes = 5
Height = 3
What is the maximum amount of time
it could take to insert a node in a
binary search tree? How about find a
node?
H = tree height
N = # of tree elements
1) O(H)
2) O(N)
3) O(N + H)
4) O(log H)
5) O(H2)
What is the maximum amount of time
it could take to insert a node in a
binary search tree? How about find a
node?
O(tree height)
Traversals
Preorder (N L R): 7 2 6 5 11
Inorder (L N R): 2 7 5 6 11
Postorder (L R N): 2 5 11 6 7
• Go to “Quizzes/Tests” section of Sakai and find
today’s classwork – fill it out
Read N L R as “first visit node, then visit left subtree, then vision right subtree”
Onward!
• Go to the TreeNodeExample code from
Monday’s class
• Try to write:
1. An iterative version of containsNode that
assumes the tree is a binary search tree
2. An iterative version of containsNode that
does not assume the tree is a binary search
tree (hint: you’ll want a stack or queue)
• How do we compute the Big O of recursive
functions?
Solve the problems in
RecurrenceRelationProblems.java
A.
B.
C.
D.
E.
Download