def make_splay_tree(numbers=None): if not numbers: numbers = [5, 2, 7, 1, 3, 6, 8, 0, 9, 4] tree = SplayTree.SplayTree() for i in numbers: tree.insert(i) return tree
def test_splay_tree_search(): time_begin = time.time() st = SplayTree.SplayTree() for i in range(1000): st.insert(i) for s in range(1000): st.search(st.root, random.randint(0, 1000)) st.print_tree_vertical() print 'Time: ', time.time() - time_begin
def test_delete__2_node_tree_with_parent_and_right_child__delete_parent( self): st = SplayTree.SplayTree() st.insert(2) st.insert(1) node1 = st.search(st.root, 1) node2 = st.search(st.root, 2) st.delete(node1) self.assertTrue(st.root == node2 and node2.parent == None and node2.left == None and node2.right == None)
def compare_insertion_and_search(): rounds = int(raw_input('How many rounds of test? ')) insertions = int( raw_input('How many insertions (nodes in the structure)? ')) top_percent = float( raw_input('How many percent of the values are searched? ')) repeat = int(raw_input('How many times those values are searched? ')) values = random.sample(range(insertions * 10), insertions) search_values = random.sample(values, int(top_percent * insertions / 100)) searches = search_values * repeat random.shuffle(searches) for round in range(rounds): print '-' * 20 sl = SkipList.SkipList() st = SplayTree.SplayTree() t = Treap.Treap() structures = [(sl, 'Skip list'), (st, 'Splay tree'), (t, 'Treap')] print '\nInsertions:' for structure in structures: time_begin = time.time() for key in range(insertions): structure[0].insert(key) print('Inserting ' + str(insertions) + ' values to empty ' + structure[1] + ' took ' + str(time.time() - time_begin) + ' seconds') print '\nSearches:' time_begin = time.time() for key in searches: sl.search(key) print('Searching ' + str(len(searches)) + ' values from skip list took ' + str(time.time() - time_begin) + ' seconds') time_begin = time.time() for key in searches: st.search(st.root, key) print('Searching ' + str(len(searches)) + ' values from splay tree took ' + str(time.time() - time_begin) + ' seconds') time_begin = time.time() for key in searches: t.search(t.root, key) print('Searching ' + str(len(searches)) + ' values from treap took ' + str(time.time() - time_begin) + ' seconds')
def start_test(self, type): results = np.zeros((50, 3)) for index, i in enumerate(self.bases): # create if type == 0: tree = BinaryTree() elif type == 1: tree = AVLTree() elif type == 2: tree = Treap() elif type == 3: tree = SplayTree() for e in self.dataset[0:i]: tree.insert(e) results[index, :] = self.test_tree(tree, type) return results
#!/usr/bin/python #encoding=utf-8 file=raw_input("Enter source file:\n") from time import clock start=clock() from SplayTree import * import re a=SplayTree() f=open(file) txt=f.read() f.close() txt=re.compile(r'[^a-z^A-Z]?([a-zA-Z]+)[^a-z^A-Z]?').findall(txt) #该正则表达式匹配单词,包括a2s中的a和s for i in txt: a.insert(i) print a.top10() #a.printall() print 'There are %d words in %s'%(a.sum,file) finsih=clock() print'Run time is %d s'%(finsih - start)
def initialize_test_delete__3_node_tree_all_left(self): self.st = SplayTree.SplayTree() self.nodes = [] for key in range(3): self.st.insert(key) self.nodes.append(self.st.search(self.st.root, key))
def test_delete__only_node(self): st = SplayTree.SplayTree() st.insert(1) node = st.search(st.root, 1) st.delete(node) self.assertTrue(st.root == None)
#!/usr/bin/python #encoding=utf-8 file = raw_input("Enter source file:\n") from time import clock start = clock() from SplayTree import * import re a = SplayTree() f = open(file) txt = f.read() f.close() txt = re.compile(r'[^a-z^A-Z]?([a-zA-Z]+)[^a-z^A-Z]?').findall( txt) #该正则表达式匹配单词,包括a2s中的a和s for i in txt: a.insert(i) print a.top10() #a.printall() print 'There are %d words in %s' % (a.sum, file) finsih = clock() print 'Run time is %d s' % (finsih - start)