Integer findRoot(Integer a) {

        // to find the root of an element we traverse the tree starting from the button

        while (a != id[a]) {
            a = id[a];
        }
        return a;

        // this is another approach using recursion however it is good to avoid recursion.
//        if (id[a] == a) {  // the recursion end when a is the root which is when a's parent is a
//            return a;
//        } else {
//            return findRoot(id[a]);
//        }
    }