Recursive insertion.

@param node Current node
@param nv Object data
@return New sibling, if the node was split.
513824
private TreeNode insert(TreeNode node, NumberVector nv) {
    // Find closest child:
    ClusteringFeature[] cfs = node.children;
    assert (cfs[0] != null) : "Unexpected empty node!";

    // Find the best child:
    ClusteringFeature best = cfs[0];
    double bestd = distance.squaredDistance(nv, best);
    for(int i = 1; i < cfs.length; i++) {
      ClusteringFeature cf = cfs[i];
      if(cf == null) {
        break;
      }
      double d2 = distance.squaredDistance(nv, cf);
      if(d2 < bestd) {
        best = cf;
        bestd = d2;
      }
    }

    // Leaf node:
    if(!(best instanceof TreeNode)) {
      // Threshold constraint satisfied?
      if(absorption.squaredCriterion(best, nv) <= thresholdsq) {
        best.addToStatistics(nv);
        node.addToStatistics(nv);
        return null;
      }
      best = new ClusteringFeature(nv.getDimensionality());
      best.addToStatistics(nv);
      ++leaves;
      if(add(node.children, best)) {
        node.addToStatistics(nv); // Update statistics
        return null;
      }
      return split(node, best);
    }
    assert (best instanceof TreeNode) : "Node is neither child nor inner?";
    TreeNode newchild = insert((TreeNode) best, nv);
    if(newchild == null || add(node.children, newchild)) {
      node.addToStatistics(nv); // Update statistics
      return null;
    }
    return split(node, newchild);
  }
640151
private void cutFromParent(Node<K, V> n) {
        if (n.o_s != null) {
            if (n.y_s != null) {
                n.y_s.o_s = n.o_s;
            }
            if (n.o_s.o_c == n) { // I am the oldest :(
                n.o_s.o_c = n.y_s;
            } else { // I have an older sibling!
                n.o_s.y_s = n.y_s;
            }
            n.y_s = null;
            n.o_s = null;
        }
    }
465041
function insert(n, p, x1, y1, x2, y2) {
    if (isNaN(p.x) || isNaN(p.y)) return; // ignore invalid points
    if (n.leaf) {
      var v = n.point;
      if (v) {
        // If the point at this leaf node is at the same position as the new
        // point we are adding, we leave the point associated with the
        // internal node while adding the new point to a child node. This
        // avoids infinite recursion.
        if ((Math.abs(v.x - p.x) + Math.abs(v.y - p.y)) < .01) {
          insertChild(n, p, x1, y1, x2, y2);
        } else {
          n.point = null;
          insertChild(n, v, x1, y1, x2, y2);
          insertChild(n, p, x1, y1, x2, y2);
        }
      } else {
        n.point = p;
      }
    } else {
      insertChild(n, p, x1, y1, x2, y2);
    }
  }
49165
private static void recursiveFindBadNodesInTable(Node n, Node cellroot, Vector<Node> nodes)
    {
        Node cell = cellroot;
        
        if (n.getNodeType() == Node.ELEMENT_NODE)
        {
            String tag = n.getNodeName();
            if (tag.equalsIgnoreCase("table"))
            {
                if (cell != null) //do not enter nested tables
                    return;
            }
            else if (tag.equalsIgnoreCase("tbody") || 
                     tag.equalsIgnoreCase("thead") || 
                     tag.equalsIgnoreCase("tfoot") ||
                     tag.equalsIgnoreCase("tr") ||
                     tag.equalsIgnoreCase("col") ||
                     tag.equalsIgnoreCase("colgroup"))
            {
            }
            else if (tag.equalsIgnoreCase("td") || tag.equalsIgnoreCase("th") || tag.equalsIgnoreCase("caption"))
            {
                cell = n;
            }
            else //other elements
            {
                if (cell == null)
                {
                    nodes.add(n);
                    return;
                }
            }
        } //other nodes
        else if (n.getNodeType() == Node.TEXT_NODE)
        {
            if (cell == null && n.getNodeValue().trim().length() > 0)
            {
                nodes.add(n);
                return;
            }
        }
        
        NodeList child = n.getChildNodes();
        for (int i = 0; i < child.getLength(); i++)
            recursiveFindBadNodesInTable(child.item(i), cell, nodes);
    }
