avl_tree provides an implementation of am AVL Tree, a self-balancing binary search-tree.

The implementation is basically a port from the implementation in Java by Justin Wheterell.

This implementation provides two custom features usually not present in AVL trees:

  1. The methods add, remove, or contains not only accept a value to be added, removed, or tested, but optionally also a compare function to be used in this very invocation only. This comes in handy, if a more efficient compare function can be used in a specific invocation. Example: the dynamically changing search tree of intersecting line segments in the Bentley-Ottman-Algorithm.

  2. The tree can (optionally) store multiple values which are equal with respect to the tree ordering, but not identical with respect to Darts identical() function. One application is again the implementation of the Y-structure in the Bentley-Ottman-Algorithm, where multiple overlapping line segments can be handled as equivalence class of line segments stored in one tree node.


A balanced tree of ints

  // create a tree, and use some methods, use the standard
  // int.compareTo function for ordering
  var tree = new AvlTree<int>();
  print(tree.inorder.toList());  // -> [0,1,2]
  print(tree.inorder.toList());  // -> [0,1]
  print(tree.contains(0));       // true

Reverse lexicographic order

Creates balanced tree of strings, ordered in reverse lexicographic order.

 // a balanced tree of strings, ordered in reverse lexicographical
 // order
 var order = (String s1, String s2) => s2.compareTo(s1);
 var tree = new AvlTree<String>(compare: order);
 tree.addAll(["aaa", "zzz"]);
 print(tree.inorder.toList);     // ["zzz", "aaa"]

A tree of strings with equivalence classes

Creates a tree of strings. The order is lexicographic with respect to the lowercase version of the strings. Strings which are equal modulo case are in the same equivalence class.

 lowerCaseCompare(s,t) => s.toLowerCase().compareTo(t.toLowerCase());
 var tree = new AvlTree<String>(compare:lowerCaseCompare,
     withEquivalenceClasses: true);
 tree.addAll(["aaa", "zzz", "AAA"]);
 print(tree.smallest);         // -> ["aaa", "AAA"]

API documentation

See output from dartdoc

Depend on it

avl_tree is available from pub.dartlang.org. Add

  avl_tree: 0.1.0

to your pubspec.yaml.

See version history.


Build Status


avl_tree is licensed under the Apache 2.0 license, see files LICENSE and NOTICE.



avl_tree provides an implementation of an AVL Tree, a self-balancing binary search-tree.