Different rules on the splitting position can result in homogeneous sub-dungeons (position between 0.45 and 0.55) or heterogeneous ones (position between 0.1 and 0.9). We can also choose to use a deeper recursion level on some parts of the dungeon so that we get smaller rooms there.
Here is an implementation in Java.
When choosing the splitting position, we have to take care not to be too close to the dungeon border. We must be able to place a room inside each generated sub-dungeon. We repeat until the lowest sub-dungeons have approximately the size of the rooms we want to generate.
It assumes you have a suitably implemented BinaryNode and Area data structure.
RNG.between(int a, int b) returns a number between a and b inclusive.
* @return A new node representing the BSP.
// initialised at 1. A depth of 0 would be a single leaf.
// this node is too small but others may not be.
// Just to make the code a bit neater.
* 0, list length does not. I have not subtracted 1 from the "from"
* values however as the splitArea method splits to the left.
// If true, split vertically.
Now we create a room with random size in each leaf of the tree. Of course, the room must be contained inside the corresponding sub-dungeon. Thanks to the BSP tree, we can't have two overlapping rooms.
To build corridors, we loop through all the leafs of the tree, connecting each leaf to its sister. If the two rooms have face-to-face walls, we can use a straight corridor. Else we have to use a Z shaped corridor.
Now we get up one level in the tree and repeat the process for the parent sub-regions. Now, we can connect two sub-regions with a link either between two rooms, or a corridor and a room or two corridors.
If we allow some rooms to fill the whole leaf, we can even have less boring dungeons.