Trees Flashcards
Tree
An undirected graph that contains no cycles and is connected
Trees vs Graphs
Cycles: Graphs contain cycles, trees don’t
Connectivity: Graphs can be disconnected, trees are always connected
Hierarchy: Trees have a hierarchical structure, graphs don’t
Subtree
A tree who whose root is a node in another tree
Binary Trees
A rooted tree in which every parent has at most 2 children
Full Binary Tree
A binary tree in which each parent has exactly 2 children
Full m-ary tree
A tree in which each parent has exactly m children
If a full binary tree has n internal vertices, what is the formula to calculate the total vertices?
2n + 1
If a full m-ary tree has n internal vertices, what is the formula to calculate the total vertices?
mn + 1
How to calculate the number of leaves based on a tree’s height?
If h >= 0, then t <= 2^h
Pre-order Traversal
- Visit the root
- Pre-order (left subtree)
- Pre-order (right subtree)
In-order Traversal
- In-order (left subtree)
- Visit the root
- In-order (right subtree)
Post-order Traversal
- Post-order (left subtree)
- Post-order (right subtree)
- Visit the root
Sorting trees rules
left child < parent
right child > parent
If left child = parent key then…
left child value < parent value
If right child value = parent key then…
right child value > parent value