Java數據結構與算法 day10 樹結構實際應用(三)

第10章 樹結構的實際應用

本章源碼:https://github.com/name365/Java-Data-structure

二叉排序樹

二叉排序樹(BST)的介紹

先看一個需求

  • 給你一個數列 (7, 3, 10, 12, 5, 1, 9),要求能夠高效的完成對數據的查詢和添加。

解決方案分析

  • 使用數組

    • 數組未排序, 優點:直接在數組尾添加,速度快。 缺點:查找速度慢.
    • 數組排序,優點:可以使用二分查找,查找速度快,缺點:爲了保證數組有序,在添加新數據時,找到插入位置後,後面的數據需整體移動,速度慢。

在這裏插入圖片描述

  • 使用鏈式存儲-鏈表
    不管鏈表是否有序,查找速度都慢,添加數據速度比數組快,不需要數據整體移動。
  • 使用二叉排序樹

二叉排序樹介紹

  • 二叉排序樹:BST: (Binary Sort(Search) Tree), 對於二叉排序樹的任何一個非葉子節點,要求左子節點的值比當前節點的值小,右子節點的值比當前節點的值大。

  • 特別說明:如果有相同的值,可以將該節點放在左子節點或右子節點

  • 比如針對前面的數據 (7, 3, 10, 12, 5, 1, 9) ,對應的二叉排序樹爲:

在這裏插入圖片描述

二叉排序樹(BST)創建和遍歷

一個數組創建成對應的二叉排序樹,並使用中序遍歷二叉排序樹,比如: 數組爲 Array(7, 3, 10, 12, 5, 1, 9) , 創建成對應的二叉排序樹爲 :

在這裏插入圖片描述

public class BinarySortTreeTest {

	public static void main(String[] args) {
		int arr[] = {7,3,10,12,5,1,9};
		BinaryTree tree = new BinaryTree();
		//循環的添加結點到二叉排序樹
		for(int i = 0 ;i< arr.length;i++){
			tree.add(new Node(arr[i]));
		}
		
		//中序遍歷二叉排序樹
		System.out.println("中序遍歷此樹:");
		tree.infixOrder(); 	//1,3,5,7,9,10,12
	}

}

//創建Node結點
class Node{
	int value;
	Node left;
	Node right;
	
	public Node(int value) {
		super();
		this.value = value;
	}
	
	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}

	//添加節點的方法
	//遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node){
		if(node == null){
			return;
		}
		
		//判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if(node.value < this.value){
			if(this.left == null){	//如果當前結點左子結點爲null
				this.left = node;
			}else{
				//遞歸的向左子樹添加
				this.left.add(node);
			}
		}else{	//添加的節點的值大於當前結點的值
			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();
		}
	}
}

//創建二叉排序樹
class BinaryTree{
	private Node root;
	//添加結點的方法
	public void add(Node node){
		if(root == null){
			root = node;	//如果root爲空則直接讓root指向node
		}else{
			root.add(node);
		}
	}
	//遍歷方法
	public void infixOrder(){
		if(root != null){
			root.infixOrder();
		}else{
			System.out.println("二叉排序樹爲空!!!");
		}
	}
}

二叉排序樹刪除結點思路圖解

二叉排序樹的刪除情況比較複雜,有下面三種情況需要考慮

1)刪除葉子節點 (比如:2, 5, 9, 12)

2)刪除只有一顆子樹的節點 (比如:1)

3)刪除有兩顆子樹的節點. (比如:7, 3,10 )

在這裏插入圖片描述

二叉排序樹刪除葉子結點

圖解 二叉排序樹 刪除結點的 三種情況

第一種情況: 刪除葉子節點 (比如:2, 5, 9, 12)
思路
(1) 需求先去找到要刪除的結點  targetNode
(2) 找到targetNode 的 父結點 parent 
(3) 確定 targetNode 是 parent的左子結點 還是右子結點
(4) 根據前面的情況來對應刪除
	左子結點 parent.left = null
	右子結點 parent.right = null;

代碼實現如下:

public class BinarySortTreeTest {

	public static void main(String[] args) {
		int arr[] = {7,3,10,12,5,1,9};
		BinaryTree tree = new BinaryTree();
		//循環的添加結點到二叉排序樹
		for(int i = 0 ;i< arr.length;i++){
			tree.add(new Node(arr[i]));
		}
		
		//中序遍歷二叉排序樹
		System.out.println("中序遍歷此樹:");
		tree.infixOrder(); 	//1,3,5,7,9,10,12
		
		//測試一下刪除葉子節點
		tree.delNode(2);
		tree.delNode(5);
		tree.delNode(9);
		System.out.println("刪除後的節點:");
		tree.infixOrder();
	}

}

//創建Node結點
class Node{
	int value;
	Node left;
	Node right;
	
	public Node(int value) {
		super();
		this.value = value;
	}
	
	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}

	//添加節點的方法
	//遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node){
		if(node == null){
			return;
		}
		
		//判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if(node.value < this.value){
			if(this.left == null){	//如果當前結點左子結點爲null
				this.left = node;
			}else{
				//遞歸的向左子樹添加
				this.left.add(node);
			}
		}else{	//添加的節點的值大於當前結點的值
			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();
		}
	}
	
	//查找要刪除的節點
	/**
	  * 
	  * @Description 
	  * @author subei
	  * @date 2020年6月13日上午8:43:01
	  * @param value 希望刪除的結點的值
	  * @return 如果找到該值返回,未找到返回null
	 */
	public Node search(int value){
		if(value == this.value){	//說明找到了
			return this;
		}else if(value < this.value){	//查找的值小於當前結點的值,向左子樹查找
			if(this.left == null){	//左子結點爲空
				return null;
			}
			return this.left.search(value);
		}else{	//查找的值不小於當前結點的值,向右子樹查找
			if(this.right == null){
				return null;
			}
			return this.right.search(value);
		}
	}
	

	//查找要刪除結點的父結點
	/**
	 * 
	 * @param value 希望刪除的結點的值
	 * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
	 */
	public Node searchP(int value){
		//如果當前結點是要刪除的結點的父結點,如果沒有就返回null
		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.searchP(value);	//向左子樹查找
			}else if(value >= this.value && this.right != null){
				return this.right.searchP(value);	//向右子樹遞歸查找
			}else {
				return null;	//未找到父結點
			}
		}
	}
	
}

//創建二叉排序樹
class BinaryTree{
	private Node root;
	//添加結點的方法
	public void add(Node node){
		if(root == null){
			root = node;	//如果root爲空則直接讓root指向node
		}else{
			root.add(node);
		}
	}
	//遍歷方法
	public void infixOrder(){
		if(root != null){
			root.infixOrder();
		}else{
			System.out.println("二叉排序樹爲空!!!");
		}
	}
	//查找要刪除的結點
	public Node search(int value){
		if(root == null){
			return null;
		}else{
			return root.search(value);
		}
	}
	//查找要刪除的節點的父節點
	public Node searchP(int value){
		if(root == null){
			return null;
		}else{
			return root.searchP(value);
		}
	}
	
	//刪除節點
	public void delNode(int value){
		if(root == null){
			return;
		}else{
			//1.需求先去找到要刪除的結點  targetNode
			Node targetNode = search(value);
			//如果沒有找到要刪除的結點
			if(targetNode ==null){
				return;
			}
			//如果我們發現當前這顆二叉排序樹只有一個結點
			if(root.left == null && root.right == null) {
				root = null;
				return;
			}
			//去找到targetNode的父結點
			Node parent = searchP(value);
			//如果要刪除的節點爲葉子節點
			if(targetNode.left == null && targetNode.right == null){
				//判斷targetNode是父節點的左子結點,還是右子節點
				if(parent.left != null && parent.left.value == value){	//左子節點
					parent.left = null;
				}else if(parent.right != null && parent.right.value == value){	//右子節點
					parent.right = null;
				}
			}
		}
	}
}

BST刪除有一顆子樹的結點

第二種情況: 刪除只有一顆子樹的節點 比如 1
思路
(1) 需求先去找到要刪除的結點  targetNode
(2) 找到targetNode 的 父結點 parent 
(3) 確定targetNode 的子結點是左子結點還是右子結點
(4) targetNode 是 parent 的左子結點還是右子結點
(5) 如果targetNode 有左子結點
	5.1 如果 targetNode 是 parent 的左子結點
		parent.left = targetNode.left;
	5.2 如果 targetNode 是 parent 的右子結點
		parent.right = targetNode.left;
(6) 如果targetNode 有右子結點
	6.1 如果 targetNode 是 parent 的左子結點
		parent.left = targetNode.right;
	6.2 如果 targetNode 是 parent 的右子結點
		parent.right = targetNode.right

代碼實現如下:

public class BinarySortTreeTest {

	public static void main(String[] args) {
		int arr[] = {7,3,10,12,5,1,9,0};
		BinaryTree tree = new BinaryTree();
		//循環的添加結點到二叉排序樹
		for(int i = 0 ;i< arr.length;i++){
			tree.add(new Node(arr[i]));
		}
		
		//中序遍歷二叉排序樹
		System.out.println("中序遍歷此樹:");
		tree.infixOrder(); 	//0,1,3,5,7,9,10,12
		
		//測試一下刪除葉子節點
		tree.delNode(1);
		System.out.println("刪除後的節點:");
		tree.infixOrder();	//0,3,5,7,9,10,12
	}

}

//創建Node結點
class Node{
	int value;
	Node left;
	Node right;
	
	public Node(int value) {
		super();
		this.value = value;
	}
	
	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}

	//添加節點的方法
	//遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node){
		if(node == null){
			return;
		}
		
		//判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if(node.value < this.value){
			if(this.left == null){	//如果當前結點左子結點爲null
				this.left = node;
			}else{
				//遞歸的向左子樹添加
				this.left.add(node);
			}
		}else{	//添加的節點的值大於當前結點的值
			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();
		}
	}
	
	//查找要刪除的節點
	/**
	  * 
	  * @Description 
	  * @author subei
	  * @date 2020年6月13日上午8:43:01
	  * @param value 希望刪除的結點的值
	  * @return 如果找到該值返回,未找到返回null
	 */
	public Node search(int value){
		if(value == this.value){	//說明找到了
			return this;
		}else if(value < this.value){	//查找的值小於當前結點的值,向左子樹查找
			if(this.left == null){	//左子結點爲空
				return null;
			}
			return this.left.search(value);
		}else{	//查找的值不小於當前結點的值,向右子樹查找
			if(this.right == null){
				return null;
			}
			return this.right.search(value);
		}
	}
	

	//查找要刪除結點的父結點
	/**
	 * 
	 * @param value 希望刪除的結點的值
	 * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
	 */
	public Node searchP(int value){
		//如果當前結點是要刪除的結點的父結點,如果沒有就返回null
		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.searchP(value);	//向左子樹查找
			}else if(value >= this.value && this.right != null){
				return this.right.searchP(value);	//向右子樹遞歸查找
			}else {
				return null;	//未找到父結點
			}
		}
	}
	
	
}

//創建二叉排序樹
class BinaryTree{
	private Node root;
	//添加結點的方法
	public void add(Node node){
		if(root == null){
			root = node;	//如果root爲空則直接讓root指向node
		}else{
			root.add(node);
		}
	}
	//遍歷方法
	public void infixOrder(){
		if(root != null){
			root.infixOrder();
		}else{
			System.out.println("二叉排序樹爲空!!!");
		}
	}
	//查找要刪除的結點
	public Node search(int value){
		if(root == null){
			return null;
		}else{
			return root.search(value);
		}
	}
	//查找要刪除的節點的父節點
	public Node searchP(int value){
		if(root == null){
			return null;
		}else{
			return root.searchP(value);
		}
	}
	
	//刪除節點
	public void delNode(int value){
		if(root == null){
			return;
		}else{
			//1.需求先去找到要刪除的結點  targetNode
			Node targetNode = search(value);
			//如果沒有找到要刪除的結點
			if(targetNode ==null){
				return;
			}
			//如果我們發現當前這顆二叉排序樹只有一個結點
			if(root.left == null && root.right == null) {
				root = null;
				return;
			}
			//去找到targetNode的父結點
			Node parent = searchP(value);
			//如果要刪除的節點爲葉子節點
			if(targetNode.left == null && targetNode.right == null){
				//判斷targetNode是父節點的左子結點,還是右子節點
				if(parent.left != null && parent.left.value == value){	//左子節點
					parent.left = null;
				}else if(parent.right != null && parent.right.value == value){	//右子節點
					parent.right = null;
				}
			}else if(targetNode.left != null && targetNode.right != null){	//刪除有兩顆子樹的節點
				
			}else{	//刪除只有一個字樹的節點
				//如果要刪除的結點有左子結點 
				if(targetNode.left != null) {
					if(parent != null) {
						//如果 targetNode 是 parent 的左子結點
						if(parent.left.value == value) {
							parent.left = targetNode.left;
						} else {	//targetNode 是 parent 的右子結點
							parent.right = targetNode.left;
						} 
					} else {
						root = targetNode.left;
					}
				}else{	//如果要刪除的結點有右子結點 
					if(parent != null){
						//如果 targetNode 是 parent 的左子結點
						if(parent.left.value == value){
							parent.left = targetNode.right;
						}else{	//如果 targetNode 是 parent 的右子結點
							parent.right = targetNode.right;
						}
					}else{
						root = targetNode.right;
					}
				}
			}
		}
	}
}

BST刪除有二顆子樹的結點

情況三: 刪除有兩顆子樹的節點. (比如:7, 310 )
思路
(1) 需求先去找到要刪除的結點  targetNode
(2) 找到targetNode 的 父結點 parent 
(3) 從targetNode 的右子樹找到最小的結點
(4) 用一個臨時變量,將 最小結點的值保存 temp = 11
(5) 刪除該最小結點
(6) targetNode.value = temp

代碼實現如下:

public class BinarySortTreeTest {

	public static void main(String[] args) {
		int arr[] = {7,3,10,12,5,1,9,0};
		BinaryTree tree = new BinaryTree();
		//循環的添加結點到二叉排序樹
		for(int i = 0 ;i< arr.length;i++){
			tree.add(new Node(arr[i]));
		}
		
		//中序遍歷二叉排序樹
		System.out.println("中序遍歷此樹:");
		tree.infixOrder(); 	//0,1,3,5,7,9,10,12
		
		//測試一下刪除葉子節點
		tree.delNode(7);
		System.out.println("刪除後的節點:");
		tree.infixOrder();	//0,1,3,5,9,10,12
	}

}

//創建Node結點
class Node{
	int value;
	Node left;
	Node right;
	
	public Node(int value) {
		super();
		this.value = value;
	}
	
	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}

	//添加節點的方法
	//遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node){
		if(node == null){
			return;
		}
		
		//判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if(node.value < this.value){
			if(this.left == null){	//如果當前結點左子結點爲null
				this.left = node;
			}else{
				//遞歸的向左子樹添加
				this.left.add(node);
			}
		}else{	//添加的節點的值大於當前結點的值
			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();
		}
	}
	
	//查找要刪除的節點
	/**
	  * 
	  * @Description 
	  * @author subei
	  * @date 2020年6月13日上午8:43:01
	  * @param value 希望刪除的結點的值
	  * @return 如果找到該值返回,未找到返回null
	 */
	public Node search(int value){
		if(value == this.value){	//說明找到了
			return this;
		}else if(value < this.value){	//查找的值小於當前結點的值,向左子樹查找
			if(this.left == null){	//左子結點爲空
				return null;
			}
			return this.left.search(value);
		}else{	//查找的值不小於當前結點的值,向右子樹查找
			if(this.right == null){
				return null;
			}
			return this.right.search(value);
		}
	}
	

	//查找要刪除結點的父結點
	/**
	 * 
	 * @param value 希望刪除的結點的值
	 * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
	 */
	public Node searchP(int value){
		//如果當前結點是要刪除的結點的父結點,如果沒有就返回null
		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.searchP(value);	//向左子樹查找
			}else if(value >= this.value && this.right != null){
				return this.right.searchP(value);	//向右子樹遞歸查找
			}else {
				return null;	//未找到父結點
			}
		}
	}
	
	
}

//創建二叉排序樹
class BinaryTree{
	private Node root;
	//添加結點的方法
	public void add(Node node){
		if(root == null){
			root = node;	//如果root爲空則直接讓root指向node
		}else{
			root.add(node);
		}
	}
	//遍歷方法
	public void infixOrder(){
		if(root != null){
			root.infixOrder();
		}else{
			System.out.println("二叉排序樹爲空!!!");
		}
	}
	//查找要刪除的結點
	public Node search(int value){
		if(root == null){
			return null;
		}else{
			return root.search(value);
		}
	}
	//查找要刪除的節點的父節點
	public Node searchP(int value){
		if(root == null){
			return null;
		}else{
			return root.searchP(value);
		}
	}
	
	//刪除節點
	public void delNode(int value){
		if(root == null){
			return;
		}else{
			//1.需求先去找到要刪除的結點  targetNode
			Node targetNode = search(value);
			//如果沒有找到要刪除的結點
			if(targetNode ==null){
				return;
			}
			//如果我們發現當前這顆二叉排序樹只有一個結點
			if(root.left == null && root.right == null) {
				root = null;
				return;
			}
			//去找到targetNode的父結點
			Node parent = searchP(value);
			//如果要刪除的節點爲葉子節點
			if(targetNode.left == null && targetNode.right == null){
				//判斷targetNode是父節點的左子結點,還是右子節點
				if(parent.left != null && parent.left.value == value){	//左子節點
					parent.left = null;
				}else if(parent.right != null && parent.right.value == value){	//右子節點
					parent.right = null;
				}
			}else if(targetNode.left != null && targetNode.right != null){	//刪除有兩顆子樹的節點
				int minVa = delRightT(targetNode.right);
				targetNode.value = minVa;
			}else{	//刪除只有一個字樹的節點
				//如果要刪除的結點有左子結點 
				if(targetNode.left != null) {
					if(parent != null) {
						//如果 targetNode 是 parent 的左子結點
						if(parent.left.value == value) {
							parent.left = targetNode.left;
						} else {	//targetNode 是 parent 的右子結點
							parent.right = targetNode.left;
						} 
					} else {
						root = targetNode.left;
					}
				}else{	//如果要刪除的結點有右子結點 
					if(parent != null){
						//如果 targetNode 是 parent 的左子結點
						if(parent.left.value == value){
							parent.left = targetNode.right;
						}else{	//如果 targetNode 是 parent 的右子結點
							parent.right = targetNode.right;
						}
					}else{
						root = targetNode.right;
					}
				}
			}
		}
	}
	
	//編寫方法
	//1.返回的 以node 爲根結點的二叉排序樹的最小結點的值
	//2.刪除node 爲根結點的二叉排序樹的最小結點
	/**
	  * 
	  * @Description 
	  * @author subei
	  * @date 2020年6月13日上午10:44:31
	  * @param node 傳入的結點(爲二叉排序樹的根結點)
	  * @return 返回的 以node 爲根結點的二叉排序樹的最小結點的值
	 */
	public int delRightT(Node node){
		Node tar = node;
		//循環的查找左子節點,就會找到最小值
		while(tar.left != null){
			tar = tar.left;
		}
		//這時 target就指向了最小結點
		//刪除最小結點
		delNode(tar.value);
		return tar.value;
	}
}

從左子樹找到最大的結點,然後刪除節點

思路
看過上面的或者已經有相關數據結構的道友就會了解,實現起來異常簡單。
    1.最小值就是二叉樹最左邊的葉子節點;
    2.而最大值就是二叉樹最左邊的葉子節點。
public class BinarySortTreeTest {

	public static void main(String[] args) {
		int arr[] = {7,3,10,12,5,1,9,2};
		BinaryTree tree = new BinaryTree();
		//循環的添加結點到二叉排序樹
		for(int i = 0 ;i< arr.length;i++){
			tree.add(new Node(arr[i]));
		}
		
		//中序遍歷二叉排序樹
		System.out.println("中序遍歷此樹:");
		tree.infixOrder(); 	//1,2,3,5,7,9,10,12
		
		//測試一下刪除葉子節點
		tree.delNode(10);
		System.out.println("刪除後的節點:");
		tree.infixOrder();	//1,2,3,5,7,9,10,12
	}

}

//創建Node結點
class Node{
	int value;
	Node left;
	Node right;
	
	public Node(int value) {
		super();
		this.value = value;
	}
	
	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}

	//添加節點的方法
	//遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node){
		if(node == null){
			return;
		}
		
		//判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if(node.value < this.value){
			if(this.left == null){	//如果當前結點左子結點爲null
				this.left = node;
			}else{
				//遞歸的向左子樹添加
				this.left.add(node);
			}
		}else{	//添加的節點的值大於當前結點的值
			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();
		}
	}
	
	//查找要刪除的節點
	/**
	  * 
	  * @Description 
	  * @author subei
	  * @date 2020年6月13日上午8:43:01
	  * @param value 希望刪除的結點的值
	  * @return 如果找到該值返回,未找到返回null
	 */
	public Node search(int value){
		if(value == this.value){	//說明找到了
			return this;
		}else if(value < this.value){	//查找的值小於當前結點的值,向左子樹查找
			if(this.left == null){	//左子結點爲空
				return null;
			}
			return this.left.search(value);
		}else{	//查找的值不小於當前結點的值,向右子樹查找
			if(this.right == null){
				return null;
			}
			return this.right.search(value);
		}
	}
	

	//查找要刪除結點的父結點
	/**
	 * 
	 * @param value 希望刪除的結點的值
	 * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
	 */
	public Node searchP(int value){
		//如果當前結點是要刪除的結點的父結點,如果沒有就返回null
		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.searchP(value);	//向左子樹查找
			}else if(value >= this.value && this.right != null){
				return this.right.searchP(value);	//向右子樹遞歸查找
			}else {
				return null;	//未找到父結點
			}
		}
	}
	
	
}

//創建二叉排序樹
class BinaryTree{
	private Node root;
	//添加結點的方法
	public void add(Node node){
		if(root == null){
			root = node;	//如果root爲空則直接讓root指向node
		}else{
			root.add(node);
		}
	}
	//遍歷方法
	public void infixOrder(){
		if(root != null){
			root.infixOrder();
		}else{
			System.out.println("二叉排序樹爲空!!!");
		}
	}
	//查找要刪除的結點
	public Node search(int value){
		if(root == null){
			return null;
		}else{
			return root.search(value);
		}
	}
	//查找要刪除的節點的父節點
	public Node searchP(int value){
		if(root == null){
			return null;
		}else{
			return root.searchP(value);
		}
	}
	
	//刪除節點
	public void delNode(int value){
		if(root == null){
			return;
		}else{
			//1.需求先去找到要刪除的結點  targetNode
			Node targetNode = search(value);
			//如果沒有找到要刪除的結點
			if(targetNode == null){
				return;
			}
			//如果我們發現當前這顆二叉排序樹只有一個結點
			if(root.left == null && root.right == null) {
				root = null;
				return;
			}
			//去找到targetNode的父結點
			Node parent = searchP(value);
			//如果要刪除的節點爲葉子節點
			if(targetNode.left == null && targetNode.right == null){
				//判斷targetNode是父節點的左子結點,還是右子節點
				if(parent.left != null && parent.left.value == value){	//左子節點
					parent.left = null;
				}else if(parent.right != null && parent.right.value == value){	//右子節點
					parent.right = null;
				}
			}else if(targetNode.left != null && targetNode.right != null){	//刪除有兩顆子樹的節點
				int maxVa = delRightT(targetNode.right);
				targetNode.value = maxVa;
			}else{	//刪除只有一個字樹的節點
				//如果要刪除的結點有左子結點 
				if(targetNode.left != null) {
					if(parent != null) {
						//如果 targetNode 是 parent 的左子結點
						if(parent.left.value == value) {
							parent.left = targetNode.left;
						} else {	//targetNode 是 parent 的右子結點
							parent.right = targetNode.left;
						} 
					} else {
						root = targetNode.left;
					}
				}else{	//如果要刪除的結點有右子結點 
					if(parent != null){
						//如果 targetNode 是 parent 的左子結點
						if(parent.left.value == value){
							parent.left = targetNode.right;
						}else{	//如果 targetNode 是 parent 的右子結點
							parent.right = targetNode.right;
						}
					}else{
						root = targetNode.right;
					}
				}
			}
		}
	}
	
	//編寫方法
	//1.返回的 以node 爲根結點的二叉排序樹的最小結點的值
	//2.刪除node 爲根結點的二叉排序樹的最小結點
	/**
	  * 
	  * @Description 
	  * @author subei
	  * @date 2020年6月13日上午10:44:31
	  * @param node 傳入的結點(爲二叉排序樹的根結點)
	  * @return 返回的 以node 爲根結點的二叉排序樹的最小結點的值
	 */
	public int delRightT(Node node){
		Node tar = node;
		//循環的查找左子節點,就會找到最大值
		while(tar.right != null){
			tar = tar.right;
		}
		//這時 target就指向了最大結點
		//刪除最大結點
		delNode(tar.value);
//		System.out.println("子樹最大:" + tar.value);
		return tar.value;
	}
}

平衡二叉樹(AVL樹)

平衡二叉樹(AVL樹)介紹

看一個案例(說明二叉排序樹可能的問題)

  • 給你一個數列{1,2,3,4,5,6},要求創建一顆二叉排序樹(BST), 並分析問題所在。

在這裏插入圖片描述

左邊BST 存在的問題分析:

  • 左子樹全部爲空,從形式上看,更像一個單鏈表.

  • 插入速度沒有影響

  • 查詢速度明顯降低(因爲需要依次比較), 不能發揮BST
    的優勢,因爲每次還需要比較左子樹,其查詢速度比
    單鏈表還慢

  • 解決方案——》平衡二叉樹(AVL)

基本介紹

  • 平衡二叉樹也叫平衡二叉搜索樹(Self-balancing binary search tree)又被稱爲AVL樹, 可以保證查詢效率較高
  • 具有以下特點:它是一 棵空樹或它的左右兩個子樹的高度差的絕對值不超過1,並且左右兩個子樹都是一棵平衡二叉樹。平衡二叉樹的常用實現方法有紅黑樹AVL替罪羊樹Treap伸展樹等。
  • 舉例說明, 看看下面哪些AVL樹, 爲什麼?

在這裏插入圖片描述

AVL樹左旋轉思路圖解

應用案例-單旋轉(左旋轉)

1.要求: 給你一個數列,創建出對應的平衡二叉樹.數列 {4,3,6,5,7,8}

2.思路分析(示意圖)

在這裏插入圖片描述

問題:當插入8 時

rightHeight() - leftHeight() > 1 成立,此時,不再是一顆avl樹了.

怎麼處理才能保證爲AVL樹 --> 進行左旋轉.

具體步驟圖解:

1.創建一個新的節點 newNode (4這個值創建),創建一個新的節點,值等於當前根節點的值.

//把新節點的左子樹設置了當前節點的左子樹

在這裏插入圖片描述

2. newNode.left = left 
//把新節點的右子樹設置爲當前節點的右子樹的左子樹

在這裏插入圖片描述

3. newNode.right =right.left;
//把當前節點的值換爲右子節點的值

在這裏插入圖片描述

4.value=right.value; 
//把當前節點的右子樹設置成右子樹的右子樹

在這裏插入圖片描述

5. right=right.right;
//把當前節點的左子樹設置爲新節點

在這裏插入圖片描述

6. left=newLeft;

在這裏插入圖片描述

源自網絡的動圖:

在這裏插入圖片描述

AVL樹高度求解

public class AVLTreeTest {

	public static void main(String[] args) {
		int[] arr = {4,3,6,5,7,8};  
		//創建一個 AVLTree對象
		AVLTree avlTree = new AVLTree();
		
		//添加結點
		for(int i=0; i < arr.length; i++) {
			avlTree.add(new Node(arr[i]));
		}
		
		//中序遍歷
		System.out.println("中序遍歷:");
		avlTree.infixOrder();	//3,4,5,6,7,8
		
		System.out.println("未經過平衡處理的樹:");
		System.out.println("樹的高度:" + avlTree.getRoot().height());	//4
		System.out.println("樹的左子樹高度:" + avlTree.getRoot().leftHeight()); // 1
		System.out.println("樹的右子樹高度:" + avlTree.getRoot().rightHeight()); // 3
		
	}

}
//創建Node結點
class Node{
	int value;
	Node left;
	Node right;
	
	public Node(int value) {
		super();
		this.value = value;
	}
	
	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}

	//添加節點的方法
	//遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node){
		if(node == null){
			return;
		}
		
		//判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if(node.value < this.value){
			if(this.left == null){	//如果當前結點左子結點爲null
				this.left = node;
			}else{
				//遞歸的向左子樹添加
				this.left.add(node);
			}
		}else{	//添加的節點的值大於當前結點的值
			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();
		}
	}
	
	//查找要刪除的節點
	/**
	  * 
	  * @Description 
	  * @author subei
	  * @date 2020年6月13日上午8:43:01
	  * @param value 希望刪除的結點的值
	  * @return 如果找到該值返回,未找到返回null
	 */
	public Node search(int value){
		if(value == this.value){	//說明找到了
			return this;
		}else if(value < this.value){	//查找的值小於當前結點的值,向左子樹查找
			if(this.left == null){	//左子結點爲空
				return null;
			}
			return this.left.search(value);
		}else{	//查找的值不小於當前結點的值,向右子樹查找
			if(this.right == null){
				return null;
			}
			return this.right.search(value);
		}
	}
	

	//查找要刪除結點的父結點
	/**
	 * 
	 * @param value 希望刪除的結點的值
	 * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
	 */
	public Node searchP(int value){
		//如果當前結點是要刪除的結點的父結點,如果沒有就返回null
		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.searchP(value);	//向左子樹查找
			}else if(value >= this.value && this.right != null){
				return this.right.searchP(value);	//向右子樹遞歸查找
			}else {
				return null;	//未找到父結點
			}
		}
	}
	
	//返回以該結點爲根結點的樹的高度
	public int height(){
		return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
	}
	
	//返回左子樹的高度
	public int leftHeight(){
		if(left == null){
			return 0;
		}
		return left.height();
	}
	
	//返回右子樹的高度
	public int rightHeight(){
		if(right == null){
			return 0;
		}
		return right.height();
	}
	
}

//創建AVL樹
class AVLTree{
	private Node root;
	
	public Node getRoot() {
		return root;
	}
	
	//添加結點的方法
	public void add(Node node){
		if(root == null){
			root = node;	//如果root爲空則直接讓root指向node
		}else{
			root.add(node);
		}
	}
	//遍歷方法
	public void infixOrder(){
		if(root != null){
			root.infixOrder();
		}else{
			System.out.println("二叉排序樹爲空!!!");
		}
	}
	//查找要刪除的結點
	public Node search(int value){
		if(root == null){
			return null;
		}else{
			return root.search(value);
		}
	}
	//查找要刪除的節點的父節點
	public Node searchP(int value){
		if(root == null){
			return null;
		}else{
			return root.searchP(value);
		}
	}
	
	//刪除節點
	public void delNode(int value){
		if(root == null){
			return;
		}else{
			//1.需求先去找到要刪除的結點  targetNode
			Node targetNode = search(value);
			//如果沒有找到要刪除的結點
			if(targetNode ==null){
				return;
			}
			//如果我們發現當前這顆二叉排序樹只有一個結點
			if(root.left == null && root.right == null) {
				root = null;
				return;
			}
			//去找到targetNode的父結點
			Node parent = searchP(value);
			//如果要刪除的節點爲葉子節點
			if(targetNode.left == null && targetNode.right == null){
				//判斷targetNode是父節點的左子結點,還是右子節點
				if(parent.left != null && parent.left.value == value){	//左子節點
					parent.left = null;
				}else if(parent.right != null && parent.right.value == value){	//右子節點
					parent.right = null;
				}
			}else if(targetNode.left != null && targetNode.right != null){	//刪除有兩顆子樹的節點
				int minVa = delRightT(targetNode.right);
				targetNode.value = minVa;
			}else{	//刪除只有一個字樹的節點
				//如果要刪除的結點有左子結點 
				if(targetNode.left != null) {
					if(parent != null) {
						//如果 targetNode 是 parent 的左子結點
						if(parent.left.value == value) {
							parent.left = targetNode.left;
						} else {	//targetNode 是 parent 的右子結點
							parent.right = targetNode.left;
						} 
					} else {
						root = targetNode.left;
					}
				}else{	//如果要刪除的結點有右子結點 
					if(parent != null){
						//如果 targetNode 是 parent 的左子結點
						if(parent.left.value == value){
							parent.left = targetNode.right;
						}else{	//如果 targetNode 是 parent 的右子結點
							parent.right = targetNode.right;
						}
					}else{
						root = targetNode.right;
					}
				}
			}
		}
	}
	
	//編寫方法
	//1.返回的 以node 爲根結點的二叉排序樹的最小結點的值
	//2.刪除node 爲根結點的二叉排序樹的最小結點
	/**
	  * 
	  * @Description 
	  * @author subei
	  * @date 2020年6月13日上午10:44:31
	  * @param node 傳入的結點(爲二叉排序樹的根結點)
	  * @return 返回的 以node 爲根結點的二叉排序樹的最小結點的值
	 */
	public int delRightT(Node node){
		Node tar = node;
		//循環的查找左子節點,就會找到最小值
		while(tar.left != null){
			tar = tar.left;
		}
		//這時 target就指向了最小結點
		//刪除最小結點
		delNode(tar.value);
		return tar.value;
	}
}

AVL樹左旋轉代碼實現

public class AVLTreeTest {

	public static void main(String[] args) {
		int[] arr = { 4, 3, 6, 5, 7, 8 };
		// 創建一個 AVLTree對象
		AVLTree avlTree = new AVLTree();

		// 添加結點
		for (int i = 0; i < arr.length; i++) {
			avlTree.add(new Node(arr[i]));
		}

		// 中序遍歷
		System.out.println("中序遍歷:");
		avlTree.infixOrder(); // 3,4,5,6,7,8

		System.out.println("經過平衡處理的樹:");
		System.out.println("樹的高度:" + avlTree.getRoot().height()); // 3
		System.out.println("樹的左子樹高度:" + avlTree.getRoot().leftHeight()); // 2
		System.out.println("樹的右子樹高度:" + avlTree.getRoot().rightHeight()); // 2
		
	}

}

// 創建Node結點
class Node {
	int value;
	Node left;
	Node right;

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

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

	// 添加節點的方法
	// 遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node) {
		if (node == null) {
			return;
		}

		// 判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if (node.value < this.value) {
			if (this.left == null) { // 如果當前結點左子結點爲null
				this.left = node;
			} else {
				// 遞歸的向左子樹添加
				this.left.add(node);
			}
		} else { // 添加的節點的值大於當前結點的值
			if (this.right == null) {
				this.right = node;
			} else {
				// 遞歸的向右子樹添加
				this.right.add(node);
			}
		}
		
		//當添加完一個結點後,如果: (右子樹的高度-左子樹的高度) > 1 , 左旋轉
		if(rightHeight() - leftHeight() > 1) {
			leftRate();//左旋轉
		}
		
	}

	// 中序遍歷
	public void infixOrder() {
		if (this.left != null) {
			this.left.infixOrder();
		}
		System.out.println(this);
		if (this.right != null) {
			this.right.infixOrder();
		}
	}

	// 查找要刪除的節點
	/**
	 * 
	 * @Description
	 * @author subei
	 * @date 2020年6月13日上午8:43:01
	 * @param value
	 *            希望刪除的結點的值
	 * @return 如果找到該值返回,未找到返回null
	 */
	public Node search(int value) {
		if (value == this.value) { // 說明找到了
			return this;
		} else if (value < this.value) { // 查找的值小於當前結點的值,向左子樹查找
			if (this.left == null) { // 左子結點爲空
				return null;
			}
			return this.left.search(value);
		} else { // 查找的值不小於當前結點的值,向右子樹查找
			if (this.right == null) {
				return null;
			}
			return this.right.search(value);
		}
	}

	// 查找要刪除結點的父結點
	/**
	 * 
	 * @param value
	 *            希望刪除的結點的值
	 * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
	 */
	public Node searchP(int value) {
		// 如果當前結點是要刪除的結點的父結點,如果沒有就返回null
		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.searchP(value); // 向左子樹查找
			} else if (value >= this.value && this.right != null) {
				return this.right.searchP(value); // 向右子樹遞歸查找
			} else {
				return null; // 未找到父結點
			}
		}
	}

	// 返回以該結點爲根結點的樹的高度
	public int height() {
		return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
	}

	// 返回左子樹的高度
	public int leftHeight() {
		if (left == null) {
			return 0;
		}
		return left.height();
	}

	// 返回右子樹的高度
	public int rightHeight() {
		if (right == null) {
			return 0;
		}
		return right.height();
	}

	// 左旋轉方法
	public void leftRate() {
		// 創建新的結點,以當前根結點的值
		Node newNode = new Node(value);
		// 把新的結點的左子樹設置成當前結點的左子樹
		newNode.left = left;
		// 把新的結點的右子樹設置成帶你過去結點的右子樹的左子樹
		newNode.right = right.left;
		// 把當前結點的值替換成右子結點的值
		value = right.value;
		// 把當前結點的右子樹設置成當前結點右子樹的右子樹
		right = right.right;
		// 把當前結點的左子樹(左子結點)設置成新的結點
		left = newNode;
	}

}

