def testDelete(self): for e, expected in self.DELETE: f = functional.avltree(t[0] for t in self.INSERT) self.assertEqual(f, self.INSERT[-1][1]) f = functional.delete(f, e) self.assertEqual(f, expected) r = recursive.AVLTree(t[0] for t in self.INSERT) self.assertEqual(r.toTuple(), self.INSERT[-1][1]) r.delete(e) self.assertEqual(r.toTuple(), expected) i = iterative.AVLTree(t[0] for t in self.INSERT) self.assertEqual(i.to_tuple(), self.INSERT[-1][1]) i.delete(e) self.assertEqual(i.to_tuple(), expected) c = cavltree.AVLTree(t[0] for t in self.INSERT) self.assertEqual(c.to_tuple(), self.INSERT[-1][1]) c.delete(e) self.assertEqual(c.to_tuple(), expected)
def testInsert(self): f = None r = recursive.AVLTree() i = iterative.AVLTree() c = cavltree.AVLTree() for e, expected in self.INSERT: f = functional.insert(f, e) self.assertEqual(f, expected) r.insert(e) self.assertEqual(r.toTuple(), expected) i.insert(e) self.assertEqual(i.to_tuple(), expected) c.insert(e) self.assertEqual(c.to_tuple(), expected)
def testInsert(self): """ First fill a tree to a given height such that the bottom layer is half full, then measure the time it takes to insert another quarter. """ result = [] output = { 'test': 'insert', 'operation': 'insert', 'types': self.TYPES, 'result': result } for height in self.HEIGHTS: bottom = layer(height) initial = capacity(height - 1) + (bottom // 2) count = bottom // 4 d = {'height': height, 'count': count} for k in output['types']: d[k] = [] result.append(d) for n in range(1, self.TRIES + 1): logging.debug('height: %d, count: %d, try: %d', height, count, n) source = list(randints(initial)) extend = list(randints(count)) # Built-in list v = [] insort(v, source) with Stopwatch() as sw: insort(v, extend) d['list'].append(sw.total) # Functional ftree = functional.avltree(source) with Stopwatch() as sw: ftree = functional.avltree(extend, ftree) d['functional'].append(sw.total) # Recursive rtree = recursive.AVLTree(source) with Stopwatch() as sw: for e in extend: rtree.insert(e) d['recursive'].append(sw.total) # Iterative itree = iterative.AVLTree(source) with Stopwatch() as sw: for e in extend: itree.insert(e) d['iterative'].append(sw.total) # Extension etree = cavltree.AVLTree(source) with Stopwatch() as sw: for e in extend: etree.insert(e) d['extension'].append(sw.total) # Check correctness f = list(functional.inorder(ftree)) r = list(rtree) i = list(itree) e = list(etree) source = sorted(set(source + extend)) self.assertEqual(v, source) self.assertEqual(f, source) self.assertEqual(r, source) self.assertEqual(i, source) self.assertEqual(e, source) with open(os.path.join(self.OUTPUT, 'insert.json'), 'w') as fp: json.dump(output, fp, indent=4)
def testDelete(self): """ First fill a tree to a given height such that the bottom layer is half full, then measure the time it takes to remove random elements until it is quarter full. """ result = [] output = { 'test': 'delete', 'operation': 'delete', 'types': self.TYPES, 'result': result } for height in self.HEIGHTS: bottom = layer(height) initial = capacity(height - 1) + (bottom // 2) count = bottom // 4 d = {'height': height, 'count': count} for k in output['types']: d[k] = [] result.append(d) for n in range(1, self.TRIES + 1): logging.debug('height: %d, count: %d, try: %d', height, count, n) source = list(randints(initial)) remove = list(choices(source, count)) # Built-in list v = [] insort(v, source) with Stopwatch() as sw: for e in remove: i = bisect.bisect_left(v, e) del v[i] d['list'].append(sw.total) # Functional ftree = functional.avltree(source) with Stopwatch() as sw: for e in remove: ftree = functional.delete(ftree, e) d['functional'].append(sw.total) # Recursive rtree = recursive.AVLTree(source) with Stopwatch() as sw: for e in remove: rtree.delete(e) d['recursive'].append(sw.total) # Iterative itree = iterative.AVLTree(source) with Stopwatch() as sw: for e in remove: itree.delete(e) d['iterative'].append(sw.total) # Extension ctree = cavltree.AVLTree(source) with Stopwatch() as sw: for e in remove: ctree.delete(e) d['extension'].append(sw.total) # Check correctness f = list(functional.inorder(ftree)) r = list(rtree) i = list(itree) c = list(ctree) source.sort() for e in remove: source.remove(e) self.assertEqual(v, source) self.assertEqual(f, source) self.assertEqual(r, source) self.assertEqual(i, source) self.assertEqual(c, source) with open(os.path.join(self.OUTPUT, 'delete.json'), 'w') as fp: json.dump(output, fp, indent=4)
def testFill(self): """ Measure the time it takes to fill a tree to a given height. """ result = [] output = { 'test': 'fill', 'operation': 'insert', 'types': self.TYPES, 'result': result } for height in self.HEIGHTS: count = capacity(height) d = {'height': height, 'count': count} for k in output['types']: d[k] = [] result.append(d) for n in range(1, self.TRIES + 1): logging.debug('height: %d, count: %d, try: %d', height, count, n) source = list(randints(count)) # Built-in list v = [] with Stopwatch() as sw: insort(v, source) d['list'].append(sw.total) # Functional with Stopwatch() as sw: ftree = functional.avltree(source) d['functional'].append(sw.total) # Recursive with Stopwatch() as sw: rtree = recursive.AVLTree(source) d['recursive'].append(sw.total) # Iterative with Stopwatch() as sw: itree = iterative.AVLTree(source) d['iterative'].append(sw.total) # Extension with Stopwatch() as sw: etree = cavltree.AVLTree(source) d['extension'].append(sw.total) # Check correctness f = list(functional.inorder(ftree)) r = list(rtree) i = list(itree) e = list(etree) source = sorted(set(source)) self.assertEqual(v, source) self.assertEqual(f, source) self.assertEqual(r, source) self.assertEqual(i, source) self.assertEqual(e, source) with open(os.path.join(self.OUTPUT, 'fill.json'), 'w') as fp: json.dump(output, fp, indent=4)