Commit fa005ce3 authored by Aurelien Dufour's avatar Aurelien Dufour
Browse files

debut des jeux de bijection arbre bnaire / arbre planaire

parent bd10958e
Loading
Loading
Loading
Loading
+1 −1
Changes for model/levelBinaryTreeToPlanarTree.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ class LevelBinaryTreeToPlanarTree:
        self.tree.root.addNode(index, nodeType)

    def checkTree(self, index: int, nodeType: str):
        copy = self.tree.copyBinaryTree()
        copy = self.tree.copyTree()
        copy.root.addNode(index, nodeType)
        if self.answer.root.checkNode(copy.root):
            self.tree.addNode(index, nodeType)
+1 −1
Changes for model/levelDyckWordToBinaryTree.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ class LevelDyckWordToBinaryTree:
        self.tree.root.addNode(index, nodeType)

    def checkTree(self, index: int, nodeType: str):
        copy = self.tree.copyBinaryTree()
        copy = self.tree.copyTree()
        copy.root.addNode(index, nodeType)
        if self.answer.root.checkNode(copy.root):
            self.tree.addNode(index, nodeType)
+1 −1
Changes for model/levelPlanarTreeToBinaryTree.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ class LevelPlanarTreeToBinaryTree:
        self.tree.root.addNode(index, nodeType)

    def checkTree(self, index: int, nodeType: str):
        copy = self.tree.copyBinaryTree()
        copy = self.tree.copyTree()
        copy.root.addNode(index, nodeType)
        if self.answer.root.checkNode(copy.root):
            self.tree.addNode(index, nodeType)
+52 −20
Changes for model/node.py: 52 added lines, 20 removed lines.
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ class Node:
    def checkNode(self, answer):
        pass


class BinaryNode(Node):
    """
    this class represents a binary node with two children
@@ -234,7 +235,9 @@ class BinaryNode(Node):
        :param answer: the node we wan to compare with
        :return: True if the nodes are the sames, False otherwise
        """
        if isinstance(answer, Leaf) and isinstance(self, Leaf):
        if isinstance(answer, EmptyNode):
            return True
        elif isinstance(answer, Leaf) and isinstance(self, Leaf):
            return True
        elif isinstance(answer, Leaf) and not isinstance(self, Leaf):
            return False
@@ -325,7 +328,7 @@ class PlanarNode(Node):
            self.width = max(self.width, child.width)

    def getDyckWord(self):
        binaryNode = planarTreeToBinaryTree(self)
        binaryNode = self.planarTreeToBinaryTree()
        return binaryNode.getDyckWord()

    def copyNode(self):
@@ -358,6 +361,8 @@ class PlanarNode(Node):
    def checkNode(self, answer):
        if isinstance(answer, EmptyNode):
            return True
        elif len(self.children) != len(answer.children):
            return False
        elif len(self.children) == 0 and len(answer.children) == 0:
            return True
        elif len(answer.children) == 0:
@@ -365,7 +370,10 @@ class PlanarNode(Node):
        elif len(self.children) == 0:
            return False
        else:
            return self.left.checkNode(answer.left) and self.right.checkNode(answer.right)
            test = True
            for i in range(len(self.children)):
                test = test and self.children[i].isEqual(answer.children[i])
            return test

    def addChild(self, planarNode):
        self.children.append(planarNode)
@@ -381,45 +389,44 @@ class PlanarNode(Node):
        else:
            return 1 + max(child.getDepth() for child in self.children)

    def toStr(self, tab=0):
        return ' ' * tab + 'NODE' + '\n' + "".join([child.toStr(tab + 2) for child in self.children])

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


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

    :param planarTree: the planar tree we want to transform
        :param self: the planar tree we want to transform
        :return: the binary tree corresponding with the planar tree
        """
    if planarTree.children:
        return planarToBinaryRec(planarTree.children[0], planarTree.children[1:])
        if self.children:
            return self.children[0].planarToBinaryRec(self.children[1:])
        else:
            return Leaf()


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

    :param planarTree: the planar 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
        """
    if planarTree.children:
        left = planarToBinaryRec(planarTree.children[0], planarTree.children[1:])

        if self.children:
            left = self.children[0].planarToBinaryRec(self.children[1:])
        else:
            left = Leaf()
        if brothers:
        right = planarToBinaryRec(brothers[0], brothers[1:])
            right = brothers[0].planarToBinaryRec(brothers[1:])
        else:
            right = Leaf()

        return BinaryNode(left, right)

    def toStr(self, tab=0):
        return ' ' * tab + 'NODE' + '\n' + "".join([child.toStr(tab + 2) for child in self.children])

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


def binaryTreeToPlanarTree(binaryTree):
    return binaryToPlanarRec(BinaryNode(binaryTree, Leaf()))
@@ -557,6 +564,31 @@ 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 toStr(self, tab=0):
        return ' ' * tab + 'EMPTY' + '\n'

+1 −1
Changes for model/tree.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ class Tree:
        self.index = 0
        self.setup_root()

    def copyBinaryTree(self):
    def copyTree(self):
        return Tree(self.root.copyNode())

    def addNode(self, index: int, nodeType: str):
Loading