Skip to main content
added 7 characters in body
Source Link
seand
  • 2.5k
  • 1
  • 20
  • 29

public void addChild(Links child) { children.add(child); }

public void addChild(Links child)
{
    children.add(child);
}

public boolean hasChild(Links child)
{
    return children.contains(child);
}

public boolean removeChild(Links child)
{
    return children.remove(child);
}

public boolean isLeaf()
{
    return children.size() <= 0;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((nodeName == null) ? 0 : nodeName.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Links other = (Links) obj;
    if (nodeName == null) {
        if (other.nodeName != null)
            return false;
    }
    else if (!nodeName.equals(other.nodeName)) //hint: this only works if nodeName is unique! If not, use a unique static counter
        return false;
    return true;
}

Edit: Just to point out, this code is not for efficiency. It is written for clarity. Do not care about efficiency before you really have to.

public void addChild(Links child) { children.add(child); }

public boolean hasChild(Links child)
{
    return children.contains(child);
}

public boolean removeChild(Links child)
{
    return children.remove(child);
}

public boolean isLeaf()
{
    return children.size() <= 0;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((nodeName == null) ? 0 : nodeName.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Links other = (Links) obj;
    if (nodeName == null) {
        if (other.nodeName != null)
            return false;
    }
    else if (!nodeName.equals(other.nodeName)) //hint: this only works if nodeName is unique! If not, use a unique static counter
        return false;
    return true;
}

Edit: Just to point out, this code is not for efficiency. It is written for clarity. Do not care about efficiency before you really have.

public void addChild(Links child)
{
    children.add(child);
}

public boolean hasChild(Links child)
{
    return children.contains(child);
}

public boolean removeChild(Links child)
{
    return children.remove(child);
}

public boolean isLeaf()
{
    return children.size() <= 0;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((nodeName == null) ? 0 : nodeName.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Links other = (Links) obj;
    if (nodeName == null) {
        if (other.nodeName != null)
            return false;
    }
    else if (!nodeName.equals(other.nodeName)) //hint: this only works if nodeName is unique! If not, use a unique static counter
        return false;
    return true;
}

Edit: Just to point out, this code is not for efficiency. It is written for clarity. Do not care about efficiency before you really have to.

Source Link
tb-
  • 2.3k
  • 12
  • 15

It is not possible to test your classes, because of:

import com.chartis.gp.support.util.BrokerSupportUtil;
import com.chartis.gp.support.vo.Links;
import com.chartis.kernel.user.UserVO;
import com.chartis.kernel.utils.Utils;

But ok, the general idea is ( if we want to use recursion):

remove(child, tree):
    if tree.isLeaf: return false
    if tree.hasChild(child): tree.removeChild(child); return true;
    for all children of tree as subtree:
        if remove(child, subtree): return true;
    return false;

Methods you will most probably need:

public void addChild(Links child) { children.add(child); }

public boolean hasChild(Links child)
{
    return children.contains(child);
}

public boolean removeChild(Links child)
{
    return children.remove(child);
}

public boolean isLeaf()
{
    return children.size() <= 0;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((nodeName == null) ? 0 : nodeName.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Links other = (Links) obj;
    if (nodeName == null) {
        if (other.nodeName != null)
            return false;
    }
    else if (!nodeName.equals(other.nodeName)) //hint: this only works if nodeName is unique! If not, use a unique static counter
        return false;
    return true;
}

Take a look at some of the tree-classes of java to see some typical methods.

And your method could look like:

public static boolean removeChildFromTree(Links child, Links tree)
{
    if (tree.isLeaf()) //we are at a leave, we can not delete any children anymore
        return false;
    if (tree.hasChild(child)) //we found it, quickly delete it
        return tree.removeChild(child);
    //we have to search all children
    for (Links subTree : tree.getChildren())
    {
        if (removeChildFromTree(child, subTree)) //try to delete on subtree
            return true;
    }
    return false;
}

Edit: Just to point out, this code is not for efficiency. It is written for clarity. Do not care about efficiency before you really have.