Skip to content

Commit

Permalink
add Btree
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ-Chen committed Mar 12, 2017
1 parent 751e08b commit 9df1dd3
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,48 @@ public void setRight(BinaryTreeNode right) {
}

public BinaryTreeNode insert(Object o) {
return null;
// 应该只需要实现这个就可以了
int curValue = (Integer) this.getData();
int insertValue = (Integer) o;

BinaryTreeNode newNode = new BinaryTreeNode();
newNode.setData(o);

if (curValue > insertValue) {
if (this.getLeft() != null) {
return this.getLeft().insert(o);
} else {
this.setLeft(newNode);
return this;
}
} else{
if (this.getRight() != null) {
return this.getRight().insert(o);
} else {
this.setRight(newNode);
return this;
}
}
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.getData()).append("\n");
sb.append(this.getLeft()).append("<--").append(this.getData()).append("\n");
sb.append(this.getData()).append("-->").append(this.getRight()).append("\n");
return sb.toString();
}

public static void main(String[] args) {
BinaryTreeNode btn = new BinaryTreeNode();
btn.setData(5);
// btn.insert(5);
btn.insert(7);
btn.insert(8);
btn.insert(9);
btn.insert(4);

System.err.println(btn);
}

}

0 comments on commit 9df1dd3

Please sign in to comment.