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
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.
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).
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
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, 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.
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
SML Complete the following programs. 1. Write a function listify that takes a list and returns...
SML Complete the following programs. 1. Write a function listify that takes a list and returns a list of lists where each element of first list becoming its own single-element list (Fill in the code here) fun main() = let val lst = (explode "rad"); in print (PolyML.makestring ( listify lst )) end; 2. Write a function splitlist that takes a list of pairs and returns a pair of lists that has the firsts of the pairs in one list...
1a           Write a function COTlistRange(xstart,xend,xstep) that returns a list that has:                - as index 0...
1a           Write a function COTlistRange(xstart,xend,xstep) that returns a list that has:                - as index 0 item, the value of xstart                - as index 1 item, the value xstart+xstep                - as index 2 item, the value (xstart+xstep)+xstep,                and so on as long as the value does equal or pass the value of xend.                However, the function should return an empty string if for positive xstep, xstart>xend or if for negative xstep, xstart < xend. Here are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT