-2

I was making program which can take postfix expression and then will create a binary expression tree of that expression .My program was running perfectly .I was testing my program by copy /pasting some samples postfix expression but the problem is now it is not printing inorder, post order and preorder but infact it is printing one weird character. While testing I might have accidentally pressed function key. I am not sure what is causing this problem. Please help me out in identifying my mistake .

This the output. please tell me why it is printing this character after a and not the expression. enter image description here

  • Recommended read: https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – πάντα ῥεῖ May 16 '21 at 21:59
  • 1
    `(struct BTstack*)malloc(sizeof(struct BTstack))` -- Why are you using `malloc` in a C++ program? You are issuing a call to `delete` with memory allocated using `malloc`. That is undefined behavior. Also, why are you using the keyword `struct` everywhere in places where it isn't necessary? It looks like you're trying to write C code, not C++. – PaulMcKenzie May 16 '21 at 22:00
  • -PaulMcKenzie I am learning data structure and professor instructed as to use this just for learning purpose ...this is not the issue right now.I have traced the whole program and it was even working fine but now I don't know what caused this problem.It is either compiler error of codeblock or it is my mistake .Please help me out .I will be forever grateful to u. – Areeba Junaid May 16 '21 at 22:08
  • @AreebaJunaid _"It is either compiler error of codeblock ..."_ That's extremely unlikely. Do as I receommended, use the debugger to strp through your code, and investigate about what's going on, and finally find out what's your mistake. You can see how the variables change at each step. – πάντα ῥεῖ May 16 '21 at 22:26
  • 1
    @AreebaJunaid _"this is not the issue right now"_ That's a very big issue! It basically makes your program having _undefined behavior_, and you cannot trust it to do anything reasonable anymore. – πάντα ῥεῖ May 16 '21 at 22:28

1 Answers1

2

You don't initialize leftChild and rightChild members of BTnodes you allocate. These pointers contain random garbage; they are generally not NULL.

When you traverse the tree, you eventually descend down to a leaf node and then attempt to traverse further down through these garbage pointers. Whereupon the program exhibits undefined behavior.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • @Ignor Tandetnik Can you plz Elaborate in detail where l made the mistake........l am finally getting there. – Areeba Junaid May 17 '21 at 04:51
  • `temp = (struct BTnode*)malloc(sizeof(struct BTnode)); temp->letter=postfix[a];` This initializes `letter` member of the new node, but but `leftChild` or `rightChild` members. Those are never initialized. – Igor Tandetnik May 17 '21 at 13:49
  • @Ignore Tandetnik l initialized them in isalpha if condition .ln this program when l got letters or digit l created a node and initialized them and stored the address of nodes in linklist stack ....that is why when l got operator .l pop two elements from stacks and stored those pop address of node in left and right child.....plz tell me now why l need to initialize again? – Areeba Junaid May 17 '21 at 15:45
  • @lgor Tandetnik l also printed the BTnode_top ->letter in main and it was printing fine – Areeba Junaid May 17 '21 at 16:10
  • You set the child pointers for the node representing the operator, but not for the nodes representing a letter or digit. You compare those pointers to `NULL` (in `if (curr==NULL)`), but you never in fact **set** them to `NULL`. Observe that you don't have, say, `leftChild = NULL;` anywhere in the program, and yet you expect `inorder(curr->leftChild)` to see `NULL` pointer and terminate the recursion. – Igor Tandetnik May 17 '21 at 16:15