Surface electrocardiography (ECG) is the art of analyzing the heart's electrical activity by applying electrodes to certain positions on the body and measuring potentials at the body surface resulting from this electrical activity. Usually, significant clinical information can be obtained from analysis of the dominant beat morphology. In this respect, identification of the dominant beats and their averaging can be very helpful, allowing clinicians to carry out the measurement of amplitudes and intervals on a beat much cleaner from noise than a generic beat selected from the entire ECG recording. In this paper a standard clustering algorithm for the morphological grouping of heartbeats has been analyzed based on K-means, different signal representations, distance metrics and validity indices. The algorithm has been tested on all the records of the MIT-BIH Arrhythmia Database (MIT-BIH AD) obtaining satisfying performances in terms of averaged dominant beat estimation, but the results have not been fully satisfactory in terms of sensitivity and specificity. In order to improve the clustering accuracy, an ad hoc algorithm based on a two-phase decision tree, which integrates additional specific knowledge related to the ECG domain, has been implemented. Similarity features extracted from every beat have been used in the decision trees for the identification of different morphological classes of ECG beats. The results, in terms of dominant beat discrimination, have been evaluated on all annotated beats of the MIT-BIH AD with sensitivity = 99.05%, specificity = 93.94%, positive predictive value = 99.32% and negative predictive value = 91.69%. Further tests have shown a very slight decrement of the performances on all detected beats of the same database using an already published QRS detector, demonstrating the validity of the algorithm in real unsupervised clustering situations where annotated beat positions are not available but beats are detected with a high-performance beat detector.