Example #1
0
 def testMinMax(self):
     self.assertEquals(self.t.findMin(), 0)
     self.assertEquals(self.t.findMax(), 9)
     # Forgot in first phase, check when there no element in tree
     t = SplayTree()
     self.assertEquals(t.findMin(), None)
     self.assertEquals(t.findMax(), None)
Example #2
0
 def testLargeInserts(self):
     t = SplayTree()
     nums = 40000
     gap = 307
     i = gap
     while i != 0:
         t.insert(i)
         i = (i + gap) % nums
Example #3
0
 def testLargeInserts(self):
     t = SplayTree()
     nums = 40000
     gap = 307
     i = gap
     while i != 0:
         t.insert(i)
         i = (i + gap) % nums
Example #4
0
class TestCase(unittest.TestCase):
    def setUp(self):
        self.keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.t = SplayTree()
        for key in self.keys:
            self.t.insert(key)

    def testInsert(self):
        for key in self.keys:
            self.assertEquals(key, self.t.find(key))

    def testRemove(self):
        for key in self.keys:
            self.t.remove(key)
            self.assertEquals(self.t.find(key), None)

    def testLargeInserts(self):
        t = SplayTree()
        nums = 40000
        gap = 307
        i = gap
        while i != 0:
            t.insert(i)
            i = (i + gap) % nums

    def testIsEmpty(self):
        self.assertFalse(self.t.isEmpty())
        t = SplayTree()
        self.assertTrue(t.isEmpty())

    def testMinMax(self):
        self.assertEquals(self.t.findMin(), 0)
        self.assertEquals(self.t.findMax(), 9)
class TestCase(unittest.TestCase):
	def setUp(self):
		self.keys = [0,1,2,3,4,5,6,7,8,9]
		self.t = SplayTree()
		for key in self.keys:
			self.t.insert(key)

	def testInsert(self):
		for key in self.keys:
			self.assertEquals(key, self.t.find(key))

	def testRemove(self):
		for key in self.keys:
			self.t.remove(key)
			self.assertEquals(self.t.find(key), None)

	def testLargeInserts(self):
		t = SplayTree()
		nums = 40000
		gap = 307
		i = gap
		while i != 0:
			t.insert(i)
			i = (i+gap) % nums

	def testIsEmpty(self):
		self.assertFalse(self.t.isEmpty())
		t = SplayTree()
		self.assertTrue(t.isEmpty())

	def testMinMax(self):
		self.assertEquals(self.t.findMin(),0)
		self.assertEquals(self.t.findMax(),9)
Example #6
0
def test0():

    s = SplayTree()

    s.insert(3)
    s.insert(2)
    s.insert(1)

    s.find(3)
    s.find(2)
    s.find(1)
Example #7
0
def sim_lru_cache(a):

    c = int(a)

    if c < num_trace - 1:
        input_file_loc = input_dir + MS[m_index] + DS[
            d_index] + "cluster_" + str(c)
        output_file_loc = output_dir + MS[m_index] + DS[
            d_index] + "rd_cluster_" + str(c)
    else:
        input_file_loc = input_dir + MS[m_index] + DS[d_index] + "single_trace"
        output_file_loc = output_dir + MS[m_index] + DS[d_index] + "rd_single"

    times, customers, keys, sizes = reader(input_file_loc)

    n = len(times)
    T = SplayTree()
    H = defaultdict(lambda: None)

    rds = []
    for j in range(n):
        rd = cal_req_rd(times[j], keys[j], sizes[j], T, H)
        rds.append(rd)

    save_rds(output_file_loc, rds, customers, c)
Example #8
0
def test():

    n1 = Node(1)
    n2 = Node(2)

    assert not n1.equals(n2)

    s = SplayTree()

    assert s.findMin() == None
    assert s.findMax() == None
    assert s.find(1) == None
    assert s.isEmpty()

    s.insert(80)
    s.insert(81)
    s.insert(82)

    s.insert(1)
    s.find(-999)
    s.insert(1)

    s.insert(3)
    s.insert(2)

    for i in range(20):
        s.insert(i)

    for i in range(40, 20, -1):
        s.insert(i)

    assert s.findMin() != None
    assert s.findMax() != None
    assert s.find(1) != None
    assert not s.isEmpty()

    s.insert(80)
    s.insert(81)
    s.insert(82)
    s.insert(82)

    s.remove(1)
    s.remove(2)
    s.remove(3)

    for i in range(20):
        s.remove(i)
Example #9
0
 def setUp(self):
     self.keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     self.t = SplayTree()
     for key in self.keys:
         self.t.insert(key)
Example #10
0
 def testIsEmpty(self):
     self.assertFalse(self.t.isEmpty())
     t = SplayTree()
     self.assertTrue(t.isEmpty())
Example #11
0
class TestCase(unittest.TestCase):
    def setUp(self):
        self.keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        self.t = SplayTree()
        for key in self.keys:
            self.t.insert(key)

    def testInsert(self):
        for key in self.keys:
            self.assertEquals(key, self.t.find(key))
            
        # Forgot in first phase, insert an elem in tree already there
        self.t.insert(self.keys[0])    

    def testRemove(self):
        for key in self.keys:
            self.t.remove(key)
            self.assertEquals(self.t.find(key), None)
        # Forgot to remove elem not in tree
        self.t.remove(-13)    

    def testLargeInserts(self):
        t = SplayTree()
        nums = 40000
        gap = 307
        i = gap
        while i != 0:
            t.insert(i)
            i = (i + gap) % nums

    def testIsEmpty(self):
        self.assertFalse(self.t.isEmpty())
        t = SplayTree()
        self.assertTrue(t.isEmpty())

    def testMinMax(self):
        self.assertEquals(self.t.findMin(), 0)
        self.assertEquals(self.t.findMax(), 9)
        # Forgot in first phase, check when there no element in tree
        t = SplayTree()
        self.assertEquals(t.findMin(), None)
        self.assertEquals(t.findMax(), None)
        
    def TearDown(self):
        del self.t
Example #12
0
 def setUp(self):
     self.keys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     self.t = SplayTree()
     for key in self.keys:
         self.t.insert(key)
Example #13
0
 def testIsEmpty(self):
     self.assertFalse(self.t.isEmpty())
     t = SplayTree()
     self.assertTrue(t.isEmpty())
Example #14
0
def main():
    #start Binary Search Tree
    bst = BST()
    timeforInsertBST = time.time()
    print(
        "First insecd crt 50000 random nodes into an empty tree and print the tree."
    )
    for i in range(50000):
        n = random.randint(0, 1000000)
        bst.insert(BSTNode(n, bst.nil, bst.nil, bst.nil))
    print("time for BST to insert", time.time() - timeforInsertBST)

    timeforSearchBST = time.time()
    print("\nNow do 10000 searches. ")
    count = 0
    for i in range(10000):
        n = random.randint(0, 1000000)
        if bst.search(n) == bst.nil:
            count = count + 1

    print("time for BST to search =", time.time() - timeforSearchBST)
    ##print time.time()-start1, "time for BST search"
    print(count)

    timeForDeleteBST = time.time()
    print("\nNow search for & delete some nodes.")
    for i in range(1000):
        n = random.randint(0, 1000000)
        z = bst.search(n)
        if z != bst.nil:
            bst.delete(z)
    print("time for BST delete =", time.time() - timeForDeleteBST)

    ##start splay tree
    splayTree = SplayTree()
    timeforInsertSplay = time.time()
    print(
        "\nFirst insert 50000 random nodes into an empty tree and print the tree."
    )
    n = random.randint(0, 1000)
    for i in range(50000):
        n = n + 1
        splayTree.insert(
            BSTNode(n, splayTree.nil, splayTree.nil, splayTree.nil))
    print("time for splay to insert =", time.time() - timeforInsertSplay)

    timeForSearchSplay = time.time()
    print("\nNow do 10000 searches. ")
    count2 = 0
    n = random.randint(0, 1000)
    for i in range(10000):
        n = n + 1
        if splayTree.search(n) == splayTree.nil:
            count2 = count2 + 1

    print("time for splay to search =", time.time() - timeForSearchSplay)
    print(count2)

    timeForDeleteSplay = time.time()
    print("\nNow search for & delete some nodes.")
    n = random.randint(0, 1000)
    for i in range(1000):
        n = n + 1
        z = splayTree.search(n)
        if z != splayTree.nil:
            splayTree.delete(z)
    print("time for splay delete =", time.time() - timeForDeleteSplay)