Find the leaf of a cluster, to get the final cluster assignment.
<p>
In contrast to {@link #insert}, this does not modify the tree.
<p>
TODO: allow "outliers"?

@param node Current node
@param nv Object data
@return Leaf this vector should be assigned to
768097
private ClusteringFeature findLeaf(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;
      }
    }

    return (best instanceof TreeNode) ? findLeaf((TreeNode) best, nv) : best;
  }
969060
private V findSmallest(Node node) {
        if (node == null) {
            return null;
        }

        while (node.left != null) {
            node = node.left;
        }

        return node.item;
    }
1072323
private Node findParentNode(long node) {
        // assert node > 0;

        // find bit representation of node
        long[] s = { node };
        BitSet bits = BitSet.valueOf(s);

        // traverse path to last node
        Node cur = root;
        for (int i = bits.length() - 2; i > 0; i--) {
            if (bits.get(i)) {
                cur = cur.o_c.y_s;
            } else {
                cur = cur.o_c;
            }
        }
        return cur;
    }
547391
@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {

		component.removeAll();
		SiteNode node = null;
		if (value instanceof SiteNode) {
			node = (SiteNode) value;
		}
		
		if (node != null) {
	        if( node.isFiltered()) {
	        	// Hide the node
	            setPreferredSize( new Dimension(0, 0) );
	            return this;
	        }

			setPreferredSize(null);	// clears the preferred size, making the node visible
			super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

			// folder / file icons with scope 'target' if relevant
			if (node.isRoot()) {
				component.add(wrap(ROOT_ICON)); // 'World' icon
			} else {
				OverlayIcon icon;
		        if (node.isDataDriven()) {
					if (node.isIncludedInScope() && ! node.isExcludedFromScope()) {
						icon = new OverlayIcon(DATA_DRIVEN_IN_SCOPE_ICON);
					} else {
						icon = new OverlayIcon(DATA_DRIVEN_ICON);
					}
		        } else if (leaf) {
					if (node.isIncludedInScope() && ! node.isExcludedFromScope()) {
						icon = new OverlayIcon(LEAF_IN_SCOPE_ICON);
					} else {
						icon = new OverlayIcon(LEAF_ICON);
					}
				} else {
					if  (expanded) {
						if (node.isIncludedInScope() && ! node.isExcludedFromScope()) {
							icon = new OverlayIcon(FOLDER_OPEN_IN_SCOPE_ICON);
						} else {
							icon = new OverlayIcon(FOLDER_OPEN_ICON);
						}
					} else {
						if (node.isIncludedInScope() && ! node.isExcludedFromScope()) {
							icon = new OverlayIcon(FOLDER_CLOSED_IN_SCOPE_ICON);
						} else {
							icon = new OverlayIcon(FOLDER_CLOSED_ICON);
						}
					}
				}
				if (node.getParent().isRoot() && node.getNodeName().startsWith("https://")) {
					// Add lock icon to site nodes with https
					icon.add(LOCK_OVERLAY_ICON);
				}
				
				component.add(wrap(DisplayUtils.getScaledIcon(icon)));
				
				Alert alert = node.getHighestAlert();
				if (alert != null) {
					component.add(wrap(alert.getIcon()));
				}

				for (ImageIcon ci : node.getCustomIcons()){
					component.add(wrap(DisplayUtils.getScaledIcon(ci)));
				}
				
			}
			setText(node.toString());
			setIcon(null);
			component.add(this);

	        for (SiteMapListener listener : listeners) {
	        	listener.onReturnNodeRendererComponent(this, leaf, node);
	        }
	        return component;
		}

		return this;
	}
365930
@Nullable
    private Node<V> findNode(Node<V> node, String path, int begin, boolean exact) {
        final int next;
        switch (node.type()) {
            case EXACT:
                final int len = node.path().length();
                if (!path.regionMatches(begin, node.path(), 0, len)) {
                    // A given path does not start with the path of this node.
                    return null;
                }
                if (len == path.length() - begin) {
                    // Matched. No more input characters.
                    // If this node is not added by a user, then we should return a catch-all child
                    // if it exists. But if 'exact' is true, we just return this node to make caller
                    // have the exact matched node.
                    return exact || node.hasValues() || !node.hasCatchAllChild() ? node
                                                                                 : node.catchAllChild();
                }
                next = begin + len;
                break;
            case PARAMETER:
                // Consume characters until the delimiter '/' as a path variable.
                final int delim = path.indexOf('/', begin);
                if (delim < 0) {
                    // No more delimiter.
                    return node;
                }
                if (path.length() == delim + 1) {
                    final Node<V> trailingSlashNode = node.child('/');
                    return trailingSlashNode != null ? trailingSlashNode : node;
                }
                next = delim;
                break;
            default:
                throw new Error("Should not reach here");
        }

        // The path is not matched to this node, but it is possible to be matched on my children
        // because the path starts with the path of this node. So we need to visit children as the
        // following sequences:
        //  - The child which is able to consume the next character of the path.
        //  - The child which has a path variable.
        //  - The child which is able to consume every remaining path. (catch-all)

        Node<V> child = node.child(path.charAt(next));
        if (child != null) {
            final Node<V> found = findNode(child, path, next, exact);
            if (found != null) {
                return found;
            }
        }
        child = node.parameterChild();
        if (child != null) {
            final Node<V> found = findNode(child, path, next, exact);
            if (found != null) {
                return found;
            }
        }
        child = node.catchAllChild();
        if (child != null) {
            return child;
        }

        return null;
    }
1091268
protected Node removeNode( Node node )
    {
        // Remove node.
        node.previous.next = node.next;
        node.next.previous = node.previous;
        node.removed = true;
        return node;
    }
201280
public static Node getRValueOfLValue(Node n) {
    Node parent = n.getParent();
    switch (parent.getToken()) {
      case ASSIGN:
      case ASSIGN_BITOR:
      case ASSIGN_BITXOR:
      case ASSIGN_BITAND:
      case ASSIGN_LSH:
      case ASSIGN_RSH:
      case ASSIGN_URSH:
      case ASSIGN_ADD:
      case ASSIGN_SUB:
      case ASSIGN_MUL:
      case ASSIGN_EXPONENT:
      case ASSIGN_DIV:
      case ASSIGN_MOD:
      case DESTRUCTURING_LHS:
        return n.getNext();
      case VAR:
      case LET:
      case CONST:
        return n.getLastChild();
      case OBJECTLIT:
      case CLASS_MEMBERS:
        return n.getOnlyChild();
      case FUNCTION:
      case CLASS:
        return parent;
      default:
        break;
    }
    return null;
  }
1304248
public static Object getUserObjectFromTreeNode(Object nodeObject)
    {
        if (nodeObject == null)
        {
            return null;
        }
        if (nodeObject instanceof DefaultMutableTreeNode)
        {
            DefaultMutableTreeNode node = 
                (DefaultMutableTreeNode)nodeObject;
            Object userObject = node.getUserObject();
            return userObject;
        }
        return null;
    }
1137949
private void addNode(Node n) {
    Node target = null;
    Node fn = null;
    String name = null;

    switch (n.getToken()) {
      // handles assignment cases like:
      // a = function() {}
      // a = b = c = function() {}
      case ASSIGN:
        if (!n.getFirstChild().isQualifiedName()) {
          compiler.report(JSError.make(n, INJECTED_FUNCTION_ON_NON_QNAME));
          return;
        }
        name = n.getFirstChild().getQualifiedName();
        // last node of chained assignment.
        fn = n;
        while (fn.isAssign()) {
          fn = fn.getLastChild();
        }
        target = n.getParent();
        break;

      // handles function case:
      // function fnName() {}
      case FUNCTION:
        name = NodeUtil.getName(n);
        fn = n;
        target = n;
        if (n.getParent().isAssign()
            && n.getParent().getJSDocInfo().isNgInject()) {
          // This is a function assigned into a symbol, e.g. a regular function
          // declaration in a goog.module or goog.scope.
          // Skip in this traversal, it is handled when visiting the assign.
          return;
        }
        break;

      // handles var declaration cases like:
      // var a = function() {}
      // var a = b = function() {}
      case VAR:
      case LET:
      case CONST:
        name = n.getFirstChild().getString();
        // looks for a function node.
        fn = getDeclarationRValue(n);
        target = n;
        break;

      // handles class method case:
      // class clName(){
      //   constructor(){}
      //   someMethod(){} <===
      // }
      case MEMBER_FUNCTION_DEF:
        Node parent = n.getParent();
        if (parent.isClassMembers()){
          Node classNode = parent.getParent();
          String midPart = n.isStaticMember() ? "." : ".prototype.";
          name = NodeUtil.getName(classNode) + midPart + n.getString();
          if (NodeUtil.isEs6ConstructorMemberFunctionDef(n)) {
            name = NodeUtil.getName(classNode);
          }
          fn = n.getFirstChild();
          if (classNode.getParent().isAssign() || classNode.getParent().isName()) {
            target = classNode.getGrandparent();
          } else {
            target = classNode;
          }
        }
        break;
      default:
        break;
    }

    if (fn == null || !fn.isFunction()) {
      compiler.report(JSError.make(n, INJECT_NON_FUNCTION_ERROR));
      return;
    }
    // report an error if the function declaration did not take place in a block or global scope
    if (!target.getParent().isScript()
        && !target.getParent().isBlock()
        && !target.getParent().isModuleBody()) {
      compiler.report(JSError.make(n, INJECT_IN_NON_GLOBAL_OR_BLOCK_ERROR));
      return;
    }
    // checks that name is present, which must always be the case unless the
    // compiler allowed a syntax error or a dangling anonymous function
    // expression.
    checkNotNull(name);
    // registers the node.
    injectables.add(new NodeContext(name, n, fn, target));
  }
