This allusion was probably included for two reasons. The first is that Tom and Becky are “imprisoned” in the cave. When they first arrive, the cave seems a magical place, and they wander into “Aladdin’s Palace” before getting lost. While they are lost, things seem far less magical than before. Becky is tired and weeping, and Tom, initially, does not know what to do to solve the problem. The author may be suggesting that some places we think are magical are also dangerous. The author might also be trying to tell us something about Tom. Tom is, at heart, a naïve and deeply innocent character, who believes that everything will be fine. This seems to be true when Tom and Becky escape the cave through Tom’s ingenuity, and much is made of his heroism; we could think of this as “a palace is built for him.” However, as we know, not all bad situations end up like this.