Commit e9a67e7f authored by Aurélien's avatar Aurélien
Browse files

refactoring et documentation

parent 4559d142
Loading
Loading
Loading
Loading
+26 −71
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ class BinaryNode(Node):

    def setup(self, depth=0, places=None, offsets=None):
        """
        this function permeats to give the position of the node compared with the others nodes of the tree in which it
        this method allows to give the position of the node compared with the others nodes of the tree in which it
        is part by filling the fields x, y and offset of the node

        :param depth: the depth of the node in the tree
@@ -159,6 +159,13 @@ class BinaryNode(Node):
        self.offset = offsets[depth]

    def add_offsets(self, offset_sum=0):
        """
        this is used to give the position of each node of the tree thanks to the offsets which are calculated with the
        method setup

        :param offset_sum:
        :return:
        """
        self.x += offset_sum
        offset_sum += self.offset
        self.height = self.y
@@ -271,7 +278,7 @@ class BinaryNode(Node):

    def addNode(self, index: int, nodeType: str, tree):
        """
        this method which permets to add a node child to the node
        this method which allows to add a node child to the node
        is will add the node of type 'nodeType' at the position 'index'
        the index is the number of the EmptyNode that we will replace in the node by a new node of type
        Leaf or BinaryNode
@@ -343,6 +350,12 @@ class PlanarNode(Node):
        self.children[index] = node

    def numberNodes(self, index=0, brothers=None):
        """
        this method is used to number each node of the tree with a prefixed route

        :param index: the number of the next node of the tree
        :param brothers: a list of the next nodes which will be numbered
        """
        if brothers is None:
            brothers = []
        if not self.children:
@@ -353,6 +366,7 @@ class PlanarNode(Node):
            self.children[0].numberNodes(index, newBrothers)

    def completeNode(self, index: int, nodeType: str, tree):

        for child in self.children:
            child.completeNode(index, nodeType, tree)

@@ -374,10 +388,6 @@ class PlanarNode(Node):
        if not self.children:
            # if they have not children, call this funtion with the brother of the current nodes
            if brothers:
                """if len(nodeBrothers) != len(brothers):
                    print("Z")
                    return True
                else:"""
                return brothers.pop(0).checkNode(nodeBrothers.pop(0), nodeBrothers, brothers)
            else:
                # if they have no children and no brothers, they are exactly the same trees
@@ -506,6 +516,9 @@ class PlanarNode(Node):


class IncompleteNode(PlanarNode):
    """
    this class represents a planarNode which has not all its children
    """
    def __init__(self, children):
        super(PlanarNode, self).__init__()
        self.children = children
@@ -668,7 +681,10 @@ class Leaf(BinaryNode):


class EmptyNode(Node):

    """
    this class represents a node of a binary tree which is incomplete, it has no children and it is intended to become a BinaryNode
    or a leaf
    """
    def __init__(self):
        super(Node, self).__init__()
        self.x = 0
@@ -742,31 +758,6 @@ class EmptyNode(Node):
    def getDepth(self):
        return 0

    def planarTreeToBinaryTree(self):
        """
        this method change a planar tree in a binary tree

        :param self: the planar tree we want to transform
        :return: the binary tree corresponding with the planar tree
        """
        return EmptyNode()

    def planarToBinaryRec(self, brothers):
        """
        this method is called recursively to get a binary tree from a planr tree

        :param self: the planar tree
        :param brothers: the brothers of the parent of the planar tree
        :return: a binary tree corresponding with the planar tree
        """
        left = EmptyNode()
        if brothers:
            right = brothers[0].planarToBinaryRec(brothers[1:])
        else:
            right = Leaf()

        return BinaryNode(left, right)

    def __str__(self):
        return self.toStr()

@@ -871,6 +862,9 @@ def testRandomTreeGeneration(nbTests, treeMin, treeMax):


def binaryTreeToPlanarTree(binaryTree):
    """
    this method change a binaryTree into a PlanarTree
    """
    return binaryToPlanarRec(BinaryNode(binaryTree, Leaf()))


@@ -919,42 +913,3 @@ def planarToBinaryRec(planarTree, brothers):
        right = Leaf()
    return BinaryNode(left, right)

# testRandomTreeGeneration(100000,1, 3)
if __name__ == '__main__':
    pt111 = IncompleteNode([])
    pt11 = IncompleteNode([pt111])
    pt12 = PlanarNode([])
    pt1 = PlanarNode([pt11, pt12])

    pt21 = PlanarNode([])
    pt2 = IncompleteNode([pt21])

    pt31 = IncompleteNode([])
    pt32 = IncompleteNode([])
    pt33 = PlanarNode([])
    pt3 = PlanarNode([pt31, pt32, pt33])

    #ptroot = IncompleteNode([pt1, pt2, pt3])

    ptroot = IncompleteNode([])

    print(ptroot.getNbChildrenFromIndex(0))

    """btroot = BinaryNode(Leaf(), BinaryNode(Leaf(), Leaf()))

    ptroot = binaryTreeToPlanarTree(btroot)"""

    """ptroot = IncompleteNode([IncompleteNode([IncompleteNode([])]),
                             IncompleteNode([IncompleteNode([]), IncompleteNode([])]),
                             IncompleteNode([IncompleteNode([]), IncompleteNode([]), IncompleteNode([])])])
    print(ptroot)
    ptroot.numberNodes()
    print(ptroot)"""
    """l1 = ["a","b"]
    c = l1[1:]
    print(c)"""

    """print(ptroot)

    btroot2 = planarTreeToBinaryTree(ptroot)"""
+67 −28
Original line number Diff line number Diff line
@@ -75,6 +75,17 @@ class MenuView(QGraphicsView):
        self.drawBinaryTree()
        self.drawPlanarTree()
        self.drawDyckWord()
        self.drawPlanarTreeToBinaryTreeArrow()
        self.drawPlanarTreeToDyckWordArrow()
        self.drawBinaryTreeToPlanarTreeArrow()
        self.drawBinaryTreeToDyckWordArrow()
        self.drawDyckWordToPlanarTreeArrow()
        self.drawDyckWordToBinaryTreeArrow()

        self.scene().addItem(self.itemsGroup)
        self.scene().setSceneRect(self.itemsGroup.sceneBoundingRect())

    def drawPlanarTreeToBinaryTreeArrow(self):
        beginX = self.c2x + 0.5 * self.circleSize - 0.5 * self.circleSize * sin(pi / 11)
        beginY = self.c2y + 0.5 * self.circleSize - 0.5 * self.circleSize * cos(pi / 11)
        endX = self.c1x + 0.5 * self.circleSize - 0.5 * self.circleSize * cos(pi / 11)
@@ -85,7 +96,45 @@ class MenuView(QGraphicsView):
        p1y = beginY - 0.5 * yLength
        p2x = beginX + 0.35 * xLength
        p2y = beginY - 0.8 * yLength
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.PLANAR_TREE_TO_BINARY_TREE)
        arrow = Arrow(GameEnum.PLANAR_TREE_TO_BINARY_TREE, beginX, beginY, endX, endY, p1x, p1y, p2x, p2y,
                      self.circleSize, self.controller)
        self.arrows.append(arrow)
        self.scene().addItem(arrow)

    def drawPlanarTreeToDyckWordArrow(self):
        beginX = self.c2x + self.circleSize
        beginY = self.c2y + 0.5 * self.circleSize
        endX = self.c3x
        endY = self.c3y + 0.5 * self.circleSize
        xLength = endX - beginX
        p1x = beginX + 0.2 * xLength
        p1y = beginY - 0.07 * self.circleSize
        p2x = beginX + 0.5 * xLength
        p2y = beginY - 0.07 * self.circleSize
        arrow = Arrow(GameEnum.PLANAR_TREE_TO_DYCK_WORD, beginX, beginY, endX, endY, p1x, p1y, p2x, p2y,
                      self.circleSize, self.controller)
        self.arrows.append(arrow)

        self.scene().addItem(arrow)

    def drawBinaryTreeToPlanarTreeArrow(self):
        beginX = self.c1x + 0.5 * self.circleSize - (sin(pi / 5) * 0.5 * self.circleSize)
        beginY = self.c1y + 0.5 * self.circleSize + (cos(pi / 5) * 0.5 * self.circleSize)
        endX = self.c2x + 0.5 * self.circleSize + (sin(pi / 7) * 0.5 * self.circleSize)
        endY = self.c2y + 0.5 * self.circleSize - (cos(pi / 7) * 0.5 * self.circleSize)
        xLength = beginX - endX
        yLength = endY - beginY
        p1x = beginX + 0.35 * xLength
        p1y = beginY + 0.15 * yLength
        p2x = beginX + 0.35 * xLength
        p2y = beginY + 0.25 * yLength
        arrow = Arrow(GameEnum.BINARY_TREE_TO_PLANAR_TREE, beginX, beginY, endX, endY, p1x, p1y, p2x, p2y,
                      self.circleSize, self.controller)
        self.arrows.append(arrow)

        self.scene().addItem(arrow)

    def drawBinaryTreeToDyckWordArrow(self):
        beginX = self.c1x + 0.5 * self.circleSize + 0.5 * self.circleSize * cos(pi / 11)
        beginY = self.c1y + 0.5 * self.circleSize + 0.5 * self.circleSize * sin(pi / 11)
        endX = self.c3x + 0.5 * self.circleSize + 0.5 * self.circleSize * sin(pi / 11)
