Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BinaryTree:
- def __init__(self, value):
- self.data = value
- self.left = None
- self.right = None
- def getLeftChild(self):
- return self.left
- def getRightChild(self):
- return self.right
- def setLeftChild(self, value):
- self.left = value
- def setRightChild(self, value):
- self.right = value
- def setRootVal(self, value):
- self.data = value
- def getRootVal(self):
- return self.data
- def insertLeft(self, value):
- if self.left == None:
- self.left = BinaryTree(value)
- else:
- n = BinaryTree(value)
- n.left = self.left
- self.left = n
- def insertRight(self, value):
- if self.right == None:
- self.right = BinaryTree(value)
- else:
- n = BinaryTree(value)
- n.right = self.right
- self.right = n
- def __str__(self):
- return (" Parent: " + str(self.data) + " LeftChild: " + str(self.left) + " RightChild: " + str(self.right))
- def placeSolutionIntoSolutionList(L1, X):
- if not L1:
- print ("Solution: " + str(X))
- return X
- y = int(max(L1))
- yComplement = int(max(X)) - y
- returnList = computeDistances(yComplement, X)
- secondChoiceReturnList = computeDistances(y, X)
- print ("this is return" + str(returnList))
- print ("this is second" + str(secondChoiceReturnList))
- treeStart = BinaryTree(X)
- if set(returnList) <= set(L1):
- print (L1)
- print ("this bullshit " + str(returnList))
- X.add(yComplement)
- for i in range(0, len(returnList)):
- if returnList[i] in L1:
- L1.remove(returnList[i])
- x_sorted = X
- left = placeSolutionIntoSolutionList(L1, x_sorted)
- treeStart.setLeftChild(left)
- X.remove(yComplement)
- for i in range(0, len(returnList)):
- L1.add(returnList[i])
- if set(secondChoiceReturnList) <= set(L1):
- X.add(y)
- for i in range(0, len(secondChoiceReturnList)):
- if secondChoiceReturnList[i] in L1:
- L1.remove(secondChoiceReturnList[i])
- x_sorted = X
- right = placeSolutionIntoSolutionList(L1, x_sorted)
- treeStart.setRightChild(right)
- X.remove(y)
- for i in range(0, len(secondChoiceReturnList)):
- L1.add(secondChoiceReturnList[i])
- return treeStart
- def computeDistances(y, X):
- distances = {}
- for i in range(0, len(X)):
- x = X[i]
- distances.add(abs(y - X[i]))
- return distances
- def main():
- readfile = open('dataSets.txt', 'r')
- lines = readfile.readlines()
- L1 = {}
- X = {}
- for line in lines:
- L1 = [int(i) for i in line.split()]
- maxLength = max(L1)
- X.append(0)
- X.append(int(maxLength))
- L1.remove(maxLength)
- print(placeSolutionIntoSolutionList(L1, X))
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement