private EvaluationNode convertNodeToEvalNode(ArrayList<EvaluationNode> evalNodes, Node node) { for (int i = 0; i < evalNodes.size(); i++) { EvaluationNode theNode = evalNodes.get(i); if (theNode.node.getValue() == node.getValue()) { return theNode; } } return null; }
private EvaluationNode buildEvaluationTree(ArrayList<Node> nodes, ArrayList<EdgeParent> edges) { @SuppressWarnings("unchecked") ArrayList<Node> noParentNodes = (ArrayList<Node>) nodes.clone(); @SuppressWarnings("unchecked") ArrayList<Node> unConnectedNodes = (ArrayList<Node>) nodes.clone(); for (int i = 0; i < edges.size(); i++) { EdgeParent edge = edges.get(i); // finding the removed node/nodes if (unConnectedNodes.contains(edge.getN1())) unConnectedNodes.remove(edge.getN1()); if (unConnectedNodes.contains(edge.getN2())) unConnectedNodes.remove(edge.getN2()); noParentNodes.remove(edge.getN2()); } // returns null if more than one node is disconnected from the heap if (unConnectedNodes.size() > 1) { errorMessage = "Your tree is incomplete. Go back and add " + " the necessary edges to complete the tree."; return null; } // taking the removed nodes out of the noParentNodes list. for (Node n : unConnectedNodes) { noParentNodes.remove(n); } Node rootNode = noParentNodes.get(0); EvaluationNode rootEvalNode = null; Node currNode = null; Node leftNode = null; Node parentNode = null; Node rightNode = null; for (int j = 0; j < nodes.size(); j++) { currNode = nodes.get(j); for (int i = 0; i < edges.size(); i++) { EdgeParent edge = edges.get(i); if (currNode.getValue() == edge.getN1().getValue()) { if (currNode.getLeft() > edge.getN2().getLeft()) { leftNode = edge.getN2(); } else if (currNode.getLeft() < edge.getN2().getLeft()) { rightNode = edge.getN2(); } } else if (currNode.getValue() == edge.getN2().getValue()) { parentNode = edge.getN1(); } } EvaluationNode evalNode = new EvaluationNode(currNode, parentNode, leftNode, rightNode); treeNodes.add(evalNode); parentNode = null; leftNode = null; rightNode = null; if (currNode.getValue() == rootNode.getValue()) { rootEvalNode = evalNode; } } for (int i = 0; i < treeNodes.size(); i++) { EvaluationNode n = treeNodes.get(i); System.out.print("Val: " + n.node.getValue()); if (n.parent != null) System.out.print(" Par: " + n.parent.getValue()); if (n.left != null) System.out.print(" Left: " + n.left.getValue()); if (n.right != null) System.out.print(" Right: " + n.right.getValue()); System.out.println(""); } return rootEvalNode; }