// 創建AVL樹
class AVLTree {
	private Node root;

	public Node getRoot() {
		return root;
	}

	// 添加結點的方法
	public void add(Node node) {
		if (root == null) {
			root = node; // 如果root爲空則直接讓root指向node
		} else {
			root.add(node);
		}
	}

	// 遍歷方法
	public void infixOrder() {
		if (root != null) {
			root.infixOrder();
		} else {
			System.out.println("二叉排序樹爲空!!!");
		}
	}

	// 查找要刪除的結點
	public Node search(int value) {
		if (root == null) {
			return null;
		} else {
			return root.search(value);
		}
	}

	// 查找要刪除的節點的父節點
	public Node searchP(int value) {
		if (root == null) {
			return null;
		} else {
			return root.searchP(value);
		}
	}

	// 刪除節點
	public void delNode(int value) {
		if (root == null) {
			return;
		} else {
			// 1.需求先去找到要刪除的結點 targetNode
			Node targetNode = search(value);
			// 如果沒有找到要刪除的結點
			if (targetNode == null) {
				return;
			}
			// 如果我們發現當前這顆二叉排序樹只有一個結點
			if (root.left == null && root.right == null) {
				root = null;
				return;
			}
			// 去找到targetNode的父結點
			Node parent = searchP(value);
			// 如果要刪除的節點爲葉子節點
			if (targetNode.left == null && targetNode.right == null) {
				// 判斷targetNode是父節點的左子結點,還是右子節點
				if (parent.left != null && parent.left.value == value) { // 左子節點
					parent.left = null;
				} else if (parent.right != null && parent.right.value == value) { // 右子節點
					parent.right = null;
				}
			} else if (targetNode.left != null && targetNode.right != null) { // 刪除有兩顆子樹的節點
				int minVa = delRightT(targetNode.right);
				targetNode.value = minVa;
			} else { // 刪除只有一個字樹的節點
				// 如果要刪除的結點有左子結點
				if (targetNode.left != null) {
					if (parent != null) {
						// 如果 targetNode 是 parent 的左子結點
						if (parent.left.value == value) {
							parent.left = targetNode.left;
						} else { // targetNode 是 parent 的右子結點
							parent.right = targetNode.left;
						}
					} else {
						root = targetNode.left;
					}
				} else { // 如果要刪除的結點有右子結點
					if (parent != null) {
						// 如果 targetNode 是 parent 的左子結點
						if (parent.left.value == value) {
							parent.left = targetNode.right;
						} else { // 如果 targetNode 是 parent 的右子結點
							parent.right = targetNode.right;
						}
					} else {
						root = targetNode.right;
					}
				}
			}
		}
	}

	// 編寫方法
	// 1.返回的 以node 爲根結點的二叉排序樹的最小結點的值
	// 2.刪除node 爲根結點的二叉排序樹的最小結點
	/**
	 * 
	 * @Description
	 * @author subei
	 * @date 2020年6月13日上午10:44:31
	 * @param node
	 *            傳入的結點(爲二叉排序樹的根結點)
	 * @return 返回的 以node 爲根結點的二叉排序樹的最小結點的值
	 */
	public int delRightT(Node node) {
		Node tar = node;
		// 循環的查找左子節點,就會找到最小值
		while (tar.left != null) {
			tar = tar.left;
		}
		// 這時 target就指向了最小結點
		// 刪除最小結點
		delNode(tar.value);
		return tar.value;
	}
}

上面的左旋轉,僅僅是左旋轉,考慮並不完全,完整的旋轉代碼,參考下方的雙旋轉!!!

AVL樹右旋轉圖解和實現

應用案例-單旋轉(右旋轉)

1.要求: 給你一個數列,創建出對應的平衡二叉樹.數列 {10,12, 8, 9, 7, 6}

2.思路分析(示意圖)

問題:當插入6leftHeight()  - rightHeight()  > 1 成立,此時,不再是一顆avl樹了.

怎麼處理 --> 進行右旋轉.[就是降低左子樹的高度], 這裏是將9 這個節點,通過右旋轉,到右子樹

在這裏插入圖片描述

1. 創建一個新的節點 newNode (10這個值創建),創建一個新的節點,值等於當前根節點的值

//把新節點的右子樹設置了當前節點的右子樹

在這裏插入圖片描述

2. newNode.right = right 
//把新節點的左子樹設置爲當前節點的左子樹的右子樹

在這裏插入圖片描述

3. newNode.left =left.right;
//把當前節點的值換爲左子節點的值

在這裏插入圖片描述

4.value=left.value; 
//把當前節點的左子樹設置成左子樹的左子樹

在這裏插入圖片描述

5. left=left.left;
//把當前節點的右子樹設置爲新節點

在這裏插入圖片描述

6. right=newLeft;

在這裏插入圖片描述

源自網絡的動圖:

在這裏插入圖片描述

代碼實現如下:

public class AVLTreeTest {

	public static void main(String[] args) {
//		int[] arr = { 4, 3, 6, 5, 7, 8 };
		int arr[] = { 10,12, 8, 9, 7, 6};
		// 創建一個 AVLTree對象
		AVLTree avlTree = new AVLTree();

		// 添加結點
		for (int i = 0; i < arr.length; i++) {
			avlTree.add(new Node(arr[i]));
		}

		// 中序遍歷
		System.out.println("中序遍歷:");
		avlTree.infixOrder(); // 3,4,5,6,7,8

		System.out.println("經過平衡處理的樹:");
		System.out.println("樹的高度:" + avlTree.getRoot().height()); // 3
		System.out.println("樹的左子樹高度:" + avlTree.getRoot().leftHeight()); // 2
		System.out.println("樹的右子樹高度:" + avlTree.getRoot().rightHeight()); // 2
		System.out.println("當前的根節點:" + avlTree.getRoot());
	}

}

// 創建Node結點
class Node {
	int value;
	Node left;
	Node right;

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

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

	// 添加節點的方法
	// 遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node) {
		if (node == null) {
			return;
		}

		// 判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if (node.value < this.value) {
			if (this.left == null) { // 如果當前結點左子結點爲null
				this.left = node;
			} else {
				// 遞歸的向左子樹添加
				this.left.add(node);
			}
		} else { // 添加的節點的值大於當前結點的值
			if (this.right == null) {
				this.right = node;
			} else {
				// 遞歸的向右子樹添加
				this.right.add(node);
			}
		}
		
		//當添加完一個結點後,如果: (右子樹的高度-左子樹的高度) > 1 , 左旋轉
		if(rightHeight() - leftHeight() > 1) {
			leftRate(); //左旋轉
		}
		
		//當添加完一個結點後,如果 (左子樹的高度 - 右子樹的高度) > 1, 右旋轉
		if(leftHeight() - rightHeight() > 1) {
			rightRotate(); //右旋轉
		}
		
	}

	// 中序遍歷
	public void infixOrder() {
		if (this.left != null) {
			this.left.infixOrder();
		}
		System.out.println(this);
		if (this.right != null) {
			this.right.infixOrder();
		}
	}

	// 查找要刪除的節點
	/**
	 * 
	 * @Description
	 * @author subei
	 * @date 2020年6月13日上午8:43:01
	 * @param value
	 *            希望刪除的結點的值
	 * @return 如果找到該值返回,未找到返回null
	 */
	public Node search(int value) {
		if (value == this.value) { // 說明找到了
			return this;
		} else if (value < this.value) { // 查找的值小於當前結點的值,向左子樹查找
			if (this.left == null) { // 左子結點爲空
				return null;
			}
			return this.left.search(value);
		} else { // 查找的值不小於當前結點的值,向右子樹查找
			if (this.right == null) {
				return null;
			}
			return this.right.search(value);
		}
	}

	// 查找要刪除結點的父結點
	/**
	 * 
	 * @param value
	 *            希望刪除的結點的值
	 * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
	 */
	public Node searchP(int value) {
		// 如果當前結點是要刪除的結點的父結點,如果沒有就返回null
		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.searchP(value); // 向左子樹查找
			} else if (value >= this.value && this.right != null) {
				return this.right.searchP(value); // 向右子樹遞歸查找
			} else {
				return null; // 未找到父結點
			}
		}
	}

	// 返回以該結點爲根結點的樹的高度
	public int height() {
		return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
	}

	// 返回左子樹的高度
	public int leftHeight() {
		if (left == null) {
			return 0;
		}
		return left.height();
	}

	// 返回右子樹的高度
	public int rightHeight() {
		if (right == null) {
			return 0;
		}
		return right.height();
	}

	// 左旋轉方法
	public void leftRate() {
		// 創建新的結點,以當前根結點的值
		Node newNode = new Node(value);
		// 把新的結點的左子樹設置成當前結點的左子樹
		newNode.left = left;
		// 把新的結點的右子樹設置成帶你過去結點的右子樹的左子樹
		newNode.right = right.left;
		// 把當前結點的值替換成右子結點的值
		value = right.value;
		// 把當前結點的右子樹設置成當前結點右子樹的右子樹
		right = right.right;
		// 把當前結點的左子樹(左子結點)設置成新的結點
		left = newNode;
	}

	//右旋轉
	public void rightRotate(){
		Node newNode = new Node(value);
		newNode.right = right;
		newNode.left = left.right;
		value = left.value;
		left = left.left;
		right = newNode;
	}
	
}

// 創建AVL樹
class AVLTree {
	private Node root;

	public Node getRoot() {
		return root;
	}

	// 添加結點的方法
	public void add(Node node) {
		if (root == null) {
			root = node; // 如果root爲空則直接讓root指向node
		} else {
			root.add(node);
		}
	}

	// 遍歷方法
	public void infixOrder() {
		if (root != null) {
			root.infixOrder();
		} else {
			System.out.println("二叉排序樹爲空!!!");
		}
	}

	// 查找要刪除的結點
	public Node search(int value) {
		if (root == null) {
			return null;
		} else {
			return root.search(value);
		}
	}

	// 查找要刪除的節點的父節點
	public Node searchP(int value) {
		if (root == null) {
			return null;
		} else {
			return root.searchP(value);
		}
	}

	// 刪除節點
	public void delNode(int value) {
		if (root == null) {
			return;
		} else {
			// 1.需求先去找到要刪除的結點 targetNode
			Node targetNode = search(value);
			// 如果沒有找到要刪除的結點
			if (targetNode == null) {
				return;
			}
			// 如果我們發現當前這顆二叉排序樹只有一個結點
			if (root.left == null && root.right == null) {
				root = null;
				return;
			}
			// 去找到targetNode的父結點
			Node parent = searchP(value);
			// 如果要刪除的節點爲葉子節點
			if (targetNode.left == null && targetNode.right == null) {
				// 判斷targetNode是父節點的左子結點,還是右子節點
				if (parent.left != null && parent.left.value == value) { // 左子節點
					parent.left = null;
				} else if (parent.right != null && parent.right.value == value) { // 右子節點
					parent.right = null;
				}
			} else if (targetNode.left != null && targetNode.right != null) { // 刪除有兩顆子樹的節點
				int minVa = delRightT(targetNode.right);
				targetNode.value = minVa;
			} else { // 刪除只有一個字樹的節點
				// 如果要刪除的結點有左子結點
				if (targetNode.left != null) {
					if (parent != null) {
						// 如果 targetNode 是 parent 的左子結點
						if (parent.left.value == value) {
							parent.left = targetNode.left;
						} else { // targetNode 是 parent 的右子結點
							parent.right = targetNode.left;
						}
					} else {
						root = targetNode.left;
					}
				} else { // 如果要刪除的結點有右子結點
					if (parent != null) {
						// 如果 targetNode 是 parent 的左子結點
						if (parent.left.value == value) {
							parent.left = targetNode.right;
						} else { // 如果 targetNode 是 parent 的右子結點
							parent.right = targetNode.right;
						}
					} else {
						root = targetNode.right;
					}
				}
			}
		}
	}

	// 編寫方法
	// 1.返回的 以node 爲根結點的二叉排序樹的最小結點的值
	// 2.刪除node 爲根結點的二叉排序樹的最小結點
	/**
	 * 
	 * @Description
	 * @author subei
	 * @date 2020年6月13日上午10:44:31
	 * @param node
	 *            傳入的結點(爲二叉排序樹的根結點)
	 * @return 返回的 以node 爲根結點的二叉排序樹的最小結點的值
	 */
	public int delRightT(Node node) {
		Node tar = node;
		// 循環的查找左子節點,就會找到最小值
		while (tar.left != null) {
			tar = tar.left;
		}
		// 這時 target就指向了最小結點
		// 刪除最小結點
		delNode(tar.value);
		return tar.value;
	}
}

上面的右旋轉,僅僅是右旋轉,考慮並不完全,完整的旋轉代碼,參考下方的雙旋轉!!!

AVL樹雙旋轉圖解和實現

應用案例-雙旋轉

前面的兩個數列,進行單旋轉(即一次旋轉)就可以將非平衡二叉樹轉成平衡二叉樹,但是在某些情況下,單旋轉不能完成平衡二叉樹的轉換。比如數列

int[] arr = { 10, 11, 7, 6, 8, 9 }; 運行原來的代碼可以看到,並沒有轉成AVL樹.

int[]arr= {2,1,6,5,7,3}; // 運行原來的代碼可以看到,並沒有轉成 AVL樹

在這裏插入圖片描述

問題分析:  
在滿足右旋轉條件時,要判斷:
(1)如果 是 左子樹的 右子樹高度 大於左子樹的左子樹時:
(2)就是 對  當前根節點的左子樹,先進行 左旋轉,
(3)然後, 再對當前根節點進行右旋轉即可

否則,直接對當前節點(根節點)進行右旋轉.即可.
  1. 先對當前節點的左子樹,進行左旋轉

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

  1. 再對當前節點,進行右旋轉

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

具體代碼分析:

public class AVLTreeTest {

	public static void main(String[] args) {
//		int[] arr = { 4, 3, 6, 5, 7, 8 };
		int arr[] = { 10,12, 8, 9, 7, 6};
		// 創建一個 AVLTree對象
		AVLTree avlTree = new AVLTree();

		// 添加結點
		for (int i = 0; i < arr.length; i++) {
			avlTree.add(new Node(arr[i]));
		}

		// 中序遍歷
		System.out.println("中序遍歷:");
		avlTree.infixOrder(); // 3,4,5,6,7,8

		System.out.println("經過平衡處理的樹:");
		System.out.println("樹的高度:" + avlTree.getRoot().height()); // 3
		System.out.println("樹的左子樹高度:" + avlTree.getRoot().leftHeight()); // 2
		System.out.println("樹的右子樹高度:" + avlTree.getRoot().rightHeight()); // 2
		System.out.println("當前的根節點:" + avlTree.getRoot());
	}

}

// 創建Node結點
class Node {
	int value;
	Node left;
	Node right;

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

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

	// 添加節點的方法
	// 遞歸的形式添加結點,注意需要滿足二叉排序樹的要求
	public void add(Node node) {
		if (node == null) {
			return;
		}

		// 判斷傳入的結點的值,和當前子樹的根結點的值的關係
		if (node.value < this.value) {
			if (this.left == null) { // 如果當前結點左子結點爲null
				this.left = node;
			} else {
				// 遞歸的向左子樹添加
				this.left.add(node);
			}
		} else { // 添加的節點的值大於當前結點的值
			if (this.right == null) {
				this.right = node;
			} else {
				// 遞歸的向右子樹添加
				this.right.add(node);
			}
		}
		
		//當添加完一個結點後,如果: (右子樹的高度-左子樹的高度) > 1 , 左旋轉
		if(rightHeight() - leftHeight() > 1) {
			//如果它的右子樹的左子樹的高度大於它的右子樹的右子樹的高度
			if(right != null && right.leftHeight() > right.rightHeight()) {
				//先對右子結點進行右旋轉
				right.rightRotate();
				//然後在對當前結點進行左旋轉
				leftRate(); //左旋轉
			} else {
				//直接進行左旋轉即可
				leftRate();
			}
			return; //重要!!!
		}
		
		//當添加完一個結點後,如果 (左子樹的高度 - 右子樹的高度) > 1, 右旋轉
		if(leftHeight() - rightHeight() > 1) {
			//如果它的左子樹的右子樹高度大於它的左子樹的高度
			if(left != null && left.rightHeight() > left.leftHeight()){
				//先對當前結點的左結點(左子樹)->左旋轉
				left.leftRate();
				//再對當前結點進行右旋轉
				rightRotate();
			}else{
				//直接進行右旋轉即可
				rightRotate();
			}
		}
		
	}

	// 中序遍歷
	public void infixOrder() {
		if (this.left != null) {
			this.left.infixOrder();
		}
		System.out.println(this);
		if (this.right != null) {
			this.right.infixOrder();
		}
	}

	// 查找要刪除的節點
	/**
	 * 
	 * @Description
	 * @author subei
	 * @date 2020年6月13日上午8:43:01
	 * @param value
	 *            希望刪除的結點的值
	 * @return 如果找到該值返回,未找到返回null
	 */
	public Node search(int value) {
		if (value == this.value) { // 說明找到了
			return this;
		} else if (value < this.value) { // 查找的值小於當前結點的值,向左子樹查找
			if (this.left == null) { // 左子結點爲空
				return null;
			}
			return this.left.search(value);
		} else { // 查找的值不小於當前結點的值,向右子樹查找
			if (this.right == null) {
				return null;
			}
			return this.right.search(value);
		}
	}

	// 查找要刪除結點的父結點
	/**
	 * 
	 * @param value
	 *            希望刪除的結點的值
	 * @return 返回的是要刪除的結點的父結點,如果沒有就返回null
	 */
	public Node searchP(int value) {
		// 如果當前結點是要刪除的結點的父結點,如果沒有就返回null
		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.searchP(value); // 向左子樹查找
			} else if (value >= this.value && this.right != null) {
				return this.right.searchP(value); // 向右子樹遞歸查找
			} else {
				return null; // 未找到父結點
			}
		}
	}

	// 返回以該結點爲根結點的樹的高度
	public int height() {
		return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
	}

	// 返回左子樹的高度
	public int leftHeight() {
		if (left == null) {
			return 0;
		}
		return left.height();
	}

	// 返回右子樹的高度
	public int rightHeight() {
		if (right == null) {
			return 0;
		}
		return right.height();
	}

	// 左旋轉方法
	public void leftRate() {
		// 創建新的結點,以當前根結點的值
		Node newNode = new Node(value);
		// 把新的結點的左子樹設置成當前結點的左子樹
		newNode.left = left;
		// 把新的結點的右子樹設置成帶你過去結點的右子樹的左子樹
		newNode.right = right.left;
		// 把當前結點的值替換成右子結點的值
		value = right.value;
		// 把當前結點的右子樹設置成當前結點右子樹的右子樹
		right = right.right;
		// 把當前結點的左子樹(左子結點)設置成新的結點
		left = newNode;
	}

	//右旋轉
	public void rightRotate(){
		Node newNode = new Node(value);
		newNode.right = right;
		newNode.left = left.right;
		value = left.value;
		left = left.left;
		right = newNode;
	}
	
}

// 創建AVL樹
class AVLTree {
	private Node root;

	public Node getRoot() {
		return root;
	}

	// 添加結點的方法
	public void add(Node node) {
		if (root == null) {
			root = node; // 如果root爲空則直接讓root指向node
		} else {
			root.add(node);
		}
	}

	// 遍歷方法
	public void infixOrder() {
		if (root != null) {
			root.infixOrder();
		} else {
			System.out.println("二叉排序樹爲空!!!");
		}
	}

	// 查找要刪除的結點
	public Node search(int value) {
		if (root == null) {
			return null;
		} else {
			return root.search(value);
		}
	}

	// 查找要刪除的節點的父節點
	public Node searchP(int value) {
		if (root == null) {
			return null;
		} else {
			return root.searchP(value);
		}
	}

	// 刪除節點
	public void delNode(int value) {
		if (root == null) {
			return;
		} else {
			// 1.需求先去找到要刪除的結點 targetNode
			Node targetNode = search(value);
			// 如果沒有找到要刪除的結點
			if (targetNode == null) {
				return;
			}
			// 如果我們發現當前這顆二叉排序樹只有一個結點
			if (root.left == null && root.right == null) {
				root = null;
				return;
			}
			// 去找到targetNode的父結點
			Node parent = searchP(value);
			// 如果要刪除的節點爲葉子節點
			if (targetNode.left == null && targetNode.right == null) {
				// 判斷targetNode是父節點的左子結點,還是右子節點
				if (parent.left != null && parent.left.value == value) { // 左子節點
					parent.left = null;
				} else if (parent.right != null && parent.right.value == value) { // 右子節點
					parent.right = null;
				}
			} else if (targetNode.left != null && targetNode.right != null) { // 刪除有兩顆子樹的節點
				int minVa = delRightT(targetNode.right);
				targetNode.value = minVa;
			} else { // 刪除只有一個字樹的節點
				// 如果要刪除的結點有左子結點
				if (targetNode.left != null) {
					if (parent != null) {
						// 如果 targetNode 是 parent 的左子結點
						if (parent.left.value == value) {
							parent.left = targetNode.left;
						} else { // targetNode 是 parent 的右子結點
							parent.right = targetNode.left;
						}
					} else {
						root = targetNode.left;
					}
				} else { // 如果要刪除的結點有右子結點
					if (parent != null) {
						// 如果 targetNode 是 parent 的左子結點
						if (parent.left.value == value) {
							parent.left = targetNode.right;
						} else { // 如果 targetNode 是 parent 的右子結點
							parent.right = targetNode.right;
						}
					} else {
						root = targetNode.right;
					}
				}
			}
		}
	}

	// 編寫方法
	// 1.返回的 以node 爲根結點的二叉排序樹的最小結點的值
	// 2.刪除node 爲根結點的二叉排序樹的最小結點
	/**
	 * 
	 * @Description
	 * @author subei
	 * @date 2020年6月13日上午10:44:31
	 * @param node
	 *            傳入的結點(爲二叉排序樹的根結點)
	 * @return 返回的 以node 爲根結點的二叉排序樹的最小結點的值
	 */
	public int delRightT(Node node) {
		Node tar = node;
		// 循環的查找左子節點,就會找到最小值
		while (tar.left != null) {
			tar = tar.left;
		}
		// 這時 target就指向了最小結點
		// 刪除最小結點
		delNode(tar.value);
		return tar.value;
	}
}

本章思維導圖

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章