def setUp(self): self.simple_trie = Trie() self.simple_trie.insert("abcccc") self.simple_trie.insert("efffddd") self.simple_trie.insert("eff") self.simple_trie.insert("efff") self.simple_trie.insert("efffz")
class TrieTestDuplicates(unittest.TestCase): def testDuplicateTrie(self): self.simple_trie = Trie() self.simple_trie.insert("aaaaaa") self.simple_trie.insert("aaaaaa") self.simple_trie.insert("aaaaaa") self.simple_trie.insert("aaaaaa") self.assertTrue(self.simple_trie.search("aaaaaa")) self.assertTrue(self.simple_trie.has_prefix("aaa")) self.assertFalse(self.simple_trie.has_prefix("c")) self.assertTrue(self.simple_trie.has_prefix(""))
def testLeafNodeNum2(self): self.simple_trie = Trie() self.simple_trie.insert("") self.simple_trie.insert("ssssssss") self.simple_trie.insert("sss") self.assertEqual(self.simple_trie.leaf_nodes_num, 1) self.simple_trie.insert("ssssssssssssssss") self.assertEqual(self.simple_trie.leaf_nodes_num, 1) self.simple_trie.insert("aaaassssssssssssssss") self.assertEqual(self.simple_trie.leaf_nodes_num, 2)
from simple_trie import Trie # create a trie from city name only us_city_names_trie = Trie() total_us_city_names = 0 with open("./city_names") as f: for line in f: total_us_city_names += 1 us_city_names_trie.insert(line) print "total city: %d; total leaf nodes: %d" % (total_us_city_names, us_city_names_trie.leaf_nodes_num) # create a trie from city name, but with state name as a prefix us_state_city_names_trie = Trie() total_us_state_city_names = 0 with open("./state_city_names") as f: for line in f: total_us_state_city_names += 1 us_state_city_names_trie.insert(line.strip()) print "total state city: %d; total leaf nodes: %d" % (total_us_state_city_names, us_state_city_names_trie.leaf_nodes_num) # create a trie from city name, but with state name as a postfix us_city_state_names_trie = Trie()
def testEmptyTrie(self): self.simple_trie = Trie() self.assertFalse(self.simple_trie.search(""))
class TrieTestEmpty(unittest.TestCase): def testEmptyTrie(self): self.simple_trie = Trie() self.assertFalse(self.simple_trie.search(""))
def testLeafNodeNum1(self): self.simple_trie = Trie() self.simple_trie.insert("AK#Koyuk") self.simple_trie.insert("AK#Koyukuk") self.assertEqual(self.simple_trie.leaf_nodes_num, 1)
class TrieTestLeafNodeNum(unittest.TestCase): def testLeafNodeNum1(self): self.simple_trie = Trie() self.simple_trie.insert("AK#Koyuk") self.simple_trie.insert("AK#Koyukuk") self.assertEqual(self.simple_trie.leaf_nodes_num, 1) def testLeafNodeNum2(self): self.simple_trie = Trie() self.simple_trie.insert("") self.simple_trie.insert("ssssssss") self.simple_trie.insert("sss") self.assertEqual(self.simple_trie.leaf_nodes_num, 1) self.simple_trie.insert("ssssssssssssssss") self.assertEqual(self.simple_trie.leaf_nodes_num, 1) self.simple_trie.insert("aaaassssssssssssssss") self.assertEqual(self.simple_trie.leaf_nodes_num, 2) def testLeafNodeNum3(self): self.simple_trie = Trie() self.simple_trie.insert("aaaaaa") self.simple_trie.insert("aaaaaa") self.assertEqual(self.simple_trie.leaf_nodes_num, 1)
class TrieTest1(unittest.TestCase): def setUp(self): self.simple_trie = Trie() self.simple_trie.insert("abcccc") self.simple_trie.insert("efffddd") self.simple_trie.insert("eff") self.simple_trie.insert("efff") self.simple_trie.insert("efffz") def testSearch(self): self.assertFalse(self.simple_trie.search("abcc")) self.assertTrue(self.simple_trie.search("abcccc")) self.assertTrue(self.simple_trie.search("efffddd")) self.assertFalse(self.simple_trie.search("efffdd")) def testStartsWithPrefix(self): self.assertEqual(self.simple_trie.starts_with_prefix("eff"), ["efff", "efffz", "efffddd"]) self.assertEqual(self.simple_trie.starts_with_prefix("xf"), [])