Loading diagrammes/.~lock.algoDePlacement.odt# −1 File deleted. Show changes model/levelPlanarTreeToBinaryTree.py +5 −4 Changes for model/levelPlanarTreeToBinaryTree.py: 5 added lines, 4 removed lines. Original line number Diff line number Diff line from model.levelEnum import getTree from model.node import BinaryNode, EmptyNode from model.node import BinaryNode, EmptyNode, binaryTreeToPlanarTree from model.tree import Tree class LevelPlanarTreeToBinaryTree: def __init__(self, index): self.index = index self.answer = getTree(index) self.binaryAnswer = getTree(index) self.answer = Tree(binaryTreeToPlanarTree(self.binaryAnswer.root)) self.tree = Tree(BinaryNode(EmptyNode(), EmptyNode())) self.score = 100 Loading @@ -23,11 +24,11 @@ class LevelPlanarTreeToBinaryTree: def checkTree(self, index: int, nodeType: str): copy = self.tree.copyTree() copy.root.addNode(index, nodeType) if self.answer.root.checkNode(copy.root): if self.binaryAnswer.root.checkNode(copy.root): self.tree.addNode(index, nodeType) return True else: return False def finished(self): return self.tree.root.isEqual(self.answer.root) return self.tree.root.isEqual(self.binaryAnswer.root) model/node.py +75 −24 Changes for model/node.py: 75 added lines, 24 removed lines. Original line number Diff line number Diff line Loading @@ -224,7 +224,7 @@ class BinaryNode(Node): if isinstance(node, Leaf): return False else: return self.left.isEqual(node.left) and self.right.isEqual(node.right) cla return self.left.isEqual(node.left) and self.right.isEqual(node.right) def checkNode(self, answer): """ Loading Loading @@ -261,6 +261,20 @@ class BinaryNode(Node): """ return self.right.addNode(self.left.addNode(index, nodeType), nodeType) def binaryTreeToPlanarTree(self): return BinaryNode(self, Leaf()).binaryToPlanarRec() def binaryToPlanarRec(self): return PlanarNode(self.left.getBrothers()) def getBrothers(self): brothers = [self.binaryToPlanarRec()] brother = self.right while isinstance(brother, BinaryNode): brothers.append(brother.binaryToPlanarRec()) brother = brother.right return brothers def toStr(self, tab=0): return ' ' * tab + 'NODE' + '\n' + "".join(self.left.toStr(tab + 2)) + "".join(self.right.toStr(tab + 2)) Loading Loading @@ -428,26 +442,6 @@ class PlanarNode(Node): return self.toStr() def binaryTreeToPlanarTree(binaryTree): return binaryToPlanarRec(BinaryNode(binaryTree, Leaf())) def binaryToPlanarRec(binaryTree): return PlanarNode(getBrothers(binaryTree.left)) def getBrothers(binaryNode: BinaryNode): if not isinstance(binaryNode, BinaryNode): return [] else: brothers = [binaryToPlanarRec(binaryNode)] brother = binaryNode.right while isinstance(brother, BinaryNode): brothers.append(binaryToPlanarRec(brother)) brother = brother.right return brothers class Leaf(BinaryNode): """ this class inherits from Node, actually, a Leaf is a Node which has no children Loading Loading @@ -494,6 +488,9 @@ class Leaf(BinaryNode): def getDepth(self): return 0 def getBrothers(self): return [] def toStr(self, tab=0): return ' ' * tab + 'LEAF' + '\n' Loading Loading @@ -537,6 +534,9 @@ class EmptyNode(Node): else: return index - 1 def getBrothers(self): return [] def setup(self, depth=0, places=None, offsets=None): if places is None: places = defaultdict(lambda: 0) Loading Loading @@ -683,9 +683,60 @@ def testRandomTreeGeneration(nbTests, treeMin, treeMax): res = "" def binaryTreeToPlanarTree(binaryTree): return binaryToPlanarRec(BinaryNode(binaryTree, Leaf())) def binaryToPlanarRec(binaryTree): if not isinstance(binaryTree, Leaf): return PlanarNode(getBrothers(binaryTree.left)) def getBrothers(binaryNode: BinaryNode): if isinstance(binaryNode, Leaf): return [] else: brothers = [binaryToPlanarRec(binaryNode)] brother = binaryNode.right while not isinstance(brother, Leaf): brothers.append(binaryToPlanarRec(brother)) brother = brother.right return brothers def planarTreeToBinaryTree(planarTree): """ this method change a planar tree in a binary tree """ if planarTree.children: return planarToBinaryRec(planarTree.children[0], planarTree.children[1:]) else: return Leaf() def planarToBinaryRec(planarTree, brothers): """ this method is called recursively to get a binary tree from a planr tree :param planarTree: the planar tree :param brothers: the brothers of the parent of the planar tree :return: a binary tree corresponding with the planar tree """ if planarTree.children: left = planarToBinaryRec(planarTree.children[0], planarTree.children[1:]) else: left = Leaf() if brothers: right = planarToBinaryRec(brothers[0], brothers[1:]) else: right = Leaf() return BinaryNode(left, right) # testRandomTreeGeneration(100000,1, 3) if __name__ == '__main__': """pt111 = PlanarNode([]) """ pt111 = PlanarNode([]) pt11 = PlanarNode([pt111]) pt12 = PlanarNode([]) pt1 = PlanarNode([pt11, pt12]) Loading @@ -710,6 +761,6 @@ if __name__ == '__main__': print(ptroot2) btroot2 = planarTreeToBinaryTree(ptroot2) btroot2 = planarTreeToBinaryTree(ptroot2)""" # print(btroot2.getDyckWord())""" # print(btroot2.getDyckWord()) view/treeView.py +62 −1 Changes for view/treeView.py: 62 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -198,7 +198,7 @@ class TreeView(QGraphicsView): self.scene().setSceneRect(self.treeItemsGroup.sceneBoundingRect()) def drawTree2(self, tree, nodeWidth, lineWidth, heightRatio, widthRatio): def drawTree3(self, tree, nodeWidth, lineWidth, heightRatio, widthRatio): branchPen = QPen(QColor(113, 125, 126)) branchPen.setWidth(lineWidth) branchPen.setCapStyle(Qt.RoundCap) Loading Loading @@ -243,6 +243,67 @@ class TreeView(QGraphicsView): self.drawTree2(tree.left, nodeWidth, lineWidth, heightRatio, widthRatio) self.drawTree2(tree.right, nodeWidth, lineWidth, heightRatio, widthRatio) def drawTree2(self, tree, nodeWidth, lineWidth, heightRatio, widthRatio): branchPen = QPen(QColor(113, 125, 126)) branchPen.setWidth(lineWidth) branchPen.setCapStyle(Qt.RoundCap) nodePen = QPen(QColor(113, 125, 126)) nodePen.setWidth(0.1 * nodeWidth) if isinstance(tree, Leaf): leaf = QGraphicsRectItem() leaf.setRect(tree.x * widthRatio - nodeWidth / 2, tree.y * heightRatio, nodeWidth, nodeWidth) leaf.setPen(nodePen) self.treeItemsGroup.addToGroup(leaf) elif isinstance(tree, EmptyNode): leaf = QGraphicsRectItem() leaf.setRect(tree.x * widthRatio, tree.y * heightRatio, nodeWidth, nodeWidth) leaf.setOpacity(0) self.treeItemsGroup.addToGroup(leaf) leafButton = LeafButton(self.controller, self.indexChildButton, 0.1 * nodeWidth) nodeButton = NodeButton(self.controller, self.indexChildButton, 0.1 * nodeWidth) self.indexChildButton += 1 leafButton.setRect(tree.x * widthRatio - nodeWidth / 2 - 0.05 * nodeWidth, tree.y * heightRatio + nodeWidth / 4, nodeWidth / 2 - 0.1 * nodeWidth, nodeWidth / 2 - 0.1 * nodeWidth) nodeButton.setRect(tree.x * widthRatio + 0.05 * nodeWidth, tree.y * heightRatio + nodeWidth / 4, nodeWidth / 2 - 0.1 * nodeWidth, nodeWidth / 2 - 0.1 * nodeWidth) self.buttons.append(leafButton) self.buttons.append(nodeButton) self.scene().addItem(leafButton) self.scene().addItem(nodeButton) else: if isinstance(tree, BinaryNode): leftLine = QGraphicsLineItem() rightLine = QGraphicsLineItem() leftLine.setLine(tree.x * widthRatio, tree.y * heightRatio + nodeWidth / 2, tree.left.x * widthRatio, tree.left.y * heightRatio) leftLine.setPen(branchPen) rightLine.setLine(tree.x * widthRatio, tree.y * heightRatio + nodeWidth / 2, tree.right.x * widthRatio, tree.right.y * heightRatio) rightLine.setPen(branchPen) self.treeItemsGroup.addToGroup(leftLine) self.treeItemsGroup.addToGroup(rightLine) node = QGraphicsEllipseItem() node.setRect(tree.x * widthRatio - nodeWidth / 2, tree.y * heightRatio, nodeWidth, nodeWidth) node.setPen(nodePen) node.setBrush(QColor(174, 214, 241)) self.treeItemsGroup.addToGroup(node) self.drawTree2(tree.left, nodeWidth, lineWidth, heightRatio, widthRatio) self.drawTree2(tree.right, nodeWidth, lineWidth, heightRatio, widthRatio) else: for child in tree.children: line = QGraphicsLineItem() line.setLine(tree.x * widthRatio, tree.y * heightRatio + nodeWidth / 2, child.x * widthRatio, child.y * heightRatio) line.setPen(branchPen) self.treeItemsGroup.addToGroup(line) node = QGraphicsEllipseItem() node.setRect(tree.x * widthRatio - nodeWidth / 2, tree.y * heightRatio, nodeWidth, nodeWidth) node.setPen(nodePen) node.setBrush(QColor(174, 214, 241)) self.treeItemsGroup.addToGroup(node) self.drawTree2(child, nodeWidth, lineWidth, heightRatio, widthRatio) class Clickable(QObject): clicked = pyqtSignal() Loading Loading
model/levelPlanarTreeToBinaryTree.py +5 −4 Changes for model/levelPlanarTreeToBinaryTree.py: 5 added lines, 4 removed lines. Original line number Diff line number Diff line from model.levelEnum import getTree from model.node import BinaryNode, EmptyNode from model.node import BinaryNode, EmptyNode, binaryTreeToPlanarTree from model.tree import Tree class LevelPlanarTreeToBinaryTree: def __init__(self, index): self.index = index self.answer = getTree(index) self.binaryAnswer = getTree(index) self.answer = Tree(binaryTreeToPlanarTree(self.binaryAnswer.root)) self.tree = Tree(BinaryNode(EmptyNode(), EmptyNode())) self.score = 100 Loading @@ -23,11 +24,11 @@ class LevelPlanarTreeToBinaryTree: def checkTree(self, index: int, nodeType: str): copy = self.tree.copyTree() copy.root.addNode(index, nodeType) if self.answer.root.checkNode(copy.root): if self.binaryAnswer.root.checkNode(copy.root): self.tree.addNode(index, nodeType) return True else: return False def finished(self): return self.tree.root.isEqual(self.answer.root) return self.tree.root.isEqual(self.binaryAnswer.root)
model/node.py +75 −24 Changes for model/node.py: 75 added lines, 24 removed lines. Original line number Diff line number Diff line Loading @@ -224,7 +224,7 @@ class BinaryNode(Node): if isinstance(node, Leaf): return False else: return self.left.isEqual(node.left) and self.right.isEqual(node.right) cla return self.left.isEqual(node.left) and self.right.isEqual(node.right) def checkNode(self, answer): """ Loading Loading @@ -261,6 +261,20 @@ class BinaryNode(Node): """ return self.right.addNode(self.left.addNode(index, nodeType), nodeType) def binaryTreeToPlanarTree(self): return BinaryNode(self, Leaf()).binaryToPlanarRec() def binaryToPlanarRec(self): return PlanarNode(self.left.getBrothers()) def getBrothers(self): brothers = [self.binaryToPlanarRec()] brother = self.right while isinstance(brother, BinaryNode): brothers.append(brother.binaryToPlanarRec()) brother = brother.right return brothers def toStr(self, tab=0): return ' ' * tab + 'NODE' + '\n' + "".join(self.left.toStr(tab + 2)) + "".join(self.right.toStr(tab + 2)) Loading Loading @@ -428,26 +442,6 @@ class PlanarNode(Node): return self.toStr() def binaryTreeToPlanarTree(binaryTree): return binaryToPlanarRec(BinaryNode(binaryTree, Leaf())) def binaryToPlanarRec(binaryTree): return PlanarNode(getBrothers(binaryTree.left)) def getBrothers(binaryNode: BinaryNode): if not isinstance(binaryNode, BinaryNode): return [] else: brothers = [binaryToPlanarRec(binaryNode)] brother = binaryNode.right while isinstance(brother, BinaryNode): brothers.append(binaryToPlanarRec(brother)) brother = brother.right return brothers class Leaf(BinaryNode): """ this class inherits from Node, actually, a Leaf is a Node which has no children Loading Loading @@ -494,6 +488,9 @@ class Leaf(BinaryNode): def getDepth(self): return 0 def getBrothers(self): return [] def toStr(self, tab=0): return ' ' * tab + 'LEAF' + '\n' Loading Loading @@ -537,6 +534,9 @@ class EmptyNode(Node): else: return index - 1 def getBrothers(self): return [] def setup(self, depth=0, places=None, offsets=None): if places is None: places = defaultdict(lambda: 0) Loading Loading @@ -683,9 +683,60 @@ def testRandomTreeGeneration(nbTests, treeMin, treeMax): res = "" def binaryTreeToPlanarTree(binaryTree): return binaryToPlanarRec(BinaryNode(binaryTree, Leaf())) def binaryToPlanarRec(binaryTree): if not isinstance(binaryTree, Leaf): return PlanarNode(getBrothers(binaryTree.left)) def getBrothers(binaryNode: BinaryNode): if isinstance(binaryNode, Leaf): return [] else: brothers = [binaryToPlanarRec(binaryNode)] brother = binaryNode.right while not isinstance(brother, Leaf): brothers.append(binaryToPlanarRec(brother)) brother = brother.right return brothers def planarTreeToBinaryTree(planarTree): """ this method change a planar tree in a binary tree """ if planarTree.children: return planarToBinaryRec(planarTree.children[0], planarTree.children[1:]) else: return Leaf() def planarToBinaryRec(planarTree, brothers): """ this method is called recursively to get a binary tree from a planr tree :param planarTree: the planar tree :param brothers: the brothers of the parent of the planar tree :return: a binary tree corresponding with the planar tree """ if planarTree.children: left = planarToBinaryRec(planarTree.children[0], planarTree.children[1:]) else: left = Leaf() if brothers: right = planarToBinaryRec(brothers[0], brothers[1:]) else: right = Leaf() return BinaryNode(left, right) # testRandomTreeGeneration(100000,1, 3) if __name__ == '__main__': """pt111 = PlanarNode([]) """ pt111 = PlanarNode([]) pt11 = PlanarNode([pt111]) pt12 = PlanarNode([]) pt1 = PlanarNode([pt11, pt12]) Loading @@ -710,6 +761,6 @@ if __name__ == '__main__': print(ptroot2) btroot2 = planarTreeToBinaryTree(ptroot2) btroot2 = planarTreeToBinaryTree(ptroot2)""" # print(btroot2.getDyckWord())""" # print(btroot2.getDyckWord())
view/treeView.py +62 −1 Changes for view/treeView.py: 62 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -198,7 +198,7 @@ class TreeView(QGraphicsView): self.scene().setSceneRect(self.treeItemsGroup.sceneBoundingRect()) def drawTree2(self, tree, nodeWidth, lineWidth, heightRatio, widthRatio): def drawTree3(self, tree, nodeWidth, lineWidth, heightRatio, widthRatio): branchPen = QPen(QColor(113, 125, 126)) branchPen.setWidth(lineWidth) branchPen.setCapStyle(Qt.RoundCap) Loading Loading @@ -243,6 +243,67 @@ class TreeView(QGraphicsView): self.drawTree2(tree.left, nodeWidth, lineWidth, heightRatio, widthRatio) self.drawTree2(tree.right, nodeWidth, lineWidth, heightRatio, widthRatio) def drawTree2(self, tree, nodeWidth, lineWidth, heightRatio, widthRatio): branchPen = QPen(QColor(113, 125, 126)) branchPen.setWidth(lineWidth) branchPen.setCapStyle(Qt.RoundCap) nodePen = QPen(QColor(113, 125, 126)) nodePen.setWidth(0.1 * nodeWidth) if isinstance(tree, Leaf): leaf = QGraphicsRectItem() leaf.setRect(tree.x * widthRatio - nodeWidth / 2, tree.y * heightRatio, nodeWidth, nodeWidth) leaf.setPen(nodePen) self.treeItemsGroup.addToGroup(leaf) elif isinstance(tree, EmptyNode): leaf = QGraphicsRectItem() leaf.setRect(tree.x * widthRatio, tree.y * heightRatio, nodeWidth, nodeWidth) leaf.setOpacity(0) self.treeItemsGroup.addToGroup(leaf) leafButton = LeafButton(self.controller, self.indexChildButton, 0.1 * nodeWidth) nodeButton = NodeButton(self.controller, self.indexChildButton, 0.1 * nodeWidth) self.indexChildButton += 1 leafButton.setRect(tree.x * widthRatio - nodeWidth / 2 - 0.05 * nodeWidth, tree.y * heightRatio + nodeWidth / 4, nodeWidth / 2 - 0.1 * nodeWidth, nodeWidth / 2 - 0.1 * nodeWidth) nodeButton.setRect(tree.x * widthRatio + 0.05 * nodeWidth, tree.y * heightRatio + nodeWidth / 4, nodeWidth / 2 - 0.1 * nodeWidth, nodeWidth / 2 - 0.1 * nodeWidth) self.buttons.append(leafButton) self.buttons.append(nodeButton) self.scene().addItem(leafButton) self.scene().addItem(nodeButton) else: if isinstance(tree, BinaryNode): leftLine = QGraphicsLineItem() rightLine = QGraphicsLineItem() leftLine.setLine(tree.x * widthRatio, tree.y * heightRatio + nodeWidth / 2, tree.left.x * widthRatio, tree.left.y * heightRatio) leftLine.setPen(branchPen) rightLine.setLine(tree.x * widthRatio, tree.y * heightRatio + nodeWidth / 2, tree.right.x * widthRatio, tree.right.y * heightRatio) rightLine.setPen(branchPen) self.treeItemsGroup.addToGroup(leftLine) self.treeItemsGroup.addToGroup(rightLine) node = QGraphicsEllipseItem() node.setRect(tree.x * widthRatio - nodeWidth / 2, tree.y * heightRatio, nodeWidth, nodeWidth) node.setPen(nodePen) node.setBrush(QColor(174, 214, 241)) self.treeItemsGroup.addToGroup(node) self.drawTree2(tree.left, nodeWidth, lineWidth, heightRatio, widthRatio) self.drawTree2(tree.right, nodeWidth, lineWidth, heightRatio, widthRatio) else: for child in tree.children: line = QGraphicsLineItem() line.setLine(tree.x * widthRatio, tree.y * heightRatio + nodeWidth / 2, child.x * widthRatio, child.y * heightRatio) line.setPen(branchPen) self.treeItemsGroup.addToGroup(line) node = QGraphicsEllipseItem() node.setRect(tree.x * widthRatio - nodeWidth / 2, tree.y * heightRatio, nodeWidth, nodeWidth) node.setPen(nodePen) node.setBrush(QColor(174, 214, 241)) self.treeItemsGroup.addToGroup(node) self.drawTree2(child, nodeWidth, lineWidth, heightRatio, widthRatio) class Clickable(QObject): clicked = pyqtSignal() Loading