We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
def _siftdown(self, ndx): left = 2 * ndx + 1 right = 2 * ndx + 2 # determine which node contains the larger value largest = ndx if (left < self._count and # 有左孩子 self._elements[left] >= self._elements[largest] and self._elements[left] >= self._elements[right]): # 原书这个地方没写实际上找的未必是largest largest = left elif right < self._count and self._elements[right] >= self._elements[largest]: largest = right if largest != ndx: self._elements[ndx], self._elements[largest] = self._elements[largest], self._elements[ndx] self._siftdown(largest)
若self.elements = [4,3,2,1,0]这种结构的话,这种会报错,是否应该先判断右孩子是否存在,然后再判断左孩子?
def _siftdown(self, ndx): left = 2 * ndx + 1 right = 2 * ndx + 2 # determine which node contains the larger value largest = ndx if (right < self._count and # 有右孩子 self._elements[right] >= self._elements[largest] and self._elements[left] <= self._elements[right]): # 原书这个地方没写实际上找的未必是largest largest = right elif left < self._count and self._elements[left] >= self._elements[largest]: largest = left if largest != ndx: self._elements[ndx], self._elements[largest] = self._elements[largest], self._elements[ndx] self._siftdown(largest)
The text was updated successfully, but these errors were encountered:
有 test case 么? self.extract() 调用 _siftdown 之前 self._count 执行了减去1的操作, 这里 right = 2*ndx * 2 . 似乎不会越界。
_siftdown
Sorry, something went wrong.
No branches or pull requests
若self.elements = [4,3,2,1,0]这种结构的话,这种会报错,是否应该先判断右孩子是否存在,然后再判断左孩子?
The text was updated successfully, but these errors were encountered: