示例#1
0
class SymbolGraph:
    def __init__(self, stream, sp, cls_g=Graph):
        self.st = ST()      # {key: i}
        for line in open(stream):
            for key in line.strip().split(sp):
                if not self.st.contains(key):
                    self.st.put(key, self.st.size())
        self.keys = [""] * self.st.size()       # reverse index, {i: key}
        for key in self.st:
            self.keys[self.st.get(key)] = key
        self.G = cls_g(self.st.size())      # use int i as vertex
        for line in open(stream):
            a = line.strip().split(sp)
            v = self.st.get(a[0])
            for i in range(1, len(a)):
                self.G.add_edge(v, self.st.get(a[i]))

    def contains(self, s):
        return self.st.contains(s)

    def index(self, s):
        return self.st.get(s)

    def name(self, v):
        return self.keys[v]

    def graph(self):
        return self.G
示例#2
0
class BSTTestCase(unittest.TestCase):
    def setUp(self):
        self.tree = BST()
        self.nodes = {5: 'five', 7: 'seven', 2: 'two', 3: 'three', 1: 'one', 8: 'eight'}
        for k, v in self.nodes.items():
            self.tree.put(k,v)

    def tearDown(self):
        del self.tree
        del self.nodes

    def testPutGet(self):
        for k, v in self.nodes.items():
            self.assertEqual(v, self.tree.get(k))
        for k in [4,6,9]:
            self.assertIsNone(self.tree.get(k))

    def testLen(self):
        self.assertEqual(6, len(self.tree))

    def testMinKey(self):
        self.assertEqual(1, self.tree.minKey())

    def testMin(self):
        self.assertTupleEqual((1, 'one'), self.tree.min())

    def testMaxKey(self):
        self.assertEqual(8, self.tree.maxKey())

    def testMax(self):
        self.assertTupleEqual((8, 'eight'), self.tree.max())

    def testIsEmpty(self):
        self.assertFalse(self.tree.isEmpty())
        self.assertTrue(BST().isEmpty())

    def testItems(self):
        self.assertSequenceEqual(sorted(self.nodes.items()), list(self.tree.items()))

    def testKeys(self):
        self.assertSequenceEqual(sorted(self.nodes.keys()), list(self.tree.keys()))

    def testContains(self):
        self.assertTrue(self.tree.contains(8))
        self.assertFalse(self.tree.contains(13))

    def testKeysBetween(self):
        self.assertSequenceEqual([2,3,5], list(self.tree.keys_between(2, 6)))

    def testNumKeysBetween(self):
        # self.nodes = {5: 'five', 7: 'seven', 2: 'two', 3: 'three', 1: 'one', 8: 'eight'}
        self.assertEqual(4, self.tree.num_between(3, 8))

    def testRank(self):
        self.assertEqual(2, self.tree.rank(3))
示例#3
0
from bst import BST
if __name__ == '__main__':
    t = BST() # 이진탐색트리 객체 t 생성
    t.put(500, 'apple')
    t.put(600, 'banana')
    t.put(200, 'melon')
    t.put(100, 'orange')
    t.put(400, 'lime')
    t.put(250, 'kiwi')
    t.put(150, 'grape')
    t.put(800, 'peach')
    t.put(700, 'cherry')
    t.put(50,  'pear')
    t.put(350, 'lemon')
    t.put(10,  'plum')
    print('전위순회:\t', end='')
    t.preorder(t.root)
    print('\n중위순회:\t', end='')
    t.inorder(t.root)
    print('\n250: ',t.get(250))
    t.delete(200)
    print('200 삭제 후:')
    print('전위순회:\t', end='')
    t.preorder(t.root)
    print('\n중위순회:\t', end='')
    t.inorder(t.root)