An efficient algorithm for automatically constructing a tree based on records in a database table