binary trees Flashcards
what is a tree and why do we use it?
an example of a nonlinear data structure
used because some operations can be made more efficient
define a binary tree
- set of nodes and directed edges
- single root node
- exactly one incoming line and zero or more outgoing links
- no cycles
preOrder traversal
root left right
node before it children
postOrder
left right node
children before node - left most nodes are always visited first
inOrder traversal
visit the node after the left child but before the right child
left node right
you will end up with a sorted list
level order traversal
cannot be done as easily using a short recursive formula
use an auxilary queue data structure
- add root node to a queue
- while the queue is not empty - pop a node and visit it add its children to the queue
complexity analysis of bin trees
best = ave = worst
o(n)
what are the level order space requirements
equal to the number of children in the last level of the tree +- n/2