二叉树(BinaryTree)

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;
	}
}

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值