Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:58:56+00:00 2026-06-13T14:58:56+00:00

I have two questions: 1) Recently I’m try to build a max heap .

  • 0

I have two questions:

1) Recently I’m try to build a max heap. Even though I read CLRS I can’t find the bug as I run it. The following is my code…

def maxHeapify(list, index):
    int = index
    left = (int+1) * 2 - 1
    right = (int+1) * 2
    largest = 0
    if left < len(list):
        if (left <= len(list)) & (list[left] >= list[int]):
            largest = left
        else: 
            largest = int
    if right < len(list):
        if (right <= len(list)) & (list[right] >= list[largest]):
            largest = right
    else:
        pass
    if largest != int:
        listNew = swapWithinList(list, int, largest)
        listNew = maxHeapify(listNew, largest)
    else:
        return listNew


def swapWithinList(list, id1, id2):
    num1 = list[id1]
    num2 = list[id2]
    listNew = list[:id1]
    listNew.append(num2)
    listNew = listNew + list[(id1+1):id2]
    listNew.append(num1)
    listNew = listNew + list[(id2+1):]
    return listNew

I give input but the console just says:

UnboundLocalError: local variable 'listNew' referenced before assignment

does it mean that I put the return statement on the wrong line or there’s something I haven’t mentioned?

2) What is a iteration?

I am a bit embarrassed when I ask the question. But what is a iteration? Wiki says each repetition of the process means it, so is it a result the loop gives each round?

And iterator seems a basic element in Python, what’s difference between iterator and iteration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:58:57+00:00Added an answer on June 13, 2026 at 2:58 pm

    1:

    Without further comments, here’s the code adapted from Wikipedia:

    def max_heapify(A, i):
        left = 2 * i + 1
        right = 2 * i + 2
        largest = i
        if left < len(A) and A[left] > A[largest]:
            largest = left
        if right < len(A) and A[right] > A[largest]:
            largest = right
        if largest != i:
            A[i], A[largest] = A[largest], A[i]
            max_heapify(A, largest)
    
    def build_max_heap(A):
        for i in range(len(A) // 2, -1, -1):
            max_heapify(A, i)
    

    Example:

    def ptree(A, i=0, indent=0):
        if i < len(A):
            print '  ' * indent, A[i]
            ptree(A, i * 2 + 1, indent + 1)
            ptree(A, i * 2 + 2, indent + 1)
    
    A = range(9) 
    build_max_heap(A)
    ptree(A)
    

    Result:

     8
       7
         3
           1
           0
         4
       6
         5
         2
    

    Let us know if you have any questions.

    2:

    Iteration in python is technically a process of repeatedly calling some object’s next() method until it raises the StopIteration exception. Object that possesses this next method is called Iterator. An object that is able to provide an Iterator for the calling code is called Iterable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions here. How can I show the header of the pop
I have two questions. How can I get the spinner in the figure below
I have two questions. Question1: How can we add a reference to a third-party
I have two questions. Does it make sense when I have to choice of
I have two questions. I know it is possible to declare class objects in
I have two questions. I want to send SMSes from a web-site in PHP
I have two questions in regards to the PyDev (Python editor for Eclipse) and
I have two questions: 1) I have a few global variables for my website
I have two questions: (1) I learned somewhere that -O3 is not recommended with
I have two questions: From personal experience, what free blog engine is the best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.