Advertisement
Checosnake

memory error

Oct 22nd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. def answer(h, q):
  2.     #Setting up Variables to use later on
  3.     size = (2**h)-1
  4.     tree = []
  5.     vals = []
  6.     final = []
  7.     #Populate Values to be inserted into tree
  8.     #Fill Tree with filler
  9.     for i in range(0,size,1):
  10.         vals.append(i+1)
  11.         tree.append(-2)
  12.     #Function used to insert values to tree
  13.     def addVal(i):
  14.         tree[i] = vals.pop(0)
  15.     #PostOrder Insertion
  16.     def postOrder(i):
  17.         if (i*2)+1 > size or (i+1)*2 > size:
  18.             addVal(i)
  19.         else:
  20.             postOrder((i*2)+1)
  21.             postOrder((i+1)*2)
  22.             addVal(i)
  23.     #returns parent node of a given index
  24.     def getParent(i):
  25.         if i == 0 or i > size:
  26.             return -1
  27.         else:
  28.             return (i-1)/2
  29.     #returns value being stored at indexed node
  30.     def getValue(i):
  31.         if i == -1:
  32.             return -1
  33.         else:
  34.             return tree[i]
  35.     #Populate Tree with Values
  36.     postOrder(0)
  37.     #return List composed of the values of the parents of provided list
  38.     for i in range(0, len(q), 1):
  39.         final.append(getValue(getParent(tree.index(q.pop(0)))))
  40.     #return List with Values of Parents
  41.     return final
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement