BST二叉排序树

package tree;

public class BinarySortTree {
	public static void main(String[] args) {
		int [] arr = {2,6,3,1,7,11 };
		BinaryTree tree = new BinaryTree();
		for (int i = 0; i < arr.length; i++) {
			tree.add(new Node(arr[i]));
		}
		tree.infixOrder();
		System.out.println("删除后=========");
		tree.delNode(2);
		tree.infixOrder();
	}
	
}
class BinaryTree{
	Node root;
	
	public void add (Node node) {
		if(root == null) {
			root = node;
		}else {
			root.add(node);
		}
	}
	public Node searchParent(int value) {
		if(root == null) {
			return null;
		}else {
			return root.searchParent(value);
		}
	}
	public Node searchTarget(int value) {
		if(root == null) {
			return null;
		}else {
			return root.searchTarget(value);
		}
	}
	/**
	 *删除指定节点
	 * @param value
	 */
	public void delNode(int value) {
		if(root == null) return;
		//第一种情况,删除的为叶子节点
		Node targetNode = searchTarget(value);
		//如果没有找到需要的节点
		if (targetNode == null) return;
		if(root.left == null && root.right == null) {
			root = null;
			return;
		}
		Node parentNode = searchParent(value);
		if(targetNode.left == null && targetNode.right == null) {
			if(parentNode.left != null && parentNode.left.value == targetNode.value) {
				parentNode.left = null;
			}else {
				parentNode.right = null;
			}
		}else if (targetNode.left !=null && targetNode.right !=null) {
			//第二种情况,删除的节点有俩个子结点
			int min = delRightTreeMin(targetNode.right);
			targetNode.value = min;
		}else {
			//第三种情况,删除的节点有一个子结点
			if(targetNode.left !=null) {
				if(parentNode !=null) {
					//是父节点的左子节点
					if(parentNode.left.value == value) {
						parentNode.left = targetNode.left;
					}else {
					//是父节点右子节点
						parentNode.right = targetNode.left;
					}
				}else {
					root = targetNode.left;
				}
				
			}else {
				if(parentNode != null) {
					//是父节点的左子节点
					if(parentNode.left.value == value) {
						parentNode.left = targetNode.right;
					}else {
					//是父节点右子节点
						parentNode.right = targetNode.right;
					}
				}else {
					root = targetNode.right;
				}
			}
		}
	}
	/**
	 * 删除右子树的最小的节点,并返回它
	 * @param node
	 * @return
	 */
	public int delRightTreeMin(Node node) {
		Node target = node;
		while(target.left != null) {
			target = target.left;
		}
		delNode(target.value);
		return target.value;
	}
	public void infixOrder() {
		if ( root !=null) {
			root.infixOrder();
		}else {
			System.out.println("对不起,您输入的是空树");
		}
	}
	
}

class Node {
	int value;
	Node left;
	Node right;
	public  Node(int value) {
		this.value = value;
	}
	/**
	 * 添加节点
	 * @param node
	 */
	public void add(Node node) {
		if (node == null) {
			return;
		}
		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);
			}
		}
	}
	/**
	 * 搜索父节点并返回
	 * @param value
	 * @return
	 */
	public Node searchParent(int value) {
		if(this.left != null && this.left.value == value ||
			this.right != null && this.right.value == value) {
			return this;
		}else {
			if(this.left !=null && value < this.value) {
				return this.left.searchParent(value);
			}else if (this.right !=null && value >= this.value) {
				return this.right.searchParent(value);
			}else {
				return null;
			}
		}
	}
	/**
	 * 搜索目标节点并返回
	 * @param value
	 * @return
	 */
	public Node searchTarget(int value) {
		if(value == this.value) {
			return this;
		}else if (value < this.value) {
			if(this.left == null) {
				return null;
			}else {
				return this.left.searchTarget(value);
			}
		}else {
			if(this.right == null) {
				return null;
			}else {
				return this.right.searchTarget(value);
			}
		}
	}
	
	/**
	 * 中序遍历
	 */
	public void infixOrder() {
		if (this.left !=null) {
			this.left.infixOrder();
		}
		System.out.println(this.value);
		
		if (this.right != null) {
			this.right.infixOrder();
		}
	}
	public String toString() {
		return "Node [value=" + value + "]";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值