數據結構Java07【二叉排序樹(添加&查找&刪除-節點)】

目   錄

P49 4.22 二叉排序樹的概述

P50 4.23 創建二叉排序樹&添加節點

P51 4.24 二叉排序樹中查找節點

P52 4.25 刪除葉子節點

P53 4.26 刪除只有一顆子樹的節點

P54 4.27 刪除有兩顆子樹的節點

代碼彙總

1、Node.java

2、BinarySortTree.java

3、TestBinarySortTree.java


P49 4.22 二叉排序樹的概述

BinarySortTree:插入(創建節點、直接插入)、查找性能較好!

P50 4.23 創建二叉排序樹&添加節點

P51 4.24 二叉排序樹中查找節點

P52 4.25 刪除葉子節點

能在Tree裏實現的都在樹裏,需要考慮左右節點的纔會寫入Node中。

P53 4.26 刪除只有一顆子樹的節點

P54 4.27 刪除有兩顆子樹的節點

代碼彙總

1、Node.java

package demo11;

public 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 node
	 */
	public void midShow(Node node) {
		if (node == null) {
			return;
		}
		midShow(node.left);
		System.out.print(node.value + "、");
		midShow(node.right);
	}

	/**
	 * 查找節點
	 * 
	 * @param value2
	 */
	public Node search(int value) {
		if (this.value == value) {
			return this;
		} else if (value < this.value) {
			if (left == null) {
				return null;
			}
			return left.search(value);
		} else {
			if (right == null) {
				return null;
			}
			return right.search(value);
		}
	}

	/**
	 * 搜索父節點
	 * 
	 * @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.value > value && this.left != null) {
				return this.left.searchParent(value);
			} else if (this.value < value && this.right != null) {
				return this.right.searchParent(value);
			}
			return null;
		}
	}
}

2、BinarySortTree.java

package demo11;

public class BinarySortTree {
	Node root;

	/**
	 * 向二叉排序樹中添加節點
	 * @param node
	 */
	public void add(Node node) {
		// 如果是一顆空樹
		if (root == null) {
			root = node;
		} else {
			root.add(node);
		}
	}

	/**
	 * 中序遍歷二叉排序樹,從小到大的順序
	 */
	public void midShow() {
		if (root != null) {
			root.midShow(root);
		}
	}

	/**
	 * 節點的查找
	 * 
	 * @param value
	 * @return
	 */
	public Node search(int value) {
		if (root == null) {
			return null;
		} else {
			return root.search(value);
		}
	}

	/**
	 * 刪除節點
	 * 
	 * @param value
	 */
	public void delete(int value) {
		if (root == null) {
			return;
		} else {
			// 找到這個節點
			Node target = search(value);
			// 如果沒有這個節點
			if (target == null) {
				return;
			}
			// 找到他的父節點
			Node parent = searchParent(value);
			// 要刪除的節點是葉子節點
			if (target.left == null && target.right == null) {
				// 要刪除的節點是父節點的左子節點
				if (parent.left.value == value) {
					parent.left = null;
					// 要刪除的節點是父節點的右子節點
				} else {
					parent.right = null;
				}
				// 要刪除的節點有兩個子節點的情況
			} else if (target.left != null && target.right != null) {
				// 刪除右子樹中值最小的節點,並獲取到該節點的值
				int min = deleteMin(target.right);
				// 替換目標節點中的值
				target.value = min;
				// 要刪除的節點有一個左子節點或右子節點
			} else {
				// 有左子節點
				if (target.left != null) {
					// 要刪除的節點是父節點的左子節點
					if (parent.left.value == value) {
						parent.left = target.left;
						// 要刪除的節點是父節點的右子節點
					} else {
						parent.right = target.left;
					}
					// 有右子節點
				} else {
					// 要刪除的節點是父節點的左子節點
					if (parent.left.value == value) {
						parent.left = target.right;
						// 要刪除的節點是父節點的右子節點
					} else {
						parent.right = target.right;
					}
				}
			}
		}
	}

	/**
	 * 刪除一顆樹中最小的節點
	 * 
	 * @param right
	 * @return
	 */
	private int deleteMin(Node node) {
		Node target = node;
		// 遞歸向左找
		while (target.left != null) {
			target = target.left;
		}
		// 刪除最小的這個節點
		delete(target.value);
		return target.value;
	}

	/**
	 * 搜索父節點
	 * 
	 * @param value
	 * @return
	 */
	public Node searchParent(int value) {
		if (root == null) {
			return null;
		} else {
			return root.searchParent(value);
		}
	}
}

3、TestBinarySortTree.java

package demo11;

public class TestBinarySortTree {

	public static void main(String[] args) {
		int[] arr = new int[] { 7, 3, 10, 12, 5, 1, 9 };
		// 創建一顆二叉排序樹
		BinarySortTree bst = new BinarySortTree();
		// 循環添加
		for (int i : arr) {
			bst.add(new Node(i));
		}
//		// 查看樹中的值
//		bst.midShow();
//		System.out.println("-----");
//		// 查找
//		Node node = bst.search(10);
//		System.out.println(node.value);
//		
//		Node node2 = bst.search(20);
//		System.out.println(node2);
//		//測試查找父節點
//		Node p1 = bst.searchParent(12);
//		System.out.println(p1.value);
//		System.out.println("-----");
//		//刪除葉子節點
//		bst.delete(5);
//		bst.midShow();
//		System.out.println("===");
//		// 刪除只有一個子節點的節點
//		bst.delete(3);
		bst.midShow();
		// 刪除有兩個子節點的節點
		bst.delete(3);
		System.out.println("----");
		bst.midShow();
	}

}

嘿嘿嘿~

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