Pseudocode to compare two trees

后端 未结 5 1981
猫巷女王i
猫巷女王i 2021-02-02 11:11

This is a problem I\'ve encountered a few times, and haven\'t been convinced that I\'ve used the most efficient logic.

As an example, presume I have two trees: one is a

5条回答
  •  清歌不尽
    2021-02-02 11:49

    If you use a sort tree, like an AVL tree, you can also traverse your tree efficiently in-order. That will return your paths in sorted order from "low" to "high". Then you can sort your directory array (e.g. Using quicksort) using the same compare method as you use in your tree algorithm.

    Then start comparing the two side by side, advancing to the next item by traversing your tree in-order and checking the next item in your sorted directory array.

    This should be more efficient in practice, but only benchmarking can tell.

提交回复
热议问题