955433
protected L putInMap(L node) {
		nodeMap.put(node.get(), node);
		identityDetector.put(node.get(), node.get());
		return node;
	}
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);
    }
923291
public Node getNode(String loc) {
    netlock.readLock().lock();
    try {
      loc = NodeBase.normalize(loc);
      if (!NodeBase.ROOT.equals(loc))
        loc = loc.substring(1);
      return clusterMap.getLoc(loc);
    } finally {
      netlock.readLock().unlock();
    }
  }
1038982
public static final Element getNextSiblingElement(Node node) {
    	
    	List<Node> siblings = node.getParent().getChildren();
    	Node n = null;
    	
    	int index = siblings.indexOf(node) + 1;
    	if(index>0 && index<siblings.size()) {
	    	n = siblings.get(index);
	    	while(!(n instanceof Element) && ++index < siblings.size()) {
	    		n = siblings.get(index);
	    	}
	    	
	    	if(index==siblings.size()) {
	    		n = null;
	    	}
    	}
    	
        return (Element) n;
    }
1278504
function (node) {
            var first = node, length = 1;
            if (node.nodeType == 11) {
                first = node.firstChild;
                length = node.childNodes.length;
            }
            this.trimBoundary(true);
            var start = this.startContainer,
                offset = this.startOffset;
            var nextNode = start.childNodes[ offset ];
            if (nextNode) {
                start.insertBefore(node, nextNode);
            } else {
                start.appendChild(node);
            }
            if (first.parentNode === this.endContainer) {
                this.endOffset = this.endOffset + length;
            }
            return this.setStartBefore(first);
        }
144567
@Nullable
  public static AccessPath fromNodeIfTrackable(Node node) {
    if (node instanceof LocalVariableNode) {
      return fromLocalVariable((LocalVariableNode) node);
    } else if (node instanceof VariableDeclarationNode) {
      return fromVariableDecl((VariableDeclarationNode) node);
    } else if (node instanceof FieldAccessNode) {
      return fromFieldAccess((FieldAccessNode) node);
    } else if (node instanceof AssignmentNode) {
      return fromNodeIfTrackable(((AssignmentNode) node).getTarget());
    }
    return null;
  }
561169
public void insert(NumberVector nv) {
    final int dim = nv.getDimensionality();
    // No root created yet:
    if(root == null) {
      ClusteringFeature leaf = new ClusteringFeature(dim);
      leaf.addToStatistics(nv);
      root = new TreeNode(dim, capacity);
      root.children[0] = leaf;
      root.addToStatistics(nv);
      ++leaves;
      return;
    }
    TreeNode other = insert(root, nv);
    // Handle root overflow:
    if(other != null) {
      TreeNode newnode = new TreeNode(dim, capacity);
      newnode.addToStatistics(newnode.children[0] = root);
      newnode.addToStatistics(newnode.children[1] = other);
      root = newnode;
    }
  }
338499
public static Node getNtResourceRecursively(Node node) throws ItemNotFoundException, RepositoryException
   {

      if (node.isNodeType("nt:resource"))
         return node;

      Item pi = node.getPrimaryItem();
      if (pi.isNode())
      {
         return getNtResourceRecursively((Node)pi);
      }
      throw new ItemNotFoundException("No nt:resource node found for " + node.getPath());
   }