Loading model/levelDyckWordToPlanarTree.py +20 −2 Original line number Diff line number Diff line from model.level import Level from model.levelEnum import getTreeDyckWordToPlanarTree from model.node import IncompleteNode, planarTreeToBinaryTree from model.node import IncompleteNode, planarTreeToBinaryTree, checkPlanarNode from model.tree import Tree Loading @@ -24,14 +24,32 @@ class LevelDyckWordToPlanarTree(Level): def checkTree(self, index: int, nodeType: str): copyTree = self.tree.copyTree() if nodeType == "branch": copyTree.completeNode(index, nodeType) copyTree = Tree(planarTreeToBinaryTree(copyTree.root)) print(copyTree.root) if self.answer.root.checkNodeBis(copyTree.root): self.tree.completeNode(index, nodeType) return True else: return False else: print(index) print(copyTree.root) depth = copyTree.root.getDepthFromIndex(index) print("depth: ") print(depth) nbChildren = copyTree.root.getNbChildrenFromIndex(index) print("children: ") print(nbChildren) copyTree.root.numberNodes() print("index: ") print(copyTree.root.index) print(checkPlanarNode(self.answer.dyckWord, copyTree.root.index, depth)) if checkPlanarNode(self.answer.dyckWord, copyTree.root.index, depth): self.tree.completeNode(index, nodeType) return True else: return False def initialize(self): self.score = 100 Loading model/node.py +145 −26 Original line number Diff line number Diff line Loading @@ -106,7 +106,7 @@ class Node: def completeNode(self, index, nodeType, tree): pass def numberNodes(self): def numberIncompleteNodes(self): pass Loading Loading @@ -230,7 +230,7 @@ class BinaryNode(Node): :param node: the node we compare with :return: True if the nodes are the sames, False otherwise """ if isinstance(node, EmptyNode): if isinstance(node, EmptyNode) or isinstance(node, IncompleteBinaryNode): return False elif isinstance(self, Leaf): if isinstance(node, Leaf): Loading Loading @@ -322,6 +322,7 @@ class PlanarNode(Node): self.width = 0 self.offset = 0 self.parent = None self.index = None for i in range(len(self.children)): self.children[i].setParent(self, i) Loading @@ -341,23 +342,59 @@ class PlanarNode(Node): def changeChild(self, node, index: int): self.children[index] = node def numberIncompleteNodes(self, index=0, brothers=None): if brothers is None: brothers = [] if not self.children: if brothers: brothers.pop(0).numberIncompleteNodes(index, brothers) else: newBrothers = self.children[1:] + brothers self.children[0].numberIncompleteNodes(index, newBrothers) def numberNodes(self, index=0, brothers=None): if brothers is None: brothers = [] self.index = index if not self.children: if brothers: brothers.pop(0).numberNodes(index, brothers) brothers.pop(0).numberNodes(index + 1, brothers) else: newBrothers = self.children[1:] + brothers self.children[0].numberNodes(index, newBrothers) self.children[0].numberNodes(index + 1, newBrothers) def completeNode(self, index: int, nodeType: str, tree): for child in self.children: child.completeNode(index, nodeType, tree) def complete(self, tree): for child in self.children: child.complete(tree) def getDepthFromIndex(self, index): self.numberIncompleteNodes() return self.getDepthFromIndexRec(index) def getDepthFromIndexRec(self, index, brothers=None): if brothers is None: brothers = [] if not self.children: if brothers: return brothers.pop(0).getDepthFromIndexRec(index, brothers) else: newBrothers = self.children[1:] + brothers return self.children[0].getDepthFromIndexRec(index, newBrothers) def getNbChildrenFromIndex(self, index): self.numberIncompleteNodes() return self.getNbChildrenFromIndexRec(index) def getNbChildrenFromIndexRec(self, index, brothers=None): if brothers is None: brothers = [] if not self.children: if brothers: return brothers.pop(0).getNbChildrenFromIndexRec(index, brothers) else: newBrothers = self.children[1:] + brothers return self.children[0].getNbChildrenFromIndexRec(index, newBrothers) def checkNode(self, node, nodeBrothers=None, brothers=None): if brothers is None: Loading Loading @@ -502,12 +539,36 @@ class PlanarNode(Node): return 1 + max(child.getDepth() for child in self.children) def toStr(self, tab=0): return ' ' * tab + 'NODE' + '\n' + "".join([child.toStr(tab + 2) for child in self.children]) return ' ' * tab + 'PNODE' + '\n' + "".join([child.toStr(tab + 2) for child in self.children]) def __str__(self): return self.toStr() class IncompleteBinaryNode(BinaryNode): def __init__(self, left, right): super(BinaryNode, self).__init__() self.left = left self.right = right # in a few funtions, we need that each node knows its parent self.left.setParent(self, 0) self.right.setParent(self, 1) self.parent = None # we need the position of the node compared with the others nodes of the tree to draw a tree self.x = 0 self.y = 0 self.height = 0 self.width = 0 self.offset = 0 def toStr(self, tab=0): return ' ' * tab + 'INCOMPLETE BINODE' + '\n' + "".join(self.left.toStr(tab + 2)) + "".join(self.right.toStr(tab + 2)) def __str__(self): return self.toStr() class IncompleteNode(PlanarNode): def __init__(self, children): super(PlanarNode, self).__init__() Loading @@ -518,7 +579,9 @@ class IncompleteNode(PlanarNode): self.width = 0 self.offset = 0 self.parent = None self.incompleteNodeIndex = None self.index = None for i in range(len(self.children)): self.children[i].setParent(self, i) Loading Loading @@ -548,8 +611,53 @@ class IncompleteNode(PlanarNode): newBrothers = self.children[1:] + brothers self.children[0].numberNodes(index + 1, newBrothers) def numberIncompleteNodes(self, index=0, brothers=None): if brothers is None: brothers = [] self.incompleteNodeIndex = index if not self.children: if brothers: brothers.pop(0).numberIncompleteNodes(index + 1, brothers) else: newBrothers = self.children[1:] + brothers self.children[0].numberIncompleteNodes(index + 1, newBrothers) def getIndex(self, incompleteNodeIndex: int): def getDepthFromIndexRec(self, index, brothers=None): if brothers is None: brothers = [] if index == self.incompleteNodeIndex: tmpNode = self cpt = 0 while tmpNode.parent is not None: cpt += 1 tmpNode = tmpNode.parent[0] return cpt else: if not self.children: if brothers: return brothers.pop(0).getDepthFromIndexRec(index, brothers) else: newBrothers = self.children[1:] + brothers return self.children[0].getDepthFromIndexRec(index, newBrothers) def getNbChildrenFromIndexRec(self, index, brothers=None): if brothers is None: brothers = [] if index == self.incompleteNodeIndex: return len(self.children) else: if not self.children: if brothers: return brothers.pop(0).getNbChildrenFromIndexRec(index, brothers) else: newBrothers = self.children[1:] + brothers return self.children[0].getNbChildrenFromIndexRec(index, newBrothers) def completeNode(self, index: int, nodeType: str, tree): if index == self.index: if index == self.incompleteNodeIndex: if nodeType == "branch": self.addChild(IncompleteNode([])) else: Loading @@ -562,14 +670,6 @@ class IncompleteNode(PlanarNode): for child in self.children: child.completeNode(index, nodeType, tree) def complete(self, tree): if self.parent is not None: self.parent[0].changeChild(PlanarNode(self.children), self.parent[1]) else: tree.root = PlanarNode(self.children) for child in self.children: child.complete(tree) def isEqual(self, node, nodeBrothers=None, brothers=None): if brothers is None: brothers = [] Loading Loading @@ -599,7 +699,7 @@ class IncompleteNode(PlanarNode): if len(newNodeBrothers) != len(newBrothers): return False else: return self.children[0].numberNodes(node.children[0], newNodeBrothers, newBrothers) return self.children[0].numberIncompleteNodes(node.children[0], newNodeBrothers, newBrothers) else: return False Loading @@ -608,7 +708,7 @@ class IncompleteNode(PlanarNode): node.setParent(self, len(self.children) - 1) def toStr(self, tab=0): return ' ' * tab + 'INCOMPLETE NODE' + " " + str(self.index) + '\n' + "".join( return ' ' * tab + 'INCOMPLETE NODE' + " " + str(self.incompleteNodeIndex) + '\n' + "".join( [child.toStr(tab + 2) for child in self.children]) def __str__(self): Loading Loading @@ -927,28 +1027,47 @@ def planarToBinaryRec(planarTree, brothers): right = planarToBinaryRec(brothers[0], brothers[1:]) else: right = Leaf() if isinstance(planarTree, IncompleteNode): return IncompleteBinaryNode(left, right) else: return BinaryNode(left, right) def checkPlanarNode(dyckWord: str, index, depth: int): cpt = 0 nbNodes = 0 for letter in dyckWord: if letter == "[": if depth == cpt: nbNodes += 1 cpt += 1 else: cpt -= 1 return nbNodes # testRandomTreeGeneration(100000,1, 3) if __name__ == '__main__': """pt111 = PlanarNode([]) pt11 = PlanarNode([pt111]) pt111 = IncompleteNode([]) pt11 = IncompleteNode([pt111]) pt12 = PlanarNode([]) pt1 = PlanarNode([pt11, pt12]) pt21 = PlanarNode([]) pt2 = PlanarNode([pt21]) pt2 = IncompleteNode([pt21]) pt31 = PlanarNode([]) pt32 = PlanarNode([]) pt31 = IncompleteNode([]) pt32 = IncompleteNode([]) pt33 = PlanarNode([]) pt3 = PlanarNode([pt31, pt32, pt33]) ptroot = PlanarNode([pt1, pt2, pt3]) #ptroot = IncompleteNode([pt1, pt2, pt3]) print(ptroot)""" ptroot = IncompleteNode([]) print(ptroot.getNbChildrenFromIndex(0)) """btroot = BinaryNode(Leaf(), BinaryNode(Leaf(), Leaf())) Loading model/tree.py +1 −1 Original line number Diff line number Diff line Loading @@ -20,7 +20,7 @@ class Tree: self.setup_root() def completeNode(self, index: int, nodeType: str): self.root.numberNodes() self.root.numberIncompleteNodes() self.root.completeNode(index, nodeType, self) self.depth = self.root.getDepth() self.dyckWord = self.root.getDyckWord() Loading Loading
model/levelDyckWordToPlanarTree.py +20 −2 Original line number Diff line number Diff line from model.level import Level from model.levelEnum import getTreeDyckWordToPlanarTree from model.node import IncompleteNode, planarTreeToBinaryTree from model.node import IncompleteNode, planarTreeToBinaryTree, checkPlanarNode from model.tree import Tree Loading @@ -24,14 +24,32 @@ class LevelDyckWordToPlanarTree(Level): def checkTree(self, index: int, nodeType: str): copyTree = self.tree.copyTree() if nodeType == "branch": copyTree.completeNode(index, nodeType) copyTree = Tree(planarTreeToBinaryTree(copyTree.root)) print(copyTree.root) if self.answer.root.checkNodeBis(copyTree.root): self.tree.completeNode(index, nodeType) return True else: return False else: print(index) print(copyTree.root) depth = copyTree.root.getDepthFromIndex(index) print("depth: ") print(depth) nbChildren = copyTree.root.getNbChildrenFromIndex(index) print("children: ") print(nbChildren) copyTree.root.numberNodes() print("index: ") print(copyTree.root.index) print(checkPlanarNode(self.answer.dyckWord, copyTree.root.index, depth)) if checkPlanarNode(self.answer.dyckWord, copyTree.root.index, depth): self.tree.completeNode(index, nodeType) return True else: return False def initialize(self): self.score = 100 Loading
model/node.py +145 −26 Original line number Diff line number Diff line Loading @@ -106,7 +106,7 @@ class Node: def completeNode(self, index, nodeType, tree): pass def numberNodes(self): def numberIncompleteNodes(self): pass Loading Loading @@ -230,7 +230,7 @@ class BinaryNode(Node): :param node: the node we compare with :return: True if the nodes are the sames, False otherwise """ if isinstance(node, EmptyNode): if isinstance(node, EmptyNode) or isinstance(node, IncompleteBinaryNode): return False elif isinstance(self, Leaf): if isinstance(node, Leaf): Loading Loading @@ -322,6 +322,7 @@ class PlanarNode(Node): self.width = 0 self.offset = 0 self.parent = None self.index = None for i in range(len(self.children)): self.children[i].setParent(self, i) Loading @@ -341,23 +342,59 @@ class PlanarNode(Node): def changeChild(self, node, index: int): self.children[index] = node def numberIncompleteNodes(self, index=0, brothers=None): if brothers is None: brothers = [] if not self.children: if brothers: brothers.pop(0).numberIncompleteNodes(index, brothers) else: newBrothers = self.children[1:] + brothers self.children[0].numberIncompleteNodes(index, newBrothers) def numberNodes(self, index=0, brothers=None): if brothers is None: brothers = [] self.index = index if not self.children: if brothers: brothers.pop(0).numberNodes(index, brothers) brothers.pop(0).numberNodes(index + 1, brothers) else: newBrothers = self.children[1:] + brothers self.children[0].numberNodes(index, newBrothers) self.children[0].numberNodes(index + 1, newBrothers) def completeNode(self, index: int, nodeType: str, tree): for child in self.children: child.completeNode(index, nodeType, tree) def complete(self, tree): for child in self.children: child.complete(tree) def getDepthFromIndex(self, index): self.numberIncompleteNodes() return self.getDepthFromIndexRec(index) def getDepthFromIndexRec(self, index, brothers=None): if brothers is None: brothers = [] if not self.children: if brothers: return brothers.pop(0).getDepthFromIndexRec(index, brothers) else: newBrothers = self.children[1:] + brothers return self.children[0].getDepthFromIndexRec(index, newBrothers) def getNbChildrenFromIndex(self, index): self.numberIncompleteNodes() return self.getNbChildrenFromIndexRec(index) def getNbChildrenFromIndexRec(self, index, brothers=None): if brothers is None: brothers = [] if not self.children: if brothers: return brothers.pop(0).getNbChildrenFromIndexRec(index, brothers) else: newBrothers = self.children[1:] + brothers return self.children[0].getNbChildrenFromIndexRec(index, newBrothers) def checkNode(self, node, nodeBrothers=None, brothers=None): if brothers is None: Loading Loading @@ -502,12 +539,36 @@ class PlanarNode(Node): return 1 + max(child.getDepth() for child in self.children) def toStr(self, tab=0): return ' ' * tab + 'NODE' + '\n' + "".join([child.toStr(tab + 2) for child in self.children]) return ' ' * tab + 'PNODE' + '\n' + "".join([child.toStr(tab + 2) for child in self.children]) def __str__(self): return self.toStr() class IncompleteBinaryNode(BinaryNode): def __init__(self, left, right): super(BinaryNode, self).__init__() self.left = left self.right = right # in a few funtions, we need that each node knows its parent self.left.setParent(self, 0) self.right.setParent(self, 1) self.parent = None # we need the position of the node compared with the others nodes of the tree to draw a tree self.x = 0 self.y = 0 self.height = 0 self.width = 0 self.offset = 0 def toStr(self, tab=0): return ' ' * tab + 'INCOMPLETE BINODE' + '\n' + "".join(self.left.toStr(tab + 2)) + "".join(self.right.toStr(tab + 2)) def __str__(self): return self.toStr() class IncompleteNode(PlanarNode): def __init__(self, children): super(PlanarNode, self).__init__() Loading @@ -518,7 +579,9 @@ class IncompleteNode(PlanarNode): self.width = 0 self.offset = 0 self.parent = None self.incompleteNodeIndex = None self.index = None for i in range(len(self.children)): self.children[i].setParent(self, i) Loading Loading @@ -548,8 +611,53 @@ class IncompleteNode(PlanarNode): newBrothers = self.children[1:] + brothers self.children[0].numberNodes(index + 1, newBrothers) def numberIncompleteNodes(self, index=0, brothers=None): if brothers is None: brothers = [] self.incompleteNodeIndex = index if not self.children: if brothers: brothers.pop(0).numberIncompleteNodes(index + 1, brothers) else: newBrothers = self.children[1:] + brothers self.children[0].numberIncompleteNodes(index + 1, newBrothers) def getIndex(self, incompleteNodeIndex: int): def getDepthFromIndexRec(self, index, brothers=None): if brothers is None: brothers = [] if index == self.incompleteNodeIndex: tmpNode = self cpt = 0 while tmpNode.parent is not None: cpt += 1 tmpNode = tmpNode.parent[0] return cpt else: if not self.children: if brothers: return brothers.pop(0).getDepthFromIndexRec(index, brothers) else: newBrothers = self.children[1:] + brothers return self.children[0].getDepthFromIndexRec(index, newBrothers) def getNbChildrenFromIndexRec(self, index, brothers=None): if brothers is None: brothers = [] if index == self.incompleteNodeIndex: return len(self.children) else: if not self.children: if brothers: return brothers.pop(0).getNbChildrenFromIndexRec(index, brothers) else: newBrothers = self.children[1:] + brothers return self.children[0].getNbChildrenFromIndexRec(index, newBrothers) def completeNode(self, index: int, nodeType: str, tree): if index == self.index: if index == self.incompleteNodeIndex: if nodeType == "branch": self.addChild(IncompleteNode([])) else: Loading @@ -562,14 +670,6 @@ class IncompleteNode(PlanarNode): for child in self.children: child.completeNode(index, nodeType, tree) def complete(self, tree): if self.parent is not None: self.parent[0].changeChild(PlanarNode(self.children), self.parent[1]) else: tree.root = PlanarNode(self.children) for child in self.children: child.complete(tree) def isEqual(self, node, nodeBrothers=None, brothers=None): if brothers is None: brothers = [] Loading Loading @@ -599,7 +699,7 @@ class IncompleteNode(PlanarNode): if len(newNodeBrothers) != len(newBrothers): return False else: return self.children[0].numberNodes(node.children[0], newNodeBrothers, newBrothers) return self.children[0].numberIncompleteNodes(node.children[0], newNodeBrothers, newBrothers) else: return False Loading @@ -608,7 +708,7 @@ class IncompleteNode(PlanarNode): node.setParent(self, len(self.children) - 1) def toStr(self, tab=0): return ' ' * tab + 'INCOMPLETE NODE' + " " + str(self.index) + '\n' + "".join( return ' ' * tab + 'INCOMPLETE NODE' + " " + str(self.incompleteNodeIndex) + '\n' + "".join( [child.toStr(tab + 2) for child in self.children]) def __str__(self): Loading Loading @@ -927,28 +1027,47 @@ def planarToBinaryRec(planarTree, brothers): right = planarToBinaryRec(brothers[0], brothers[1:]) else: right = Leaf() if isinstance(planarTree, IncompleteNode): return IncompleteBinaryNode(left, right) else: return BinaryNode(left, right) def checkPlanarNode(dyckWord: str, index, depth: int): cpt = 0 nbNodes = 0 for letter in dyckWord: if letter == "[": if depth == cpt: nbNodes += 1 cpt += 1 else: cpt -= 1 return nbNodes # testRandomTreeGeneration(100000,1, 3) if __name__ == '__main__': """pt111 = PlanarNode([]) pt11 = PlanarNode([pt111]) pt111 = IncompleteNode([]) pt11 = IncompleteNode([pt111]) pt12 = PlanarNode([]) pt1 = PlanarNode([pt11, pt12]) pt21 = PlanarNode([]) pt2 = PlanarNode([pt21]) pt2 = IncompleteNode([pt21]) pt31 = PlanarNode([]) pt32 = PlanarNode([]) pt31 = IncompleteNode([]) pt32 = IncompleteNode([]) pt33 = PlanarNode([]) pt3 = PlanarNode([pt31, pt32, pt33]) ptroot = PlanarNode([pt1, pt2, pt3]) #ptroot = IncompleteNode([pt1, pt2, pt3]) print(ptroot)""" ptroot = IncompleteNode([]) print(ptroot.getNbChildrenFromIndex(0)) """btroot = BinaryNode(Leaf(), BinaryNode(Leaf(), Leaf())) Loading
model/tree.py +1 −1 Original line number Diff line number Diff line Loading @@ -20,7 +20,7 @@ class Tree: self.setup_root() def completeNode(self, index: int, nodeType: str): self.root.numberNodes() self.root.numberIncompleteNodes() self.root.completeNode(index, nodeType, self) self.depth = self.root.getDepth() self.dyckWord = self.root.getDyckWord() Loading