#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct Node
{
char INFO;
struct Node *LEFT;
struct Node *RIGHT;
};
typedef struct Node Simpul;
Simpul *ROOT,*P;
char X;
void Inisialisasi()
{
ROOT=NULL;
P=NULL;
}
void BuatSimpul(char X)
{
P=(Simpul *)malloc(sizeof(Simpul));
P->INFO=X;
P->RIGHT=NULL;
P->LEFT=NULL;
}
void BuatAkar()
{
ROOT=P;
}
void PreOrder(Simpul *T)
{
if(T!=NULL)
{
printf("%c",T->INFO);
PreOrder(T->LEFT);
PreOrder(T->RIGHT);
}
}
void InOrder(Simpul *T)
{
if(T!=NULL)
{
InOrder(T->LEFT);
printf("%c",T->INFO);
InOrder(T->RIGHT);
}
}
void PostOrder(Simpul *T)
{
if(T!=NULL)
{
PostOrder(T->LEFT);
PostOrder(T->RIGHT);
printf("%c",T->INFO);
}
}
void main()
{
Inisialisasi();
BuatSimpul('A');BuatAkar();
BuatSimpul('B');ROOT->LEFT=P;
BuatSimpul('C');ROOT->RIGHT=P;
BuatSimpul('G');ROOT->RIGHT->RIGHT=P;
BuatSimpul('E');ROOT->LEFT->RIGHT=P;
BuatSimpul('F');ROOT->RIGHT->LEFT=P;
BuatSimpul('L');ROOT->RIGHT->LEFT->LEFT=P;
BuatSimpul('M');ROOT->RIGHT->LEFT->RIGHT=P;
printf("\n Preorder :");PreOrder(ROOT);
printf("\n Inorder :");InOrder(ROOT);
printf("\n Postorder :");PostOrder(ROOT);
}
0 komentar:
Posting Komentar