Thursday 1 May 2008

AVL TREE

praktikum struktur data hari Jumat tanggal, 2 Mei 208. Di lab kita belajar buat avl tree. buset si Kiki kebut amat ngetiknya, dia udah selesai. hasil codingnya aku posting di sini. ini dia......


#include
#include

typedef struct tree
{
int nilai;
int status;
struct tree *left;
struct tree *right;
} AVLTree;

void preorder (AVLTree **root)
{
if ((*root)==NULL);
else
{
printf ("%d %d\n", (*root)->nilai, (*root)->status);
preorder (&(*root)->left);
preorder (&(*root)->right);
}
}

void insert (AVLTree **root, int value)
{
AVLTree *temp;
int cek=100;
temp=(AVLTree*)malloc(sizeof(AVLTree));
temp->status=0;
temp->right=NULL;
temp->left=NULL;
if ((*root)==NULL)
{
*root=temp;
(*root)->nilai=value;
}
else
{
if (value<(*root)->nilai)
{
if ((*root)->left!=NULL)
{
cek=((*root)->left)->status;
}
else
{
(*root)->status -=1;
}
insert (&(*root)->left, value);
if ((((*root)->left)->status!=cek)&&(cek!=100)&&(((*root)->left)->status!=0))
{
(*root)->status -=1;
}
}
else
{
if ((*root)->right!=NULL)
{
cek=((*root)->right)->status;
}
else
{
(*root)->status +=1;
}
insert(&(*root)->right, value);
if ((((*root)->right)->status!=cek)&&(cek!=100)&&(((*root)->right)->status!=0))
{
(*root)->status +=1;
}
}
}
}
int main ()
{
AVLTree *root=NULL;
insert (&root,12);
insert (&root,4);
insert (&root,2);
insert (&root,8);
insert (&root,14);
insert (&root,13);
insert (&root,6);
insert (&root,10);
insert (&root,16);
preorder(&root);
getch();
return 0;
}

No comments:

Windu Purnomo

by saying the name of alloh the lord who created

JAVAku

JAVAku

Mari Sholat di Awal Waktu