I am trying to implement a post-order propositional calculator using a binary tree in a recursive fashion. T represents True, F false, N not, A and, and O or. I tried running the code with GDB, but the compilation results in a seg fault at the function postOrderEval(). The postOrderEval() was provided by the textbook so I think there is something wrong with createTreeFromExpression() but I cannot catch the logic error. Any help would be appreciated. Below is the code with relevant comments.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EXPRESSION_SIZE 100
#define TRUE 1
#define FALSE 0
typedef enum {not, and, or, true, false} logical;
typedef struct treeNode *treePtr;
typedef struct treeNode {
treePtr left_child;
logical data;
short int value;
treePtr right_child;
} node;
/**Evaluates the binary tree of truth values and operators in post-order*/
void postOrderEval(treePtr node) {
if (node) {
postOrderEval(node->left_child);
postOrderEval(node->right_child);
switch (node->data){
case not: node->value = !node->right_child->value;
break;
case and: node->value = node->right_child->value
&& node->left_child->value;
break;
case or: node->value = node->right_child->value
|| node->left_child->value;
break;
case true: node->value = TRUE; break;
case false: node->value = FALSE; break;
}//end switch
}//end if
}
treePtr createTreeFromExpression(const char *expression, int *index) {
treePtr newNode = (treePtr)malloc(sizeof(node));
if (newNode) {
switch (expression[*index]) {
case 'N': newNode->data = not; break;
case 'A': newNode->data = and; break;
case 'O': newNode->data = or; break;
case 'T': newNode->data = true; break;
case 'F': newNode->data = false; break;
default: free(newNode); return NULL;
}
(*index)++; // Move to the next character
}
newNode->left_child = createTreeFromExpression(expression, index);
newNode->right_child = createTreeFromExpression(expression, index);
return newNode;
}
void printExpression(treePtr node) {
if (node) {
switch (node->data) {
case not: printf("N"); break;
case and: printf("A"); break;
case or: printf("O"); break;
case true: printf("T"); break;
case false: printf("F"); break;
}
printExpression(node->left_child);
printExpression(node->right_child);
}
}
//Driver code
int main() {
char expression[MAX_EXPRESSION_SIZE];
printf("Enter a propositional expression without space or newline.\n");
printf("Press ENTER when you're done. \n");
scanf("%s", expression);
int index = 0;
treePtr root = createTreeFromExpression(expression, &index);
// Print the original and swapped post-order traversals
printf("Proposition in post-order: ");
printExpression(root);
printf("\n");
// Evaluate the expression
postOrderEval(root);
// Display the result
printf("Propositional Tree result: %s\n", (root->value == TRUE) ? "TRUE" : "FALSE");
return 0;
}
Here is the sample input:
Input: TFANTA Output: Proposition in post-order: TFANTA (seg fault) (should return: TRUE)
Input: FFA Output: Proposition in post-order: FFA (seg fault) (should return: FALSE)
Input: NTFANFTAO Output: Proposition in post-order: NTFANFTAO (seg fault) (should return: TRUE)
I figured it out and thought I wanted to share the code here.