python 2-3 tree

class Node:
    def __init__(self, leftValue, rightValue=None):
        self.leftValue = leftValue
        self.rightValue = rightValue
        self.leftChild = None
        self.centerChild = None
        self.rightChild = None
        self.parent = None
    def isLeaf(self):
        return self.leftChild == None

    def childList(self):
        result = [self.leftChild, self.centerChild, self.rightChild]
        return [i for i in result if i != None]

    def valueList(self):
        result = [self.leftValue, self.rightValue]
        return [i for i in result if i != None]
    def isEmpty(self):
        return len(self.valueList())==0
def setParent(parent,child,position='l'):
    if position == 'l':
        parent.leftChild = child
        if child!=None:
            child.parent = parent
    elif position == 'c':
        parent.centerChild = child
        if child!= None:
            child.parent = parent
    else:
        parent.rightChild = child
        if child!=None:
            child.parent = parent

def add(N, n):
    if N.rightValue == None:
        [Nx, Ny] = sorted([N,n],key=lambda i:i.leftValue)
        x, y = Nx.leftValue, Ny.leftValue
        Nxy = Node(x, y)
        setParent(Nxy,Nx.leftChild,'l')
        setParent(Nxy,Ny.centerChild,position='r')
        childrenList = [i for i in N.childList() if n.leftValue not in i.valueList()]+n.childList()
        setParent(child=findOne(childrenList, lambda i: x < i.leftValue < y),
                  parent=Nxy,position='c')

        return Nxy
    else:
        [x, y, z] = sorted(N.valueList()+n.valueList())
        l_link = [i for i in N.childList() if n.leftValue not in i.valueList()]+n.childList()
        Nx,Ny,Nz=Node(x),Node(y),Node(z)
        setParent(Nx,findOne(l_link, lambda i: i.leftValue < x),'l')
        setParent(Nx,findOne(l_link, lambda i: x < i.leftValue < y),'c')
        setParent(Nz,findOne(l_link, lambda i: y < i.leftValue < z),'l')
        setParent(Nz,findOne(l_link,lambda i:  z<i.leftValue),position='c')
        setParent(Ny,Nx,'l')
        setParent(Ny,Nz,'c')
        return Ny


def findOne(list, filter_):
    for x in list:
        if filter_(x):
            return x
    return None


def insert(root, item):
    if root == None:
        return Node(item)
    if root.isLeaf():
        return add(root, Node(item))
    else:
        if item < root.leftValue:
            retval = insert(root.leftChild, item)
            if set(root.leftChild.valueList()) <= set(retval.valueList()):
                setParent(root,retval,'l')
                return root
            else:
                return add(root, retval)
        elif root.rightValue == None or item < root.rightValue:
            retval = insert(root.centerChild, item)
            if set(root.centerChild.valueList()) <= set(retval.valueList()):
                setParent(parent=root,child=retval,position='c')
                return root
            else:
                return add(root, retval)
        else:
            retval = insert(root.rightChild, item)
            if set(root.rightChild.valueList()) <= set(retval.valueList()):
                setParent(parent=root,child=retval,position='r')
                return root
            else:
                return add(root, retval)



def bfs(N):
    q = []
    q.append(N)
    while q:
        p = q.pop(0)
        for v in p.valueList():
            print(v)
        for n in p.childList():
            q.append(n)

## 测试
root = Node('S')
root = insert(root, 'E')
root = insert(root, 'A')
root = insert(root, 'R')
root = insert(root, 'C')
root = insert(root, 'H')
root = insert(root, 'X')
root = insert(root, 'M')
root = insert(root, 'P')
root = insert(root, 'L')

root_ = Node('A')
root_ = insert(root_,'C')
root_ = insert(root_,'E')
root_ = insert(root_,'H')
root_ = insert(root_,'L')
root_ = insert(root_,'M')
root_ = insert(root_,'P')
root_ = insert(root_,'R')
root_ = insert(root_,'S')
root_ = insert(root_,'X')
root_ = insert(root_,'Z')

bfs(root_)
posted @ 2016-07-14 17:48  Salaku  阅读(304)  评论(0编辑  收藏  举报