Loading controller/binaryTreeToPlanarTree.py +5 −8 Changes for controller/binaryTreeToPlanarTree.py: 5 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -39,16 +39,13 @@ class BinaryTreeToPlanarTree: self.launchLevel() def addNode(self, index: int, nodeType: str): self.levelGui.error(index, "node") if self.level.checkTree(index): self.levelGui.treeView.createTree() if self.level.finished(): self.levelGui.finish() if self.level.checkTree(index, nodeType): self.levelGui.planarTreeView.createTree() """if self.level.finished(): self.levelGui.finish()""" else: self.levelGui.error(index)""" self.levelGui.error(index, nodeType) def addBranch(self, index: int): self.levelGui.error(index, "branch") Loading model/levelBinaryTreeToPlanarTree.py +7 −5 Changes for model/levelBinaryTreeToPlanarTree.py: 7 added lines, 5 removed lines. Original line number Diff line number Diff line from model.levelEnum import getTree from model.node import BinaryNode, EmptyNode, PlanarNode, IncompleteNode from model.node import BinaryNode, EmptyNode, PlanarNode, IncompleteNode, binaryTreeToPlanarTree from model.tree import Tree Loading @@ -7,13 +7,14 @@ class LevelBinaryTreeToPlanarTree: def __init__(self, index): self.index = index self.answer = getTree(index) self.planarAnswer = Tree(binaryTreeToPlanarTree(self.answer.root)) self.tree = None self.score = None self.initialize() def initialize(self): self.score = 100 self.tree = Tree(IncompleteNode([IncompleteNode([])])) self.tree = Tree(IncompleteNode([])) def nextLevel(self): return LevelBinaryTreeToPlanarTree(self.index + 1) Loading @@ -27,9 +28,10 @@ class LevelBinaryTreeToPlanarTree: def checkTree(self, index: int, nodeType: str): copy = self.tree.copyTree() copy.root.addNode(index, nodeType) if self.answer.root.checkNode(copy.root): self.tree.addNode(index, nodeType) copy.completeNode(index, nodeType) """if self.planarAnswer.root.checkNode(copy.root):""" if True: self.tree.completeNode(index, nodeType) return True else: return False Loading model/node.py +103 −11 Changes for model/node.py: 103 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -103,6 +103,12 @@ class Node: def checkNode(self, answer): pass def completeNode(self, index, nodeType, tree): pass def numberNodes(self): pass class BinaryNode(Node): """ Loading Loading @@ -304,6 +310,25 @@ class PlanarNode(Node): self.height = 0 self.width = 0 self.offset = 0 self.parent = None for i in range(len(self.children)): self.children[i].setParent(self, i) def setParent(self, node, index): """ this method gives a parent to this tree :param node: the parent of the tree :param index: the side this node is placed for his parent """ self.parent = (node, index) def addChild(self, node): self.children.append(node) node.setParent(self, len(self.children) - 1) def changeChild(self, node, index: int): self.children[index] = node def setup(self, depth=0, places=None, offsets=None): nb_children = len(self.children) Loading Loading @@ -351,14 +376,28 @@ class PlanarNode(Node): copyChildren.append(child.copyNode()) return PlanarNode(copyChildren) def addNode(self, index, nodeType): def completeNode(self, index, nodeType, tree): if len(self.children) == 0: return index else: for child in self.children: index = child.addNode(index, nodeType) index = child.completeNode(index, nodeType, tree) return index def addNode(self, index: int, nodeType: str): if index == 0 and isinstance(self, EmptyNode): if nodeType == "node": self.parent[0].changeChild(BinaryNode(EmptyNode(), EmptyNode()), self.parent[1]) else: self.parent[0].changeChild(Leaf(), self.parent[1]) return -1 elif isinstance(self, EmptyNode): return index - 1 elif isinstance(self, Leaf): return index else: return self.right.addNode(self.left.addNode(index, nodeType), nodeType) def isEqual(self, node): if isinstance(node, EmptyNode): return False Loading Loading @@ -389,9 +428,6 @@ class PlanarNode(Node): test = test and self.children[i].isEqual(answer.children[i]) return test def addChild(self, planarNode): self.children.append(planarNode) def getDepth(self): """ this method return the depht of the tree Loading @@ -409,6 +445,7 @@ class PlanarNode(Node): def __str__(self): return self.toStr() class IncompleteNode(PlanarNode): def __init__(self, children): super(PlanarNode, self).__init__() Loading @@ -418,8 +455,57 @@ class IncompleteNode(PlanarNode): self.height = 0 self.width = 0 self.offset = 0 self.parent = None self.index = None def setParent(self, node, index): """ this method gives a parent to this tree :param node: the parent of the tree :param index: the side this node is placed for his parent """ self.parent = (node, index) def numberNodes(self, index=0, brothers=None): print(brothers) if brothers is None: brothers = [] self.index = index if not self.children: if brothers: brothers[0].numberNodes(index + 1, brothers.pop(0)) else: newBrothers = self.children[1:] + brothers self.children[0].numberNodes(index + 1, newBrothers) def completeNode(self, index: int, nodeType: str, tree): print("---------------------") print(index) print(self) print("---------------------") if index == self.index: if nodeType == "branch": self.addChild(IncompleteNode([])) else: if self.parent is not None: completedNode = PlanarNode(self.children) self.parent[0].changeChild(completedNode, self.parent[1]) else: tree.root = PlanarNode(self.children) else: for child in self.children: child.completeNode(index, nodeType, tree) def addChild(self, node): self.children.append(node) node.setParent(self, len(self.children) - 1) def toStr(self, tab=0): return ' ' * tab + 'INCOMPLETE NODE' + " " + str(self.index) + '\n' + "".join([child.toStr(tab + 2) for child in self.children]) def __str__(self): return self.toStr() class Leaf(BinaryNode): Loading Loading @@ -715,7 +801,6 @@ def planarToBinaryRec(planarTree, brothers): # testRandomTreeGeneration(100000,1, 3) if __name__ == '__main__': """pt111 = PlanarNode([]) pt11 = PlanarNode([pt111]) pt12 = PlanarNode([]) Loading @@ -733,13 +818,20 @@ if __name__ == '__main__': print(ptroot)""" """btroot = BinaryNode(Leaf(), BinaryNode(Leaf(), Leaf())) btroot = BinaryNode(Leaf(), BinaryNode(Leaf(), Leaf())) print(btroot) ptroot = binaryTreeToPlanarTree(btroot) ptroot = binaryTreeToPlanarTree(btroot)""" ptroot = IncompleteNode([IncompleteNode([IncompleteNode([])]), IncompleteNode([IncompleteNode([]), IncompleteNode([])]), IncompleteNode([IncompleteNode([]), IncompleteNode([]), IncompleteNode([])])]) print(ptroot) ptroot.numberNodes() print(ptroot) """l1 = ["a","b"] c = "c" print(l1 + [c])""" btroot2 = planarTreeToBinaryTree(ptroot) """print(ptroot) btroot2 = planarTreeToBinaryTree(ptroot)""" model/tree.py +10 −2 Changes for model/tree.py: 10 added lines, 2 removed lines. Original line number Diff line number Diff line from collections import defaultdict from model.node import Node Loading @@ -21,6 +19,16 @@ class Tree: self.dyckWord = self.root.getDyckWord() self.setup_root() def completeNode(self, index: int, nodeType: str): print("aaaaaaaaaaaaa") print(self.root) self.root.numberNodes() print(self.root) self.root.completeNode(index, nodeType, self) self.depth = self.root.getDepth() self.dyckWord = self.root.getDyckWord() self.setup_root() def setup_root(self): self.root.setup() self.root.add_offsets() Loading view/binaryTreeToDyckWordGui.py −1 Changes for view/binaryTreeToDyckWordGui.py: 0 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ class BinaryTreeToDyckWordGui(QWidget): self.createButtons() self.createTreeView() self.layout = QHBoxLayout() self.layout.addWidget(self.treeView, 66) self.layout.addWidget(self.dyckWordGroupBox, 33) Loading Loading
controller/binaryTreeToPlanarTree.py +5 −8 Changes for controller/binaryTreeToPlanarTree.py: 5 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -39,16 +39,13 @@ class BinaryTreeToPlanarTree: self.launchLevel() def addNode(self, index: int, nodeType: str): self.levelGui.error(index, "node") if self.level.checkTree(index): self.levelGui.treeView.createTree() if self.level.finished(): self.levelGui.finish() if self.level.checkTree(index, nodeType): self.levelGui.planarTreeView.createTree() """if self.level.finished(): self.levelGui.finish()""" else: self.levelGui.error(index)""" self.levelGui.error(index, nodeType) def addBranch(self, index: int): self.levelGui.error(index, "branch") Loading
model/levelBinaryTreeToPlanarTree.py +7 −5 Changes for model/levelBinaryTreeToPlanarTree.py: 7 added lines, 5 removed lines. Original line number Diff line number Diff line from model.levelEnum import getTree from model.node import BinaryNode, EmptyNode, PlanarNode, IncompleteNode from model.node import BinaryNode, EmptyNode, PlanarNode, IncompleteNode, binaryTreeToPlanarTree from model.tree import Tree Loading @@ -7,13 +7,14 @@ class LevelBinaryTreeToPlanarTree: def __init__(self, index): self.index = index self.answer = getTree(index) self.planarAnswer = Tree(binaryTreeToPlanarTree(self.answer.root)) self.tree = None self.score = None self.initialize() def initialize(self): self.score = 100 self.tree = Tree(IncompleteNode([IncompleteNode([])])) self.tree = Tree(IncompleteNode([])) def nextLevel(self): return LevelBinaryTreeToPlanarTree(self.index + 1) Loading @@ -27,9 +28,10 @@ class LevelBinaryTreeToPlanarTree: def checkTree(self, index: int, nodeType: str): copy = self.tree.copyTree() copy.root.addNode(index, nodeType) if self.answer.root.checkNode(copy.root): self.tree.addNode(index, nodeType) copy.completeNode(index, nodeType) """if self.planarAnswer.root.checkNode(copy.root):""" if True: self.tree.completeNode(index, nodeType) return True else: return False Loading
model/node.py +103 −11 Changes for model/node.py: 103 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -103,6 +103,12 @@ class Node: def checkNode(self, answer): pass def completeNode(self, index, nodeType, tree): pass def numberNodes(self): pass class BinaryNode(Node): """ Loading Loading @@ -304,6 +310,25 @@ class PlanarNode(Node): self.height = 0 self.width = 0 self.offset = 0 self.parent = None for i in range(len(self.children)): self.children[i].setParent(self, i) def setParent(self, node, index): """ this method gives a parent to this tree :param node: the parent of the tree :param index: the side this node is placed for his parent """ self.parent = (node, index) def addChild(self, node): self.children.append(node) node.setParent(self, len(self.children) - 1) def changeChild(self, node, index: int): self.children[index] = node def setup(self, depth=0, places=None, offsets=None): nb_children = len(self.children) Loading Loading @@ -351,14 +376,28 @@ class PlanarNode(Node): copyChildren.append(child.copyNode()) return PlanarNode(copyChildren) def addNode(self, index, nodeType): def completeNode(self, index, nodeType, tree): if len(self.children) == 0: return index else: for child in self.children: index = child.addNode(index, nodeType) index = child.completeNode(index, nodeType, tree) return index def addNode(self, index: int, nodeType: str): if index == 0 and isinstance(self, EmptyNode): if nodeType == "node": self.parent[0].changeChild(BinaryNode(EmptyNode(), EmptyNode()), self.parent[1]) else: self.parent[0].changeChild(Leaf(), self.parent[1]) return -1 elif isinstance(self, EmptyNode): return index - 1 elif isinstance(self, Leaf): return index else: return self.right.addNode(self.left.addNode(index, nodeType), nodeType) def isEqual(self, node): if isinstance(node, EmptyNode): return False Loading Loading @@ -389,9 +428,6 @@ class PlanarNode(Node): test = test and self.children[i].isEqual(answer.children[i]) return test def addChild(self, planarNode): self.children.append(planarNode) def getDepth(self): """ this method return the depht of the tree Loading @@ -409,6 +445,7 @@ class PlanarNode(Node): def __str__(self): return self.toStr() class IncompleteNode(PlanarNode): def __init__(self, children): super(PlanarNode, self).__init__() Loading @@ -418,8 +455,57 @@ class IncompleteNode(PlanarNode): self.height = 0 self.width = 0 self.offset = 0 self.parent = None self.index = None def setParent(self, node, index): """ this method gives a parent to this tree :param node: the parent of the tree :param index: the side this node is placed for his parent """ self.parent = (node, index) def numberNodes(self, index=0, brothers=None): print(brothers) if brothers is None: brothers = [] self.index = index if not self.children: if brothers: brothers[0].numberNodes(index + 1, brothers.pop(0)) else: newBrothers = self.children[1:] + brothers self.children[0].numberNodes(index + 1, newBrothers) def completeNode(self, index: int, nodeType: str, tree): print("---------------------") print(index) print(self) print("---------------------") if index == self.index: if nodeType == "branch": self.addChild(IncompleteNode([])) else: if self.parent is not None: completedNode = PlanarNode(self.children) self.parent[0].changeChild(completedNode, self.parent[1]) else: tree.root = PlanarNode(self.children) else: for child in self.children: child.completeNode(index, nodeType, tree) def addChild(self, node): self.children.append(node) node.setParent(self, len(self.children) - 1) def toStr(self, tab=0): return ' ' * tab + 'INCOMPLETE NODE' + " " + str(self.index) + '\n' + "".join([child.toStr(tab + 2) for child in self.children]) def __str__(self): return self.toStr() class Leaf(BinaryNode): Loading Loading @@ -715,7 +801,6 @@ def planarToBinaryRec(planarTree, brothers): # testRandomTreeGeneration(100000,1, 3) if __name__ == '__main__': """pt111 = PlanarNode([]) pt11 = PlanarNode([pt111]) pt12 = PlanarNode([]) Loading @@ -733,13 +818,20 @@ if __name__ == '__main__': print(ptroot)""" """btroot = BinaryNode(Leaf(), BinaryNode(Leaf(), Leaf())) btroot = BinaryNode(Leaf(), BinaryNode(Leaf(), Leaf())) print(btroot) ptroot = binaryTreeToPlanarTree(btroot) ptroot = binaryTreeToPlanarTree(btroot)""" ptroot = IncompleteNode([IncompleteNode([IncompleteNode([])]), IncompleteNode([IncompleteNode([]), IncompleteNode([])]), IncompleteNode([IncompleteNode([]), IncompleteNode([]), IncompleteNode([])])]) print(ptroot) ptroot.numberNodes() print(ptroot) """l1 = ["a","b"] c = "c" print(l1 + [c])""" btroot2 = planarTreeToBinaryTree(ptroot) """print(ptroot) btroot2 = planarTreeToBinaryTree(ptroot)"""
model/tree.py +10 −2 Changes for model/tree.py: 10 added lines, 2 removed lines. Original line number Diff line number Diff line from collections import defaultdict from model.node import Node Loading @@ -21,6 +19,16 @@ class Tree: self.dyckWord = self.root.getDyckWord() self.setup_root() def completeNode(self, index: int, nodeType: str): print("aaaaaaaaaaaaa") print(self.root) self.root.numberNodes() print(self.root) self.root.completeNode(index, nodeType, self) self.depth = self.root.getDepth() self.dyckWord = self.root.getDyckWord() self.setup_root() def setup_root(self): self.root.setup() self.root.add_offsets() Loading
view/binaryTreeToDyckWordGui.py −1 Changes for view/binaryTreeToDyckWordGui.py: 0 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ class BinaryTreeToDyckWordGui(QWidget): self.createButtons() self.createTreeView() self.layout = QHBoxLayout() self.layout.addWidget(self.treeView, 66) self.layout.addWidget(self.dyckWordGroupBox, 33) Loading