Exemplo n.º 1
0
    def test_one_node(self):

        t01 = rbtree.RedBlackTree()
        n01 = rbtree.RedBlackNode(1.0)
        t01.RB_INSERT(n01)

        self.assertEqual(t01._T_root, n01)
        self.assertEqual(n01.p, t01._T_nil)
        self.assertEqual(n01.left, t01._T_nil)
        self.assertEqual(n01.right, t01._T_nil)

        self.assertEqual(t01.size(), 1)
        self.assertEqual(t01.ROOT(), n01)
        num_list = [v for v in t01]
        self.assertEqual(len(num_list), 1)
        self.assertEqual(num_list[0], 1.0)

        #        t01.draw('test_one_node', True, 'pdf', 'vertical' )

        t01.RB_DELETE(n01)

        self.assertEqual(t01._T_root, t01._T_nil)
        self.assertEqual(t01.size(), 0)

        self.assertEqual(n01.p, t01._T_nil)
        self.assertEqual(n01.left, t01._T_nil)
        self.assertEqual(n01.right, t01._T_nil)
        self.assertEqual(t01.size(), 0)
        self.assertEqual(t01.ROOT(), t01._T_nil)
        num_list = [v for v in t01]
        self.assertEqual(len(num_list), 0)
Exemplo n.º 2
0
    def test_random(self):

        NUM_OPERATIONS = 10000
        RATIO_INSERTS_OVER_DELETES = 0.5

        t01 = rbtree.RedBlackTree()
        node_list = []
        num_set = set()

        for i in range(0, NUM_OPERATIONS):

            r_ops = random.uniform(0.0, 1.0)

            if r_ops <= RATIO_INSERTS_OVER_DELETES:

                while True:
                    num = random.uniform(-100, 100)
                    if num not in num_set:
                        num_set.add(num)
                        break
                node = rbtree.RedBlackNode(num)
                t01.RB_INSERT(node)
                node_list.append(node)

            else:
                if len(node_list) > 0:
                    r_index = random.randint(0, len(node_list) - 1)
                    n = node_list[r_index]
                    node_list.remove(n)
                    num_set.remove(n._comparable)
                    t01.RB_DELETE(n)

        num_list_from_min = []
        n_cur = t01.TREE_MINIMUM(t01.ROOT())
        while not n_cur is t01.NIL():
            num_list_from_min.append(n_cur._comparable)
            n_cur = t01.TREE_SUCCESSOR(n_cur)

        num_list_from_iter = [n for n in t01]

        num_list_from_max = []
        n_cur = t01.TREE_MAXIMUM(t01.ROOT())
        while not n_cur is t01.NIL():
            num_list_from_max.append(n_cur._comparable)
            n_cur = t01.TREE_PREDECESSOR(n_cur)

        num_list = list(num_set)
        for i, n in enumerate(sorted(num_list)):
            self.assertEqual(n, num_list_from_min[i])
            self.assertEqual(n, num_list_from_iter[i])

        for i, n in enumerate(sorted(num_list, reverse=True)):
            self.assertEqual(n, num_list_from_max[i])

        self.assertEqual(t01.size(), len(num_list))

        t01.draw('test_random_10000_operations', True, 'pdf', 'vertical')
Exemplo n.º 3
0
    def test_empty(self):

        t01 = rbtree.RedBlackTree()
        n_nil = t01._T_nil
        self.assertEqual(n_nil.p, n_nil)
        self.assertEqual(n_nil.left, n_nil)
        self.assertEqual(n_nil.right, n_nil)
        self.assertEqual(n_nil._rbtype, -1)
        self.assertEqual(n_nil._comparable, 0)

        self.assertEqual(t01._T_root, n_nil)
        self.assertEqual(t01.size(), 0)
        self.assertEqual(t01.ROOT(), n_nil)
        self.assertEqual(t01.NIL(), n_nil)
        num_list = [v for v in t01]
        self.assertEqual(len(num_list), 0)
