Extended Visitors Example - Self-balancing Trees 

COMP 310    Java Resources  Eclipse Resources

Balanced Trees

Binary Search Trees (BST's) are great for storing data and give us great speed...or do they?

  1. Trees offer much faster access than lists, but only if the tree so long as all the branches are nearly the same length, i.e. "balanced".
  2. A balanced tree gives O(log n) behavior, while lists give O(n) behavior.  (See the quick refresher on "Big-O" analysis of programs.)
  3. Worst case scenario for an unbalanced tree is O(n) behavior.

One really wants BSTs and other trees to be balanced for optimal performance!

 

2-3-4 Trees / B-Trees

Kind of like an n-ary tree but with multiple pieces of data on each tree root (node).

An extension of the idea of a BST where the ordering of the data in the child trees depends on the data in the parent node.

Additional materials:

 

Take-away

The main points of this discussion, besides teaching you about 2-3-4 and B-trees, is for you to see the following in action:

 

© 2020 by Stephen Wong