Categories are not showing up according to the hierarchy

the categories can be order differently, it will depend of the arguments in the get_categories() function, once none is specified, it will assume the default.
try this to have an idea:

$cats = get_categories();
echo '<pre>';
print_r($cats);
echo </pre>';
exit();

This will give you everything store in $cats variable.

For example normally I use the following code for categories until 3 sub-categories level

$cats = get_categories( array('taxonomy' => 'product_cat', 'hide_empty' => true) );
foreach ($cats as $key => $cat):
    if ($cat->parent == 0):
        $cat->name;
        foreach ($cats as $key => $cat2):
            if ($cat2->parent == $cat->term_id):
                $cat2->name;
                foreach ($cats as $key => $cat3):
                    if ($cat3->parent == $cat2->term_id):
                        $cat3->name;
                    endif;
                endforeach;
            endif;
        endforeach;
    endif;
endforeach;

notice that the get_categories has arguments, and is fetching only the woocommerce categories in this case.

in your website try to give the argument hierarchical and use the print_r function to debug, then looking at this second peace of code right before I think you can figure it out.

$cats = get_categories(array('hierarchical' => 1);
echo '<pre>';
print_r($cats);
echo </pre>';
exit();

For a more exact answer if still needed, please use the first code given and share the answer you get.
Espero ter ajudado!(“Hope this helps!”).