简单实现二叉排序树BST

/**
 * @Author TangXi
 * @Date 2020/3/5 16:14
 * 简单实现二叉排序树
 */
public class BinarySortTree {

	private Node root;

	public void add(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			this.add(new Node(arr[i]));
		}
	}

	public void add(Node node) {
		if (node == null) {
			return;
		}
		if (root == null) {
			root = node;
		} else {
			root.add(node);
		}
	}

	public void preOrder() {
		if (root == null) {
			throw new NullPointerException("树为空,不允许遍历");
		}
		root.preOrder();
	}

	public void midOrder() {
		if (root == null) {
			throw new NullPointerException("树为空,不允许遍历");
		}
		root.midOrder();
	}

	public Node findParent(int val) {
		return root.findParent(val);
	}

	public Node find(int val){
		return root.find(val);
	}

	public void del(int val){
		root.del(val);
	}

	private class Node {
		int value;

		Node left;
		Node right;

		public Node(int value) {
			this.value = value;
		}

		public void add(Node node) {
			if (node.value < this.value) {
				if (this.left == null) {
					this.left = node;
				} else {
					this.left.add(node);
				}
			} else {
				if (this.right == null) {
					this.right = node;
				} else {
					this.right.add(node);
				}
			}
		}

		public void preOrder() {

			System.out.print(value + " ");

			if (this.left != null) {
				this.left.preOrder();
			}

			if (this.right != null) {
				this.right.preOrder();
			}
		}

		public void midOrder() {

			if (this.left != null) {
				this.left.midOrder();
			}

			System.out.print(value + " ");

			if (this.right != null) {
				this.right.midOrder();
			}
		}

		/**
		 * 删除操作
		 * 情况1:该节点是叶子节点
		 * 情况2:该节点只有一个子树
		 * 情况3:该节点有两个子树
		 */
		public void del(int value) {
			//节点不存在
			Node node = find(value);
			if(node==null){
				return;
			}
			//节点没有父节点(根节点情况)
			Node parent = findParent(value);
			if(parent==null){
				if(node.left==null&&node.right==null){
					root = null;
				} else if(node.left==null&&node.right!=null){
					//只有右边
					root = root.right;
				} else if(node.left!=null&&node.right==null){
					//只有左边
					root = root.left;
				} else {
					//两边都有
					del2(root);
				}
				return;
			}
			//叶子节点情况
			if(node.left==null&&node.right==null){
				Node parent1 = findParent(node);
				if(parent1.left!=null&&parent1.left.value==node.value){
					parent1.left = null;
				} else {
					parent1.right = null;
				}
				return;
			}
			//当有一个子树(左)
			if(node.left!=null&&node.right==null){
				Node parent1 = findParent(node);
				if(parent1.left!=null&&parent1.left.value==node.value){
					parent1.left = parent1.left.left;
				} else {
					parent1.right = parent1.right.left;
				}
				return;
			}
			//当有一个子树(右)
			if(node.left==null&&node.right!=null){
				Node parent1 = findParent(node);
				if(parent1.right!=null&&parent1.right.value==node.value){
					parent1.right = parent1.right.right;
				} else {
					parent1.left = parent1.left.right;
				}
				return;
			}
			//两边都有
			if(node.left!=null&&node.right!=null){
				del2(node);
			}

		}

		/**
		 * 包含两个子节点
		 */
		private void del2(Node root){
			//两边都有
			Node minNode = findMinNode(root.right);
			Node parent1 = findParent(minNode.value);
			root.value = minNode.value;
			if(parent1.left.value==minNode.value){
				parent1.left = null;
			} else {
				parent1.right = null;
			}
		}

		//右子树
		private Node findMinNode(Node node){
			Node temp = node;
			while(temp.left!=null){
				temp = temp.left;
			}
			return temp;
		}

		/**
		 * 找到父节点
		 *
		 * @param value
		 * @return 返回父节点,为空为没找到
		 */
		private Node findParent(int value) {
			if ((this.left != null && this.left.value == value)
					|| (this.right != null && this.right.value == value)) {
				return this;
			} else {
				if (value < this.value && this.left != null) {
					return this.left.findParent(value);
				} else if (value >= this.value && this.right != null) {
					return this.right.findParent(value);
				} else {
					return null;
				}
			}
		}

		private Node findParent(Node node){
			return findParent(node.value);
		}

		private Node find(int value){
			if(this.value==value){
				return this;
			} else {
				if(value < this.value){
					if(this.left==null){
						return null;
					}
					return this.left.find(value);
				} else {
					if(this.right==null){
						return null;
					}
					return this.right.find(value);
				}
			}
		}

		@Override
		public String toString() {
			return value + " ";
		}

	}
}

仅作为笔记,如有错误,请指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值