Click here to Skip to main content
11,414,670 members (70,544 online)
Click here to Skip to main content

AVL Binary Tree for C++

, 4 Jan 2006
Rate this:
Please Sign up or sign in to vote.
Using the HeightBalancedTree C++ template as an array or as a sorted sequence.

Introduction

Very often, I had wanted a container that provides fast insertion and fast indexing. I also often had the need for sorted containers. Here, I present an AVL binary tree implementation that serves all these needs.

Background

Using the code

You can use the HeightBalancedTree<> template as either a sequence container that supports fast arbitrary insertions and deletions of objects, or as a sorted container.

If you use the tree as a fast sequence container, use the methods InsertByIndex, FindByIndex, and RemoveByIndex.

If you use the tree as a sorted container, use the methods InsertComparable, FindComparable, and RemoveComparable. These methods use the user-provided Comparator type to compare the objects stored inside the container.

You can always use the overloaded operator[] that works just like FindByIndex. It throws a std::out_of_range exception if the index is invalid.

All by-index modification and access methods of the container take approximately O(log(n)) time.

All by-comparison modification and access methods of the container take approximately O(k * log(n)) time, where k is the time taken to compare any two objects.

Here's some sample code that creates and modifies a random-access array of size_ts:

typedef HeightBalancedTree<size_t> SizeTTree;
SizeTTree tree;
for (size_t i = 0; i < n; ++i)
{
    tree.InsertByIndex(indices[i], data[i]);
}
for (size_t i = 0; i < n; ++i)
{
    tree.RemoveByIndex(0);
}

Here's some sample code that creates and modifies a sorted sequence of strings:

#ifdef UNICODE
    std::wostream & tcout = wcout;
    typedef HeightBalancedTree<
        std::basic_string<wchar_t>,
        StringComparator<
            std::basic_string<wchar_t>
        >
    > TStringTree;
#else
    std::ostream & tcout = cout;
    typedef HeightBalancedTree<
        std::string,
        StringComparator<std::string>
    > TStringTree;
#endif

TStringTree tree;
tree.InsertComparable(TEXT("ABE"));
tree.InsertComparable(TEXT("aBdE"));
tree.InsertComparable(TEXT("AbCd"));
tree.InsertComparable(TEXT("aBc"));
tree.InsertComparable(TEXT("AbD"));
tree.InsertComparable(TEXT("aBe"));
for (size_t i = 0; i < tree.Size(); ++i)
{
    tcout << "tree[" << i << "] = 
                  " << tree[i] << endl;
}

Points of Interest

Tell me what you think of this. I'm open to suggestions.

History

  • 2005, December, 14 - 1.0.1 Fixed a bug in FindComparable: the returned index was wrong.
  • 2005, December, 13 - Initial version uploaded to CodeProject.

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

Share

About the Author

Dimiter Georgiev

Bulgaria Bulgaria
No Biography provided

Comments and Discussions

 
GeneralYou have my 5 Pin
Pablo Yabo14-Oct-10 5:41
memberPablo Yabo14-Oct-10 5:41 
GeneralCouple of observations... Pin
yafan5-Jan-06 5:30
memberyafan5-Jan-06 5:30 
GeneralRe: Couple of observations... Pin
Dimiter Georgiev5-Jan-06 6:42
memberDimiter Georgiev5-Jan-06 6:42 
GeneralAnother Wish List Item Pin
scott035629-Dec-05 2:38
memberscott035629-Dec-05 2:38 
GeneralAnother Wish List Item Pin
scott035620-Dec-05 9:46
memberscott035620-Dec-05 9:46 
GeneralRe: Another Wish List Item Pin
Dimiter Georgiev21-Dec-05 2:35
memberDimiter Georgiev21-Dec-05 2:35 
GeneralRe: Another Wish List Item Pin
Dimiter Georgiev21-Dec-05 2:36
memberDimiter Georgiev21-Dec-05 2:36 
GeneralRe: Another Wish List Item Pin
Dimiter Georgiev21-Dec-05 12:41
memberDimiter Georgiev21-Dec-05 12:41 
GeneralLatest Version Pin
Dimiter Georgiev15-Dec-05 5:25
memberDimiter Georgiev15-Dec-05 5:25 
GeneralWish List... Pin
JimD.999914-Dec-05 7:14
memberJimD.999914-Dec-05 7:14 
GeneralRe: Wish List... Pin
Dimiter Georgiev14-Dec-05 7:20
memberDimiter Georgiev14-Dec-05 7:20 
GeneralRe: Wish List... Pin
Dimiter Georgiev14-Dec-05 7:49
memberDimiter Georgiev14-Dec-05 7:49 
GeneralRe: Wish List... Pin
Dimiter Georgiev14-Dec-05 7:54
memberDimiter Georgiev14-Dec-05 7:54 
GeneralRe: Wish List... Pin
JimD.999914-Dec-05 8:04
memberJimD.999914-Dec-05 8:04 
GeneralRe: Wish List... Pin
Dimiter Georgiev14-Dec-05 8:29
memberDimiter Georgiev14-Dec-05 8:29 
GeneralRe: Wish List... Pin
Dimiter Georgiev18-Dec-05 12:35
memberDimiter Georgiev18-Dec-05 12:35 
GeneralLazy Delete Method Pin
Michael Holm14-Dec-05 2:59
memberMichael Holm14-Dec-05 2:59 
GeneralRe: Lazy Delete Method Pin
Dimiter Georgiev14-Dec-05 4:01
memberDimiter Georgiev14-Dec-05 4:01 
Questionwhat about STL's map/set ? Pin
Iftahh13-Dec-05 14:41
memberIftahh13-Dec-05 14:41 
AnswerRe: what about STL's map/set ? Pin
<span style='color:#00aa00'>emilio_grv</span>13-Dec-05 22:19
memberemilio_grv13-Dec-05 22:19 
GeneralRe: what about STL's map/set ? Pin
Gast12814-Dec-05 6:33
memberGast12814-Dec-05 6:33 
GeneralRe: what about STL's map/set ? Pin
JimD.999914-Dec-05 7:09
memberJimD.999914-Dec-05 7:09 
GeneralRe: what about STL's map/set ? Pin
Gast12814-Dec-05 7:29
memberGast12814-Dec-05 7:29 
GeneralRe: what about STL's map/set ? Pin
JimD.999914-Dec-05 7:49
memberJimD.999914-Dec-05 7:49 
GeneralRe: what about STL's map/set ? Pin
Gast12814-Dec-05 8:11
memberGast12814-Dec-05 8:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150427.4 | Last Updated 4 Jan 2006
Article Copyright 2005 by Dimiter Georgiev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid