插入等值元素 [英] Inserting an equal value element

查看:80
本文介绍了插入等值元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究二进制搜索树,我想知道如果您尝试插入与根值相同的元素该怎么办?

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?

推荐答案

BST的定义是它是有序集合,因此不允许插入重复项。 。这通常是由于在BST顶部构建了更复杂的结构。根据所需的行为,您可能希望在插入重复项时引发异常,错误或无提示地忽略。

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.

这篇关于插入等值元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