二叉查找树(二叉搜索数)增、删、三种遍历的java实现

实际上,如果不采用换节点的方式,而采用替换数据的方式,操作会更简单,但是为了清楚的表现出算法思想,这里采用换节点的方式

public class BinaryTree {
	class Node{
		int value;
		Node left;
		Node right;
		Node(int value){this.value = value;}
	}
	private Node root;
	public Node getRoot() {
		return this.root;
	}
	// add node
	private void addNode(Node node,Node newNode) {
		if(newNode.value == node.value) return;
		else if(newNode.value < node.value)
			if(node.left == null) {
				node.left = newNode;
				return;
			}else addNode(node.left,newNode);
		else 
			if(node.right == null) {
				node.right = newNode;
				return;
			}else addNode(node.right,newNode);
	}
	public void add(int value) {
	// process empty binary tree
		if(root == null) {
			root = new Node(value);
			return;
		}
		Node newNode = new Node(value);
		addNode(root,newNode);
	}
	public boolean remove(int target) {
		Node parent = null, current = root;
		boolean isLeftNode = true; // record left or right
		// find target
		while(current != null && current.value != target) {
			parent = current;
			if(current.value > target) {
				if(current.left == null) 
					current = null;
				else {
					current = current.left;
					isLeftNode = true;
				}
			}else {
				if(current.right == null)
					current = null;
				else {
					current = current.right;
					isLeftNode = false;
				}
			}
		}
		// case 1: NOT FOUND
		if(current == null) return false;
		// case 2: there aren't children node
		else if(current.left == null && current.right == null) {
			if(current == root) root = null;
			else if(isLeftNode) parent.left = null;
			else parent.right = null;
		// case 3 : there is a children node
		}else if(current.left == null && current.right != null){
			if(current == root) root = current;
			else if(isLeftNode) parent.left = current.right;
			else parent.right = current.right;
		}else if(current.left != null && current.right == null) {
			if(current == root) root = current;
			else if(isLeftNode) parent.left = current.left;
			else parent.right = current.left;
		// case 4: there are two children node
		}else {
			/*
			 * 1、find the minimum children node of right node
			 * 2、the minimum node replaces the target node
			 */
			Node minParent = current;
			Node minNode = current.right;
			while(minNode != null && minNode.left != null) {
				minParent = minNode;
				minNode = minNode.left;
			}
			if(minParent == current) {
				minNode.left = current.left;
			}else {
				minParent.left = null;
				minNode.right = current.right;
				minNode.left = current.left;
			}
			if(parent==null) root = minNode;
			else if(isLeftNode) parent.left = minNode;
			else parent.right = minNode;
		}
		return true;
	}
	// first self, then left, then right
	public void preOrderTraversal(Node node) {
		if(node == null) return;
		System.out.printf("%d ",node.value);
		preOrderTraversal(node.left);
		preOrderTraversal(node.right);
	}
	// first left, then self, then right
	public void inOrderTraversal(Node node) {
		if(node == null) return;
		inOrderTraversal(node.left);
		System.out.printf("%d ",node.value);
		inOrderTraversal(node.right);
	}
	// first left, then right, then self
	public void postOrderTraversal(Node node) {
		if(node == null) return;
		postOrderTraversal(node.left);
		postOrderTraversal(node.right);
		System.out.printf("%d ",node.value);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安河桥北久铭心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值