in scala/collection/immutable
class Tree

abstract class Tree[A,B](view: (A) => Ordered[A])
extends Object
with ScalaObject
Implementing classes or objects:
class TreeMap[A,B](view: (A) => Ordered[A])
class TreeSet[A](view: (A) => Ordered[A])

General Balanced Trees - highly efficient functional dictionaries. An efficient implementation of Prof. Arne Andersson's General Balanced Trees. These have no storage overhead compared to plain unbalanced binary trees, and their performance is in general better than AVL trees.

This implementation does not balance the trees after deletions. Since deletions don't increase the height of a tree, this should be OK in most applications. A balance method is provided for those cases where rebalancing is needed.

The tree consists of entries conatining a key with an order. Concrete implementations of the tree class has to suply the function entryKey that given an entry in the tree return its key.

When instanciating the tree an order for the keys has to be supplied.

Author:
Erik Stenman
Version:
1.0, 10/07/2003

Field Summary
abstract type This
     The type returned when creating a new tree.
protected type aNode
     The type of nodes that the tree is build from.
protected val tree: GBTree[A,B]
     The nodes in the tree.

Method Summary
protected abstract def New(sz: Int, t: GBTree[A,B]): This
     This abstract method should be defined by a concrete implementation C[T] as something like:
     override def New(sz:Int,t:aNode):This {
       new C[T](order) {
        override def size=sz;
        override protected val tree:aNode=t;
     }
    
The concrete implementation should also override the def of This override type This = C[T];
  def add(key: A, entry: B): This
     A new tree with the entry added is returned, assuming that key is not in the tree.
  def balance: This
     Create a new balanced tree from the tree.
protected def balance_list(list: List[B], s: Int): GBTree[A,B]
protected def balance_list_1(list: List[B], s: Int): Tuple2[GBTree[A,B],List[B]]
  def delete_any(key: A): This
     Removes the key from the tree.
  def entries: Iterator[B]
     Gives you an iterator over all elements in the tree.
abstract def entryKey(entry: B): A
     Returns the key of an entry.
  def findValue(key: A): Option[B]
     Check if this map maps key to a value and return the value if it exists.
  def is_defined(key: A): Boolean
     Is the given key mapped to a value by this map?
  def size: Int
     The size of the tree, returns 0 (zero) if the tree is empty.
  def update_or_add(key: A, entry: B): This
     A new tree with the entry added is returned, if key is not in the tree, otherwise the key is updated with the new entry.

Methods inherited from java/lang/Object-class
clone, eq, equals, finalize, getClass, hashCode, notify, notifyAll, synchronized, toString, wait, wait, wait

Methods inherited from scala/Any-class
!=, ==, asInstanceOf, isInstanceOf, match

Object Summary
case object GBNil

Class Summary
protected case class INode(t1: GBTree[A,B], height: Int, siz: Int)
protected case class ITree(t: GBTree[A,B])

Field Detail

This

  abstract type This <: Tree[A,B]
The type returned when creating a new tree. This type should be defined by concrete implementations e.g.
   class C[T](...) extends Tree[A,B](...) {
     type This = C[T];
   

aNode

  protected type aNode = GBTree[A,B]
The type of nodes that the tree is build from.

tree

  protected val tree: GBTree[A,B]
The nodes in the tree.
Method Detail

New

  protected abstract def New(sz: Int, t: GBTree[A,B]): This
This abstract method should be defined by a concrete implementation C[T] as something like:
     override def New(sz:Int,t:aNode):This {
       new C[T](order) {
        override def size=sz;
        override protected val tree:aNode=t;
     }
    
The concrete implementation should also override the def of This override type This = C[T];

size

  def size: Int
The size of the tree, returns 0 (zero) if the tree is empty.
Returns:
The number of nodes in the tree as an integer.

entryKey

  abstract def entryKey(entry: B): A
Returns the key of an entry. This method has to be defined by concrete implementations of the class.

is_defined

  def is_defined(key: A): Boolean
Is the given key mapped to a value by this map?
Parameters:
key - the key
Returns:
true, iff there is a mapping for key in this map

add

  def add(key: A, entry: B): This
A new tree with the entry added is returned, assuming that key is not in the tree.

update_or_add

  def update_or_add(key: A, entry: B): This
A new tree with the entry added is returned, if key is not in the tree, otherwise the key is updated with the new entry.

delete_any

  def delete_any(key: A): This
Removes the key from the tree.

findValue

  def findValue(key: A): Option[B]
Check if this map maps key to a value and return the value if it exists.
Parameters:
key - the key of the mapping of interest
Returns:
the value of the mapping, if it exists

entries

  def entries: Iterator[B]
Gives you an iterator over all elements in the tree. The iterator structure corresponds to the call stack of an in-order traversal. Note: The iterator itself has a state, i.e., it is not functional.

balance

  def balance: This
Create a new balanced tree from the tree. Might be useful to call after many deletions, since deletion does not rebalance the tree.

balance_list

  protected def balance_list(list: List[B], s: Int): GBTree[A,B]

balance_list_1

  protected def balance_list_1(list: List[B], s: Int): Tuple2[GBTree[A,B],List[B]]