AVLTree - template version | CodeGuru

AVLTree – template version

Download Source and Demo project: avl.zip I have implemented the binary tree’s of Addison-Velski and Landis (AVL-Tree’s), which allow Standard-operation 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 17, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Download Source and Demo project: avl.zip

I have implemented the binary tree’s of Addison-Velski and Landis (AVL-Tree’s), which
allow Standard-operation 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-Tree’s is a
strategy of restructurate the tree after Insert- and Delete-operations. The
restructuration itself only takes logarithmical time. So the trees are high-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 hundrets 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 efficient? 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 item. 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

Last updated 16 Sep 1998

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.