Java实现二叉树

没有使用父节点,有很多地方不方便。直接贴代码把

插入代码:

public void insert(T value) {
		BNode node = new BNode(value);
		if (node != null)
			insert(node);
	}

	private void insert(BNode node) {
		int cmp = 0;
		BNode pre = null;
		BNode p = this.root;
		while (p != null) {
			pre = p;
			cmp = node.value.compareTo(p.value);
			if (cmp < 0)
				p = p.left;
			else if (cmp > 0)
				p = p.right;
		}
		if (pre == null)
			this.root = node;
		else {
			cmp = node.value.compareTo(pre.value);
			if (cmp < 0)
				pre.left = node;
			else
				pre.right = node;
		}
	}

删除代码:

public void delete(T value) {
		int cmp = 0;
		BNode par = null;
		BNode cur = root;
		while (cur != null) {
			cmp = value.compareTo(cur.value);
			if (cmp < 0) {
				par = cur;
				cur = cur.left;
			} else if (cmp > 0) {
				par = cur;
				cur = cur.right;
			} else
				break;
		}
		if (cur != null)
			delete(cur, par);
	}

	private void delete(BNode node, BNode par) {
		if (node.left == null)
			tranplant(par, node, node.right);
		else if (node.right == null)
			tranplant(par, node, node.left);
		else {
			BNodePreCur res = minimum(node, node.right);
			BNode pre2 = res.par;
			BNode next = res.next;
			if (node.right != next) {
				tranplant(pre2, next, next.right);
				;
				next.right = node.right;
			}
			tranplant(par, node, next);
			next.left = node.left;
		}

	}

	/**
	 * 将子树u替换成v
	 * 
	 * @param par
	 * @param u
	 * @param v
	 */
	private void tranplant(BNode par, BNode u, BNode v) {
		if (par == null)
			root = v;
		else if (par.left == u)
			par.left = v;
		else
			par.right = v;
	}

	/**
	 * 找到right最左边的结点及其父节点
	 * 
	 * @param pre
	 * @param right
	 * @return
	 */
	private BTree<T>.BNodePreCur minimum(BTree<T>.BNode pre, BTree<T>.BNode right) {
		BNodePreCur res = new BNodePreCur();
		while (right.left != null) {
			pre = right;
			right = right.left;
		}
		res.par = pre;
		res.next = right;
		return res;
	}

完整代码:

package algorithm;

public class BTree<T extends Comparable<T>> {
	class BNodePreCur {
		BNode par;
		BNode next;
	}

	class BNode {
		T value;
		BNode left;
		BNode right;

		public BNode(T value) {
			this.value = value;
		}
	}

	BNode root;

	public void insert(T value) {
		BNode node = new BNode(value);
		if (node != null)
			insert(node);
	}

	private void insert(BNode node) {
		int cmp = 0;
		BNode pre = null;
		BNode p = this.root;
		while (p != null) {
			pre = p;
			cmp = node.value.compareTo(p.value);
			if (cmp < 0)
				p = p.left;
			else if (cmp > 0)
				p = p.right;
		}
		if (pre == null)
			this.root = node;
		else {
			cmp = node.value.compareTo(pre.value);
			if (cmp < 0)
				pre.left = node;
			else
				pre.right = node;
		}
	}

	public void delete(T value) {
		int cmp = 0;
		BNode par = null;
		BNode cur = root;
		while (cur != null) {
			cmp = value.compareTo(cur.value);
			if (cmp < 0) {
				par = cur;
				cur = cur.left;
			} else if (cmp > 0) {
				par = cur;
				cur = cur.right;
			} else
				break;
		}
		if (cur != null)
			delete(cur, par);
	}

	private void delete(BNode node, BNode par) {
		if (node.left == null)
			tranplant(par, node, node.right);
		else if (node.right == null)
			tranplant(par, node, node.left);
		else {
			BNodePreCur res = minimum(node, node.right);
			BNode pre2 = res.par;
			BNode next = res.next;
			if (node.right != next) {
				tranplant(pre2, next, next.right);
				;
				next.right = node.right;
			}
			tranplant(par, node, next);
			next.left = node.left;
		}

	}

	/**
	 * 将子树u替换成v
	 * 
	 * @param par
	 * @param u
	 * @param v
	 */
	private void tranplant(BNode par, BNode u, BNode v) {
		if (par == null)
			root = v;
		else if (par.left == u)
			par.left = v;
		else
			par.right = v;
	}

	/**
	 * 找到right最左边的结点及其父节点
	 * 
	 * @param pre
	 * @param right
	 * @return
	 */
	private BTree<T>.BNodePreCur minimum(BTree<T>.BNode pre, BTree<T>.BNode right) {
		BNodePreCur res = new BNodePreCur();
		while (right.left != null) {
			pre = right;
			right = right.left;
		}
		res.par = pre;
		res.next = right;
		return res;
	}

	public void inorder_tra() {
		inorder_tra(root);
		System.out.println();
	}

	private void inorder_tra(BNode node) {
		if (node == null)
			return;
		if (node.left != null)
			inorder_tra(node.left);
		System.out.print(node.value + "  ");
		if (node.right != null)
			inorder_tra(node.right);
	}
}

测试类:

package algorithm;

public class BTreeTest {
	public static void main(String[] args) {
		int[] arr = { 12, 3, 54, 2, 465, 5, 46, 8, 6, 4, 1, 7, 9, 10, 11 };
		BTree<Integer> bTree = new BTree<Integer>();
		bTree = buildBTree(bTree, arr);
		bTree.inorder_tra();
		delete(bTree, arr);
	}

	private static void delete(BTree<Integer> bTree, int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			bTree.delete(arr[i]);
			bTree.inorder_tra();
		}
	}

	private static BTree<Integer> buildBTree(BTree<Integer> bTree, int[] arr) {
		for (int i = 0; i < arr.length; i++)
			bTree.insert(arr[i]);
		return bTree;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值