This paper presents an algorithm, based on the self-balancing binary search tree, to form learning groups. It aims to generate learning groups that are intra-homogeneous (student performance similarity within the group), interhomogeneous (group performance similarity between groups), and of balanced size. The algorithm mainly uses the 2-3 tree and the 2-3-4 tree as two implementations of a self-balancing binary search tree to form student blocks with close GPAs (grade point averages) and balanced sizes. Then, groups are formed from those blocks in a greedy manner. The experiment showed the efficiency of the proposed algorithm, compared to traditional forming methods, in balancing the size of the groups and improving their intra-and inter-homogeneity by up to 26%, regardless of the used version of the self-balancing binary search tree (2-3 or 2-3-4). For small samples of students, the use of the 2-3-4 tree was distinguished for improving intra-and interhomogeneity compared to the 2-3 tree. As for large samples of students, experiments showed that the 2-3 tree was better than the 2-3-4 tree in improving the inter-homogeneity, while the 2-3-4 tree was distinguished in improving the intra-homogeneity.