Question

In: Computer Science

Write a function child_indices(parent, branch_factor) that returns a list containing the list of indices where the...

Write a function child_indices(parent, branch_factor) that returns a list containing the list of indices where the children of the node at index parent will be stored for a heap list with the given branch_factor (ie, a branch_factor-heap). Python language

Notes:

  • The function should return all possible indices and the indices should be in order from smallest to biggest.
  • The values in the heap list start at index 1 - that is the root of the heap is at index 1 in the heap list.
Test Result
print(child_indices(1,2))
[2, 3]
print(child_indices(2,2))
[4, 5]
print(child_indices(1,3))
[2, 3, 4]

Solutions

Expert Solution

Hope this will help you, if you have any doubt please let me know.

Please let me know if you want any modification in the program (I have used a for loop in it)

Notes:-There are no restriction is provided in the statement, hence I have made this way, over here we have to just compute child indices. (We are not caring about the value stored at those indices)

We have also given the values in the heap list start at index 1. That is the root of the heap is at index 1 in the heap list.

Code is well commented also output of screenshot is attached.

Please feel free to ask me anything at anytime.

Also tested with additional test cases please see the screenshot

------------------Code-----------------

def child_indices(parent, branch_factor):
indices=[] #list that returns a branch indices
if(branch_factor>=2): #checking if branch_factor is greater than 1
# for loop iterate till branch factor (means i have a value starting from 0 to branch factor-1)
for i in range(branch_factor):
# then calculting indices using following formula
#lets take a parent=2 and branch_factor 3
# first root node is 1 whose childer is at index 2,3,4
#now parent=2 whose child are at index 5,6,7
#first child is obtained =3(branch_factor)*2(parent)-((branch_factor)3-2)+(i=0 for first iteration)
#firstchild=6-1+0(i=0)=5
#seconchild=6-1+1 (i=1)=6
#same for child three,
#works for all cases
k=branch_factor*parent-(branch_factor-2)+i # calculating child indices
indices.append(k)# adding indices into list
#useless case
else:
k=branch_factor*parent+1 # if branch_factor is 1 its child index is incremented by 1
indices.append(k) # adding indices into list
return indices # returning the list

print(child_indices(1,2))
print(child_indices(2,2))
print(child_indices(1,3))


-------------------------------------------output----------


Related Solutions

Write a function parent_index_3_heap(child_index) that returns the index of the parent of the node at the...
Write a function parent_index_3_heap(child_index) that returns the index of the parent of the node at the given child_index. A None value should be returned if the child has no parent. Notes: This function is obviously for a 3-heap! The values in the heap list start at index 1 - that is the root of the heap is at index 1 in the heap list. This is for Python
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all...
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all letters occurring in the parameter text. For example: if __name__ == '__main__': d = letter_frequencies('hello, world') print(d) # show the contents of this dictionary will produce the following output: {'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.1, 'e': 0.1, 'f': 0.0, 'g': 0.0, 'h': 0.1, 'i': 0.0, 'j': 0.0, 'k': 0.0, 'l': 0.3, 'm': 0.0, 'n': 0.0, 'o': 0.2, 'p': 0.0, 'q': 0.0,'r': 0.1,...
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT