2015-02-11 15 views
-4

当我运行这个时,什么也没有显示,甚至没有错误信息。所以,我想知道什么是错的这个问题以及如何正确地调用这些功能。(我猜的东西错了,当我把这个功能)如何在python中调用这个函数

class BinHeap: 
    def makeheap(self): 
      self.heapList=[0]; 
      self.currentSize=0; 

    def perUp(self,i): 
      while i // 2 > 0: 
        if self.heapList[i]<self.heapList[i//2]: 
          tmp=self.heapList[i//2] 
          self.heapList[i//2]=self.heapList[i] 
          self.heapList[i]=tmp 
        i = i // 2 

    def insert(self,k): 
      self.heapList.append(k) 
      self.currentSize = self.currentSize+1 
      self.perUp(self.currentSize) 

    def percDown(self,i): 
      while (i * 2) <= self.currentSize: 
        mc = self.minChild(i) 
      if self.heapList[i] > self.heapList[mc]: 
        tmp = self.heapList[i] 
      self.heapList[i] = self.heapList[mc] 
      self.heapList[mc] = tmp 
      i = mc 

    def minChild(self,i): 
      if i * 2 + 1 > self.currentSize: 
        return i * 2 
      else: 
         if self.heapList[i*2] < self.heapList[i*2+1]: 
          return i * 2 
        else: 
          return i * 2 + 1 
    def delMin(self): 
      retval = self.heapList[1] 
      self.heapList[1] = self.heapList[self.currentSize] 
      self.currentSize = self.currentSize - 1 
      self.heapList.pop() 
      self.percDown(1) 
      return retval 

    def buildHeap(self,alist): 
      i = len(alist) // 2 
      self.currentSize = len(alist) 
      self.heapList = [0] + alist[:] 
      while (i > 0): 
        self.percDown(i) 
        i = i - 1 

bh=BinHeap() 
bh.buildHeap([9,5,6,2,3]) 
print("BinHeap") 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
+3

Python代码块可用于缩进;如果这是正确的缩进,那么您创建类为'bh = BinHeap()'和后缀的对象的代码将缩进为类的一部分。移出来,你应该开始看到一些执行。 – 2015-02-11 04:57:46

回答

0

Python是非常讲究的间距。你弄乱了空格/格式。编辑你的代码,让最后几行在它前面没有空格/标签,至少应该得到一两个错误。

# This is a subroutine to be executed when called 
def buildHeap(self, alist): 
    i = len(alist) // 2 
    self.currentSize = len(alist) 
    self.heapList = [0] + alist[:] 
    while (i > 0): 
     self.percDown(i) 
     i = i - 1  

# these are commands to be executed now 
bh = BinHeap() 
bh.buildHeap([9,5,6,2,3]) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
print(bh.delMin()) 
+0

我知道格式应该如何,因为StackOverflow一直要求我根据需要调整格式,或者我无法提交此问题。 – christ 2015-02-11 05:19:28

+0

然后我认为我们不能帮助你。您呈现的代码无论格式如何,都会显示至少一些错误。回到'print(“hello world”)'并继续前进。 – 2015-02-11 05:55:36

0

编辑:在您的代码中没有巨大的缩进问题。 “什么也没有显示,甚至没有错误信息”..这是肯定的!你的代码在buildHeap函数中的while循环中被阻塞了!它将出现在percDown功能这样做:

while (i * 2) <= self.currentSize: 
     mc = self.minChild(i) 

,然后让术语,“环回”到:

def minChild(self,i): 
     if i * 2 + 1 > self.currentSize: 

       return i * 2 

     else: 
        if self.heapList[i*2] < self.heapList[i*2+1]: 

         return i * 2 
        else: 
         return i * 2 + 1 

它开始这一次又一次的没有做,告诉你在什么贝壳。 很显然我会给你一些建议'回合它:

1)在你的代码行之间使用注释来澄清你在做什么。

a = b + C#It's stupid but "a" is equal to "b" plus "c" 

2)打印功能是你的朋友!它可以帮助你当贝壳“不说话”。 我为您提供打印语句代码的一些作品,只是看你如何使用它:

def buildHeap(self,alist): 
     i = len(alist) // 2 
     self.currentSize = len(alist) 
     self.heapList = [0] + alist[:] 
     while (i > 0): 
       print 'something wrong ..' 
       self.percDown(i) 
       print 'ok' 
       i = i - 1 
def percDown(self,i): 
     while (i * 2) <= self.currentSize: 
       print 'we re in the perc_Down' 
       mc = self.minChild(i) 
       print 'end of min child min child' 
     if self.heapList[i] > self.heapList[mc]: 
       tmp = self.heapList[i] 
     self.heapList[i] = self.heapList[mc] 
     self.heapList[mc] = tmp 
     i = mc 

def minChild(self,i): 
     if i * 2 + 1 > self.currentSize: 
       print 'if my min child' 
       return i * 2 
       print 'maybe stuck here ..' 
     else: 
        if self.heapList[i*2] < self.heapList[i*2+1]: 
         print '*' 
         return i * 2 
        else: 
         return i * 2 + 1 
         print '*' 

3)请不要做“如果”像你这样的语句在minChild功能做: 只要尝试将声明包含在第一个if!像:

#this is your organization 
if ... :    #1) if 
    #doing stuff 
else:     #1) else 
    if ... :       #2) if 
     #stuff again 
    else:       #2) else 
     .... 
#try this one 
if ... and ... :  #1) if .. and .. 2) if 

需要再次有所不同吗?那么你可以做elif声明:

if x < y: 
    print .... 
elif x > y: 
    print .... 
else: 
    print .... 

用这种方法解决方案更加优雅和可读。 还有一件事: 在Python中执行“函数”类时,构建您的“init”。每个对象都将具有构造函数中描述的属性,如:

class BinHeap: 
    def __init__(self): 
     self.currentSize = 0 
     self.heapList = []