Balanced Trees
Searching in Balanced and Unbalanced Trees -
- Consider the problem of searching for a node in a tree:
- Balanced trees, of course, give best case behavior
- Unbalanced trees, of course, give worst case behavior
- Some example data that can produce an unbalanced tree, when inserted:
- Sorted
- Reverse sorted
- Alternating large and small keys
- Large sequences of any of the above
- How to avoid this worst case behavior:
- Create a data structure that is guaranteed to be balanced (or close to it)
Balanced Tree Data Structures
- 2-3-4 Trees:
- Have 2, 3, or 4 children per node
- Red-Black Trees: binary tree; each node
has a color
- Binary tree
- Each node has is red or black
- A Red node is part its parent (in equivalent 234 tree)
- Maximum depth difference between trees is one half
- AVL Trees: depth of left and right subtrees of each node are kept with
one of each other
- height of left and right subtrees of each node are kept within one of
each other
- B Trees: many values per node; short trees; used for disk storage
- Many values per node
- Result in short, balanced trees
- Used for disk storage
- B+-Trees
- B tree variant
- Interior nodes are just indices
- All data is in leaves
- Enables easy sequential access