业余学习整理
public class BinaryTreeNode<T> {
private T data;
private BinaryTreeNode<T> leftChild;
private BinaryTreeNode<T> rightChild;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BinaryTreeNode<T> getLeftChild() {
return leftChild;
}
public void setLeftChild(BinaryTreeNode<T> leftChild) {
this.leftChild = leftChild;
}
public BinaryTreeNode<T> getRightChild() {
return rightChild;
}
public void setRightChild(BinaryTreeNode<T> rightChild) {
this.rightChild = rightChild;
}
}
public class BinaryTree<T> {
private BinaryTreeNode<T> root;
public BinaryTree(){
this.root = null;
}
public BinaryTree(BinaryTreeNode<T> root,BinaryTreeNode<T> leftChild,BinaryTreeNode<T> rightChild){
this.root = new BinaryTreeNode<T>();
this.root.setLeftChild(leftChild);
this.root.setRightChild(rightChild);
}
public BinaryTreeNode<T> getRoot() {
return root;
}
public void setRoot(BinaryTreeNode<T> root) {
this.root = root;
}
public void print(){
this.print(this.root);
}
private void print(BinaryTreeNode<?> node){
if(null == node){
return;
}
System.out.print(node.getData());
this.print(node.getLeftChild());
this.print(node.getRightChild());
}
public interface BinaryTreeNodeSort{
public void run(BinaryTreeNode<?> node);
public void print(Object data);
}
public static abstract class AbsBinaryTreeNodeSort implements BinaryTreeNodeSort{
@Override
public void print(Object data) {
if(null == data){
return;
}
System.out.print(data);
}
}
public enum Traversal{
//分层
LEVEL(new AbsBinaryTreeNodeSort(){
@Override
public void run(BinaryTreeNode<?> node) {
if(null == node){
return;
}
Queue<BinaryTreeNode<?>> queue = new LinkedList<BinaryTreeNode<?>>();
queue.add(node);
while(!queue.isEmpty()){
BinaryTreeNode<?> n = queue.remove();
this.print(n.getData());
if(null != n.getLeftChild()){
queue.add(n.getLeftChild());
}
if(null != n.getRightChild()){
queue.add(n.getRightChild());
}
}
}
}),
/**
* N currentNode ,L leftNode ,R rightNode
*/
NLR(new AbsBinaryTreeNodeSort(){
@Override
public void run(BinaryTreeNode<?> node) {
if(null == node){return;}
this.print(node.getData());
this.run(node.getLeftChild());
this.run(node.getRightChild());
}
}),
LNR(new AbsBinaryTreeNodeSort(){
@Override
public void run(BinaryTreeNode<?> node) {
if(null == node){return;}
this.run(node.getLeftChild());
this.print(node.getData());
this.run(node.getRightChild());
}
}),
LRN(new AbsBinaryTreeNodeSort(){
@Override
public void run(BinaryTreeNode<?> node) {
if(null == node){return;}
this.run(node.getLeftChild());
this.run(node.getRightChild());
this.print(node.getData());
}
}),
NRL(new AbsBinaryTreeNodeSort(){
@Override
public void run(BinaryTreeNode<?> node) {
if(null == node){return;}
this.print(node.getData());
this.run(node.getRightChild());
this.run(node.getLeftChild());
}
}),
RNL(new AbsBinaryTreeNodeSort(){
@Override
public void run(BinaryTreeNode<?> node) {
if(null == node){return;}
this.run(node.getRightChild());
this.print(node.getData());
this.run(node.getLeftChild());
}
}),
RLN(new AbsBinaryTreeNodeSort(){
@Override
public void run(BinaryTreeNode<?> node) {
if(null == node){return;}
this.run(node.getRightChild());
this.run(node.getLeftChild());
this.print(node.getData());
}
});
private Traversal(BinaryTreeNodeSort sort){
this.sort = sort;
}
private BinaryTreeNodeSort sort;
public BinaryTreeNodeSort getSort() {
return sort;
}
public void setSort(BinaryTreeNodeSort sort) {
this.sort = sort;
}
public void print(BinaryTreeNode<?> node){
this.sort.run(node);
}
}
public static <I> BinaryTreeNode<I> createTreeNode(I data,BinaryTreeNode<I> leftChild,BinaryTreeNode<I> rightChild){
BinaryTreeNode<I> node = new BinaryTreeNode<I>();
node.setData(data);
if(null != leftChild){
node.setLeftChild(leftChild);
}
if(null != rightChild){
node.setRightChild(rightChild);
}
return node;
}
public static void main(String[] args) {
BinaryTreeNode<String> d = BinaryTree.createTreeNode("D",null,null);
BinaryTreeNode<String> e = BinaryTree.createTreeNode("E",null,null);
BinaryTreeNode<String> f = BinaryTree.createTreeNode("F",null,null);
BinaryTreeNode<String> b = BinaryTree.createTreeNode("B",d,e);
BinaryTreeNode<String> c = BinaryTree.createTreeNode("C",null,f);
BinaryTreeNode<String> a = BinaryTree.createTreeNode("A",b,c);
BinaryTree<String> tree = new BinaryTree<String>();
tree.setRoot(a);
tree.print();
System.out.println();
System.out.print("LEVEL:");
Traversal.LEVEL.print(tree.getRoot());
System.out.println();
System.out.print("NLR:");
Traversal.NLR.print(tree.getRoot());
System.out.println();
System.out.print("LNR:");
Traversal.LNR.print(tree.getRoot());
System.out.println();
System.out.print("LRN:");
Traversal.LRN.print(tree.getRoot());
System.out.println();
System.out.print("NRL:");
Traversal.NRL.print(tree.getRoot());
System.out.println();
System.out.print("RNL:");
Traversal.RNL.print(tree.getRoot());
System.out.println();
System.out.print("RLN:");
Traversal.RLN.print(tree.getRoot());
System.out.println();
}
}
//打印结果
ABDECF
LEVEL:ABCDEF
NLR:ABDECF
LNR:DBEACF
LRN:DEBFCA
NRL:ACFBED
RNL:FCAEBD
RLN:FCEDBA