Ejemplo n.º 1
0
def testRandomSID():
    # Initialize data structures.
    bst = BinarySearchTree()

    # Generate sample sets:
    (s, i, d) = generateRandomSIDSampleSet(210, 40, 10, 40)

    bm = SIDBenchmark(
        bst.search,
        bst.insert,
        bst.delete,
        s,
        i,
        d,
        # STOP,
        210,
        # BENCHMARK_START,
        40,
        # BENCHMARK_LENGTH,
        10,
        # BENCHMARK_INTERVAL
        40)

    print("\nSearch plot:")
    print(bm.search_plot.coordinates)

    print("\nInsert plot:")
    print(bm.insert_plot.coordinates)

    print("\nDelete plot:")
    print(bm.delete_plot.coordinates)
Ejemplo n.º 2
0
def randomAllSmall():
    """Performs a small random data test on ALL data structures under consideration."""

    # Initialize data structures
    bst = BinarySearchTree()
    stromberg_treap = StrombergTreap()
    jenks_treap = JenksTreap()
    pyskiplist = PySkipList()
    redblacktree = RedBlackTree()
    # avltree = AVLTree()

    # Custom benchmarking parameters
    stop = 5200
    bm_start = 200
    bm_length = 20
    bm_interval = 200

    # Final setup, and invocation.
    filename = "plots/randomAllSmall.tex"
    functions = \
        [bst.insert, stromberg_treap.insert, jenks_treap.insert, pyskiplist.insert, redblacktree.add]
    samples = generateRandomSamples(stop)
    title = "Insert: All Data Structures, Random Input (Small Data Set)"
    graph(filename,
          functions,
          samples,
          title=title,
          stopindex=stop,
          bm_startindex=bm_start,
          bm_interval=bm_interval,
          bm_length=bm_length,
          repeat=1)
Ejemplo n.º 3
0
def randomAllTinyRepeat():
    """Performs a tiny random data test on ALL data structures under consideration."""

    # Initialize data structures
    bst = BinarySearchTree()
    stromberg_treap = StrombergTreap()

    # Custom benchmarking parameters
    stop = 1010
    bm_start = 100
    bm_length = 10
    bm_interval = 50

    # Final setup, and invocation.
    filename = "plots/randomAllTinyRepeat.tex"
    functions = [bst.insert, stromberg_treap.insert]
    samples = generateRandomSamples(stop)
    title = "Insert: All Data Structures, Random Input (Tiny Data Set) - Median of 11 runs"
    graph(filename,
          functions,
          samples,
          title=title,
          stopindex=stop,
          bm_startindex=bm_start,
          bm_interval=bm_interval,
          bm_length=bm_length,
          repeat=11)
Ejemplo n.º 4
0
def testGraphMixedSID():
    # Options and parameters
    size = 20
    bm_start = 0
    bm_length = 3
    bm_interval = 5
    filename = "plots/testGraphMixedSID.tex"

    # Generate operation sequence
    ops = generateRandomOperationSequence(size)

    # Initialize data structures
    bst = BinarySearchTree()
    stromberg_treap = StrombergTreap()
    jenks_treap = JenksTreap()
    pyskiplist = PySkipList()
    redblacktree = RedBlackTree()
    sortedlist = SortedList()
    builtinlist = List()

    # Create functions list
    searches = \
        [
            bst.search,
            stromberg_treap.get_key,
            jenks_treap.__getitem__,
            pyskiplist.search,
            redblacktree.find_node,
            sortedlist.search,
            builtinlist.search,
        ]
    inserts = \
        [
            bst.insert,
            stromberg_treap.insert,
            jenks_treap.insert,
            pyskiplist.insert,
            redblacktree.add,
            sortedlist.add,
            builtinlist.add
        ]
    deletes = \
        [
            bst.delete,
            stromberg_treap.remove,
            jenks_treap.__delitem__,
            pyskiplist.remove,
            redblacktree.remove,
            sortedlist.remove,
            builtinlist.remove
        ]

    # Run the graph
    graphMixedSID(filename, searches, inserts, deletes, ops, bm_start,
                  bm_length, bm_interval)
Ejemplo n.º 5
0
def testPgfPlot():
    # Create data structures
    bst = BinarySearchTree()
    mylist = []

    # Create samples
    inputs = generateRandomSamples(STOP - START)

    graph(
        "plots/testpgfplot.tex",
        [bst.insert, mylist.append],
        inputs,
        xlabel="TEST X LABEL",
        ylabel="TEST Y LABEL",
        title="TEST TITLE",
    )
Ejemplo n.º 6
0
def insertRandom():
    """Performs a small random data test on ALL data structures under consideration."""

    # Initialize data structures
    bst = BinarySearchTree()
    stromberg_treap = StrombergTreap()
    jenks_treap = JenksTreap()
    pyskiplist = PySkipList()
    redblacktree = RedBlackTree()
    # avltree = AVLTree()

    # Final setup, and invocation.
    filename = "plots/insertRandom.tex"
    functions = \
        [bst.insert, stromberg_treap.insert, jenks_treap.insert, pyskiplist.insert, redblacktree.add]
    samples = generateRandomSamples(SAMPLE_SIZE)
    title = "Insert: Random Input"
    graph(filename, functions, samples, title=title)
Ejemplo n.º 7
0
def randomSID():
    """Performs a full search, insert, and delete benchmark set on randomized input data."""

    (search_samples, insert_samples,
     delete_samples) = generateRandomSIDSampleSet()  # 210, 40, 10, 40)

    # Initialize data structures
    bst = BinarySearchTree()
    stromberg_treap = StrombergTreap()
    jenks_treap = JenksTreap()
    pyskiplist = PySkipList()
    redblacktree = RedBlackTree()
    # avltree = AVLTree()

    # Final setup, and invocation.
    base_filename = "plots/random.tex"
    search_filename = base_filename[0:-4] + "_search" + base_filename[-4:]
    insert_filename = base_filename[0:-4] + "_insert" + base_filename[-4:]
    delete_filename = base_filename[0:-4] + "_delete" + base_filename[-4:]
    searches = \
        [bst.search, stromberg_treap.get_key, jenks_treap.__getitem__, pyskiplist.search, redblacktree.find_node]
    inserts = \
        [bst.insert, stromberg_treap.insert, jenks_treap.insert, pyskiplist.insert, redblacktree.add]
    deletes = \
        [bst.delete, stromberg_treap.remove, jenks_treap.__delitem__, pyskiplist.remove, redblacktree.remove]
    title = "Random Input"
    graphSID(
        search_filename,
        insert_filename,
        delete_filename,
        searches,
        inserts,
        deletes,
        search_samples,
        insert_samples,
        delete_samples,
        # 210,
        # 40,
        # 10,
        # 40,
        title=title)
    def test_fuzz(self):
        """Perform some basic randomized input testing."""
        repeat_times = 5
        sample_set = range(-1000, 1001)

        bst = BinarySearchTree()
        for i in range(repeat_times):
            # Insert elements
            for j in random.sample(sample_set, len(sample_set)):
                bst.insert(j)

            # Search for elements
            for j in random.sample(sample_set, len(sample_set)):
                self.assertEqual(j, bst.search(j))

            # Delete elements
            for j in random.sample(sample_set, len(sample_set)):
                self.assertTrue(bst.delete(j))
                self.assertIsNone(bst.search(j))
Ejemplo n.º 9
0
import random
import sys

sys.path.insert(
    1, "C:\Users\Tim\Desktop\competition\clrs\datastructures_old_py2")
from datastructures.binarysearchtree import BinarySearchTree, Node

if __name__ == "__main__":
    bst = BinarySearchTree()
    keys = range(100)
    random.shuffle(keys)
    for key in keys:
        bst.insert(Node(key))
    x = bst.search(50)
    print bst.successor(x).key
    print bst.predecessor(x).key
Ejemplo n.º 10
0
        return True
    last_seen = {"value": -float("inf")}
    return check_subtree(binary_tree.root)


def is_binary_search_tree2(binary_tree):
    """Returns True if the given binary tree is in fact a
    binary search tree.
    """
    in_order_nodes = []
    in_order_traversal(in_order_nodes, binary_tree.root)
    for i in xrange(len(in_order_nodes) - 1):
        if in_order_nodes[i].key > in_order_nodes[i + 1].key:
            return False
    return True
    # return any(in_order_nodes[i].key > in_order_nodes[i + 1].key for i in xrange(len(in_order_nodes) - 1))


def in_order_traversal(node_list, root):
    if root is not None:
        in_order_traversal(node_list, root.left)
        node_list.append(root)
        in_order_traversal(node_list, root.right)

if __name__ == "__main__":
    bst = BinarySearchTree()
    for i in xrange(10):
        bst.insert(Node(i))
    bst.root.left = Node(999)
    print is_binary_search_tree_min_max(bst)
Ejemplo n.º 11
0
def same_tree2(root1, root2):
    q = Queue()
    q.enqueue(root1)
    q.enqueue(root2)
    while not q.queue_empty():
        x, y = q.dequeue(), q.dequeue()
        if x.key != y.key:
            return False
        if x.left is not None and y.left is not None:
            q.enqueue(x.left)
            q.enqueue(y.left)
        elif x.left is not y.left:
            return False
        if x.right is not None and y.right is not None:
            q.enqueue(x.right)
            q.enqueue(y.right)
        elif x.right is not y.right:
            return False
    return True


if __name__ == "__main__":
    t1 = BinarySearchTree()
    t2 = BinarySearchTree()
    for i in xrange(1000):
        t1.insert(Node(i))
    print "DONE"
    for i in xrange(900, 1000):
        t2.insert(Node(i))
    print is_subtree(t1, t2)
Ejemplo n.º 12
0
import datastructures.binarytreeutilities as btutils
from datastructures.binarysearchtree import BinaryTree
from datastructures.binarysearchtree import BinarySearchTree

print("Hello!")
bst = BinarySearchTree()
bst.insert(13)
bst.insert(10)
bst.insert(19)
bst.insert(6)
bst.insert(14)
bst.insert(20)
bst.insert(12)
bst.insert(15)
print(bst.root.data)
print(btutils.get_number_of_leaves(bst.root))
print(btutils.get_printable_tree(bst.root))
Ejemplo n.º 13
0
def testMixedSIDPlotSizes():
    """Runs a small test that inserts and deletes between benchmarksexpects known values.

    The operation sequence is as follows:
        1. Insert up to first benchmark.
        2. Search through first benchmark.
        3. Search up to second benchmark.
        4. Insert through second benchmark.
        5. Search up to third benchmark.
        6. Delete through third benchmark.
        7. Search up to fourth benchmark.
        8. Insert and delete in equal numbers through fourth benchmark.

    Expected values are thus easily determined, I expect a specific plot in return.

    For simplicity, the operation between benchmarks is always search, so we don't have to simulate it here at all."""

    # Our printed expected values.
    expected = []

    # Convenience locals
    s = [(MixedSIDBenchmarkPlot.SEARCH, 0)]
    i = [(MixedSIDBenchmarkPlot.INSERT, 0)]
    d = [(MixedSIDBenchmarkPlot.DELETE, 0)]

    # Test parameters
    bm_start = 4
    bm_length = 4
    bm_interval = 8

    # Get us to the first benchmark
    ops = i * bm_start
    size = bm_start
    index = bm_start

    # Run the first simulated benchmark: search.
    expected.append((index, size))
    ops += s * bm_length
    # size does not change.

    # Fast forward
    index += bm_interval
    ops += s * (bm_interval - bm_length)

    # Run the second simulated benchmark: insert.
    expected.append(
        (index, statistics.median(list(range(size + 1,
                                             size + bm_length + 1)))))
    ops += i * bm_length
    size += bm_length

    # Fast forward
    index += bm_interval
    ops += s * (bm_interval - bm_length)

    # Run the third simulated benchmark: delete.
    expected.append(
        (index,
         statistics.median(list(range(size - 1, size - bm_length - 1, -1)))))
    ops += d * bm_length
    size -= bm_length

    # Fast forward
    index += bm_interval
    ops += s * (bm_interval - bm_length)

    # Run the first simulated benchmark: insert and delete equally.
    expected += (index, size)
    ops += (i + d) * (bm_length // 2)
    # size does not change.

    # Now, run the real simulation.
    sim_plot = MixedSIDBenchmarkPlot.plotSizes(ops, bm_start, bm_length,
                                               bm_interval)

    # And just for kicks, run the actual benchmark on these operations.
    bst = BinarySearchTree()
    real_plot = MixedSIDBenchmarkPlot(ops, bst.search, bst.insert, bst.delete,
                                      bm_start, bm_length, bm_interval)

    # Print for comparison.
    print("MixedSIDBenchmarkPlot test:")
    print("\t operations: " + str(ops))
    print("\tPlease hand-check that the following match:")
    print("\t\t" + str(expected))
    print("\t\t" + str(sim_plot.coordinates))
    print("\tReal benchmark's output:")
    print("\t\t" + str(real_plot.coordinates))
Ejemplo n.º 14
0
def worstCaseSID():
    """Performs a full search, insert, and delete benchmark set on all data structures using the worst-case inputs
    for binary search trees."""

    # Custom test parameters, because a million items will just take eons.
    stop = 10000
    bm_start = 500
    bm_length = 100
    bm_interval = 500

    # Worst-case insert: all increasing.
    insert_samples = list(range(stop))

    # Worst-case delete: always the leaf in our worst-case structure.
    delete_samples = insert_samples[:]
    delete_samples.reverse()

    # Worst-case search: search for the last bm_length elements in our worst-case structure. But this is harder
    # to create since it only does a search at our various intervals.
    search_samples = []
    current = bm_start
    while current < stop:
        search_samples += list(range(current - bm_length, current))
        current = min(stop, current + bm_interval)

    # Initialize data structures
    bst = BinarySearchTree()
    stromberg_treap = StrombergTreap()
    jenks_treap = JenksTreap()
    pyskiplist = PySkipList()
    redblacktree = RedBlackTree()
    # avltree = AVLTree()

    # Final setup, and invocation.
    base_filename = "plots/worstCase.tex"
    search_filename = base_filename[0:-4] + "_search" + base_filename[-4:]
    insert_filename = base_filename[0:-4] + "_insert" + base_filename[-4:]
    delete_filename = base_filename[0:-4] + "_delete" + base_filename[-4:]
    searches = \
        [
            bst.search,
            stromberg_treap.get_key,
            jenks_treap.__getitem__,
            pyskiplist.search,
            redblacktree.find_node
        ]
    inserts = \
        [
            bst.insert,
            stromberg_treap.insert,
            jenks_treap.insert,
            pyskiplist.insert,
            redblacktree.add
        ]
    deletes = \
        [
            bst.delete,
            stromberg_treap.remove,
            jenks_treap.__delitem__,
            pyskiplist.remove,
            redblacktree.remove
        ]
    title = "Worst-Case Input"
    graphSID(search_filename,
             insert_filename,
             delete_filename,
             searches,
             inserts,
             deletes,
             search_samples,
             insert_samples,
             delete_samples,
             stop,
             bm_start,
             bm_length,
             bm_interval,
             title=title)
Ejemplo n.º 15
0
def min_height_binary_search_tree(sorted_integers):
    bst = BinarySearchTree()
    root = insert_middle(bst, sorted_integers, 0, len(sorted_integers))
    bst.insert(root)
    return bst
    def test_basic_checks(self):
        bst = BinarySearchTree()

        # Root node: 7
        bst.insert(7)
        self.assertEqual(7, bst.root.key, "Insert key as root")

        #   7
        #  /
        # 3
        bst.insert(3)
        self.assertEqual(3, bst.root.left.key, "Insert key as root's left child")

        #   7
        #  / \
        # 3   11
        bst.insert(11)
        self.assertEqual(11, bst.root.right.key, "Insert key as root's right child")

        #     7
        #    / \
        #   3   11
        #  /
        # 1
        bst.insert(1)
        self.assertEqual(1, bst.root.left.left.key, "Insert key as left child of non-root node")

        #     7
        #    / \
        #   3   11
        #  /     \
        # 1       13
        bst.insert(13)
        self.assertEqual(13, bst.root.right.right.key, "Insert key as right child of non-root node")

        #     7
        #    / \
        #   3   11
        #  /   / \
        # 1   9   13
        bst.insert(9)
        self.assertEqual(9, bst.root.right.left.key, "Insert key as left child of right child")

        #      7
        #    /   \
        #   3     11
        #  / \    / \
        # 1   5  9   13
        bst.insert(5)
        self.assertEqual(5, bst.root.left.right.key, "Insert key as right child of left child")

        for i in range(1, 14, 2):
            self.assertEqual(i, bst.search(i), "Search full tree for {}.".format(i))

        for i in range(0, 15, 2):
            self.assertIsNone(bst.search(i), "Search full tree for nonexistent items.")

        # By adding a few more elements, we can test all basic deletion cases with a bit of planning.
        # (Some corner cases may not be covered, e.g. testing whether a replacement works with both None and Node)
        #      7
        #    /   \
        #   3     11
        #  / \    / \
        # 1   5  9   13
        #       /   /
        #      8  12
        bst.insert(8)
        bst.insert(12)

        # Possible deletion cases:
        # A - delete root with successor as leftmost child of right subtree (IOS)
        # B - delete root with left and right children, right has no left subtree
        # C - delete root with only right child
        # D - delete root with only left child
        # E - delete root with no children, leaving an empty tree
        # F - delete non-root node with successor as leftmost child of right subtree (IOS)
        # G - delete non-root node with only right child
        # H - delete non-root node with only left child
        # I - delete non-root node with left and right children, right child is a leaf
        # J - delete non-root leaf

        # Deletions, in order, matched with cases above:
        # 7  - A
        # 3  - I
        # 11 - F
        # 9  - J
        # 5  - H
        # 12 - G
        # 8  - B
        # 13 - D
        # This leaves a tree with one node, 1.

        # Then we'll insert 2 to form the tree
        #    1
        #     \
        #      2
        # And we'll delete:
        # 1 - C
        # 2 - E

        for i in [7, 3, 11, 9, 5, 12, 8, 13]:
            self.assertTrue(bst.delete(i), "Test deletion cases")
            self.assertIsNone(bst.search(i), "Search for deleted item")

        bst.insert(2)
        for i in [1, 2]:
            self.assertTrue(bst.delete(i), "Test deletion cases")
            self.assertIsNone(bst.search(i), "Search for deleted item")

        self.assertFalse(bst.delete(6), "Delete item that's not in the tree")
        self.assertFalse(bst.delete(7), "Delete item previously removed from tree")
        self.assertIsNone(bst.search(1), "Search empty tree")