Java数据结构和算法之二叉排序树

二叉排序树介绍

二叉排序树:BST: (Binary Sort(Search) Tree), 对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。

特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点

比如针对前面的数据 (7, 3, 10, 12, 5, 1, 9) ,对应的二叉排序树为:
在这里插入图片描述在这里插入图片描述在这里插入图片描述

二叉排序树的删除

二叉排序树的删除情况比较复杂,有下面三种情况需要考虑

删除叶子节点 (比如:2, 5, 9, 12)
删除只有一颗子树的节点 (比如:1)
删除有两颗子树的节点. (比如:7, 3,10 )
在这里插入图片描述

代码实现:
package com.guigu.binarysorttree;


public class BinarySortTreeDemo {
	public static void main(String[] args) {
		int[] arr = { 7, 3, 10, 12, 5, 1, 9, 2 };
		BinarySortTree binarySortTree = new BinarySortTree();
		for (int i = 0; i < arr.length; i++) {
			binarySortTree.add(new Node(arr[i]));
		}

		System.out.println("中序遍历二叉树:");
		binarySortTree.infixOrder();
		
		// 测试删除节点
		binarySortTree.delNode(12);
		binarySortTree.delNode(5);
		binarySortTree.delNode(3);
		binarySortTree.delNode(7);
		binarySortTree.delNode(9);
		
		
		System.out.println("删除节点后的二叉树:");
		binarySortTree.infixOrder();
	}
}

class BinarySortTree {
	private Node root;

	// 查找要删除的节点
	public Node search(int value) {
		if(root == null) {
			return null;
		}else {
			return root.search(value);
		}
	}
	
	// 查找要删除的父节点
	public Node searchParent(int value) {
		if(root == null) {
			return null;
		}else {
			return root.searchParent(value);
		}
	}
	
	// 编写方法:
	// 1. 返回以node为根节点的二叉排序树的最小节点值
	// 2. 删除node为根节点的二叉排序树的最小节点
	/**
	 * 
	 * @param node 传入的节点(当做二叉排序树的根节点)
	 * @return 返回以node为根节点的二叉排序树的最小节点值
	 */
	public int delRightTreeMin(Node node) {
		Node target = node;
		while(target.left != null) {
			target = target.left; // 一直找到当前节点的最左子节点
		}
		delNode(target.value); // 删除最小节点
		return target.value;
	}
	
	
	// 删除节点
	public void delNode(int value) {
		if(root == null) {
			return ;
		} else {
			// 1. 先去找到要删除的节点 targetNode
			Node targetNode = search(value);
			// 如果没有找到要删除的节点
			if(targetNode == null) { 
				return ;
			}
			// 如果targetNode不为null,说明存在,如果树只有一个节点,那肯定是根节点
			// 如果我们发现当前这颗二叉排序树只有一个节点
			if(root.left == null && root.right == null) {
				root = null;
				return ;
			}
			// 去找到父节点
			Node parentNode = searchParent(value);
			// 如果要删除的节点是叶子节点,既然是叶子节点那,肯定有父节点。只有根节点的那种情况已经考虑
			if(targetNode.left == null && targetNode.right == null) {
				// 判断targetNode是parentNode的左子节点还是右子节点
				if(parentNode.left != null && parentNode.left.value == value) { // 是左子节点
					parentNode.left = null;
				}else if(parentNode.right != null && parentNode.right.value == value) {
					parentNode.right = null; // 是右子节点
				} 
			}else if(targetNode.left != null && targetNode.right != null) {
				// 找出当前节点右子树的最小值
				int minValue = delRightTreeMin(targetNode.right);
				targetNode.value = minValue;
			}else { // 只有一颗子树的节点
				// 如果要删除的节点有左子节点
				if(targetNode.left != null) {
					if(parentNode != null) {
						// 如果targetNode是parentNode的左子节点
						if(parentNode.left.value == value) {
							parentNode.left = targetNode.left;
						} else { //  targetNode 是 parent 的右子结点
							parentNode.right = targetNode.left;
						}
					} else {
						root = targetNode.left;
					}
				}else { // 如果要删除的节点有右子节点
					if(parentNode != null) {
						// 如果targetNode是parentNode的左子节点
						if(parentNode.left.value == value) {
							parentNode.left = targetNode.right;
						} else {
							parentNode.right = targetNode.right;
						}
					} else {
						root = targetNode.right;
					}
				}
			}
		}
	}
	
	
	// 添加节点
	public void add(Node node) {
		if (root == null) { // 如果根节点为空,直接让root指向node
			root = node;
		} else { // 否则递归查找插入
			root.add(node);
		}
	}

	// 中序遍历
	public void infixOrder() {
		if (root != null) {
			root.infixOrder();
		} else {
			System.out.println("二叉排序数,为空无法遍历");
		}
	}

	public Node getRoot() {
		return root;
	}

	public void setRoot(Node root) {
		this.root = root;
	}

}

// 创建node节点
class Node {
	int value;
	Node left;
	Node right;

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

	// 查找要删除的节点
	/**
	 * @param value 希望删除的节点的值
	 * @return 如果找到返回该节点,否则返回null
	 */
	public Node search(int value) {
		if(value == this.value) { // 找到就是该节点
			return this;
		} else if(value < this.value) { // 如果小于当前节点的值,则递归向左子树查找
			if(this.left == null) { // 如果当前节点左子树为null
				return null;
			}
			return this.left.search(value);
		} else {
			if(this.right == null) {
				return null;
			}
			return this.right.search(value);
		}
	}
	
	// 查找待删除节点的父节点
	public Node searchParent(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.searchParent(value); // 向左子树递归查找
			}else if(value > this.value && this.right != null) {
				return this.right.searchParent(value); // 向右子树递归查找
			} else {
				return null; // 没有父节点
			}
		}
	}
	
	// 添加节点的方法:
	// 递归的形式添加节点,注意需要满足二叉排序树的要求
	public void add(Node node) {
		if (node == null) {
			return;
		}

		// 判断传入节点的值和当前子树的根节点的关系
		if (node.value < this.value) {
			// 如果当前节点左子节点为null
			if (this.left == null) {
				this.left = node;
			} else {
				// 递归向左子树添加
				this.left.add(node);
			}
		} else { // 如果传入节点的值大有当前节点的值向右查找
			// 如果当前节点右子节点为null
			if (this.right == null) {
				this.right = node;
			} else {
				// 递归向右子树添加
				this.right.add(node);
			}
		}
	}

	// 中序遍历
	public void infixOrder() {
		// 向左子树递归
		if (this.left != null) {
			this.left.infixOrder();
		}
		// 输出当前节点
		System.out.println(this);
		// 向右子树递归
		if (this.right != null) {
			this.right.infixOrder();
		}
	}

	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}
}

测试截图

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值