How does a sentinel node offer benefits over NULL?

后端 未结 5 1258
梦谈多话
梦谈多话 2020-12-07 09:31

On the Sentinel Node wikipedia page it says that the benefits of a sentinel node over NULL are :

  • Increased speed of operations
  • Reduced algorithmic cod
5条回答
  •  既然无缘
    2020-12-07 09:46

    The answer to your question (1) is in the last sentence of the linked Wikipedia entry: "As nodes that would normally link to NULL now link to "nil" (including nil itself), it removes the need for an expensive branch operation to check for NULL."

    Normally you need to test a node for NULL before accessing it. If instead you have a valid nil node then you don't need to do this first test, saving a comparison and a conditional branch, which can otherwise be expensive on modern superscalar CPUs when the branch is mis-predicted.

提交回复
热议问题