class Fibheap: def __init__(self): self.min = None self.n = 0 class Node: def __init__(self, key): self.key = key self.refresh() def refresh(self): self.degree = 0 self.parent = None self.child = None self.left = self self.right = self self.mark = False def insert(H, x): x.refresh() if H.min == None: H.min = x else: w = H.min.left y = H.min x.left = w x.right = y w.right = x y.left = x if x.key < H.min.key: H.min = x H.n = H.n + 1 def addlst(y, x): if y == None or x == None: return z = y while z.right != y: z.parent = x.parent z = z.right z.parent = x.parent y.left = x.left x.left.right = y z.right = x x.left = z def extract(H): x = H.min if x != None: if x.child != None: addlst(x.child, x) x.left.right = x.right x.right.left = x.left if x == x.right: H.min = None else: H.min = x.right H.n = H.n - 1 x.refresh() return x def dec_key(H, x, k): assert(k <= x.key) if k == x.key: return x.key = k y = x.parent if y != None and x.key < y.key: cut(H, x, y) cascading_cut(H, y) if x.key < H.min.key: H.min = x def make_heap(): return Fibheap() def make_node(k): return Node(k) def cut(H, x, y): x.left.right = x.right x.right.left = x.left y.degree = y.degree - 1 y.child = x.right if x == x.right: y.child = None x.parent = None x.mark = False x.left = H.min.left x.right = H.min x.left.right = x x.right.left = x def cascading_cut(H, y): z = y.parent if z != None: if y.mark == False: y.mark = True else: cut(H, y, z) def union(H1, H2): H = Fibheap() if H1.min != None and H2.min == None: H.min = H1.min H.n = H1.n elif H1.min == None and H2.min != None: H.min = H2.min H.n = H2.n elif H1.min != None and H2.min != None: addlst(H2.min, H1.min) if H1.min.key <= H2.min.key: H.min = H1.min else: H.min = H2.min H.n = H1.n + H2.n return H def print_heap(s): x = s first_iteration = True while x != None and (first_iteration or x != s): first_iteration = False f1.write(x.key+'\n') if x.child != None: print_heap(x.child, indent+char, char) x = x.right f=open("in_sec21_4.txt","r") f1=open("out_34_sec21_4.txt","a") s=f.readline() s1=s.split(',') len1= len(s1) min = None max = None nodes = [] H=make_heap() for i in range(len1): n = make_node(s1[i]) if min == None or n.key < min.key: min = n if max == None or n.key > max.key: max = n nodes.append(n) insert(H, n) f1.write('After insertion :\n') print_heap(H.min) extract(H) a=54 f1.write('After deletion:\n') for i in range(len1): if a != s1[i]: f1.write(str(s1[i])) f1.write('\n')