二叉树的java实现
平时对二叉树的概念遗忘颇多,故而花费几个小时的时间,重新过一遍,方便自己以后在查看jdk源码的时候更好的理解,以及在以后的可能的数据建模的过程中能更准确的描绘数据原型。
代码实现
package jdk.collection.tree;
import java.util.LinkedList;
import java.util.Queue;
/**
*
* a BinaryTree which his left is less then node,and node leff then right,the same to others
* <p>
*
* @version 1.0.0,2017-3-19
* @author xiongyouzheng
* @since 1.0.0
*/
public class BinaryTree implements BinaryTreeInterface<Integer> {
private Node<Integer> root;
@Override
public void display() {
}
public Node<Integer> getRoot() {
return root;
}
public void setRoot(Node<Integer> root) {
this.root = root;
}
@Override
public void add(Integer data) {
if (null == root) {// empty
root = new Node<Integer>(data);
return;
}
if (null == root.getLeft() && null == root.getRight()) {// only one ele
if (data.intValue() < root.getData().intValue()) {
root.setLeft(new Node<Integer>(data));
return;
} else {
root.setRight(new Node<Integer>(data));
return;
}
}
Node<Integer> node = root;
while (null != node) {
// compare with root first
if (data.intValue() < node.getData().intValue()) {
// goes to left
if (null != node.getLeft()) {
node = node.getLeft();
} else {
node.setLeft(new Node<Integer>(data));// find it ,insert
return;
}
} else {
if (data.intValue() > node.getData().intValue()) {
// goes to right
if (null != node.getRight()) {
node = node.getRight();
} else {
node.setRight(new Node<Integer>(data));// find it ,insert
return;
}
} else {
throw new IllegalArgumentException("data is same to root!!!");
}
}
}
}
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
binaryTree.add(5);
binaryTree.add(10);
binaryTree.add(3);
binaryTree.add(4);
binaryTree.add(9);
// binaryTree.displayNLRRecrusive(binaryTree.getRoot());
binaryTree.displayNLR(binaryTree.getRoot());
}
public void displayNLRRecrusive(Node<Integer> node) {
System.out.println(node.getData());
if (null != node.getLeft()) {
displayNLRRecrusive(node.getLeft());
}
if (null != node.getRight()) {
displayNLRRecrusive(node.getRight());
}
}
// use no recursive method to display the tree
public void displayNLR(Node<Integer> node) {
Queue<Node<Integer>> queue = new LinkedList<Node<Integer>>();
queue.add(node);// push the root to queue
while (!queue.isEmpty()) {// display until queue is not empty
Node<Integer> data = queue.poll();// pop the root data
System.out.println(data.getData());
if (null != data.getLeft()) {// push the left to queue
queue.add(data.getLeft());
}
if (null != data.getRight()) {// push the right to queue
queue.add(data.getRight());
}
}
}
@Override
public String toString() {
return "BinaryTree [root=" + root + "]";
}
}
interface BinaryTreeInterface<T> {
void display();
void add(T data);
}
class Node<T> {
private T data;
private Node<T> left;
private Node<T> right;
public Node() {
// default constructor
}
public Node(T data) {
this.data = data;
}
void add(T data) {
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getLeft() {
return left;
}
public void setLeft(Node<T> left) {
this.left = left;
}
public Node<T> getRight() {
return right;
}
public void setRight(Node<T> right) {
this.right = right;
}
@Override
public String toString() {
return "Node [data=" + data + ", left=" + left + ", right=" + right + "]";
}
}