7997
public Node nextNode() throws NoSuchElementException
   {
      initialize();
      if (next == null)
      {
         throw new NoSuchElementException();
      }
      NodeImpl n = next;
      fetchNext();
      return n;
   }
243788
public static Node copyTreeToNode(Node tree, Node node)
    {
        DOMResult result = new DOMResult(node);
        if (Utility.copyTreeToResult(tree, result))
            return node.getLastChild();
        else
            return null;    // Failure
    }
1097119
@Override
	public void addNode(Node n) {
		if (nodeAccess.getCapacity() < getNodeIndex(nodePos) + NODE_SIZE)
			nodeAccess.ensureCapacity(nodeAccess.getCapacity() + INITIAL_NODE_FILE_SIZE);
		nodIdMapping.put(n.getId(), nodePos);
		
		long nodeIndex = getNodeIndex(nodePos);
		
		nodeAccess.setLong( nodeIndex      , n.getId() );
		nodeAccess.setLong( nodeIndex + 8  ,    -1l    );
		nodeAccess.setLong( nodeIndex + 16 ,    -1l    );
		
		nodeAccess.setLong( 0 , ++nodePos );
		
	}
1109989
public void addIntermediary(AbstractPlanNode node) {

        // transfer this node's children to node
        Iterator<AbstractPlanNode> it = m_children.iterator();
        while (it.hasNext()) {
            AbstractPlanNode child = it.next();
            it.remove();                          // remove this.child from m_children
            assert child.getParentCount() == 1;
            child.clearParents();                 // and reset child's parents list
            node.addAndLinkChild(child);          // set node.child and child.parent
        }

        // and add node to this node's children
        assert(m_children.size() == 0);
        addAndLinkChild(node);
    }
601808
@CanIgnoreReturnValue
  private Node<K, V> addNode(@Nullable K key, @Nullable V value, @Nullable Node<K, V> nextSibling) {
    Node<K, V> node = new Node<K, V>(key, value);
    if (head == null) { // empty list
      head = tail = node;
      keyToKeyList.put(key, new KeyList<K, V>(node));
      modCount++;
    } else if (nextSibling == null) { // non-empty list, add to tail
      tail.next = node;
      node.previous = tail;
      tail = node;
      KeyList<K, V> keyList = keyToKeyList.get(key);
      if (keyList == null) {
        keyToKeyList.put(key, keyList = new KeyList<K, V>(node));
        modCount++;
      } else {
        keyList.count++;
        Node<K, V> keyTail = keyList.tail;
        keyTail.nextSibling = node;
        node.previousSibling = keyTail;
        keyList.tail = node;
      }
    } else { // non-empty list, insert before nextSibling
      KeyList<K, V> keyList = keyToKeyList.get(key);
      keyList.count++;
      node.previous = nextSibling.previous;
      node.previousSibling = nextSibling.previousSibling;
      node.next = nextSibling;
      node.nextSibling = nextSibling;
      if (nextSibling.previousSibling == null) { // nextSibling was key head
        keyToKeyList.get(key).head = node;
      } else {
        nextSibling.previousSibling.nextSibling = node;
      }
      if (nextSibling.previous == null) { // nextSibling was head
        head = node;
      } else {
        nextSibling.previous.next = node;
      }
      nextSibling.previous = node;
      nextSibling.previousSibling = node;
    }
    size++;
    return node;
  }
955835
public static <N> ImmutableGraph<N> singletonDirectedGraph(N node) {
        final MutableGraph<N> graph = GraphBuilder.directed().build();
        graph.addNode(node);
        return ImmutableGraph.copyOf(graph);
    }
