斐波那契堆的实现--python


class FibNode:
    def __init__(self, key):
        self.key = key
        self.degree = 0
        self.p = 0
        self.child = 0
        self.left =0
        self.right = 0
        self.mark = False

    def get_child(self, cn):
        cn.p = self
        self.degree = self.degree + 1
        if self.child == 0:
            self.child=cn
            cn.left =cn
            cn.right=cn
        else:
            c=self.child
            cn.right=c
            cn.left=c.left
            cn.left.right=cn

    def print_node(self):
        if self.child !=0:
            c=self.child
            cl=c.left
            while 1:
                print("%d has child: %d" % (self.key, c.key))
                c.print_node()
                if cl==c:
                    break
                c=c.right

    def search_child(self,key):
        w=self
        v=self
        res=0
        while 1:
            if w.key==key:
                return w
            if w.child!=0:
                res=w.child.search_child(key)
            if res!=0:
                return res
            w=w.right
            if w==v:
                return 0


def make_heap():
    return Fib(0,0)


class Fib():
    def __init__(self,n,minx):
        self.n=n
        self.min=minx

    def insert(self,x):
        if self.min==0:
            self.min=x
            x.left=x
            x.right=x
        else:
            x.right=self.min
            x.left=self.min.left
            x.left.right=x
            if x.key y.key:
                    v=x
                    x=y
                    y=v
                self.Link(y,x)
                A[d]=0
                d=d+1
            A[d]=x
            if w==t:
                break
            w=temp

        self.min=0
        for i in range(0,int(math.log(self.n,2)+1)):
            if A[i]!=0:
                if self.min==0:
                    self.min=A[i]
                    self.left=A[i]
                    self.right=A[i]
                else:
                    A[i].right=self.min
                    A[i].left=self.min.left
                    A[i].left.right=A[i]
                    self.min.left=A[i]
                    if A[i].keyx.key:
            print("error: new key is greater than current key!")
        x.key=k
        y=x.p
        if y!=0 and x.keykey:
            return 0
        else:
            cr=w
            while 1:
                if cr.key==key:
                    return cr
                else:
                    if cr.child!=0:
                        res=cr.child.search_child(key)
                    if res!=0:
                        return res
                    cr=cr.right
                    if cr==w:
                        return 0

    def print_heap(self,key):
        pass
        root=self.min
        c=root
        print("min node and root node is: %d "% c.key)
        c.print_node()
        c=c.right
        while c!=root:
            print("root node is :%d" % c.key)
            c.print_node()
            c=c.right

你可能感兴趣的:(数据结构与算法)