Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1.  
  2. class BinaryTree:
  3.  
  4. def __init__(self, value):
  5. self.data = value
  6. self.left = None
  7. self.right = None
  8.  
  9. def getLeftChild(self):
  10. return self.left
  11.  
  12. def getRightChild(self):
  13. return self.right
  14.  
  15. def setLeftChild(self, value):
  16. self.left = value
  17.  
  18. def setRightChild(self, value):
  19. self.right = value
  20.  
  21. def setRootVal(self, value):
  22. self.data = value
  23.  
  24. def getRootVal(self):
  25. return self.data
  26.  
  27. def insertLeft(self, value):
  28. if self.left == None:
  29. self.left = BinaryTree(value)
  30. else:
  31. n = BinaryTree(value)
  32. n.left = self.left
  33. self.left = n
  34.  
  35. def insertRight(self, value):
  36. if self.right == None:
  37. self.right = BinaryTree(value)
  38. else:
  39. n = BinaryTree(value)
  40. n.right = self.right
  41. self.right = n
  42.  
  43. def __str__(self):
  44. return (" Parent: " + str(self.data) + " LeftChild: " + str(self.left) + " RightChild: " + str(self.right))
  45.  
  46. def placeSolutionIntoSolutionList(L1, X):
  47.  
  48. if not L1:
  49. print ("Solution: " + str(X))
  50. return X
  51.  
  52. y = int(max(L1))
  53. yComplement = int(max(X)) - y
  54. returnList = computeDistances(yComplement, X)
  55. secondChoiceReturnList = computeDistances(y, X)
  56.  
  57. print ("this is return" + str(returnList))
  58. print ("this is second" + str(secondChoiceReturnList))
  59.  
  60. treeStart = BinaryTree(X)
  61.  
  62. if set(returnList) <= set(L1):
  63.  
  64. print (L1)
  65. print ("this bullshit " + str(returnList))
  66.  
  67. X.add(yComplement)
  68. for i in range(0, len(returnList)):
  69. if returnList[i] in L1:
  70. L1.remove(returnList[i])
  71.  
  72. x_sorted = X
  73. left = placeSolutionIntoSolutionList(L1, x_sorted)
  74. treeStart.setLeftChild(left)
  75.  
  76. X.remove(yComplement)
  77. for i in range(0, len(returnList)):
  78. L1.add(returnList[i])
  79.  
  80. if set(secondChoiceReturnList) <= set(L1):
  81.  
  82. X.add(y)
  83. for i in range(0, len(secondChoiceReturnList)):
  84. if secondChoiceReturnList[i] in L1:
  85. L1.remove(secondChoiceReturnList[i])
  86.  
  87. x_sorted = X
  88. right = placeSolutionIntoSolutionList(L1, x_sorted)
  89. treeStart.setRightChild(right)
  90.  
  91. X.remove(y)
  92. for i in range(0, len(secondChoiceReturnList)):
  93. L1.add(secondChoiceReturnList[i])
  94.  
  95. return treeStart
  96.  
  97. def computeDistances(y, X):
  98.  
  99. distances = {}
  100. for i in range(0, len(X)):
  101. x = X[i]
  102. distances.add(abs(y - X[i]))
  103.  
  104. return distances
  105.  
  106. def main():
  107.  
  108. readfile = open('dataSets.txt', 'r')
  109. lines = readfile.readlines()
  110. L1 = {}
  111. X = {}
  112.  
  113. for line in lines:
  114. L1 = [int(i) for i in line.split()]
  115.  
  116. maxLength = max(L1)
  117. X.append(0)
  118. X.append(int(maxLength))
  119. L1.remove(maxLength)
  120.  
  121. print(placeSolutionIntoSolutionList(L1, X))
  122.  
  123. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement