from TreeLexer import TreeLexer
from TreeParser import TreeParser
from TreeVisitor import TreeVisitor

from antlr4 import InputStream, CommonTokenStream
import sys

class MyTreeVisitor(TreeVisitor):
    def visitLeaf(self, ctx):
        return True
    
    def visitNode(self, ctx):
        nodes = ctx.int_tree();
        if(len(nodes)>2):
            return False
        for node in nodes:
            if(not self.visit(node)):
                return False
        return True
    
    def visitTop(self, ctx):
        return self.visit(ctx.int_tree())

def main():
    lexer = TreeLexer(InputStream(sys.stdin.read()))
    stream = CommonTokenStream(lexer)
    parser = TreeParser(stream)
    tree = parser.int_tree_top()
    visitor = MyTreeVisitor()
    b = visitor.visit(tree)
    print("Is it a binary tree ? " + str(b))


if __name__ == '__main__':
    main()
