Chuyển biểu thức trung tố thành tiền tố sử dụng cây nhị phân !
code =>:unamused:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc>
typedef char DataType;
typedef struct Node
{
DataType data;
struct Node* left;
struct Node* right;
};
typedef Node* Tree;
void MakeNullTree(Tree &T)
{
T=NULL;
}
int EmptyTree(Tree T)
{
return(T==NULL);
}
Tree LeftChild(Tree T)
{
if(T->left !=NULL)
return T->left;
else
return NULL;
}
Tree RightChild(Tree T)
{
if(T->right !=NULL)
return T->right;
else
return NULL;
}
Tree Create(DataType v; left l; right r)
{
Tree N = (Tree)malloc(sizeof(Node));
N->data = v;
N->left = l;
N->right = r;
}
void InOrder(Tree T)
{
if(!EmptyTree(T))
{
InOrder(LeftChild(T));
printf("%c ",T->data);
InOrder(RightChild(T));
}
}
void PreOrder(Tree T)
{
if(!EmptyTree(T))
{
printf("%c ",T->data);
PreOrderOrder(LeftChild(T));
PreOrderOrder(RightChild(T));
}
}
int main()
{
Tree a = Create('a', NULL, NULL);
Tree b = Create('b', NULL, NULL);
Tree c = Create('c', NULL, NULL);
Tree d = Create('d', NULL, NULL);
Tree e = Create('e', NULL, NULL);
Tree + = Create('+', a, b);
Tree * = Create('*', +, c);
Tree / = Create('/', d, e);
Tree - = Create('-', *, /);
printf("Bieu thuc trung to la: ");
InOrder(a);
printf("\nBieu thuc tien to la: ");
PreOrder(a);
return 0;
}