initial copy of rbtree code