Skip to content

sabirove/avlset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status License Maven Central

This is an AVL tree based java.util.NavigableSet implementation.

The AVL tree is a self-balancing binary search tree named after its two Soviet inventors, Georgy Adelson-Velsky (on the right) and Evgenii Landis (on the left), who published it in their 1962 paper "An algorithm for the organization of information". It was the first such data structure to be invented.

In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.

AVL trees are often compared with red–black trees because both support the same set of operations and take O(log n) time for the basic operations. For lookup-intensive applications, AVL trees are faster than red–black trees because they are more strictly balanced. Similar to red–black trees, AVL trees are height-balanced.

source: https://en.wikipedia.org/wiki/AVL_tree

Compared with the reference TreeSet implementation

Memory

java.util.TreeSet is a red-black tree based NavigableSet implementation that delegates to the underlying TreeMap by using it's EntrySet as the actual set. Map values are filled with dummy object references which yields an overhead of extra 8 bytes for each node.

TreeMap.Entry

  • compressed pointers: 40 bytes
  • regular pointers: 64 bytes

AvlSet.Node

  • with compressed pointers: 32 bytes
  • no compressed pointers: 56 bytes
Performance

Here's a quick benchmark covering the basic set of operations (add/contains/remove) executed against the very same dataset of 1 million random integers:

Roughly AvlSet is 2 - 7% faster across the board which is negligible.

Benchmark parameters:

TODO
  • "subset view" family of APIs (descendingSet, subSet, tailSet, headSet) is not implemented for now
  • no ConcurrentModificationException safeguard implemented for now

About

AVL tree based java.util.NavigableSet implementation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages