public Node(int data, Node left, Node right) { this.data = data; this.left = left; this.right = right; if (left == null && right == null) setDepth(1); else if (left == null) setDepth(right.getDepth() + 1); else if (right == null) setDepth(left.getDepth() + 1); else setDepth(Math.max(left.getDepth(), right.getDepth()) + 1); }
public void evalDepth(Node root) { if (root.getDepth() != -1) return; if (root.getChildren() == null) { root.setDepth(0); return; } ArrayList<Node> children = root.getChildren(); int depthChildren = -1; for (Node node : children) { if (node.getDepth() < 0) this.evalDepth(node); if (depthChildren > node.getDepth() || depthChildren == -1) depthChildren = node.getDepth(); } root.setDepth(depthChildren + 1); return; }
/** * Expands the node's state therefore generating a list of successor nodes. Expansion takes place * by invoking the problem's operators apply() method on the node's state. * * @param node node whose state is to be expanded. * @param problem problem to solve * @param generatedStates List<State> the nodes that are in the frontier of the search algorithm. * @param expandedStates List<State> the states that have been expended so far along the search * process. * @return List<Node> containing the successor nodes. */ public List<Node> expand( Node node, Problem problem, List<State> generatedStates, List<State> expandedStates) { List<Node> successorNodes = new ArrayList<Node>(); Node successorNode = null; State currentState = null; State successorState = null; // If neither node or problem are null if (node != null && problem != null) { // Get the node's state. currentState = node.getState(); // Remove current state from the list of generated states. generatedStates.remove(currentState); // Insert current state to the list of generated states. expandedStates.add(currentState); // If the state is not null if (currentState != null) { // Go over the list of problem operators for (Operator operator : problem.getOperators()) { // Apply the operator to the current state successorState = operator.apply(currentState); // If the operator has been successfully applied and the resulting successor // state hadn't been previously generated nor expanded if (successorState != null && !generatedStates.contains(successorState) && !expandedStates.contains(successorState)) { // make a new node to contain the new successor state successorNode = new Node(successorState); successorNode.setOperator(operator.getName()); successorNode.setParent(node); successorNode.setDepth(node.getDepth() + 1); // add the newly created node to the list of successor nodes. successorNodes.add(successorNode); // Insert current successor State to the list of generated states generatedStates.add(successorState); } } } } return successorNodes; }
// IconFocusedMethods public static void merge( Node currentNode, JoeTree tree, OutlineLayoutManager layout, boolean withSpaces) { JoeNodeList nodeList = tree.getSelectedNodes(); // Get merged text StringBuffer buf = new StringBuffer(); boolean didMerge = false; if (withSpaces) { for (int i = 0, limit = nodeList.size(); i < limit; i++) { Node node = nodeList.get(i); // Skip if node is not editable if (!node.isEditable()) { continue; } didMerge = true; node.getMergedValueWithSpaces(buf, i); } } else { for (int i = 0, limit = nodeList.size(); i < limit; i++) { Node node = nodeList.get(i); // Skip if node is not editable if (!node.isEditable()) { continue; } didMerge = true; node.getMergedValue(buf); } } // It's possible all nodes were read-only. If so then abort. if (!didMerge) { return; } // Get youngest editable node Node youngestNode = null; for (int i = 0, limit = nodeList.size(); i < limit; i++) { Node node = nodeList.get(i); if (node.isEditable()) { youngestNode = node; break; } } // Abort if no editable nodes found. if (youngestNode == null) { return; } Node parent = youngestNode.getParent(); CompoundUndoableReplace undoable = new CompoundUndoableReplace(parent); Node newNode = new NodeImpl(tree, buf.toString()); newNode.setDepth(youngestNode.getDepth()); newNode.setCommentState(youngestNode.getCommentState()); undoable.addPrimitive(new PrimitiveUndoableReplace(parent, youngestNode, newNode)); // Iterate over the remaining selected nodes deleting each one int mergeCount = 1; for (int i = 0, limit = nodeList.size(); i < limit; i++) { Node node = nodeList.get(i); // Abort if node is not editable if (!node.isEditable() || node == youngestNode) { continue; } undoable.addPrimitive(new PrimitiveUndoableReplace(parent, node, null)); mergeCount++; } if (!undoable.isEmpty()) { if (withSpaces) { if (mergeCount == 1) { undoable.setName("Merge Node with Spaces"); } else { undoable.setName( new StringBuffer() .append("Merge ") .append(mergeCount) .append(" Nodes with Spaces") .toString()); } } else { if (mergeCount == 1) { undoable.setName("Merge Node"); } else { undoable.setName( new StringBuffer().append("Merge ").append(mergeCount).append(" Nodes").toString()); } } tree.getDocument().getUndoQueue().add(undoable); undoable.redo(); } return; }