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_)

转载于:https://www.cnblogs.com/Salaku/p/5671226.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值