1098357
protected function insertIntoTree(&$node, $existingNode = null)
    {

        $treeOriginTagDef = $node->getNodeRef()->getElement()->getSchema()->getTreeOriginTagDef();

        if(empty($treeOriginTagDef))
            throw new NodeException('Cannot insert a node in tree order mode without a tree origin tag definition');

        if($treeOriginTagDef->isMultiple())
            throw new NodeException('Cannot have more than 1 tree origin tag');

        if($treeOriginTagDef->Direction == 'out')
            $treeOrigin = $node->getOutTag($treeOriginTagDef->Id);
        else
            $treeOrigin = $node->getInTag($treeOriginTagDef->Id);

        if(empty($treeOrigin))
            throw new NodeException('Cannot insert a node into the tree without the tree origin tag');

        $treeOriginPartial = $treeOrigin->toPartial();

        $parentTreeID = '';
        if($node->getTreeParent() != false)
        {
            $parentNode = $this->NodeLookupDAO->getByNodeRef(new NodeRef($node->getNodeRef()->getElement(), $node->getTreeParent()));

            $parentTreeID = $parentNode->TreeID;
        }

        // fetch max tree id (if parentTreeID is blank, return last top-level tree id)
        $maxChildTreeID = $this->fetchMaxChildTreeID($node, $treeOriginTagDef, $treeOriginPartial, $parentTreeID);

        if(!is_null($existingNode) && $maxChildTreeID == $existingNode->TreeID)
        {
            $node->TreeID = $existingNode->TreeID;
            return;
        }

        if(empty($maxChildTreeID))
        {
            $newTreeID = $parentTreeID . '0001';

        // add this to the children
        } else {

            $last4 = substr($maxChildTreeID, -4);
            if(strtolower($last4) == 'ffff')
                throw new NodeException('This tree leaf has reached the limit of its children');

            $newTreeID = $parentTreeID . str_pad(dechex(hexdec($last4)+1), 4, '0', STR_PAD_LEFT);
        }

        while(!$this->Locks->getLock('treeid:'.$treeOriginPartial.':'.$newTreeID, 0)) {

            $last4 = substr($newTreeID, -4);
            if(strtolower($last4) == 'ffff')
                throw new NodeException('This tree leaf has reached the limit of its children');

            $newTreeID = $parentTreeID . str_pad(dechex(hexdec($last4)+1), 4, '0', STR_PAD_LEFT);

        }

        $node->TreeID = $newTreeID;
    }
819802
public void addNode(N node)
    {
        if(!nodes.containsKey(node))
            nodes.put(node, new Pair<HashSet<N>, HashSet<N>>(new HashSet<N>(), new HashSet<N>()));
    }
399095
function(current, position) {

      var x, newNode, node, firstChild;

      // create a new node
      newNode = current.cloneNode(true);

      // create new node in mirror
      if (position == 'before') {

        // insert element
        current.parentNode.insertBefore(newNode, current);

      }

      // re-init sortable
      hashedit.setupSortable();

      return newNode;

    }
330362
def add_sibling(self, pos=None, **kwargs):
        """"""
        pos = self._prepare_pos_var_for_add_sibling(pos)

        if len(kwargs) == 1 and 'instance' in kwargs:
            # adding the passed (unsaved) instance to the tree
            newobj = kwargs['instance']
            if newobj.pk:
                raise NodeAlreadySaved("Attempted to add a tree node that is "\
                    "already in the database")
        else:
            # creating a new object
            newobj = get_result_class(self.__class__)(**kwargs)

        if not self.node_order_by:
            newobj.sib_order = self.__class__._get_new_sibling_order(pos,
                                                                     self)
        newobj.parent_id = self.parent_id
        newobj.save()
        return newobj
167037
public void insertNode(int n, int pos)
  {

    if (!m_mutable)
      throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");

    insertElementAt(n, pos);
  }
538671
public void addChild(final DiffNode node)
	{
		if (node == this)
		{
			throw new IllegalArgumentException("Detected attempt to add a node to itself. " +
					"This would cause inifite loops and must never happen.");
		}
		else if (node.isRootNode())
		{
			throw new IllegalArgumentException("Detected attempt to add root node as child. " +
					"This is not allowed and must be a mistake.");
		}
		else if (node.getParentNode() != null && node.getParentNode() != this)
		{
			throw new IllegalArgumentException("Detected attempt to add child node that is already the " +
					"child of another node. Adding nodes multiple times is not allowed, since it could " +
					"cause infinite loops.");
		}
		if (node.getParentNode() == null)
		{
			node.setParentNode(this);
		}
		children.put(node.getElementSelector(), node);
		if (state == State.UNTOUCHED && node.hasChanges())
		{
			state = State.CHANGED;
		}
	}
912871
public void add(final ObjectSinkNode node) {
        if ( this.firstNode == null ) {
            this.firstNode = node;
            this.lastNode = node;
        } else {
            this.lastNode.setNextObjectSinkNode( node );
            node.setPreviousObjectSinkNode( this.lastNode );
            this.lastNode = node;
        }
        this.size++;
    }