如何不允许将重复项添加到二进制搜索树中?

时间:2016-11-14 21:20:04

标签: java duplicates binary-search-tree

我一直试图找出如何编辑我的给定代码,如果它已经存在,它将不允许元素进入我的二叉树。我的提示是在测试人员之外修改两个不同的类来这样做。

我的第一堂课是:

public void addNode(Node <T> newNode){
        Comparable<T> tempElement = (Comparable<T>) newNode.element;

        int comp = tempElement.compareTo(element);
          if (comp < 0)
          {  
             if (left == null) 
                 {left = newNode;}
             else 
                 {left.addNode(newNode);}
          }
          else
          {  
             if (right == null) 
                 {right = newNode;}
             else 
                 {right.addNode(newNode);}

          }
    }

第二个是:

public void add(T obj) // add root first
    {
        Node<T> newNode = new Node<T>(obj);
        if (root == null) {
            root = newNode;
        } else {
            root.addNode(newNode);
        }
        count++;

    }

如果有人能帮助我,我将不胜感激!

2 个答案:

答案 0 :(得分:2)

假设您使用的Comparable实例与equals一致;那就是:

a.compareTo(b) == 0  iff  a.equals(b)

然后,只需检查以下情况即可消除重复:

a.compareTo(b) == 0

在当前代码中实现此目的的最简单方法是检查comp的值:

if (comp < 0)
{
  // Insert to the left, as you currently do.
}
else if (comp > 0)
{
  // Insert to the right, as you currently do.
}
else
{
  // Handle the duplicate: maybe do nothing, maybe throw an exception etc.
  // If you want to do nothing, you don't need the else block at all.
}

要解决问题的后半部分,请更改addNode方法以返回boolean,其中true表示已添加节点,false表示未添加该节点“T

然后:

Node<T> newNode = new Node<T>(obj);
if (root == null) {
  root = newNode;
  count++;  // "added" unconditionally.
} else {
  if (root.addNode(newNode)) count++;  // Only increment if it was really added.
}

答案 1 :(得分:0)

尝试将您的项目添加到Set中,因为集合只允许一个相等的项目实例。

/**
 * CursusMoment
 *
 * @ORM\Table(name="cursus_moment")
 * @ORM\Entity(repositoryClass="CursusBundle\Repository\CursusMomentRepository")
 */
class CursusMoment
{
    /**

     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="start_date", type="date")
     */
    private $startDate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="end_date", type="date")
     */
    private $endDate;

    /**
     * @var int
     *
     * @ORM\Column(name="cursus", type="integer")
     * @ORM\ManyToOne(targetEntity="Cursus", inversedBy="cursusMoments")
     */
    private $cursus;

    /**
     * @var int
     *
     * @ORM\Column(name="cursusSubscriptions", type="integer")
     * @ORM\OneToMany(targetEntity="CursusSubscription", mappedBy="cursusMoment", cascade={"persist"})
     */
    private $cursusSubscriptions;

    /**
     * CursusMoment constructor
     */
    public function __construct()
    {
        $this->cursusSubscriptions = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * Set cursus
     *
     * @param integer $cursus
     *
     * @return CursusMoment
     */
    public function setCursus($cursus)
    {
        $this->cursus = $cursus;

        return $this;
    }