Exemplo n.º 4
0
    def test_insertions_and_then_deletions(self):

        NUM_INSERTS = 100000
        NUM_DELETES = 99950

        t01 = rbtree.RedBlackTree()
        num_list = []
        num_set = set()
        node_list = []
        for i in range(0, NUM_INSERTS):
            while True:
                num = random.uniform(-100, 100)
                if num not in num_set:
                    num_set.add(num)
                    break
            node = rbtree.RedBlackNode(num)
            t01.RB_INSERT(node)
            node_list.append(node)
            num_list.append(num)

        num_list_from_min = []
        n_cur = t01.TREE_MINIMUM(t01.ROOT())
        while not n_cur is t01.NIL():
            num_list_from_min.append(n_cur._comparable)
            n_cur = t01.TREE_SUCCESSOR(n_cur)

        num_list_from_iter = [n for n in t01]

        num_list_from_max = []
        n_cur = t01.TREE_MAXIMUM(t01.ROOT())
        while not n_cur is t01.NIL():
            num_list_from_max.append(n_cur._comparable)
            n_cur = t01.TREE_PREDECESSOR(n_cur)

        for i, n in enumerate(sorted(num_list)):
            self.assertEqual(n, num_list_from_min[i])
            self.assertEqual(n, num_list_from_iter[i])

        for i, n in enumerate(sorted(num_list, reverse=True)):
            self.assertEqual(n, num_list_from_max[i])

        self.assertEqual(t01.size(), NUM_INSERTS)

        random.shuffle(node_list)

        for i in range(0, NUM_DELETES):
            n_remove = node_list[i]
            t01.RB_DELETE(n_remove)

        node_list_2 = node_list[NUM_DELETES:]
        num_list_2 = [n._comparable for n in node_list_2]

        num_list_from_min_2 = []
        n_cur = t01.TREE_MINIMUM(t01.ROOT())
        while not n_cur is t01.NIL():
            num_list_from_min_2.append(n_cur._comparable)
            n_cur = t01.TREE_SUCCESSOR(n_cur)

        num_list_from_iter_2 = [n for n in t01]

        num_list_from_max_2 = []
        n_cur = t01.TREE_MAXIMUM(t01.ROOT())
        while not n_cur is t01.NIL():
            num_list_from_max_2.append(n_cur._comparable)
            n_cur = t01.TREE_PREDECESSOR(n_cur)

        for i, n in enumerate(sorted(num_list_2)):
            self.assertEqual(n, num_list_from_min_2[i])
            self.assertEqual(n, num_list_from_iter_2[i])

        for i, n in enumerate(sorted(num_list_2, reverse=True)):
            self.assertEqual(n, num_list_from_max_2[i])

        self.assertEqual(t01.size(), NUM_INSERTS - NUM_DELETES)

        t01.draw('test_100000_insertions_and_then_99900_deletions', True,
                 'pdf', 'vertical')
Exemplo n.º 5
0
def main3():
	f=rbtree.RedBlackTree()
	print("REDBLACKTREE RUNTIME:")
	for i in range (500):
		f.add(i)
Exemplo n.º 6
0
        Returns the sibling of the node, as well as the side it is on
        e.g

            20 (A)
           /     \
        15(B)    25(C)

        _get_sibling(25(C)) => 15(B), 'R'
        """
        parent = node.parent
        if node.value >= parent.value:
            sibling = parent.left
            direction = 'L'
        else:
            sibling = parent.right
            direction = 'R'
        return sibling, direction


if __name__ == '__main__':
    import random
    import rbtree

    t = rbtree.RedBlackTree()
    for i in range(0, 30):
        v = random.choice(range(-100, 100))
        print("adding {}".format(v))
        t.add(v)

    print("\n".join([_ for _ in t.iter_dump()]))
Exemplo n.º 7
0
 def __init__(self, mmap_base=0x7ffffffff000 - MMAP_BASE_RND_SIZE):
     self.mmap_base = mmap_base
     self.curr_mmap = self.mmap_base
     self.mm_rb = rbtree.RedBlackTree()
     self.mmap = None