コード例 #1
0
    def fusion(self, tree_node, tree_fusion_node, left=True):
        """It performs fusion between adjacent nodes in order to resolve underflow conditions"""
        # if left == True s is at left of w
        w = tree_node
        s = tree_fusion_node
        u = w._parent
        p_parent = w._list_parent._node._parent  #if left else w._list_parent._node._parent

        tree = RedBlackTreeMap()
        p = tree.add(
            p_parent.key())  # To obtain a position of a generic RBTree
        if left:
            w.tree().catenate(w._tree, p, left=False, T2=s._tree)

            w.tree()._l.fusion(s._tree._l, right=True)
            root = w.tree().root()
            left = w.tree().left(root)
            right = w.tree().right(root)
            w.tree()._update_black_height(left if left is not None else right)

            self.check_overflow(w)

        else:
            s.tree().catenate(s._tree, p, left=True, T2=w._tree)

            s.tree()._l.fusion(w._tree._l, right=False)
            root = s.tree().root()
            left = s.tree().left(root)
            right = s.tree().right(root)
            s.tree()._update_black_height(left if left is not None else right)

            self.check_overflow(s)
            #s.tree().add(p_parent.key())

        del (u.tree()[p.key()])
コード例 #2
0
    class Node():

        __slots__ = '_tree', '_parent', '_list_parent', '_children'

        def __init__(self, tree=None):
            if tree is None:
                self._tree = RedBlackTreeMap()
            else:
                self._tree = tree
            self._parent = None
            self._children = self._tree._l

        def tree(self):
            return self._tree

        def children(self):
            return self._children

        def is_empty(self):
            return self._size == 0

        def elements(self):
            for pos in self._tree.breadthfirst():
                yield pos.key()

        def positions(self):
            for pos in self._tree.breadthfirst():
                yield pos
コード例 #3
0
 def __init__(self, tree=None):
     if tree is None:
         self._tree = RedBlackTreeMap()
     else:
         self._tree = tree
     self._parent = None
     self._children = self._tree._l
コード例 #4
0
    def split(self, tree_node):
        """It splits the external B-Tree node, recalling the same internal operation"""
        p = tree_node.tree()._get_median()

        #split operation, called on internal BSTs, returns the obtained subtrees
        if tree_node._parent is None:
            T1, T2 = tree_node.tree().split(p)
        else:
            T1, T2 = tree_node.tree().split(p)

        #parent = tree_node._parent

        #tree_node._parent = tree_node

        #Creo un nuovo albero
        node_left = self.Node(T1)
        node_right = self.Node(T2)

        if tree_node._parent is None:  #Sono la radice
            tree_parent = RedBlackTreeMap()
            np = tree_parent.add(p.key())
            node_parent = self.Node(tree_parent)
            self._root = node_parent
            tree_node._parent = tree_node

            #parent-children references settings
            np._node._left_out._node._child = node_left
            node_left._parent = node_parent
            node_left._list_parent = np._node._left_out

            self._update_children(node_left)

            #parent-children references settings
            np._node._right_out._node._child = node_right
            node_right._parent = node_parent
            node_right._list_parent = np._node._right_out

            self._update_children(node_right)

            self._num_node += 2

            #parent._child = node_parent
        else:
            node_parent = tree_node._parent
            new_p = node_parent.tree().add(p.key())

            #parent-children references settings
            new_p._node._left_out._node._child = node_left
            node_left._parent = node_parent
            node_left._list_parent = new_p._node._left_out

            self._update_children(node_left)

            #parent-children references settings
            new_p._node._right_out._node._child = node_right
            node_right._parent = node_parent
            node_right._list_parent = new_p._node._right_out

            self._update_children(node_right)

            self._num_node += 1
            self.check_overflow(node_parent)
コード例 #5
0
    def test_catenate(self):
        tree = self._T
        p50 = tree.add(50)
        p30 = tree.add(30)
        p60 = tree.add(60)
        p25 = tree.add(25)
        p35 = tree.add(35)

        tree2 = RedBlackTreeMap()
        p20 = tree2.add(20)

        tree3 = RedBlackTreeMap()
        p10 = tree3.add(10)
        p5 = tree3.add(5)
        p15 = tree3.add(15)

        l = [p50, p20, p60, p10, p30, p5, p15, p25, p35]

        tree.catenate(tree, pivot=tree2.root(), T2=tree3)

        for i, pos in enumerate(tree.breadthfirst()):
            self.assertEqual(pos, l[i])

        tree4 = RedBlackTreeMap()
        p70 = tree4.add(70)

        tree5 = RedBlackTreeMap()
        p80 = tree5.add(80)
        p75 = tree5.add(75)
        p85 = tree5.add(85)

        tree.catenate(tree, pivot=tree4.root(), T2=tree5, left=False)

        l = [p50, p20, p70, p10, p30, p60, p80, p5, p15, p25, p35, p75, p85]

        for i, pos in enumerate(tree.breadthfirst()):
            self.assertEqual(pos, l[i])
            print(pos.key(), " = ", l[i].key())
コード例 #6
0
 def setUp(self):
     self._T = RedBlackTreeMap()
コード例 #7
0
ファイル: verifica4.py プロジェクト: francescofalanga92/tdp
from pkg_4.my_rb_tree_map import MyRBTreeMap
from drawtree import draw_level_order
from util import randList
from TdP_collections.map.red_black_tree import RedBlackTreeMap

print("Albero RB casuale:")
rb_tree = MyRBTreeMap()
rb_treet = RedBlackTreeMap()

rl = randList(1, 100, 30)
i = 0
for element in rl:
    rb_tree[element] = "test"
    rb_treet[element] = "test"
    i += 1
    if i == 20:
        elem20 = element
    if i == 15:
        elem15 = element
    if i == 25:
        elem25 = element
draw_level_order(str(rb_tree))
print("Albero RB casuale non My:")
print(str(rb_treet))
print("Albero RB T:")
rb_tree1 = MyRBTreeMap()
rb_tree1[1] = "test"
draw_level_order(str(rb_tree1))
rb_tree2 = MyRBTreeMap()
rb_tree2[2] = "test"
print("Albero RB T1:")