tugas struktur data terakhir_pohonbiner

5
Program pohon Biner (Tree) Bosca simbolon 1282024 Sistem informasi #include <stdio.h> #include <malloc.h> #define nil NULL //stuct nod struct nod { struct nod *left; char data; struct nod *right; }; typedef struct nod NOD; typedef NOD POKOK; NOD *NodBaru(char item) { NOD *n; n = (NOD*) malloc(sizeof(NOD)); if(n != NULL) { n->data = item; n->left = NULL; n->right = NULL; }

Upload: boscasimbolon-boscasimbolon

Post on 31-Jul-2015

102 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Tugas struktur data terakhir_pohonBiner

Program pohon Biner (Tree)

Bosca simbolon

1282024

Sistem informasi

#include <stdio.h>

#include <malloc.h>

#define nil NULL

//stuct nod

struct nod

{

struct nod *left;

char data;

struct nod *right;

};

typedef struct nod NOD;

typedef NOD POKOK;

NOD *NodBaru(char item)

{

NOD *n;

n = (NOD*) malloc(sizeof(NOD));

if(n != NULL)

{

n->data = item;

n->left = NULL;

n->right = NULL;

}

Page 2: Tugas struktur data terakhir_pohonBiner

return n;

}

void BinaPokok(POKOK **T)

{

*T = NULL;

}

typedef enum { FALSE = 0, TRUE = 1} BOOL;

BOOL PokokKosong(POKOK *T)

{

return((BOOL)(T == NULL));

}

void TambahNod(NOD **p, char item)

{

NOD *n;

n = NodBaru(item);

*p = n;

}

void preorder(POKOK *T)

{

if(!PokokKosong(T))

{

printf("%c ", T->data);

preorder(T->left);

preorder(T->right);

}

}

void inorder(POKOK *T)

{

if(!PokokKosong(T))

{

Page 3: Tugas struktur data terakhir_pohonBiner

inorder(T->left);

printf("%c ", T->data);

inorder(T->right);

}

}

void postorder(POKOK *T)

{

if(!PokokKosong(T))

{

postorder(T->left);

postorder(T->right);

printf("%c ", T->data);

}

}

int main()

{

POKOK *pohon;

char buah;

BinaPokok(&pohon);

TambahNod(&pohon, buah = 'B');

TambahNod(&pohon->left, buah = 'O');

TambahNod(&pohon->left->right, buah = 'S');

TambahNod(&pohon->right, buah = 'K');

TambahNod(&pohon->right->right, buah = 'A');

TambahNod(&pohon->right->right->left, buah = 'S');

TambahNod(&pohon->right->right->right, buah = 'I');

TambahNod(&pohon->right->right->right->left, buah = 'M');

TambahNod(&pohon->right->right->right->left->right, buah = 'B');

TambahNod(&pohon->right->right->right->left->right->right, buah = 'O');

TambahNod(&pohon->right->right->right->left->right->right->right, buah = 'L');

Page 4: Tugas struktur data terakhir_pohonBiner

TambahNod(&pohon->right->right->right->left->right->right->right->right, buah = 'O');

TambahNod(&pohon->right->right->right->left->right->right->right->right->right, buah = 'N');

printf("Tampilan secara Preorder: ");

preorder(pohon);

printf("\nTampilan secara Inorder: ");

inorder(pohon);

printf("\nTampilan secara Postorder: ");

postorder(pohon);

printf("\n\n");

return 0;

}

Screenshoot Running

Page 5: Tugas struktur data terakhir_pohonBiner

Preorder : B O S K A S I M B O L O N

O

S

O

B

I

A

K

L

B

M

S

N