Skip to content

Commit

Permalink
Merge pull request #8 from dutekt/master
Browse files Browse the repository at this point in the history
二叉树
  • Loading branch information
zavier authored Feb 25, 2017
2 parents 3a9761a + 4a04135 commit 676f50a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@

public class BinaryTreeNode {

private Object data;
private Integer data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public BinaryTreeNode insert(Object o) {
return null;
public BinaryTreeNode insert(Integer o) {
if (o <= data) {
if (left == null) {
left = new BinaryTreeNode(o);
return left;
}
return left.insert(o);
} else {
if (right == null) {
right = new BinaryTreeNode(o);
return right;
}
return right.insert(o);
}
}

public Object getData() {
public BinaryTreeNode(Integer data) {
this.data = data;
}

public Integer getData() {
return data;
}

public void setData(Object data) {
public void setData(Integer data) {
this.data = data;
}

Expand All @@ -34,4 +50,8 @@ public void setRight(BinaryTreeNode right) {
this.right = right;
}

@Override
public String toString() {
return data + " " + left + " " + right;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.coding2017.basic;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Created by kaitao.li on 17/2/24.
*/
public class BinaryTreeNodeTest {

@Test
public void insert() throws Exception {
BinaryTreeNode binaryTreeNode = new BinaryTreeNode(5);
binaryTreeNode.insert(4);
binaryTreeNode.insert(6);
binaryTreeNode.insert(5);
assertTrue(binaryTreeNode.getLeft().getData() == 4);
assertTrue(binaryTreeNode.getRight().getData() == 6);
assertTrue(binaryTreeNode.getLeft().getRight().getData() == 5);
System.out.println(binaryTreeNode);
}

}

0 comments on commit 676f50a

Please sign in to comment.