private:
void Init(Int_t i) void RootIsEmpty() void RootIsFull() protected:
void DecrNofKeys() Int_t IdxAdd(const TObject& obj) void IncrNofKeys() public:
TBtree TBtree(Int_t ordern = 3) virtual void ~TBtree() virtual void Add(TObject* obj) virtual void AddAfter(TObject*, TObject* obj) virtual void AddAt(TObject* obj, Int_t) virtual void AddBefore(TObject*, TObject* obj) virtual void AddFirst(TObject* obj) virtual void AddLast(TObject* obj) virtual TObject* After(TObject* obj) const virtual TObject* At(Int_t idx) const virtual TObject* Before(TObject* obj) const static TClass* Class() virtual void Clear(Option_t* option) virtual void Delete(Option_t* option) virtual TObject* FindObject(const char* name) const virtual TObject* FindObject(const TObject* obj) const virtual TObject* First() const virtual TClass* IsA() const virtual TObject* Last() const virtual TIterator* MakeIterator(Bool_t dir = kIterForward) const TObject* operator[](Int_t i) const Int_t Order() Int_t Rank(const TObject* obj) const virtual TObject* Remove(TObject* obj) virtual void ShowMembers(TMemberInspector& insp, char* parent) virtual void Streamer(TBuffer& b) void StreamerNVirtual(TBuffer& b)Data Members
private:
TBtNode* fRoot root node of btree Int_t fOrder the order of the tree (should be > 2) Int_t fOrder2 order*2+1 (assumes a memory access is Int_t fInnerLowWaterMark inner node low water mark Int_t fLeafLowWaterMark leaf low water mark Int_t fInnerMaxIndex maximum inner node index Int_t fLeafMaxIndex maximum leaf index
TBtree B-tree class. TBtree inherits from the TSeqCollection ABC. /*
The following are modifications of Knuth's properties on page 478:
The values of InnerLowWaterMark and LeafLowWaterMark may actually be set by the user when the tree is initialized, but currently they are set automatically to:
InnerLowWaterMark = ceiling(Order/2) LeafLowWaterMark = Order - 1
If the tree is only filled, then all the nodes will be at least 2/3 full. They will almost all be exactly 2/3 full if the elements are added to the tree in order (either increasing or decreasing). [Knuth says McCreight's experiments showed almost 100% memory utilization. I don't see how that can be given the algorithms that Knuth gives. McCreight must have used a different scheme for balancing. [No, he used a different scheme for splitting: he did a two-way split instead of the three way split as we do here. Which means that McCreight does better on insertion of ordered data, but we should do better on insertion of random data.]]
It must also be noted that B-trees were designed for DISK access algorithms, not necessarily in-memory sorting, as we intend it to be used here. However, if the order is kept small (< 6?) any inefficiency is negligible for in-memory sorting. Knuth points out that balanced trees are actually preferable for memory sorting. I'm not sure that I believe this, but it's interesting. Also, deleting elements from balanced binary trees, being beyond the scope of Knuth's book (p. 465), is beyond my scope. B-trees are good enough.
A B-tree is declared to be of a certain ORDER (3 by default). This number determines the number of keys contained in any interior node of the tree. Each interior node will contain ORDER keys, and therefore ORDER+1 pointers to sub-trees. The keys are numbered and indexed 1 to ORDER while the pointers are numbered and indexed 0 to ORDER. The 0th ptr points to the sub-tree of all elements that are less than key[1]. Ptr[1] points to the sub-tree that contains all the elements greater than key[1] and less than key[2]. etc. The array of pointers and keys is allocated as ORDER+1 pairs of keys and nodes, meaning that one key field (key[0]) is not used and therefore wasted. Given that the number of interior nodes is small, that this waste allows fewer cases of special code, and that it is useful in certain of the methods, it was felt to be a worthwhile waste.
The size of the exterior nodes (leaf nodes) does not need to be related to the size of the interior nodes at all. Since leaf nodes contain only keys, they may be as large or small as we like independent of the size of the interior nodes. For no particular reason other than it seems like a good idea, we will allocate 2*(ORDER+1) keys in each leaf node, and they will be numbered and indexed from 0 to 2*ORDER+1. It does have the advantage of keeping the size of the leaf and interior arrays the same, so that if we find allocation and de-allocation of these arrays expensive, we can modify their allocation to use a garbage ring, or something.
Both of these numbers will be run-time constants associated with each tree (each tree at run-time can be of a different order). The variable "order" is the order of the tree, and the inclusive upper limit on the indices of the keys in the interior nodes. The variable "order2" is the inclusive upper limit on the indices of the leaf nodes, and is designed
(1) to keep the sizes of the two kinds of nodes the same; (2) to keep the expressions involving the arrays of keys looking somewhat the same: lower limit upper limit for inner nodes: 1 order for leaf nodes: 0 order2 Remember that index 0 of the inner nodes is special.
Currently, order2 = 2*(order+1).
Picture: (also see Knuth Vol 3 pg 478) +--+--+--+--+--+--... | | | | | | parent--->| | | | | | | | +*-+*-+*-+--+--+--... | | | +----+ | +-----+ | +-----+ | V | V +----------+ | +----------+ | | | | | this->| | | | |<--sib +----------+ | +----------+ V data
It is conceptually VERY convenient to think of the data as being the very first element of the sib node. Any primitive that tells sib to perform some action on n nodes should include this 'hidden' element. For InnerNodes, the hidden element has (physical) index 0 in the array, and in LeafNodes, the hidden element has (virtual) index -1 in the array. Therefore, there are two 'size' primitives for nodes:
Psize - the physical size: how many elements are contained in the array in the node. Vsize - the 'virtual' size; if the node is pointed to by element 0 of the parent node, then Vsize == Psize; otherwise the element in the parent item that points to this node 'belongs' to this node, and Vsize == Psize+1;
Parent nodes are always InnerNodes.
These are the primitive operations on Nodes:
Append(elt) - adds an element to the end of the array of elements in a node. It must never be called where appending the element would fill the node. Split() - divide a node in two, and create two new nodes. SplitWith(sib) - create a third node between this node and the sib node, divvying up the elements of their arrays. PushLeft(n) - move n elements into the left sibling PushRight(n) - move n elements into the right sibling BalanceWithRight() - even up the number of elements in the two nodes. BalanceWithLeft() - ditto
To allow this implementation of btrees to also be an implementation of sorted arrays/lists, the overhead is included to allow O(log n) access of elements by their rank (`give me the 5th largest element'). Therefore, each Item keeps track of the number of keys in and below it in the tree (remember, each item's tree is all keys to the RIGHT of the item's own key).
[ [ < 0 1 2 3 > 4 < 5 6 7 > 8 < 9 10 11 12 > ] 13 [ < 14 15 16 > 17 < 18 19 20 > ] ] 4 1 1 1 1 4 1 1 1 5 1 1 1 1 7 3 1 1 1 4 1 1 1
*/
Create a B-tree of certain order (by default 3).
Delete B-tree. Objects are not deleted unless the TBtree is the owner (set via SetOwner()).
Add object to B-tree.
Cannot use this method since B-tree decides order.
May not use this method since B-tree decides order.
Remove all objects from B-tree. Does NOT delete objects unless the TBtree is the owner (set via SetOwner()).
Remove all objects from B-tree AND delete all heap based objects.
Find object using its name (see object's GetName()). Requires sequential search of complete tree till object is found.
Find object using the objects Compare() member function.
Add object and return its index in the tree.
Initialize a B-tree.
Returns a B-tree iterator.
Returns the rank of the object in the tree.
Remove an object from the tree.
The root of the tree is full. Create an InnerNode that points to it, and then inform the InnerNode that it is full.
If root is empty clean up its space.
Stream all objects in the btree to or from the I/O buffer.
void IncrNofKeys() void DecrNofKeys() void AddFirst(TObject* obj) void AddLast(TObject* obj) void AddAt(TObject* obj, Int_t) void AddAfter(TObject*, TObject* obj) void AddBefore(TObject*, TObject* obj) TObject* At(Int_t idx) const TObject* First() const TObject* Last() const Int_t Order() TObject* operator[](Int_t i) const TClass* Class() TClass* IsA() const void ShowMembers(TMemberInspector& insp, char* parent) void StreamerNVirtual(TBuffer& b)