python 键值对的树实现

 

#coding:utf-8
__author__ = 'similarface'

class KeyedBinaryTree:
    def __init__(self):self.tree=EmptyNode()
    def __repr__(self):return repr(self.tree)
    def lookup(self,key):return self.tree.lookup(key)
    def insert(self,key,val):self.tree=self.tree.insert(key,val)

class EmptyNode:
    def __repr__(self):
        return '*'
    def lookup(self,key):
        return None

    def insert(self,key,val):
        return BinaryNode(self,key,val,self)

class BinaryNode:
    def __init__(self,left,key,val,right):
        self.key,self.val=key,val
        self.left,self.right=left,right

    def lookup(self,key):
        if self.key==key:
            return self.val
        elif self.key>key:
            return self.left.lookup(key)
        else:
            return self.right.lookup(key)
    def insert(self,key,val):
        if self.key==key:
            self.val=val
        elif self.key>key:
            self.left=self.left.insert(key,val)
        elif self.key<key:
            self.right=self.right.insert(key,val)
        return self

    def __repr__(self):
        return ('(%s,%s=%s,%s)'%(repr(self.left),repr(self.key),repr(self.val),repr(self.right)))

if __name__=='__main__':
    t=KeyedBinaryTree()
    for (key,val) in [('bbb',1),('aaa',2),('ccc',3)]:
        t.insert(key,val)
    print(t)
    print(t.lookup('aaa'),t.lookup('ccc'))
    t.insert('ddd',4)
    t.insert('aaa',5)
    print(t)

 

posted @ 2016-01-13 22:49  similarface  阅读(503)  评论(0编辑  收藏  举报