This is a reference implementation of a Self-Balancing Merkle Search Tree (i.e., an Authenticated Data Structure ). This can be used to efficiently represent the essential state necessary to validate Bitcoin  transactions: the set of available unspent transaction outputs (UTXOs).
Authenticated Data Structures are used in protocols between three parties: 1) the trusted Source, which creates data and publishes a digest; 2) the untrusted Server, which maintains a copy of the data and responds to queries; and 3) the Client, which verifies the Server's responses against the known digest. In this implementation, the data structure requires O(N) storage and each operation takes O(log N) time. These operations can also be verified in worst-case O(log N) time using a Verification Object (a path through a Merkle tree). A verifying Client is only required to maintain O(1) state (specifically, the Merkle tree root hash).
RedBlackis a general purpose Red-Black binary search tree  that can easily be augmented with a 'digest' function. Typically, the digest should include a secure hash function, forming a dynamic Merkle tree .
utxo_merkle.py: a specialization of
RedBlackby definining a digest/serialization protocol appropriate for Bitcoin UTXOs.
merkle_scan.py: a script that iterates through the Bitcoin blockchain, from the genesis block to the head, incrementally updating the
RedBlackas it goes (requires Gavin Andresen's python bitcointools.
treedot: produces graphviz illustrations of the tree (requires graphviz (dot))
test_redblack: unit tests for
Authenticated insertion/balancing in a red-black merkle tree http://imgur.com/a/KNeq5#0