Thursday, 10 April 2014
Program tree preorder inorder postorder Bahasa C
Program Tree tentang preorder postorder dan inorder.
tampilannya :
jgn lupa tinggalkan kometar yah. add FB :
https://www.facebook.com/lutfi.brown
berikut ini codingnya :
----------------------------------------------------------------------------------------
#include <stdio.h>
#include <malloc.h>
struct node {
struct node *left;
char info;
struct node *right;
};
typedef struct node NODE;
typedef NODE P;
NODE *NodeBaru(char item) {
NODE*n;
n = (NODE*) malloc(sizeof(NODE));
if(n != NULL) {
n->info = item;
n->left = NULL;
n->right = NULL;
}
return n;
}
void crP(P **T) {
*T = NULL;
}
typedef enum { FALSE = 0, TRUE = 1} BOOL;
BOOL PK(P *T) {
return((BOOL)(T == NULL));
}
void TambahNode(NODE **p, char item) {
NODE *n;
n = NodeBaru(item);
*p = n;
}
void preOrder(P *T) {
if(!PK(T)) {
printf("%c ", T->info);
preOrder(T->left);
preOrder(T->right);
}
}
void inOrder(P *T) {
if(!PK(T)) {
inOrder(T->left);
printf("%c ", T->info);
inOrder(T->right);
}
}
void postOrder(P *T) {
if(!PK(T)) {
postOrder(T->left);
postOrder(T->right);
printf("%c ", T->info);
}
}
int main()
{
P *a;
char b;
crP(&a);
TambahNode(&a, b = 'a');
TambahNode(&a->left, b = 'b');
TambahNode(&a->left->right, b = 'd');
TambahNode(&a->right, b = 'e');
TambahNode(&a->right->right, b = 'f');
TambahNode(&a->right->right->left, b = 'c');
printf("Tampilan secara PreOrder: ");
preOrder(a);
printf("\nTampilan secara InOrder: ");
inOrder(a);
printf("\nTampilan secara PostOrder: ");
postOrder(a);
printf("\n\n");
getch();
}
---------------------------------------------THANK YOU------------------------------
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment