Skip to content

Commit

Permalink
fix: add child incre parent length
Browse files Browse the repository at this point in the history
  • Loading branch information
tusik committed Jan 25, 2019
1 parent 69cc588 commit bdae7a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/java/cx/by/HappyTree/TreeNode.java
Expand Up @@ -43,7 +43,7 @@ public boolean setParent(Object node) {
this.parent = node;
Object tmp = node;
while (null != ((TreeNode) tmp).parent()){
tmp = root.parent();
tmp = ((TreeNode) tmp).parent();
}
if(tmp instanceof HappyTree)
root = (HappyTree) tmp;
Expand Down Expand Up @@ -80,8 +80,6 @@ public ArrayList<TreeNode> getAllChild() {
resList.add((TreeNode) node);
}
return resList;
}else {

}
return null;
}
Expand All @@ -94,8 +92,6 @@ public ArrayList<TreeNode> getAllChild(TreeNode current) {
resList.add((TreeNode) node);
}
return resList;
}else {

}
return null;
}
Expand Down Expand Up @@ -141,9 +137,16 @@ public boolean addChild(Object... node) {
for (TreeNode tree:children) {
if(maxLen<tree.length())maxLen=tree.length();
}
for(int i = -1; i<(((TreeNode) item).length()-maxLen);i++){
incrLength();
if(parent!=null&&((parent().length-this.length)==1||(parent().length-this.length)<=maxLen)){
for(int i = -1; i<(((TreeNode) item).length()-maxLen);i++){
incrLength();
}
}else {
for(int i = -1; i<(((TreeNode) item).length()-maxLen);i++){
length++;
}
}

children.add((TreeNode) item);
((TreeNode)item).setParent(this);
}else {
Expand Down Expand Up @@ -204,7 +207,7 @@ public Object contain() {
@Override
public void incrLength() {
length++;
if(null!=parent){
if(null!=parent()){
((TreeNode)parent).incrLength();
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/MainTest.java
Expand Up @@ -54,8 +54,14 @@ public void A_addChildTest(){
TestCase.assertNull(node2.getAllChild());
TestCase.assertNull(node1.getAllChild(node2));
buildData();
TreeNode node2_2 = new TreeNode();
node2.addChild(node2_2);
TestCase.assertEquals(happyTree.length(),5);
TestCase.assertEquals(happyTree.children().size(),3);
TestCase.assertFalse(node2.addChild("asd"));
TreeNode node1_1_1_1_1 = new TreeNode();
node1_1_1_1.addChild(node1_1_1_1_1);
TestCase.assertEquals(happyTree.length(),6);
}
@Test
public void B_getAllChildTest(){
Expand Down Expand Up @@ -100,6 +106,7 @@ public void G_getAllChildTest(){
buildData();
List<TreeNode>list = node1.getAllChild(node2);
TestCase.assertEquals(list.size(),1);
TestCase.assertEquals(node1.findAll("node1_1").get(0),node1_1);
}
@Test
public void H_clearTest(){
Expand All @@ -114,4 +121,10 @@ public void I_splitTest(){
TestCase.assertEquals(happyTree.split(node1),node1);
TestCase.assertEquals(happyTree.children().size(),2);
}
@Test
public void J_equalTest(){
TreeNode node1 = new TreeNode();
TreeNode node2 = new TreeNode();
TestCase.assertEquals(true,node1.equals(node2));
}
}

0 comments on commit bdae7a9

Please sign in to comment.