private void removeThisFirstLevelChildFromSubtree(Node node) throws NodeDoesNotExistException { if (node.getPosition().equals(Constants.POSITION.RIGHT.toString()) && this.isChildAlreadyExists(node, this.getRight())) { this.getRight().remove(node.getId()); } else if (node.getPosition().equals(Constants.POSITION.LEFT.toString()) && this.isChildAlreadyExists(node, this.getLeft())) { this.getLeft().remove(node.getId()); } else throw new NodeDoesNotExistException(); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; return getId().equals(node.getId()); }
public Node addThisChild(Node node, int index) { ArrayList<String> siblings = this.getChildSubTree(); if (!this.isChildAlreadyExists(node, siblings)) { siblings.add(index, node.getId()); } node.setParentId(this.getId()); if (this.isARoot()) { this.addThisFirstLevelChildToSubTree(node); } return this; }
public Node(String id, String text, Node parent, String rootId, int index) { this._id = id; this.name = text; this.left = new ArrayList<>(); this.right = new ArrayList<>(); this.childSubTree = new ArrayList<>(); this.parentId = (parent != null) ? parent.getId() : null; this.rootId = rootId; this.depth = (parent != null) ? parent.getDepth() + 1 : 0; this.index = index; this.position = (parent != null) ? ((this.rootId.equals(this.parentId) ? this.getFirstLevelChildPosition() : parent.getPosition())) : null; }
public Node removeThisChild(Node node) throws NodeDoesNotExistException { if (this.isARoot()) { this.removeThisFirstLevelChildFromSubtree(node); } if (this.isChildAlreadyExists(node, this.childSubTree)) this.getChildSubTree().remove(node.getId()); else throw new NodeDoesNotExistException(); return node; }
private void addThisFirstLevelChildToSubTree(Node node) { if (node.getPosition().equals(Constants.POSITION.RIGHT.toString())) { ArrayList<String> rightSubTree = this.getRight(); if (this.isChildAlreadyExists(node, rightSubTree)) rightSubTree.remove(node.getId()); rightSubTree.add(rightSubTree.size(), node.getId()); } if (node.getPosition().equals(Constants.POSITION.LEFT.toString())) { ArrayList<String> leftSubTree = this.getLeft(); if (this.isChildAlreadyExists(node, leftSubTree)) leftSubTree.remove(node.getId()); leftSubTree.add(leftSubTree.size(), node.getId()); } }
private boolean isChildAlreadyExists(Node node, ArrayList<String> siblings) { return siblings.contains(node.getId()); }