/// <summary> /// On adding a node to a tree, if it already exists, and if /// Tree#duplicates() returns false, #isDuplicateOf() will be /// called. /// </summary> /// <remarks> /// On adding a node to a tree, if it already exists, and if /// Tree#duplicates() returns false, #isDuplicateOf() will be /// called. The added node can then be asked for the node that /// prevails in the tree using #duplicateOrThis(). This mechanism /// allows doing find() and add() in one run. /// </remarks> public virtual Tree Add(Tree newNode, int cmp) { if (cmp < 0) { if (_subsequent == null) { _subsequent = newNode; _size++; } else { _subsequent = _subsequent.Add(newNode); if (_preceding == null) { return(RotateLeft()); } return(Balance()); } } else { if (cmp > 0 || newNode.Duplicates()) { if (_preceding == null) { _preceding = newNode; _size++; } else { _preceding = _preceding.Add(newNode); if (_subsequent == null) { return(RotateRight()); } return(Balance()); } } else { return(newNode.OnAttemptToAddDuplicate(this)); } } return(this); }
/// <summary> /// On adding a node to a tree, if it already exists, and if /// Tree#duplicates() returns false, #isDuplicateOf() will be /// called. /// </summary> /// <remarks> /// On adding a node to a tree, if it already exists, and if /// Tree#duplicates() returns false, #isDuplicateOf() will be /// called. The added node can then be asked for the node that /// prevails in the tree using #duplicateOrThis(). This mechanism /// allows doing find() and add() in one run. /// </remarks> public virtual Tree Add(Tree newNode, int cmp) { if (cmp < 0) { if (_subsequent == null) { _subsequent = newNode; _size++; } else { _subsequent = _subsequent.Add(newNode); if (_preceding == null) { return RotateLeft(); } return Balance(); } } else { if (cmp > 0 || newNode.Duplicates()) { if (_preceding == null) { _preceding = newNode; _size++; } else { _preceding = _preceding.Add(newNode); if (_subsequent == null) { return RotateRight(); } return Balance(); } } else { return newNode.OnAttemptToAddDuplicate(this); } } return this; }