/** * ���ýڵ���� * * @param clusteringFeature ��ǰ�ڵ� * @param level ��ǰ���ֵ */ private void setTreeLevel(ClusteringFeature clusteringFeature, int level) { LeafNode leafNode = null; NonLeafNode nonLeafNode = null; if (clusteringFeature instanceof LeafNode) { leafNode = (LeafNode) clusteringFeature; } else if (clusteringFeature instanceof NonLeafNode) { nonLeafNode = (NonLeafNode) clusteringFeature; } if (nonLeafNode != null) { nonLeafNode.setLevel(level); level++; // �����ӽڵ� if (nonLeafNode.getNonLeafChilds() != null) { for (NonLeafNode n1 : nonLeafNode.getNonLeafChilds()) { setTreeLevel(n1, level); } } else { for (LeafNode n2 : nonLeafNode.getLeafChilds()) { setTreeLevel(n2, level); } } } else { leafNode.setLevel(level); level++; // �����Ӿ۴� for (Cluster c : leafNode.getClusterChilds()) { c.setLevel(level); } } }
@Override @SuppressWarnings("unchecked") public void visit(BranchNode branchNode, NodeStorageManager storage) { assert branchNode.mutable; Node<? extends Node<?>> bestChild = findWritableBestBranch(branchNode, item, storage); if (bestChild != null) { bestChild.accept(this, storage); return; } // Exceptional case - empty node. So do this last if (branchNode.getNumChildren() == 0) { // We are making a change so clone the node BranchNode updateBranch = storage.getWriteableBranch(branchNode.getRef()); LeafNode newLeaf = storage.createLeafNode(updateBranch.getRef()); updateBranch.add(new Branch((RefImpl) newLeaf.getRef())); newLeaf.accept(this, storage); return; } // FIXME: This happens if an enum splitter has been configured too small // we shouldn't need to pre-allocate the branches in that case throw new Error("InsertFailedException(item);"); }
/** * Creates an instant node, if supported. * * @return The node that was created * @exception XMPPException */ public LeafNode createNode() throws XMPPException { PubSub reply = (PubSub) sendPubsubPacket(Type.SET, new NodeExtension(PubSubElementType.CREATE)); NodeExtension elem = (NodeExtension) reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); LeafNode newNode = new LeafNode(con, elem.getNode()); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Get a map whose keys are selected nodes and values are the list of selected cells in each node. * * @return a Map<LeafNode, int[]> describing all selected cells */ public Map<LeafNode, int[]> getMapOfSelectedCells() { Map<LeafNode, int[]> toReturn = new HashMap<LeafNode, int[]>(selectionNode.size()); for (LeafNode leaf : selectionNode) { if (leaf.hasCellSelection()) { int[] cells = leaf.getCellSelection(); int[] copy = new int[cells.length]; System.arraycopy(cells, 0, copy, 0, cells.length); toReturn.put(leaf, copy); } } return toReturn; }
private void addChild(Node<K, V> node, Node<K, V> child) { if (child.getType() == NodeType.LEAF) { LeafNode leaf = (LeafNode) child; for (int i = 0; i < leaf.size(); i++) { node.put((K) leaf.getKeys().get(i), (V) leaf.getValues().get(i)); } } else if (child.getType() == NodeType.GUIDE) { GuideNode guide = (GuideNode) child; for (int i = 0; i < guide.size(); i++) { addChild(node, (Node<K, V>) guide.getKids().get(i)); } } }
@Test public void testUnderflowRemoveEnd() { final LeafNode leaf = leaf(1, 2, 3, 4); final RemoveResult result = leaf._removed(4, 1); final LeafNode newLeaf = (LeafNode) result.newNode; assertArrayEquals(new Integer[] {1, 2, 3}, newLeaf.keys); assertArrayEquals(new Integer[] {100, 200, 300}, newLeaf.values); assertEquals(true, result.underflowed); assertEquals(1, result.leftSeparator); }
@Test public void testSimpleRemoveNotFound() { final LeafNode leaf = leaf(1, 2, 4, 5); final RemoveResult result = leaf._removed(3, 1); final LeafNode newLeaf = (LeafNode) result.newNode; assertArrayEquals(new Integer[] {1, 2, 4, 5}, newLeaf.keys); assertArrayEquals(new Integer[] {100, 200, 400, 500}, newLeaf.values); assertEquals(false, result.underflowed); assertEquals(1, result.leftSeparator); }
@Override public void add(double x, double y, int depth, LeafNode<T> node) { if (node.supportsMultipleValues()) { values.addAll(((MultiValuedLeafNode<T>) node).values); } else { values.add(((SingleValueLeafNode<T>) node).value); } }
/** * Testing LeafNode methods. Because LeafNode inherits from QuadTreeNode, bounds on the node will * be tested with QuadTreeNode because none of those methods are overridden in LeafNode * * @author Sam Whitlock (cs61b-eo) * @param void * @return void */ @Test leafNodeTest1() { LeafNode<QuadPoint> node = new LeafNode<QuadPoint>(null, null, 0, 0, 10, 10); QuadPoint pt1 = new QuadPoint(1, 1); QuadPoint pt2 = new QuadPoint(9, 9); Leaf<QuadPoint> lf1 = new Leaf<QuadPoint>(pt1, null, null); Leaf<QuadPoint> lf2 = new Leaf<QuadPoint>(pt2, null, null); node.insert(lf1); node.insert(lf2); assertTrue(node.kids.contains(lf1) && node.kids.contains(lf2)); ArrayList<Leaf<QuadPoint>> boundedKids = node.boundedChildren(0, 0, 10, 10); assertTrue(boundedKids.contains(lf1) && boundedKids.contains(lf2)); boundedKids.clear(); boundedKids = node.boundedChildren(0, 0, 3, 3); assertTrue(boundedKids.contains(lf1) && !boundedKids.contains(lf2)); boundedKids.clear(); boundedKids = node.boundedChildren(7, 7, 10, 10); assertTrue(boundedKids.contains(lf2) && !boundedKids.contains(lf1)); boundedKids.clear(); boundedKids = node.boundedChildren(3, 3, 7, 7); assertTrue(boundedKids.isEmpty()); node.remove(lf1); node.remove(lf2); assertTrue(node.kids.isEmpty()); }
public void addCandidate(LeafNode<T> node) { double dx = (x - node.x) * (x - node.x); double dy = (y - node.y) * (y - node.y); double distanceSquared = dx + dy; if (distanceSquared < radiusSquared) { if (dx > maxDistanceSquared) { maxDistanceSquared = distanceSquared; } valueCount += node.getValueCount(); queue.add(new NodeWithDistance<T>(node, distanceSquared)); } }
/** * ����CF���������� * * @return */ private ClusteringFeature buildCFTree() { NonLeafNode rootNode = null; LeafNode leafNode = null; Cluster cluster = null; for (String[] record : totalDataRecords) { cluster = new Cluster(record); if (rootNode == null) { // CF��ֻ��1���ڵ��ʱ������ if (leafNode == null) { leafNode = new LeafNode(); } leafNode.addingCluster(cluster); if (leafNode.getParentNode() != null) { rootNode = leafNode.getParentNode(); } } else { if (rootNode.getParentNode() != null) { rootNode = rootNode.getParentNode(); } // �Ӹ�ڵ㿪ʼ����������Ѱ�ҵ��������Ŀ��Ҷ�ӽڵ� LeafNode temp = rootNode.findedClosestNode(cluster); temp.addingCluster(cluster); } } // ���������ҳ�������Ľڵ� LeafNode node = cluster.getParentNode(); NonLeafNode upNode = node.getParentNode(); if (upNode == null) { return node; } else { while (upNode.getParentNode() != null) { upNode = upNode.getParentNode(); } return upNode; } }
@Override public void updateNode(Instance inst) throws Exception { super.updateDistribution(inst); for (int i = 0; i < inst.numAttributes(); i++) { Attribute a = inst.attribute(i); if (i != inst.classIndex()) { ConditionalSufficientStats stats = m_nodeStats.get(a.name()); if (stats == null) { if (a.isNumeric()) { stats = new GaussianConditionalSufficientStats(); } else { stats = new NominalConditionalSufficientStats(); } m_nodeStats.put(a.name(), stats); } stats.update( inst.value(a), inst.classAttribute().value((int) inst.classValue()), inst.weight()); } } }
public void add(double x, double y, int depth, LeafNode<T> value) { if ((depth % 2) == 0) { // current node is split on X axis if (x < splitValue) { // left subtree if (left == null) { left = value; } else { if (left.isLeaf()) { // left == leaf node , split at Y-axis and re-insert final LeafNode<T> tmp = (LeafNode<T>) left; if (tmp.x == x && tmp.y == y) { if (!tmp.supportsMultipleValues()) { left = new MultiValuedLeafNode<>((SingleValueLeafNode<T>) tmp); } left.add(x, y, depth, value); } else { final double splitY = (tmp.y + y) / 2.0; final NonLeafNode<T> newNode = new NonLeafNode<>(splitY); newNode.add(tmp.x, tmp.y, depth + 1, tmp); newNode.add(x, y, depth + 1, value); left = newNode; } } else { left.add(x, y, depth + 1, value); } } } else { // right subtree if (right == null) { right = value; } else { if (right.isLeaf()) { // right == leaf node , split at Y-axis and re-insert final LeafNode<T> tmp = (LeafNode<T>) right; if (tmp.x == x && tmp.y == y) { if (!tmp.supportsMultipleValues()) { right = new MultiValuedLeafNode<>((SingleValueLeafNode<T>) tmp); } right.add(x, y, depth, value); } else { final double splitY = (tmp.y + y) / 2.0; final NonLeafNode<T> newNode = new NonLeafNode<>(splitY); newNode.add(tmp.x, tmp.y, depth + 1, tmp); newNode.add(x, y, depth + 1, value); right = newNode; } } else { right.add(x, y, depth + 1, value); } } } } else { // current node is split on Y axis if (y < splitValue) { // left subtree if (left == null) { left = value; } else { if (left.isLeaf()) { // left == leaf node , split at X-axis and re-insert final LeafNode<T> tmp = (LeafNode<T>) left; if (tmp.x == x && tmp.y == y) { if (!tmp.supportsMultipleValues()) { left = new MultiValuedLeafNode<>((SingleValueLeafNode<T>) tmp); } left.add(x, y, depth, value); } else { final double splitX = (tmp.x + x) / 2.0; final NonLeafNode<T> newNode = new NonLeafNode<>(splitX); newNode.add(tmp.x, tmp.y, depth + 1, tmp); newNode.add(x, y, depth + 1, value); left = newNode; } } else { left.add(x, y, depth + 1, value); } } } else { // right subtree if (right == null) { right = value; } else { if (right.isLeaf()) { // right == leaf node , split at X-axis and re-insert final LeafNode<T> tmp = (LeafNode<T>) right; if (tmp.x == x && tmp.y == y) { if (!tmp.supportsMultipleValues()) { right = new MultiValuedLeafNode<>((SingleValueLeafNode<T>) tmp); } right.add(x, y, depth, value); } else { final double splitX = (tmp.x + x) / 2.0; final NonLeafNode<T> newNode = new NonLeafNode<>(splitX); newNode.add(tmp.x, tmp.y, depth + 1, tmp); newNode.add(x, y, depth + 1, value); right = newNode; } } else { right.add(x, y, depth + 1, value); } } } } }
private void logResults( NanoTimer timer, ArrayList<NextItem> results, NodeStorageManager storage) { float t = timer.getMillis(); if (searchCount == 0) { searchStartTime = System.currentTimeMillis(); } if (log.isDebugEnabled() && results.size() > 0) { LeafNode leaf = (LeafNode) results.get(0).getLeaf(); ShowSplitsVisitor visitor = new ShowSplitsVisitor(); leaf.accept(visitor, storage); log.debug("Split order of first item: " + visitor.toString()); } // Log some info about the work done if (log.isInfoEnabled()) { log.info( "# results: " + results.size() + ", Nodes: " + getNodesExpanded() + ", Leaves: " + getLeafNodesExpanded() + ", Time (ms): " + timer.getMillis()); } totalResults += results.size(); searchTime += t; searchCount++; if (searchCount == 1000) { float avTime = searchTime / searchCount; float avElapsed = (float) (System.currentTimeMillis() - searchStartTime) / searchCount; float avResults = (float) (totalResults) / searchCount; log.info("====================== SEARCH STATS ============================="); log.info( "Elapsed time per search: " + avElapsed + "ms (i.e. actual rate: " + 1000 / avElapsed + " searches per sec)"); log.info( "Mean time doing search: " + avTime + "ms (i.e. potential rate: " + 1000 / avTime + " searches per sec)"); log.info( "Mean results per search: " + avResults + " (=> SearchTime per result =" + avTime / avResults + "ms)"); log.info("Non-search time (elapsed - search): " + (avElapsed - avTime) + "ms"); searchTime = 0.0f; searchCount = 0; totalResults = 0; } }
/** * ��ʾCF���������� * * @param rootNode CF����ڵ� */ private void showCFTree(ClusteringFeature rootNode) { // �ո���������� int blankNum = 5; // ��ǰ����� int currentLevel = 1; LinkedList<ClusteringFeature> nodeQueue = new LinkedList<>(); ClusteringFeature cf; LeafNode leafNode; NonLeafNode nonLeafNode; ArrayList<Cluster> clusterList = new ArrayList<>(); String typeName; nodeQueue.add(rootNode); while (nodeQueue.size() > 0) { cf = nodeQueue.poll(); if (cf instanceof LeafNode) { leafNode = (LeafNode) cf; typeName = LEAFNODE; if (leafNode.getClusterChilds() != null) { for (Cluster c : leafNode.getClusterChilds()) { nodeQueue.add(c); } } } else if (cf instanceof NonLeafNode) { nonLeafNode = (NonLeafNode) cf; typeName = NON_LEAFNODE; if (nonLeafNode.getNonLeafChilds() != null) { for (NonLeafNode n1 : nonLeafNode.getNonLeafChilds()) { nodeQueue.add(n1); } } else { for (LeafNode n2 : nonLeafNode.getLeafChilds()) { nodeQueue.add(n2); } } } else { clusterList.add((Cluster) cf); typeName = CLUSTER; } if (currentLevel != cf.getLevel()) { currentLevel = cf.getLevel(); System.out.println(); System.out.println("|"); System.out.println("|"); } else if (currentLevel == cf.getLevel() && currentLevel != 1) { for (int i = 0; i < blankNum; i++) { System.out.print("-"); } } System.out.print(typeName); System.out.print("N:" + cf.getN() + ", LS:"); System.out.print("["); for (double d : cf.getLS()) { System.out.print(MessageFormat.format("{0}, ", d)); } System.out.print("]"); } System.out.println(); System.out.println("*******���շֺõľ۴�****"); // ��ʾ�Ѿ��ֺ���ľ۴ص� for (int i = 0; i < clusterList.size(); i++) { System.out.println("Cluster" + (i + 1) + "��"); for (double[] point : clusterList.get(i).getData()) { System.out.print("["); for (double d : point) { System.out.print(MessageFormat.format("{0}, ", d)); } System.out.println("]"); } } }
@Override public void visit(LeafNode leafNode, NodeStorageManager storage) { assert (leafNode.mutable); assert (!leafNode.contains(item)); // Must not insert same thing more than once! leafNode.safeInsert(item, storage); }
@Override public void updateNode(Instance inst) { super.updateDistribution(inst); }