zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    phpdocumentor生成代码注释文档(linux)
    phpstorm扩展
    es教程
    康威定律
    k8s
    tidb调研
    netty 在线教程
    McQueenRPC源码阅读
    DIY一些基于netty的开源框架
    性能测试
  • 原文地址:https://www.cnblogs.com/similarface/p/5128795.html
Copyright © 2011-2022 走看看