61B-25: Advanced Trees, incl. Geometric
Contents
Traversals
Level Order
- Traverse top-to-bottom, left-to-right (like reading in English):
- We say that the nodes are ‘visited’ in the given order.
Depth First Traversals
- Preorder, Inorder, Postorder
- Basic (rough) idea: Traverse “deep nodes” (e.g. A) before shallow ones (e.g. F).
preorder
|
|
D B A C F E G
inorder
|
|
A B C D E F G
postorder
|
|
A C B E G F D
trick to think about
visitor pattern
|
|
|
|
|
|
What is the runtime of a preorder traversal in terms of N, the number of nodes? (in code below, assume the visit action takes constant time)
- Θ(N) : Every node visited exactly once. Constant work per visit.
Level Order Traversal
Iterative Deepening
|
|
For algorithms whose runtime depends on height, difference between bushy tree and spindly tree can be huge!
Range Finding
问题描述
Easy approach, just do a traversal of the whole tree, and use visitor pattern to collect matching items. $\Theta(N)$
better way: Pruning, Restricting our search to only nodes that might contain the answers we seek. $\Theta(log N+R)$ ,其中R是匹配的的个数。
Spatial Trees
2D Range Finding, Building Trees of Two Dimensional Data