Loading model/node.py +67 Changes for model/node.py: 67 added lines, 0 removed lines. Original line number Diff line number Diff line from collections import defaultdict from random import randint def setup(node, depth=0, places=None, offsets=None): if isinstance(node, BinaryNode): if not isinstance(node, Leaf) and not isinstance(node, EmptyNode): nb_children = 2 else: nb_children = 0 else: nb_children = len(node.children) if places is None: places = defaultdict(lambda: 0) if offsets is None: offsets = defaultdict(lambda: 0) node.y = depth if isinstance(node, BinaryNode): if not isinstance(node, Leaf) and not isinstance(node, EmptyNode): setup(node.left, depth, places, offsets) setup(node.right, depth, places, offsets) else: for child in node.children: setup(child, depth, places, offsets) if nb_children == 0: place = places[depth] node.x = place else: if isinstance(node, BinaryNode): place = (node.left.x + node.right.x) / 2 else: place = (node.children[0].x + node.children[nb_children - 1].x) / 2 offsets[depth] = max(offsets[depth], places[depth] - place) if nb_children != 0: node.x = place + offsets[depth] places[depth] = node.x + 1 node.offset = offsets[depth] def add_offsets(node, offset_sum=0): node.x += offset_sum offset_sum += node.offset node.height = node.y node.width = node.x if isinstance(node, BinaryNode): if not isinstance(node, Leaf) and not isinstance(node, EmptyNode): add_offsets(node.left, offset_sum) add_offsets(node.right, offset_sum) else: for child in node.children: add_offsets(child, offset_sum) def putHole(word, start, end): """ this method permeates to create a hole in a dyck word, a part of the word is removed and replaced by some '?' Loading Loading @@ -84,6 +140,12 @@ class BinaryNode: self.right = right self.right.setParent(self, 'r') self.parent = None self.x = 0 self.y = 0 self.height = 0 self.width = 0 self.offset = 0 def setParent(self, node, side): """ Loading Loading @@ -231,6 +293,11 @@ class PlanarNode: def __init__(self, children: list): self.children = children self.x = 0 self.y = 0 self.height = 0 self.width = 0 self.offset = 0 def addChild(self, planarNode): self.children.append(planarNode) Loading model/tree.py +4 −2 Changes for model/tree.py: 4 added lines, 2 removed lines. Original line number Diff line number Diff line from model.node import BinaryNode, PlanarNode from collections import defaultdict from model.node import BinaryNode, PlanarNode, Leaf, setup, add_offsets class BinaryTree: Loading @@ -24,3 +24,5 @@ class PlanarTree: def __init__(self, root: PlanarNode): self.root = root self.depth = root.getDepth() view/treeView.py +3 Changes for view/treeView.py: 3 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -163,6 +163,9 @@ class TreeView(QGraphicsView): self.drawTree(tree.left, (leftEnd[0], leftEnd[1]), branchWidth / 1.8, branchHeight, nodeWidth, lineWidth) self.drawTree(tree.right, (rightEnd[0], rightEnd[1]), branchWidth / 1.8, branchHeight, nodeWidth, lineWidth) def drawTree2(self, branchWidth, branchHeight, nodeWidth, lineWidth): pass class Clickable(QObject): clicked = pyqtSignal() Loading Loading
model/node.py +67 Changes for model/node.py: 67 added lines, 0 removed lines. Original line number Diff line number Diff line from collections import defaultdict from random import randint def setup(node, depth=0, places=None, offsets=None): if isinstance(node, BinaryNode): if not isinstance(node, Leaf) and not isinstance(node, EmptyNode): nb_children = 2 else: nb_children = 0 else: nb_children = len(node.children) if places is None: places = defaultdict(lambda: 0) if offsets is None: offsets = defaultdict(lambda: 0) node.y = depth if isinstance(node, BinaryNode): if not isinstance(node, Leaf) and not isinstance(node, EmptyNode): setup(node.left, depth, places, offsets) setup(node.right, depth, places, offsets) else: for child in node.children: setup(child, depth, places, offsets) if nb_children == 0: place = places[depth] node.x = place else: if isinstance(node, BinaryNode): place = (node.left.x + node.right.x) / 2 else: place = (node.children[0].x + node.children[nb_children - 1].x) / 2 offsets[depth] = max(offsets[depth], places[depth] - place) if nb_children != 0: node.x = place + offsets[depth] places[depth] = node.x + 1 node.offset = offsets[depth] def add_offsets(node, offset_sum=0): node.x += offset_sum offset_sum += node.offset node.height = node.y node.width = node.x if isinstance(node, BinaryNode): if not isinstance(node, Leaf) and not isinstance(node, EmptyNode): add_offsets(node.left, offset_sum) add_offsets(node.right, offset_sum) else: for child in node.children: add_offsets(child, offset_sum) def putHole(word, start, end): """ this method permeates to create a hole in a dyck word, a part of the word is removed and replaced by some '?' Loading Loading @@ -84,6 +140,12 @@ class BinaryNode: self.right = right self.right.setParent(self, 'r') self.parent = None self.x = 0 self.y = 0 self.height = 0 self.width = 0 self.offset = 0 def setParent(self, node, side): """ Loading Loading @@ -231,6 +293,11 @@ class PlanarNode: def __init__(self, children: list): self.children = children self.x = 0 self.y = 0 self.height = 0 self.width = 0 self.offset = 0 def addChild(self, planarNode): self.children.append(planarNode) Loading
model/tree.py +4 −2 Changes for model/tree.py: 4 added lines, 2 removed lines. Original line number Diff line number Diff line from model.node import BinaryNode, PlanarNode from collections import defaultdict from model.node import BinaryNode, PlanarNode, Leaf, setup, add_offsets class BinaryTree: Loading @@ -24,3 +24,5 @@ class PlanarTree: def __init__(self, root: PlanarNode): self.root = root self.depth = root.getDepth()
view/treeView.py +3 Changes for view/treeView.py: 3 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -163,6 +163,9 @@ class TreeView(QGraphicsView): self.drawTree(tree.left, (leftEnd[0], leftEnd[1]), branchWidth / 1.8, branchHeight, nodeWidth, lineWidth) self.drawTree(tree.right, (rightEnd[0], rightEnd[1]), branchWidth / 1.8, branchHeight, nodeWidth, lineWidth) def drawTree2(self, branchWidth, branchHeight, nodeWidth, lineWidth): pass class Clickable(QObject): clicked = pyqtSignal() Loading