Cool use of trees. Admittedly, I didn't really follow your post very well, but I am wondering if the efficiency could be improved by using other algorithms similar to
Adelson-Velskii Landis trees (which automatically balance themselves to make sure one side of the tree is not longer than the opposite side of it), which would make searching for behaviors much faster. Do you know if that would be applicable in this situation? I'm sure there is some way it could be worked over to behave correctly.