1,抽象数据类型:
public interface BinaryTree {
public boolean isEmpty();
public Object root();
public void makeTree(Object root, Object left, Object right);
public BinaryTree removeLeftSubtree();
public BinaryTree removeRightSubtree();
public void preOrder(Method visit);
public void inOrder(Method visit);
public void postOrder(Method visit);
public void levelOrder(Method visit);
}
2,树节点定义:
public class BinaryTreeNode {
Object element;
BinaryTreeNode leftChild; // left subtree
BinaryTreeNode rightChild; // right subtree
public BinaryTreeNode() {
}
public BinaryTreeNode(Object theElement) {
element = theElement;
}
public BinaryTreeNode(Object theElement, BinaryTreeNode theleftChild,
BinaryTreeNode therightChild) {
element = theElement;
leftChild = theleftChild;
rightChild = therightChild;
}
public BinaryTreeNode getLeftChild() {
return leftChild;
}
public BinaryTreeNode getRightChild() {
return rightChild;
}
public Object getElement() {
return element;
}
public void setLeftChild(BinaryTreeNode theLeftChild) {
leftChild = theLeftChild;
}
public void setRightChild(BinaryTreeNode theRightChild) {
rightChild = theRightChild;
}
public void setElement(Object theElement) {
element = theElement;
}
public String toString() {
return element.toString();
}
}
3,实现
public class LinkedBinaryTree implements BinaryTree {
BinaryTreeNode root; // root node
static Method visit; // visit method to use during a traversal
static Object[] visitArgs = new Object[1];
// parameters of visit method
static int count; // counter
static Class[] paramType = { BinaryTreeNode.class };
// type of parameter for visit
static Method theAdd1; // method to increment count by 1
static Method theOutput; // method to output node element
// method to initialize class data members
static {
try {
Class lbt = LinkedBinaryTree.class;
theAdd1 = lbt.getMethod("add1", paramType);
theOutput = lbt.getMethod("output", paramType);
} catch (Exception e) {
}
}
public static void output(BinaryTreeNode t) {
System.out.print(t.element + " ");
}
public static void add1(BinaryTreeNode t) {
count++;
}
public boolean isEmpty() {
return root == null;
}
public Object root() {
return (root == null) ? null : root.element;
}
public void makeTree(Object root, Object left, Object right) {
this.root = new BinaryTreeNode(root, ((LinkedBinaryTree) left).root,
((LinkedBinaryTree) right).root);
}
public BinaryTree removeLeftSubtree() {
if (root == null)
throw new IllegalArgumentException("tree is empty");
LinkedBinaryTree leftSubtree = new LinkedBinaryTree();
leftSubtree.root = root.leftChild;
root.leftChild = null;
return (BinaryTree) leftSubtree;
}
public BinaryTree removeRightSubtree() {
if (root == null)
throw new IllegalArgumentException("tree is empty");
LinkedBinaryTree rightSubtree = new LinkedBinaryTree();
rightSubtree.root = root.rightChild;
root.rightChild = null;
return (BinaryTree) rightSubtree;
}
public void preOrder(Method visit) {
this.visit = visit;
thePreOrder(root);
}
static void thePreOrder(BinaryTreeNode t) {
if (t != null) {
visitArgs[0] = t;
try {
visit.invoke(null, visitArgs);
} // visit tree root
catch (Exception e) {
System.out.println(e);
}
thePreOrder(t.leftChild); // do left subtree
thePreOrder(t.rightChild); // do right subtree
}
}
public void inOrder(Method visit) {
this.visit = visit;
theInOrder(root);
}
static void theInOrder(BinaryTreeNode t) {
if (t != null) {
theInOrder(t.leftChild); // do left subtree
visitArgs[0] = t;
try {
visit.invoke(null, visitArgs);
} // visit tree root
catch (Exception e) {
System.out.println(e);
}
theInOrder(t.rightChild); // do right subtree
}
}
public void postOrder(Method visit) {
this.visit = visit;
thePostOrder(root);
}
static void thePostOrder(BinaryTreeNode t) {
if (t != null) {
thePostOrder(t.leftChild); // do left subtree
thePostOrder(t.rightChild); // do right subtree
visitArgs[0] = t;
try {
visit.invoke(null, visitArgs);
} // visit tree root
catch (Exception e) {
System.out.println(e);
}
}
}
public void levelOrder(Method visit) {
ArrayQueue q = new ArrayQueue();
BinaryTreeNode t = root;
while (t != null) {
visitArgs[0] = t;
try {
visit.invoke(null, visitArgs);
} // visit tree root
catch (Exception e) {
System.out.println(e);
}
// put t's children on queue
if (t.leftChild != null)
q.put(t.leftChild);
if (t.rightChild != null)
q.put(t.rightChild);
// get next node to visit
t = (BinaryTreeNode) q.remove();
}
}
public void preOrderOutput() {
preOrder(theOutput);
}
public void inOrderOutput() {
inOrder(theOutput);
}
public void postOrderOutput() {
postOrder(theOutput);
}
public void levelOrderOutput() {
levelOrder(theOutput);
}
public int size() {
count = 0;
preOrder(theAdd1);
return count;
}
public int height() {
return theHeight(root);
}
static int theHeight(BinaryTreeNode t) {
if (t == null)
return 0;
int hl = theHeight(t.leftChild); // height of left subtree
int hr = theHeight(t.rightChild); // height of right subtree
if (hl > hr)
return ++hl;
else
return ++hr;
}
}