Anasayfa » C++ » AVL Veri Yapısı

AVL Veri Yapısı

7 Nisan 2010  |  Yazar: coders  |  2 Yorum  |  99 kez okundu
Facebook'da Paylaş Twitter'da Paylas FriendFeed'de Paylaş AVL Veri Yapısı

Bu konumuzda AVL veri yapısına değineceğiz.Anlaşılırlık bakımından kısa açıklma yapalım.
Genellikle öğrencilere ödev olarak verilen kodlama bakımından oldukça zor
bir ödevdir.Bu dönem benim de ihtiyacım olmuş,zamanım olmadığı için netten araştırmıştım.
Ama gördüm ki nette genellikle bu veri yapısının Java kodu bulunmakta.İstedim ki bir ilk olarak
bizim sitemizde yer alsın yazmış olduğum AVL veri yapısını sizlere sunuyorum.Kodları vermeden önce
konuya ufaktan değinmeye yarar olduğunu umuyorum.

Avl veri yapısı ilk olarak 1962 yılında iki rus matematikçi G. M. Adelson-Velskil ve E. M. Landis
tarafından yaratılmıştır.Daha sonra bu veri yapısısimlerinin baş harfi olan AVL olarak adlandırılmıştır.
Bildiğimiz gibi ikili ağaç yapılarında öyle durumlar vardırki ya sol ağacınız alabildiğine uzun ya da
sağ ağacınız alabildiğine uzundur.Bu tür istisnai durumları engellemek için AVL veri yapısı yaratılmış
olup her ekleme , silem operasyonundan sonra balance (denkleştirme) yapılmalıdır.Şimdi sırası ile
önce bu kodun header.h ‘ ını verelim ve daha sonra avl.c kodunu yazalım;

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

typedef struct tree_nodes{                                                              //Create the sutructure of data

        int number;

        int balance;

        struct tree_nodes *leftSub;                                                     //The variable of the node 

        struct tree_nodes *rightSub;

}tree_nodes;

typedef struct{                                                                                 //Create the head node that holds the root "root" node adress and "counter" count value of nodes

        int counter;

        struct tree_nodes *root;

}tree_Root;

=== Define the functions ===

int menu_select(void);                                                                  //The option of the menu selection

tree_Root* create_tree();                                                               //Creates the head that holds the adress of root node

tree_nodes* insert_to_AVL(tree_Root *pRoot,tree_nodes *pWalk,tree_nodes *pNew,int taller);//Inserts the nodes to the tree 

tree_nodes* allocate_memory(void);                                              //Allocates the neccessary memory space

void display_all(tree_nodes *pDisp);                                    //Display the whole elements

int delete_AVL(tree_Root *pTree,int delete_value);              //Delete one node from tree 

void destroy(tree_nodes *pDestroy);                                             //Destroy the whole list

Şimdi kullanıcıdan gerekli verileri alıp veri yapısını oluşturduğumuz main kodunu verelim;

/***********************************************************************************
* Created by: coders.gen.tr
* Written : Coders
*
* THIS PROGRAM CREATES A TREE STRUCTURE AND STORES NUMBERS
* THEN DOES THE GIVEN FUNCTIONS
*
************************************************************************************/
#include"header.h"

//== MAIN FUNCTION ==
void main (void)
{ //Initialize the variables
int choise = 0,enter = 0,taller =0; //selection "choise" ,given number "enter",level value "taller"
int delete_value = 0; //deletekey "delete_value"
tree_Root *pRoot; //pRoot: head node pointer
tree_nodes *pNew; //poNew: new node pointer

//Print the aim of program
printf("____________________________________________________________n");
printf("nTHIS PROGRAM CREATES AN AVL TREE STRUCTURE AND STORES NUMBERSnTHEN DOES THE GIVEN FUNCTIONSn");
printf("____________________________________________________________");

pRoot = create_tree(); //Create the head node and initialize

for(;;) //Infinite loop for the menu
{
choise= menu_select(); //Give the main menu until the enetered value is 4
switch(choise) //Different call of fumction according to selection
{
case 1: //Insert data to tree
{
printf("n____ IN ORDER TO FINISH ADDING ENTER -1 ____n");

//Continue to adding until given value is -1
for(;;)
{
printf("Enter your number: ");
scanf("%d",&enter);

if(enter == -1)
break;

pNew = allocate_memory();
pNew ->number = enter;

//Insertion function
pRoot ->root = insert_to_AVL(pRoot,pRoot ->root,pNew,taller);

}
} break;
case 2: //Display the whole elements
{
if(pRoot ->counter == 0)

printf("nt!!!NOTHING entered to display!!!");
else
{
printf("n*** THE LIST CONTAINS:t");
display_all(pRoot ->root);
}
} break;
case 3: //Delete the selected value
{
fflush(stdin);

if(pRoot ->counter == 0)

printf("nt!!!The list is already empty!!!");
else
{
printf("nEnter your value to delete: ");
scanf("%d",&delete_value);

delete_AVL(pRoot,delete_value);
}

} break;
case 4: //Destroy the whole tree
{
destroy(pRoot ->root);
printf("n****** The whole data DELETED *******");
free(pRoot);
pRoot = create_tree();
}
break;
case 5:
{
free(pRoot);
exit(0);
}
}
printf("n");
system("pause");
system("cls");
}
}

//==Create the head node space and return the address===
tree_Root* create_tree()
{
tree_Root *New_head; //Define the pointer name and type

New_head = (tree_Root*)malloc(sizeof(tree_Root)); //Allocate the neccessary memory space

if(!New_head){ //If memory not avaliable caution exit
printf("Out of memoryn");
exit(0);
}

New_head ->root = '

Kullanım bakımından kodun sonuna küçük bir kullanım manualı ekledim.Umarım AVL C kodu arayanlar en iyi şekilde faydalanırlar.Herkese iyi çalışmalar.

Saygılarımla.

Facebok'ta Paylaş

Benzer Yazılar

Etiketler: , , , , , , , , , , , , , , , ,
avatar

Ahmet Ates

http://www.coders.gen.tr/ 25 yasindayim. Z.K.U Biyomedikal Cihaz Teknolojisi bölümünü okudum, Programlama dilleri arasindan ilgilendiğim ve profesyonel olarak hizmet verdiğim dil Fortran'dir. Web olarak Php,Css ve hazır sistemler olarak Wordpress ve Vbulletine hayranlık besliyorum.

2 Yorum »

  • malik diyor ki:

    söylediğin şeyd çok haklısınız ilk kez bi avl kodu buldum ama dev c ile hataları düzeltemedim. Maalsef de pek bilgim olmadığı için de pek değiştirme şansım olmadı. yardımcı olabilir misiniz acaba
    hatalardan bazıları söyle
    header.h: No such file or directory.
    `rotate_right' cannot be used as a function vs .
    benim düşüncem avl.h dosyasını eklerken mi hata yaptık acaba :S

    • coders diyor ki:

      Kodlarda herhangi bir hata yok, sorun avl.h dosyasında değil, sitedeki simple tag eklentisinden, bazi kelimelerde etiketler linklenmiş, ve o yuzdem hata olusuyordu kaldirdim bu stabil hali ile deneyebilirsin kolay gelsin..

Bu yazı hakkında birşeyler demek ister misiniz?

RSS üzerinden bu yazıya yapılan yorumları takip edin.

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir.

Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Programlama