@@ -96,7 +145,13 @@ class MenuView(QGraphicsView):
        p1y = beginY + 0.15 * yLength
        p2x = beginX + 0.9 * xLength
        p2y = beginY + 0.45 * yLength
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.BINARY_TREE_TO_DYCK_WORD)
        arrow = Arrow(GameEnum.BINARY_TREE_TO_DYCK_WORD, beginX, beginY, endX, endY, p1x, p1y, p2x, p2y,
                      self.circleSize, self.controller)
        self.arrows.append(arrow)

        self.scene().addItem(arrow)

    def drawDyckWordToPlanarTreeArrow(self):
        beginX = self.c3x + 0.5 * self.circleSize - (sin(pi / 4) * 0.5 * self.circleSize)
        beginY = self.c3y + 0.5 * self.circleSize + (cos(pi / 4) * 0.5 * self.circleSize)
        endX = self.c2x + 0.5 * self.circleSize + (sin(pi / 4) * 0.5 * self.circleSize)
@@ -106,7 +161,12 @@ class MenuView(QGraphicsView):
        p1y = beginY + 0.1 * self.circleSize
        p2x = beginX - 0.6 * xLength
        p2y = beginY + 0.1 * self.circleSize
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.DYCK_WORD_TO_PLANAR_TREE)
        arrow = Arrow(GameEnum.DYCK_WORD_TO_PLANAR_TREE, beginX, beginY, endX, endY, p1x, p1y, p2x, p2y,
                      self.circleSize, self.controller)
        self.arrows.append(arrow)
        self.scene().addItem(arrow)

    def drawDyckWordToBinaryTreeArrow(self):
        beginX = self.c3x + 0.5 * self.circleSize - (sin(pi / 7) * 0.5 * self.circleSize)
        beginY = self.c3y + 0.5 * self.circleSize - (cos(pi / 7) * 0.5 * self.circleSize)
        endX = self.c1x + 0.5 * self.circleSize + (sin(pi / 5) * 0.5 * self.circleSize)
@@ -117,31 +177,10 @@ class MenuView(QGraphicsView):
        p1y = beginY - 0.1 * yLength
        p2x = beginX - 1 * xLength
        p2y = beginY - 0.45 * yLength
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.DYCK_WORD_TO_BINARY_TREE)
        beginX = self.c1x + 0.5 * self.circleSize - (sin(pi / 5) * 0.5 * self.circleSize)
        beginY = self.c1y + 0.5 * self.circleSize + (cos(pi / 5) * 0.5 * self.circleSize)
        endX = self.c2x + 0.5 * self.circleSize + (sin(pi / 7) * 0.5 * self.circleSize)
        endY = self.c2y + 0.5 * self.circleSize - (cos(pi / 7) * 0.5 * self.circleSize)
        xLength = beginX - endX
        yLength = endY - beginY
        p1x = beginX + 0.35 * xLength
        p1y = beginY + 0.15 * yLength
        p2x = beginX + 0.35 * xLength
        p2y = beginY + 0.25 * yLength
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.BINARY_TREE_TO_PLANAR_TREE)
        beginX = self.c2x + self.circleSize
        beginY = self.c2y + 0.5 * self.circleSize
        endX = self.c3x
        endY = self.c3y + 0.5 * self.circleSize
        xLength = endX - beginX
        p1x = beginX + 0.2 * xLength
        p1y = beginY - 0.07 * self.circleSize
        p2x = beginX + 0.5 * xLength
        p2y = beginY - 0.07 * self.circleSize
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.PLANAR_TREE_TO_DYCK_WORD)

        self.scene().addItem(self.itemsGroup)
        self.scene().setSceneRect(self.itemsGroup.sceneBoundingRect())
        arrow = Arrow(GameEnum.DYCK_WORD_TO_BINARY_TREE, beginX, beginY, endX, endY, p1x, p1y, p2x, p2y,
                      self.circleSize, self.controller)
        self.arrows.append(arrow)
        self.scene().addItem(arrow)

    def drawArrow(self, beginX: float, beginY: float, endX: float, endY: float, p1x: float, p1y: float, p2x: float,
                  p2y: float, game: GameEnum):