from csc148_queue import Queue

class Tree:
    ''' Represent a Bare-bones Tree ADT'''

    def __init__(self, value=None, children=None):
        ''' (Tree, object, list-of-Tree) -> NoneType

        Create Tree(self) with content value and 0 or more Tree children.
        '''
        self.value = value
        # copy children if not None
        self.children = children.copy() if children else []
        # functional if is equivalent to:
       #if not children:
           #self.children = []
       #else:
           #self.children = children.copy()

    def __repr__(self):
        ''' (Tree) -> str

        Return representation of Tree (self) as string that
        can be evaluated into an equivalent Tree.

        >>> t1 = Tree(5)
        >>> t1
        Tree(5)
        >>> t2 = Tree(7, [t1])
        >>> t2
        Tree(7, [Tree(5)])
        '''
        # Our __repr__ is recursive, because it can also be called via repr...!
        return ('Tree({}, {})'.format(repr(self.value), repr(self.children))
                if self.children
                else 'Tree({})'.format(repr(self.value)))

    def __eq__(self, other):
        ''' (Tree, object) -> bool

        Return whether this Tree is equivalent to other.


        >>> t1 = Tree(5)
        >>> t2 = Tree(5, [])
        >>> t1 == t2
        True
        >>> t3 = Tree(5, [t1])
        >>> t2 == t3
        False
        '''
        return (isinstance(other, Tree) and
                self.value == other.value and
                self.children == other.children)

    def __str__(self, indent=0):
        ''' (Tree, int) -> str

        Produce a user-friendly string representation of Tree (self).

        >>> t = Tree(17)
        >>> print(t)
        17
        >>> t1 = Tree(19, [t, Tree(23)])
        >>> print(t1)
        19
           17
           23
        >>> t3 = Tree(29, [Tree(31), t1])
        >>> print(t3)
        29
           31
           19
              17
              23
        '''
        root_str = indent * ' ' + str(self.value)
        return '\n'.join([root_str] +
                         [c.__str__(indent + 3) for c in self.children])


    def __contains__(self, v):
        ''' (Tree, v) -> bool

        Return whether Tree self contains v.

        >>> t = Tree(17)
        >>> t.__contains__(17)
        True
        >>> t = descendents_from_list(Tree(19), [1, 2, 3, 4, 5, 6, 7], 3)
        >>> t.__contains__(5)
        True
        >>> t.__contains__(18)
        False
        '''
        if self.children == []:
            return self.value == v
        else:
            return (self.value == v) or (any([v in n for n in self.children]))

def leaf_count(t):
    ''' (Tree) -> int

    Return the number of leaves in Tree t.

    >>> t = Tree(7)
    >>> leaf_count(t)
    1
    >>> t = descendents_from_list(Tree(7), [0, 1, 3, 5, 7, 9, 11, 13], 3)
    >>> leaf_count(t)
    6
    '''
    if len(t.children) == 0:
        return 1
    else:
        return sum([leaf_count(child) for child in t.children])

def descendents_from_list(t, L, arity):
    ''' (Tree, list, int) -> Tree

    Populate t's descendents from L, filling them
    in in level order, with up to arity children per node.
    Then return t.

    >>> descendents_from_list(Tree(0), [1, 2, 3, 4], 2)
    Tree(0, [Tree(1, [Tree(3), Tree(4)]), Tree(2)])
    '''
    q = Queue()
    q.enqueue(t)
    L = L.copy()
    while not q.is_empty(): # unlikely to happen
        new_t = q.dequeue()
        for i in range(0,arity):
            if len(L) == 0:
                return t # our work here is done
            else:
                new_t_child = Tree(L.pop(0))
                new_t.children.append(new_t_child)
                q.enqueue(new_t_child)
    return t



if __name__ == '__main__':
    import doctest
    doctest.testmod()
