I have a binary tree of functions and terminal values. I’d like to print this tree as a lisp statement would be represented!
For example, a tree with just a root of “+” and terminals of “2” and 4″ would read (+ (2 4)).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to do an preorder traversal of the binary tree. So if you have the tree:
You would want to visit +, 5, -, 3, 2, in that order. You can do this recursively as follows (assuming your nodes have the fields value, left, and right):
Notice that you simply visit the current node, then the left child, then the right child.