Commit 9044a60b authored by Aurélien's avatar Aurélien
Browse files

amélioration affichage arbre

parent fca66f43
Loading
Loading
Loading
Loading
+100 −63
Changes for model/node.py: 100 added lines, 63 removed lines.
Original line number Diff line number Diff line
@@ -2,67 +2,6 @@ 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 + 1, places, offsets)
            setup(node.right, depth + 1, places, offsets)
    else:
        for child in node.children:
            setup(child, depth + 1, 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)
            node.height = max(node.left.height, node.height)
            node.width = max(node.right.width, node.width)
            add_offsets(node.right, offset_sum)
            node.height = max(node.right.height, node.height)
            node.width = max(node.right.width, node.width)
    else:
        for child in node.children:
            add_offsets(child, offset_sum)
            node.height = max(node.height, child.height)
            node.width = max(node.width, child.width)


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 '?'
@@ -135,12 +74,22 @@ def completeWord2(dyckWord, letters):
    return newWord


class BinaryNode:
class Node:

    def setup(self, depth=0, places=None, offsets=None):
        pass

    def add_offsets(self, offset_sum=0):
        pass


class BinaryNode(Node):
    """
    this class represents a binary node with two children
    """

    def __init__(self, left, right):
        super(Node, self).__init__()
        self.left = left
        self.left.setParent(self, 'l')
        self.right = right
@@ -152,6 +101,36 @@ class BinaryNode:
        self.width = 0
        self.offset = 0

    def setup(self, depth=0, places=None, offsets=None):
        if places is None:
            places = defaultdict(lambda: 0)
        if offsets is None:
            offsets = defaultdict(lambda: 0)
        self.y = depth

        self.left.setup(depth + 1, places, offsets)
        self.right.setup(depth + 1, places, offsets)

        place = (self.left.x + self.right.x) / 2

        offsets[depth] = max(offsets[depth], places[depth] - place)

        self.x = place + offsets[depth]

        places[depth] = self.x + 1
        self.offset = offsets[depth]

    def add_offsets(self, offset_sum=0):
        self.x += offset_sum
        offset_sum += self.offset
        self.height = self.y
        self.width = self.x
        self.left.add_offsets(offset_sum)
        self.height = max(self.left.height, self.height)
        self.width = max(self.left.width, self.width)
        self.right.add_offsets(offset_sum)
        self.height = max(self.right.height, self.height)
        self.width = max(self.right.width, self.width)

    def setParent(self, node, side):
        """
@@ -292,12 +271,13 @@ class BinaryNode:
            return BinaryNode(self.left.copyBinaryNode(), self.right.copyBinaryNode())


class PlanarNode:
class PlanarNode(Node):
    """
    this class represents a planar node with several children
    """

    def __init__(self, children: list):
        super(Node, self).__init__()
        self.children = children
        self.x = 0
        self.y = 0
@@ -305,6 +285,42 @@ class PlanarNode:
        self.width = 0
        self.offset = 0

    def setup(self, depth=0, places=None, offsets=None):
        nb_children = len(self.children)
        if places is None:
            places = defaultdict(lambda: 0)
        if offsets is None:
            offsets = defaultdict(lambda: 0)
        self.y = depth

        for child in self.children:
            child.setup(depth + 1, places, offsets)
            child.setup(depth + 1, places, offsets)

        if nb_children == 0:
            place = places[depth]
            self.x = place
        else:
            place = (self.children[0].x + self.children[nb_children - 1].x) / 2

        offsets[depth] = max(offsets[depth], places[depth] - place)

        if nb_children != 0:
            self.x = place + offsets[depth]

        places[depth] = self.x + 1
        self.offset = offsets[depth]

    def add_offsets(self, offset_sum=0):
        self.x += offset_sum
        offset_sum += self.offset
        self.height = self.y
        self.width = self.x
        for child in self.children:
            child.add_offsets(offset_sum)
            self.height = max(self.height, child.height)
            self.width = max(self.width, child.width)

    def addChild(self, planarNode):
        self.children.append(planarNode)

@@ -392,6 +408,27 @@ class Leaf(BinaryNode):
        self.width = 0
        self.offset = 0

    def setup(self, depth=0, places=None, offsets=None):
        if places is None:
            places = defaultdict(lambda: 0)
        if offsets is None:
            offsets = defaultdict(lambda: 0)
        self.y = depth

        place = places[depth]
        self.x = place

        offsets[depth] = max(offsets[depth], places[depth] - place)

        places[depth] = self.x + 1
        self.offset = offsets[depth]

    def add_offsets(self, offset_sum=0):
        self.x += offset_sum
        offset_sum += self.offset
        self.height = self.y
        self.width = self.x

    def getDyckWord(self):
        return ""

+3 −3
Changes for model/tree.py: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
from collections import defaultdict

from model.node import BinaryNode, PlanarNode, Leaf, setup, add_offsets
from model.node import BinaryNode, PlanarNode


class BinaryTree:
@@ -22,8 +22,8 @@ class BinaryTree:
        self.setup_root()

    def setup_root(self):
        setup(self.root)
        add_offsets(self.root)
        self.root.setup()
        self.root.add_offsets()

class PlanarTree:
    def __init__(self, root: PlanarNode):
+2 −2
Changes for view/treeView.py: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -223,9 +223,9 @@ class TreeView(QGraphicsView):
        else:
            leftLine = QGraphicsLineItem()
            rightLine = QGraphicsLineItem()
            leftLine.setLine(tree.x * widthRatio, tree.y * heightRatio, tree.left.x * widthRatio, tree.left.y * heightRatio)
            leftLine.setLine(tree.x * widthRatio, tree.y * heightRatio + nodeWidth / 2, tree.left.x * widthRatio, tree.left.y * heightRatio)
            leftLine.setPen(branchPen)
            rightLine.setLine(tree.x * widthRatio, tree.y * heightRatio, tree.right.x * widthRatio, tree.right.y * heightRatio)
            rightLine.setLine(tree.x * widthRatio, tree.y * heightRatio + nodeWidth / 2, tree.right.x * widthRatio, tree.right.y * heightRatio)
            rightLine.setPen(branchPen)
            self.treeItemsGroup.addToGroup(leftLine)
            self.treeItemsGroup.addToGroup(rightLine)