Loading model/levelDyckWordToPlanarTree.py +1 −1 Original line number Diff line number Diff line from model.level import Level from model.levelEnum import getTreeDyckWordToPlanarTree from model.node import IncompleteNode, planarTreeToBinaryTree, checkPlanarNode, binaryTreeToPlanarTree from model.node import IncompleteNode, binaryTreeToPlanarTree from model.tree import Tree Loading model/node.py +5 −133 Original line number Diff line number Diff line Loading @@ -106,7 +106,7 @@ class Node: def completeNode(self, index, nodeType, tree): pass def numberIncompleteNodes(self): def numberNodes(self): pass Loading Loading @@ -342,60 +342,20 @@ 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 + 1, brothers) brothers.pop(0).numberNodes(index, brothers) else: newBrothers = self.children[1:] + brothers self.children[0].numberNodes(index + 1, newBrothers) self.children[0].numberNodes(index, newBrothers) def completeNode(self, index: int, nodeType: str, tree): for child in self.children: child.completeNode(index, nodeType, 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: brothers = [] Loading Loading @@ -545,30 +505,6 @@ class PlanarNode(Node): 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 @@ -579,7 +515,6 @@ 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)): Loading Loading @@ -611,53 +546,8 @@ 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): pass 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.incompleteNodeIndex: if index == self.index: if nodeType == "branch": self.addChild(IncompleteNode([])) else: Loading Loading @@ -699,7 +589,7 @@ class IncompleteNode(PlanarNode): if len(newNodeBrothers) != len(newBrothers): return False else: return self.children[0].numberIncompleteNodes(node.children[0], newNodeBrothers, newBrothers) return self.children[0].numberNodes(node.children[0], newNodeBrothers, newBrothers) else: return False Loading Loading @@ -1027,27 +917,9 @@ 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 = IncompleteNode([]) 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.numberIncompleteNodes() self.root.numberNodes() self.root.completeNode(index, nodeType, self) self.depth = self.root.getDepth() self.dyckWord = self.root.getDyckWord() Loading Loading
model/levelDyckWordToPlanarTree.py +1 −1 Original line number Diff line number Diff line from model.level import Level from model.levelEnum import getTreeDyckWordToPlanarTree from model.node import IncompleteNode, planarTreeToBinaryTree, checkPlanarNode, binaryTreeToPlanarTree from model.node import IncompleteNode, binaryTreeToPlanarTree from model.tree import Tree Loading
model/node.py +5 −133 Original line number Diff line number Diff line Loading @@ -106,7 +106,7 @@ class Node: def completeNode(self, index, nodeType, tree): pass def numberIncompleteNodes(self): def numberNodes(self): pass Loading Loading @@ -342,60 +342,20 @@ 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 + 1, brothers) brothers.pop(0).numberNodes(index, brothers) else: newBrothers = self.children[1:] + brothers self.children[0].numberNodes(index + 1, newBrothers) self.children[0].numberNodes(index, newBrothers) def completeNode(self, index: int, nodeType: str, tree): for child in self.children: child.completeNode(index, nodeType, 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: brothers = [] Loading Loading @@ -545,30 +505,6 @@ class PlanarNode(Node): 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 @@ -579,7 +515,6 @@ 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)): Loading Loading @@ -611,53 +546,8 @@ 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): pass 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.incompleteNodeIndex: if index == self.index: if nodeType == "branch": self.addChild(IncompleteNode([])) else: Loading Loading @@ -699,7 +589,7 @@ class IncompleteNode(PlanarNode): if len(newNodeBrothers) != len(newBrothers): return False else: return self.children[0].numberIncompleteNodes(node.children[0], newNodeBrothers, newBrothers) return self.children[0].numberNodes(node.children[0], newNodeBrothers, newBrothers) else: return False Loading Loading @@ -1027,27 +917,9 @@ 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 = IncompleteNode([]) 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.numberIncompleteNodes() self.root.numberNodes() self.root.completeNode(index, nodeType, self) self.depth = self.root.getDepth() self.dyckWord = self.root.getDyckWord() Loading