def balanced_btree(frdm, size): """ Generates a random balanced tree of the given ``size``. Each element is generated by a call to ``frdm``. ``size`` should be strictly positive. :param frdm: callable :param size: int :return: BTree """ current_size = 1 btr = Leaf(frdm()) while current_size < size: bt2 = btr.map(lambda x: frdm(), lambda x: frdm()) btr = Node(frdm(), btr, bt2) current_size = 2 * current_size + 1 return btr
def test_map_leaf(): bt = Leaf(1) res = bt.map(lambda x: x + 1, lambda x: x - 1) exp = Leaf(2) assert exp == res