def __graft(bt, lbt, rbt): val = bt.get_value() if bt.is_node(): return Node(val, __graft(bt.get_left(), lbt, rbt), __graft(bt.get_right(), lbt, rbt)) else: # bt.is_leaf() return Node(val, lbt, rbt) if val.is_critical() else bt
def test_r2b_1(): ch = SList() ch.append(RNode(2)) ch.append(RNode(3)) rn = RNode(1, ch) res = rn.r2b() exp = Node(1, Node(2, Leaf(None), Node(3, Leaf(None), Leaf(None))), Leaf(None)) assert res == exp
def __insert_values_btree(val1, val2, btr): # pylint disable=missing-docstring if btr.is_leaf(): return Node(btr.get_value(), Leaf(val1), Leaf(val2)) # btr.is_node() rdm = random.randint(1, 101) if rdm > 50: # insert left left = __insert_values_btree(val1, val2, btr.get_left()) right = btr.get_right() return Node(btr.get_value(), left, right) # insert right left = btr.get_left() right = __insert_values_btree(val1, val2, btr.get_right()) return Node(btr.get_value(), left, right)
def __lv2ibt(segment): stack = [] has_crit_b = False if segment.empty(): raise EmptyError( "An empty Segment cannot be transformed into a BTree") for i in range(segment.length() - 1, -1, -1): v = segment[i] if v.is_leaf(): stack.append(Leaf(v)) elif v.is_critical(): stack.append(Leaf(v)) if has_crit_b: raise IllFormedError( "A ill-formed Segment cannot be transformed into a BTree" ) else: has_crit_b = True else: if len(stack) < 2: raise IllFormedError( "A ill-formed Segment cannot be transformed into a BTree" ) lv = stack.pop() rv = stack.pop() stack.append(Node(v, lv, rv)) if len(stack) != 1: raise IllFormedError( "A ill-formed Segment cannot be transformed into a BTree") else: return has_crit_b, stack[0]
def test_deserialization_1_1(): # btree4 exp = Node(1, Node(2, Node(4, Leaf(6), Leaf(7)), Node(5, Leaf(8), Leaf(9))), Leaf(3)) seg1 = Segment([TaggedValue(1, "C")]) seg2 = Segment([TaggedValue(2, "C")]) seg3 = Segment([TaggedValue(4, "C")]) seg4 = Segment([TaggedValue(6, "L")]) seg5 = Segment([TaggedValue(7, "L")]) seg6 = Segment([TaggedValue(5, "C")]) seg7 = Segment([TaggedValue(8, "L")]) seg8 = Segment([TaggedValue(9, "L")]) seg9 = Segment([TaggedValue(3, "L")]) lt = LTree([seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8, seg9]) res = lt.deserialization() assert res == exp
def test_serialization_1_1(): # btree4 bt = Node(1, Node(2, Node(4, Leaf(6), Leaf(7)), Node(5, Leaf(8), Leaf(9))), Leaf(3)) m = 1 res = LTree.init_from_bt(bt, m) seg1 = Segment([TaggedValue(1, "C")]) seg2 = Segment([TaggedValue(2, "C")]) seg3 = Segment([TaggedValue(4, "C")]) seg4 = Segment([TaggedValue(6, "L")]) seg5 = Segment([TaggedValue(7, "L")]) seg6 = Segment([TaggedValue(5, "C")]) seg7 = Segment([TaggedValue(8, "L")]) seg8 = Segment([TaggedValue(9, "L")]) seg9 = Segment([TaggedValue(3, "L")]) exp = LTree([seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8, seg9]) assert res == exp
def test_deserialization_1_4(): # btree4 seg1 = Segment([TaggedValue(1, "C")]) seg2 = Segment([TaggedValue(2, "C")]) seg3 = Segment( [TaggedValue(4, "N"), TaggedValue(6, "L"), TaggedValue(7, "L")]) seg4 = Segment( [TaggedValue(5, "N"), TaggedValue(8, "L"), TaggedValue(9, "L")]) seg5 = Segment([TaggedValue(3, "L")]) lt = LTree([seg1, seg2, seg3, seg4, seg5]) res = lt.deserialization() exp = Node(1, Node(2, Node(4, Leaf(6), Leaf(7)), Node(5, Leaf(8), Leaf(9))), Leaf(3)) assert res == exp
def test_serialization_1_3(): # btree4 bt = Node(1, Node(2, Node(4, Leaf(6), Leaf(7)), Node(5, Leaf(8), Leaf(9))), Leaf(3)) m = 3 res = LTree.init_from_bt(bt, m) seg1 = Segment( [TaggedValue(1, "N"), TaggedValue(2, "C"), TaggedValue(3, "L")]) seg2 = Segment( [TaggedValue(4, "N"), TaggedValue(6, "L"), TaggedValue(7, "L")]) seg3 = Segment( [TaggedValue(5, "N"), TaggedValue(8, "L"), TaggedValue(9, "L")]) exp = LTree([seg1, seg2, seg3]) assert res == exp
def test_deserialization_2_2(): # btree5 exp = Node( 13, Node(3, Leaf(1), Leaf(1)), Node(9, Node(7, Node(3, Leaf(1), Leaf(1)), Node(3, Leaf(1), Leaf(1))), Leaf(1))) seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment([TaggedValue(3, "C")]) seg3 = Segment([TaggedValue(1, "L")]) seg4 = Segment([TaggedValue(1, "L")]) seg5 = Segment([TaggedValue(9, "C")]) seg6 = Segment([TaggedValue(7, "C")]) seg7 = Segment([TaggedValue(3, "C")]) seg8 = Segment([TaggedValue(1, "L")]) seg9 = Segment([TaggedValue(1, "L")]) seg10 = Segment([TaggedValue(3, "C")]) seg11 = Segment([TaggedValue(1, "L")]) seg12 = Segment([TaggedValue(1, "L")]) seg13 = Segment([TaggedValue(1, "L")]) lt = LTree([ seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8, seg9, seg10, seg11, seg12, seg13 ]) res = lt.deserialization() assert res == exp
def test_serialization_2_5(): # btree5 bt = Node( 13, Node(3, Leaf(1), Leaf(1)), Node(9, Node(7, Node(3, Leaf(1), Leaf(1)), Node(3, Leaf(1), Leaf(1))), Leaf(1))) m = 5 res = LTree.init_from_bt(bt, m) seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg3 = Segment( [TaggedValue(9, "N"), TaggedValue(7, "C"), TaggedValue(1, "L")]) seg4 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg5 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) exp = LTree([seg1, seg2, seg3, seg4, seg5]) assert res == exp
def test_deserialization_2_3(): # btree5 exp = Node( 13, Node(3, Leaf(1), Leaf(1)), Node(9, Node(7, Node(3, Leaf(1), Leaf(1)), Node(3, Leaf(1), Leaf(1))), Leaf(1))) seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg3 = Segment( [TaggedValue(9, "N"), TaggedValue(7, "C"), TaggedValue(1, "L")]) seg4 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) seg5 = Segment( [TaggedValue(3, "N"), TaggedValue(1, "L"), TaggedValue(1, "L")]) lt = LTree([seg1, seg2, seg3, seg4, seg5]) res = lt.deserialization() assert res == exp
def test_serialization_2_2(): # btree5 bt = Node( 13, Node(3, Leaf(1), Leaf(1)), Node(9, Node(7, Node(3, Leaf(1), Leaf(1)), Node(3, Leaf(1), Leaf(1))), Leaf(1))) m = 2 res = LTree.init_from_bt(bt, m) seg1 = Segment([TaggedValue(13, "C")]) seg2 = Segment([TaggedValue(3, "C")]) seg3 = Segment([TaggedValue(1, "L")]) seg4 = Segment([TaggedValue(1, "L")]) seg5 = Segment([TaggedValue(9, "C")]) seg6 = Segment([TaggedValue(7, "C")]) seg7 = Segment([TaggedValue(3, "C")]) seg8 = Segment([TaggedValue(1, "L")]) seg9 = Segment([TaggedValue(1, "L")]) seg10 = Segment([TaggedValue(3, "C")]) seg11 = Segment([TaggedValue(1, "L")]) seg12 = Segment([TaggedValue(1, "L")]) seg13 = Segment([TaggedValue(1, "L")]) exp = LTree([ seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8, seg9, seg10, seg11, seg12, seg13 ]) assert res == exp
def test_r2b_2(): rn5 = RNode(5) rn6 = RNode(6) rn3 = RNode(3, SList([rn5, rn6])) rn2 = RNode(2) rn4 = RNode(4) rn1 = RNode(1, SList([rn2, rn3, rn4])) res = rn1.r2b() exp = \ Node(1, Node(2, Leaf(None), Node(3, Node(5, Leaf(None), Node(6, Leaf(None), Leaf(None) ) ), Node(4, Leaf(None), Leaf(None) ) ) ), Leaf(None) ) assert res == exp
def ill_balanced_btree(frdm, size): """ Generates a random list-like 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: btr = Node(frdm(), Leaf(frdm()), btr) current_size = current_size + 2 return btr
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_b2r_node_from_rt(): bt = Node( 1, Node( 2, Leaf(None), Node(3, Node(5, Leaf(None), Node(6, Leaf(None), Leaf(None))), Node(4, Leaf(None), Leaf(None)))), Leaf(None)) rn5 = RNode(5) rn6 = RNode(6) rn3 = RNode(3, SList([rn5, rn6])) rn2 = RNode(2) rn4 = RNode(4) exp = RNode(1, SList([rn2, rn3, rn4])) res = RNode(bt) assert res == exp
def test_zipwith_node(): bt1 = Node(1, Leaf(2), Leaf(3)) bt2 = Node(4, Leaf(5), Leaf(6)) exp = Node(5, Leaf(7), Leaf(9)) res = bt1.map2(fun.add, bt2) assert exp == res
def test_zipwith_leaf_node(): bt1 = Leaf(1) bt2 = Node(4, Leaf(5), Leaf(6)) with pytest.raises(AssertionError): bt1.map2(fun.add, bt2)
def test_zipwith_node_leaf(): bt1 = Node(1, Leaf(2), Leaf(3)) bt2 = Leaf(2) with pytest.raises(AssertionError): bt1.map2(fun.add, bt2)
def test_is_leaf_false(): bt = Node(1, Leaf(1), Leaf(1)) exp = False res = bt.is_leaf() assert exp == res
def __remove_annotation(bt): v = bt.get_value() return Leaf(v.get_value()) if bt.is_leaf() else Node( v.get_value(), __remove_annotation(bt.get_left()), __remove_annotation(bt.get_right()))
def test_reduce_node(): bt = Node(1, Leaf(2), Leaf(3)) res = bt.reduce(lambda x, y, z: max(x, max(y, z))) exp = 3 assert exp == res
def test_getchr_node_left(): c = 1 bt = Node(3, Node(4, Leaf(2), Leaf(6)), Leaf(2)) res = bt.getchr(c) exp = Node(2, Node(6, Leaf(c), Leaf(c)), Leaf(c)) assert res == exp
def test_zip_node_leaf(): bt1 = Node(1, Leaf(2), Leaf(3)) bt2 = Leaf(2) with pytest.raises(AssertionError): bt1.zip(bt2)
def test_is_node_true(): bt = Node(1, Leaf(1), Leaf(1)) exp = True res = bt.is_node() assert exp == res
def test_map_node(): bt = Node(1, Leaf(2), Leaf(3)) res = bt.map(fun.incr, fun.decr) exp = Node(0, Leaf(3), Leaf(4)) assert exp == res
def test_getchl_node_right(): c = 1 bt = Node(3, Leaf(2), Node(4, Leaf(2), Leaf(6))) res = bt.getchl(c) exp = Node(2, Leaf(c), Node(2, Leaf(c), Leaf(c))) assert res == exp
def test_mapt_node(): bt = Node(1, Leaf(2), Leaf(3)) res = bt.mapt(fun.incr, max3) exp = Node(3, Leaf(3), Leaf(4)) assert exp == res
def r2b1(t, ss): a = t.get_value() left = r2b2(t.get_children()) right = r2b2(ss) return Node(a, left, right)
def test_zip_leaf_node(): bt1 = Leaf(1) bt2 = Node(4, Leaf(5), Leaf(6)) with pytest.raises(AssertionError): bt1.zip(bt2)