Loading model/levelBinaryTreeToPlanarTree.py +4 −1 Changes for model/levelBinaryTreeToPlanarTree.py: 4 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -29,8 +29,11 @@ class LevelBinaryTreeToPlanarTree: def checkTree(self, index: int, nodeType: str): copy = self.tree.copyTree() copy.completeNode(index, nodeType) print("checckkkk") print(self.planarAnswer.root) print(copy.root) """if self.planarAnswer.root.checkNode(copy.root):""" if True: if 1: self.tree.completeNode(index, nodeType) return True else: Loading model/node.py +70 −31 Changes for model/node.py: 70 added lines, 31 removed lines. Original line number Diff line number Diff line Loading @@ -335,24 +335,48 @@ class PlanarNode(Node): brothers = [] if not self.children: if brothers: brother = brothers.pop(0) print("A") print(brother) brother.numberNodes(index, brothers) brothers.pop(0).numberNodes(index, brothers) else: newBrothers = self.children[1:] + brothers print("B") print(self.children[0]) self.children[0].numberNodes(index, newBrothers) def completeNode(self, index: int, nodeType: str, tree): print("---------------------") print(index) print(self) print("---------------------") for child in self.children: child.completeNode(index, nodeType, tree) def checkNode(self, node, nodeBrothers=None, brothers=None): if brothers is None: brothers = [] if nodeBrothers is None: nodeBrothers = [] if isinstance(node, IncompleteNode): return False else: if not self.children: if node.children: return False else: if brothers: if not nodeBrothers: return False else: return brothers.pop(0).checkNode(nodeBrothers.pop(0), nodeBrothers, brothers) else: if nodeBrothers: return True else: return False else: if len(self.children) == len(node.children): newBrothers = self.children[1:] + brothers newNodeBrothers = node.children[1:] + nodeBrothers if len(newNodeBrothers) != len(newBrothers): return False else: return self.children[0].numberNodes(node.children[0], newNodeBrothers, newBrothers) else: return False def setup(self, depth=0, places=None, offsets=None): nb_children = len(self.children) if places is None: Loading Loading @@ -399,14 +423,6 @@ class PlanarNode(Node): copyChildren.append(child.copyNode()) return PlanarNode(copyChildren) def completeNode(self, index, nodeType, tree): if len(self.children) == 0: return index else: for child in self.children: 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": Loading Loading @@ -491,17 +507,12 @@ class IncompleteNode(PlanarNode): self.parent = (node, index) def numberNodes(self, index=0, brothers=None): print("bro:") print(brothers) if brothers is None: brothers = [] self.index = index if not self.children: if brothers: brother = brothers.pop(0) print("A") print(brother) brother.numberNodes(index + 1, brothers) brothers.pop(0).numberNodes(index + 1, brothers) else: newBrothers = self.children[1:] + brothers print("B") Loading @@ -509,10 +520,6 @@ class IncompleteNode(PlanarNode): 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([])) Loading @@ -526,12 +533,46 @@ class IncompleteNode(PlanarNode): for child in self.children: child.completeNode(index, nodeType, tree) def checkNode(self, node, nodeBrothers=None, brothers=None): if brothers is None: brothers = [] if nodeBrothers is None: nodeBrothers = [] if not isinstance(node, IncompleteNode): return False else: if not self.children: if node.children: return False else: if brothers: if not nodeBrothers: return False else: return brothers.pop(0).checkNode(nodeBrothers.pop(0), nodeBrothers, brothers) else: if nodeBrothers: return True else: return False else: if len(self.children) == len(node.children): newBrothers = self.children[1:] + brothers newNodeBrothers = node.children[1:] + nodeBrothers if len(newNodeBrothers) != len(newBrothers): return False else: return self.children[0].numberNodes(node.children[0], newNodeBrothers, newBrothers) else: return False 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]) return ' ' * tab + 'INCOMPLETE NODE' + " " + str(self.index) + '\n' + "".join( [child.toStr(tab + 2) for child in self.children]) def __str__(self): return self.toStr() Loading Loading @@ -864,5 +905,3 @@ if __name__ == '__main__': """print(ptroot) btroot2 = planarTreeToBinaryTree(ptroot)""" Loading
model/levelBinaryTreeToPlanarTree.py +4 −1 Changes for model/levelBinaryTreeToPlanarTree.py: 4 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -29,8 +29,11 @@ class LevelBinaryTreeToPlanarTree: def checkTree(self, index: int, nodeType: str): copy = self.tree.copyTree() copy.completeNode(index, nodeType) print("checckkkk") print(self.planarAnswer.root) print(copy.root) """if self.planarAnswer.root.checkNode(copy.root):""" if True: if 1: self.tree.completeNode(index, nodeType) return True else: Loading
model/node.py +70 −31 Changes for model/node.py: 70 added lines, 31 removed lines. Original line number Diff line number Diff line Loading @@ -335,24 +335,48 @@ class PlanarNode(Node): brothers = [] if not self.children: if brothers: brother = brothers.pop(0) print("A") print(brother) brother.numberNodes(index, brothers) brothers.pop(0).numberNodes(index, brothers) else: newBrothers = self.children[1:] + brothers print("B") print(self.children[0]) self.children[0].numberNodes(index, newBrothers) def completeNode(self, index: int, nodeType: str, tree): print("---------------------") print(index) print(self) print("---------------------") for child in self.children: child.completeNode(index, nodeType, tree) def checkNode(self, node, nodeBrothers=None, brothers=None): if brothers is None: brothers = [] if nodeBrothers is None: nodeBrothers = [] if isinstance(node, IncompleteNode): return False else: if not self.children: if node.children: return False else: if brothers: if not nodeBrothers: return False else: return brothers.pop(0).checkNode(nodeBrothers.pop(0), nodeBrothers, brothers) else: if nodeBrothers: return True else: return False else: if len(self.children) == len(node.children): newBrothers = self.children[1:] + brothers newNodeBrothers = node.children[1:] + nodeBrothers if len(newNodeBrothers) != len(newBrothers): return False else: return self.children[0].numberNodes(node.children[0], newNodeBrothers, newBrothers) else: return False def setup(self, depth=0, places=None, offsets=None): nb_children = len(self.children) if places is None: Loading Loading @@ -399,14 +423,6 @@ class PlanarNode(Node): copyChildren.append(child.copyNode()) return PlanarNode(copyChildren) def completeNode(self, index, nodeType, tree): if len(self.children) == 0: return index else: for child in self.children: 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": Loading Loading @@ -491,17 +507,12 @@ class IncompleteNode(PlanarNode): self.parent = (node, index) def numberNodes(self, index=0, brothers=None): print("bro:") print(brothers) if brothers is None: brothers = [] self.index = index if not self.children: if brothers: brother = brothers.pop(0) print("A") print(brother) brother.numberNodes(index + 1, brothers) brothers.pop(0).numberNodes(index + 1, brothers) else: newBrothers = self.children[1:] + brothers print("B") Loading @@ -509,10 +520,6 @@ class IncompleteNode(PlanarNode): 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([])) Loading @@ -526,12 +533,46 @@ class IncompleteNode(PlanarNode): for child in self.children: child.completeNode(index, nodeType, tree) def checkNode(self, node, nodeBrothers=None, brothers=None): if brothers is None: brothers = [] if nodeBrothers is None: nodeBrothers = [] if not isinstance(node, IncompleteNode): return False else: if not self.children: if node.children: return False else: if brothers: if not nodeBrothers: return False else: return brothers.pop(0).checkNode(nodeBrothers.pop(0), nodeBrothers, brothers) else: if nodeBrothers: return True else: return False else: if len(self.children) == len(node.children): newBrothers = self.children[1:] + brothers newNodeBrothers = node.children[1:] + nodeBrothers if len(newNodeBrothers) != len(newBrothers): return False else: return self.children[0].numberNodes(node.children[0], newNodeBrothers, newBrothers) else: return False 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]) return ' ' * tab + 'INCOMPLETE NODE' + " " + str(self.index) + '\n' + "".join( [child.toStr(tab + 2) for child in self.children]) def __str__(self): return self.toStr() Loading Loading @@ -864,5 +905,3 @@ if __name__ == '__main__': """print(ptroot) btroot2 = planarTreeToBinaryTree(ptroot)"""