以下是我用java实现数据结构中的二叉树
package com.husiwang.BinaryTree; /** * Created by SiwangHu on 2015/2/3. */ public class TreeNode { private int Key; //编码 private Object Data; //数据缓存区 private TreeNode LeftChild; //左孩子 private TreeNode RightChild; //右孩子 private boolean Visit; //访问标记 public TreeNode(){ Key=0; Data=null; LeftChild=null; RightChild=null; Visit=false; } public TreeNode(Object data) { Key=0; Data = data; LeftChild=null; RightChild=null; Visit=false; } public TreeNode(Object data,TreeNode leftChild,TreeNode rightChild){ Key=0; Data=data; LeftChild=leftChild; RightChild=rightChild; Visit=false; } public TreeNode(int key,Object data,TreeNode leftChild,TreeNode rightChild){ Key=key; Data=data; LeftChild=leftChild; RightChild=rightChild; Visit=false; } public TreeNode(boolean visit, TreeNode rightChild, TreeNode leftChild, Object data, int key) { Visit = visit; RightChild = rightChild; LeftChild = leftChild; Data = data; Key = key; } public int getKey() { return Key; } public Object getData() { return Data; } public TreeNode getLeftChild() { return LeftChild; } public TreeNode getRightChild() { return RightChild; } public boolean isVisit() { return Visit; } public void setKey(int key) { Key = key; } public void setData(Object data) { Data = data; } public void setLeftChild(TreeNode leftChild) { LeftChild = leftChild; } public void setRightChild(TreeNode rightChild) { RightChild = rightChild; } public void setVisit(boolean visit) { Visit = visit; } @Override public String toString() { return "TreeNode{" + "Key=" + Key + ", Data=" + Data + ", LeftChild=" + LeftChild + ", RightChild=" + RightChild + ", Visit=" + Visit + '}'; } } package com.husiwang.BinaryTree; /** * Created by SiwangHu on 2015/2/3. */ public class BinaryTree { private TreeNode Root; //根结点 private Visit Visited; //访问结点 public BinaryTree() { Visited=new Visit(); Root=null; } public BinaryTree(TreeNode root) { Visited=new Visit(); Root = root; } public TreeNode getRoot() { return Root; } public void setRoot(TreeNode root) { Root = root; } public boolean IsEmpty(){ if(Root==null) return true; else return false; } public int getHeight(){ return getHeight(Root); } public int getHeight(TreeNode root){ if(root==null) return 0; else{ int L=getHeight(root.getLeftChild()); int R=getHeight(root.getRightChild()); return (L<R)?(R+1):(L+1); } } public int getLength(){ return getLength(Root); } public int getLength(TreeNode root){ if(root==null) return 0; else{ return 1+getLength(root.getLeftChild())+getLength(root.getRightChild()); } } public TreeNode getParent(TreeNode node){ return (Root==null||Root==node)?null:getParent(Root,node); } public TreeNode getParent(TreeNode root,TreeNode node){ if(root==null) return null; else{ if(root.getLeftChild()==node||root.getRightChild()==node) return root; else{ TreeNode temp; if((temp=getParent(root.getLeftChild(),node))!=null) return temp; else{ return getParent(root.getRightChild(),node); } } } } public TreeNode getLeftNode(TreeNode node){ return (node==null)?null:node.getLeftChild(); } public TreeNode getRightNode(TreeNode node){ return (node==null)?null:node.getRightChild(); } public void DestoryTree(){ DestoryTree(Root); } public void DestoryTree(TreeNode root){ if(root!=null){ DestoryTree(root.getLeftChild()); DestoryTree(root.getRightChild()); root=null; } } public void preOrderTree(){ if(Root!=null){ preOrderTree(Root,Visited); } } public void preOrderTree(TreeNode root,Visit Visited){ if(root!=null){ Visited.visited(root); if(root.getLeftChild()!=null) preOrderTree(root.getLeftChild(),Visited); if(root.getRightChild()!=null) preOrderTree(root.getRightChild(),Visited); } } public void inOrderTree(){ if(Root!=null){ inOrderTree(Root, Visited); } } public void inOrderTree(TreeNode root,Visit Visited){ if(root!=null){ if(root.getLeftChild()!=null) inOrderTree(root.getLeftChild(),Visited); Visited.visited(root); if(root.getRightChild()!=null) inOrderTree(root.getRightChild(),Visited); } } public void postOrderTree(){ if(Root!=null){ postOrderTree(Root, Visited); } } public void postOrderTree(TreeNode root,Visit Visited){ if(root!=null){ if(root.getLeftChild()!=null) postOrderTree(root.getLeftChild(),Visited); if(root.getRightChild()!=null) postOrderTree(root.getRightChild(),Visited); Visited.visited(root); } } } package com.husiwang.BinaryTree; /** * Created by SiwangHu on 2015/2/3. */ //父类引用指向子类,通过继承该类可实现回调函数 public class Visit { public void visited(TreeNode node){ System.out.println(node.toString()); } }