Inserting an equal value element

倖福魔咒の 提交于 2019-12-21 06:48:49

问题


I am currently studying binary search trees, and I was wondering what do you do if you try to insert an element that is the same value as the root? Where does it go?


回答1:


The definition of BST is that it is a ordered set, thus duplicates are not allowed to be inserted. This is usually due to more complex structures being built atop the BST. Depending on the desired behavior, you may want to throw an exception, error or silently ignore when duplicates are inserted.

However, depending on your comparison function you can store duplicates on the left or right subtree, but remember to keep your traversals and insertion sides consistent.



来源:https://stackoverflow.com/questions/9384730/inserting-an-equal-value-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!