dsa4
dsa4
#include <string.h>
#include <cctype>
#include <queue>
struct node
char data;
node *left;
node *right;
};
class tree
public:
node *top;
void expression(char[]);
};
class stack1
node *data[30];
int top;
public:
stack1()
top = -1;
int empty()
if (top == -1)
return 1;
return 0;
data[++top] = p;
node *pop()
return (data[top--]);
};
int operandCount = 0;
int operatorCount = 0;
if (isalnum(expr[i])) {
operandCount++;
} else {
// For a valid prefix, the number of operands must always be greater than or equal to the
number of operators
return false;
char c;
stack1 s;
int len, i;
len = strlen(prefix);
top->left = NULL;
top->right = NULL;
if (isalnum(prefix[i]))
{
top->data = prefix[i];
s.push(top);
t2 = s.pop();
t1 = s.pop();
top->data = prefix[i];
top->left = t2;
top->right = t1;
s.push(top);
top = s.pop();
if (root == NULL)
return;
queue<node *> q;
q.push(root);
int level = 0;
while (!q.empty())
{
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
levelNodeCount--;
stack1 s1, s2; // stack s1 is for flag to check the left-right subtree
node *T = top;
s1.push(T);
while (!s1.empty())
T = s1.pop();
s2.push(T);
if (T->left != NULL)
s1.push(T->left);
if (T->right != NULL)
s1.push(T->right);
while (!s2.empty())
top = s2.pop();
if (node == NULL)
return;
del(node->left);
del(node->right);
free(node);
int main()
{
char expr[20];
tree t;
while (true) {
if (isValidPrefix(expr)) {
} else {
t.expression(expr);
t.display_levelwise(t.top);
t.non_rec_postorder(t.top);
t.del(t.top);
return 0;
}
OUTPUT: