Click here to Skip to main content
15,889,808 members
Articles / Desktop Programming / MFC
Article

Addison-Velski and Landis (AVL) Binary Trees

Rate me:
Please Sign up or sign in to vote.
4.43/5 (6 votes)
22 Feb 2000 78.6K   1.7K   31   4
Describes an implementation of AVL Trees.
  • Download demo project - 54 Kb
  • I have implemented the binary trees of Addison-Velski and Landis (AVL-Trees), which allow standard operations like insert, search and delete in logarithmical time. Ordinary binary trees can become very obscure. They can become a linear structure and the basic operations take linear and not logarithmical time. The advantage of the AVL-Trees is a strategy of restructuring the tree after insert and delete operations. The restructuring operation itself only takes logarithmical time. So the trees are highly efficient binary trees to hold a large number of items. I use such trees to sort data. The sort of data using AVL-Trees takes n*log(n) time, so you can sort a lot of data in optimal time. I have sorted with that technology hundreds of thousands of items within a quarter of an hour. It is very easy to eliminate duplicates, duplicates will not be inserted in the tree. What other algorithm works so efficiently? And the greatest advantage is that the code is absolutely easy to use, 10 lines are enough to sort a file. The Trees are implemented as templates, so you can use everything you want as tree items. Items only must be comparable. The code is self-documentating. The classes are CAVLNode<class T>, CAVLTree<class T> and CAVLTreeIterator<class T>.

    Here is an example to sort a file (file should not have identical lines, otherwise the duplicates are eliminated):

    // sample code
    #include <fstream.h>
    #include "AVLBaum.h"
    
    void SortFile(const char* ifname, const char* ofname)
    {
    	ifstream is(ifname);
    	ofstream os(ofname);
    	char buffer[255];
    	CAVLTree<CString> tree;
    	while (is.getline(buffer, sizeof(buffer)-1)
    		tree.Insert(new CString(buffer));
    	CAVLTreeIterator<CString> iter(tree);
    	while (iter)
    	{
    		os << *(iter.GetData()) << endl;
    		iter++;
    	}
    }
    
    // end of sample code

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer
    Germany Germany
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionAVL Trees Pin
    Ben McNamara13-Sep-15 17:45
    Ben McNamara13-Sep-15 17:45 
    QuestionHELP! Pin
    nguyenspk3-May-12 23:48
    nguyenspk3-May-12 23:48 
    GeneralAdelson-Velskii, not "Addison-Velski" Pin
    Vassili Postnikov20-Sep-00 20:41
    sussVassili Postnikov20-Sep-00 20:41 
    GeneralExcellent stuff. Pin
    Nick Fenwick22-Aug-00 2:00
    sussNick Fenwick22-Aug-00 2:00 
    I'm very pleased to find this on the web, it's all I've found in a few hours of (idly) searching the web for a tree implementation using c++ templates. Maybe I just don't know where to look.

    A brief discussion of other technologies that achieve the same aim would be appreciated, to put this work into context. e.g. why there is no tree representation in the STL, and what compilers and technologies these classes would be compatible with (i.e. Microsoft-only traits! - see use of LPTSTR and UINT in the code, although I see you've wrapped references to CDC in #ifdef...#endif pairs to make them invisible to non-MS compilers).

    The names were slightly confusing, calling the node CAVLNode and the tree CAVLTree. By the conventions to which you appear to be attempting to adhere, only classes derived from CObject should be prefixed with 'C'. Basically I'm griping from a GNU perspective - lots of people will want to use this code outside the Microsoft domain and will have to tweak it to work, why not design from a free perspective and allow the users to constrain themselves to a particular platform and SDK later?

    I must say I disagree with the way you structured your project, although these comments are largely cosmetic. You have hardcoded the location of the mfc32[d].lib files, and have both mfc42.lib and mfc42d.lib as File nodes in the Project File View. Surely the lib files should be mentioned in the "Project Settings->Link->Object/Library modules" field, mfc42.lib for Release builds and mfc42d.lib for Debug builds? Then the default library search path will be used and the lib files found easily instead of having to hardcode their location.

    Other than that, this looks very promising. I hope it proves useful in the project I have in mind.

    Cheers, Neek :

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.