It's a bit tricky to do this in the general case since it highly depends on markup, but it looks like what you want is to change the class of an anchor any of whose next siblings contains an anchor with class active. The following should do the trick, with the slight optimization that it will skip any anchors that are already so marked. It uses the nextAll() method and :not, and :has selectors.
$('a:not(.active)').nextAll(':has(a.active)')
.addClass('active');
If you need the initial anchor restricted to a certain level, just change the initial selector so that it is specific to the level that you need. The easiest way is to base it off a particular element $('#myList > a:not(.active)')
or with a particular class $('a.topLevel:not(.active)')
.