forked from diliuzuzhanghao/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
group15/1503_1311822904/myCollection/src/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import java.util.Objects; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Integer data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Integer getData() { | ||
return data; | ||
} | ||
public void setData(Integer data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
//TODO | ||
public BinaryTreeNode insert(Integer o){ | ||
if(data==null){ | ||
data=o; | ||
return this; | ||
} | ||
|
||
BinaryTreeNode b=new BinaryTreeNode(); | ||
b.setData(o); | ||
|
||
if(Objects.equals(data, o)){ | ||
return this; | ||
} | ||
if(data<o){ | ||
if(left==null){ | ||
left=b; | ||
return b; | ||
} | ||
left.insert(o); | ||
} | ||
if(data>o){ | ||
if(right==null){ | ||
right=b; | ||
return b; | ||
} | ||
right.insert(o); | ||
} | ||
return b; | ||
} | ||
|
||
public void showAll(){ | ||
if(right!=null){ | ||
right.showAll(); | ||
} | ||
System.out.print(data+" "); | ||
if(left!=null){ | ||
left.showAll(); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
group15/1503_1311822904/myCollection/test/BinaryTreeNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by Administrator on 2017/2/25. | ||
*/ | ||
public class BinaryTreeNodeTest { | ||
|
||
|
||
@Test | ||
public void showAll() throws Exception { | ||
BinaryTreeNode b=new BinaryTreeNode(); | ||
b.insert(4); | ||
b.insert(5); | ||
b.insert(-1); | ||
b.insert(44); | ||
b.insert(34); | ||
b.insert(49); | ||
b.showAll(); | ||
} | ||
|
||
} |