I\'m currently implementing an algorithm in which I need to know how many numbers, from the ones that have already been read, are smaller than the one that\'s currently being pr
You could store the number of leaves connected to the left (less-than) side in every node, and increment this count for every branch you pass on the left when inserting a new leaf.
The number of values smaller than the newly inserted value can be calculated at the same time, by adding all the counts of the branches you pass on the right while inserting.
Below is a JavaScript code snippet to demonstrate the method:
function CountTree() { // tree constructor
this.root = null;
this.insert = function(value) {
var branch = null, node = this.root, count = 0, after;
while (node != null) { // traverse tree to find position
branch = node;
after = value > node.value; // compare to get direction
if (after) {
node = branch.right; // pass on the right (greater)
count += branch.count; // add all nodes to the left of branch
} else {
node = branch.left; // pass on the left (smaller or equal)
++branch.count; // increment branch's count
}
} // attach new leaf
if (branch == null) this.root = new Leaf(value);
else if (after) branch.right = new Leaf(value);
else branch.left = new Leaf(value);
return count;
}
function Leaf(value) { // leaf constructor
this.value = value;
this.left = null;
this.right = null;
this.count = 1;
}
}
var t = new CountTree(), v = [5, 3, 8, 2, 4, 7, 9, 1, 4, 7, 8];
for (var i in v) {
document.write("Inserting " + v[i] + " (found " + t.insert(v[i]) + " smaller)
");
}
This is the tree from the example in the code, when inserting the last element (the second value 8). Every node has the count of nodes to its left (including itself) printed underneath its right vertex. When inserting the second 8, you pass the nodes with value 5, 8 and 7; of these, 5 and 7 are passed on the right, and the sum of their counts is 6 + 2 = 8, so there are 8 values smaller that 8 in the tree: 1, 2, 3, 4, 4, 5, 7 and 7. The first node with value 8 is passed on the left, so its count will be incremented from 3 to 4.