树节点类: package org.accp.ds.tree; public class TreeNode<E>{ public E nodeValue; public TreeNode lChild; public TreeNode rChild; public TreeNode() { // TODO Auto-generated constructor stub this.nodeValue = null; this.lChild = null; this.rChild = null; } public TreeNode(E nodeValue) { super(); this.nodeValue = nodeValue; } public TreeNode(E nodeValue, TreeNode lChild, TreeNode rChild) { super(); this.nodeValue = nodeValue; this.lChild = lChild; this.rChild = rChild; } } 树遍历回调接口: package org.accp.ds.tree; /**树回调接口*/ public interface ITreeCallBack { /**观察节点*/ public <E> void visit(TreeNode<E> element); } 树接口类: package org.accp.ds.tree; public interface ITree { /**先序遍历*/ public void preOrder(ITreeCallBack treeCallBack); /**中序遍历*/ public void inOrder(ITreeCallBack treeCallBack); /**后序遍历*/ public void fllowUp(ITreeCallBack treeCallBack); } 二叉树实现类: package org.accp.ds.tree; /**二叉树*/ public class BinaryTree<E> implements ITree{ TreeNode<E> rootNode; public BinaryTree() { this.rootNode = null; } public BinaryTree(TreeNode<E> rootNode) { this.rootNode = rootNode; } /**后序遍历*/ @Override public void fllowUp(ITreeCallBack treeCallBack) { // TODO Auto-generated method stub if (rootNode!=null) _fllowUp(rootNode, treeCallBack); } /**后序遍历[递归函数]*/ private void _fllowUp(TreeNode<E> e,ITreeCallBack treeCallBack){ if (e==null) return; if (e.lChild!=null)//访问左子树 _fllowUp(e.lChild, treeCallBack); if (e.rChild!=null)//访问右子树 _fllowUp(e.rChild, treeCallBack); if (treeCallBack!=null)//访问当前节点 treeCallBack.visit(e); } /**中序遍历*/ @Override public void inOrder(ITreeCallBack treeCallBack) { // TODO Auto-generated method stub if (rootNode!=null){ _inOrder(rootNode, treeCallBack); } } /**中序遍历[递归函数]*/ private void _inOrder(TreeNode<E> e,ITreeCallBack treeCallBack){ if (e==null) return; if (e.lChild!=null)//访问左子树 _inOrder(e.lChild, treeCallBack); if (treeCallBack!=null)//访问当前节点 treeCallBack.visit(e); if (e.rChild!=null)//访问有子树 _inOrder(e.rChild, treeCallBack); } /**先序遍历*/ @Override public void preOrder(ITreeCallBack treeCallBack) { // TODO Auto-generated method stub if (rootNode!=null){ _preOrder(rootNode, treeCallBack); } } /**先序遍历[递归函数]*/ private void _preOrder(TreeNode<E> e,ITreeCallBack treeCallBack){ if (e==null){ return; } if (treeCallBack!=null){//访问当前节点 treeCallBack.visit(e);//通过回调观察这个节点 } if (e.lChild!=null){//访问左子节点 _preOrder(e.lChild,treeCallBack); } if (e.rChild!=null){//访问右子节点 _preOrder(e.rChild, treeCallBack); } } } 测试类: package org.accp.ds.tree.utest; import org.accp.ds.tree.BinaryTree; import org.accp.ds.tree.ITree; import org.accp.ds.tree.ITreeCallBack; import org.accp.ds.tree.TreeNode; public class BinaryTreeTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //构造一颗完全二叉树如下所示 /* * 10 * 20 30 * 40 50 60 70 * 80 90 * * */ TreeNode<Integer> n10 = new TreeNode<Integer>(10); TreeNode<Integer> n20 = new TreeNode<Integer>(20); TreeNode<Integer> n30 = new TreeNode<Integer>(30); TreeNode<Integer> n40 = new TreeNode<Integer>(40); TreeNode<Integer> n50 = new TreeNode<Integer>(50); TreeNode<Integer> n60 = new TreeNode<Integer>(60); TreeNode<Integer> n70 = new TreeNode<Integer>(70); TreeNode<Integer> n80 = new TreeNode<Integer>(80); TreeNode<Integer> n90 = new TreeNode<Integer>(90); n10.lChild = n20; n10.rChild = n30; n20.lChild = n40; n20.rChild = n50; n30.lChild = n60; n30.rChild = n70; n40.lChild = n80; n40.rChild = n90; //构造二叉树对象 ITree tree = new BinaryTree<Integer>(n10); //先序遍历示范 // tree.PreOrder(new ITreeCallBack() { // // @Override // public <E> void visit(TreeNode<E> element) { // // TODO Auto-generated method stub // StringBuilder line = new StringBuilder("当前节点"); // line.append(element.nodeValue); // if (element.lChild!=null){ // line.append("[左子节点:"+element.lChild.nodeValue+"]"); // } // if (element.rChild!=null){ // line.append("[右子节点:"+element.rChild.nodeValue+"]"); // } // System.out.println(line.toString()); // } // }); //中序遍历示范 // tree.inOrder(new ITreeCallBack() { // // @Override // public <E> void visit(TreeNode<E> element) { // // TODO Auto-generated method stub // StringBuilder line = new StringBuilder("当前节点"); // line.append(element.nodeValue); // if (element.lChild!=null){ // line.append("[左子节点:"+element.lChild.nodeValue+"]"); // } // if (element.rChild!=null){ // line.append("[右子节点:"+element.rChild.nodeValue+"]"); // } // System.out.println(line.toString()); // } // }); //后序遍历示范 tree.fllowUp(new ITreeCallBack() { @Override public <E> void visit(TreeNode<E> element) { // TODO Auto-generated method stub StringBuilder line = new StringBuilder("当前节点"); line.append(element.nodeValue); if (element.lChild!=null){ line.append("[左子节点:"+element.lChild.nodeValue+"]"); } if (element.rChild!=null){ line.append("[右子节点:"+element.rChild.nodeValue+"]"); } System.out.println(line.toString()); } }); } }