二叉搜索樹描述

</pre><pre name="code" class="java">/*
 *  二叉查找樹
 */
public class BinarySearchTree {
	//根節點
	private TreeNode root = null;
	
	public static void main(String[] args) {
		BinarySearchTree bst = new BinarySearchTree();
		bst.insertTreeNode(new TreeNode(12));
		bst.insertTreeNode(new TreeNode(5));
		bst.insertTreeNode(new TreeNode(2));
		bst.insertTreeNode(new TreeNode(9));
		bst.insertTreeNode(new TreeNode(18));
		bst.insertTreeNode(new TreeNode(15));
		bst.insertTreeNode(new TreeNode(17));
		bst.insertTreeNode(new TreeNode(19));
		//打印生成的樹
		System.out.print("生成的樹爲:");
		bst.printTree();
		//打印最大值和最小值
		System.out.println("最大值爲:"+bst.searchMaximum(bst.root).key);
		System.out.println("最小值爲:"+bst.searchMinimum(bst.root).key);
		//插入數據
		bst.insertTreeNode(new TreeNode(13));
		System.out.print("插入13後的樹爲:");
		bst.printTree();
		//刪除數據
		bst.deleteTreeNode(bst.searchTreeNode(bst.root, 12));
		System.out.print("刪除12後的樹爲:");
		bst.printTree();	
		//查看後繼節點
		System.out.println("17的後繼節點爲:"+bst.searchSuccessor(bst.searchTreeNode(bst.root, 17)).key);
		//查看前驅節點
		System.out.println("17的前驅節點爲:"+bst.searchPredecessor(bst.searchTreeNode(bst.root, 17)).key);
	}
	
	//構造函數
	public BinarySearchTree() {
		this.root = null;
	}
	
	//遍歷二叉樹
	private void traverseTree(TreeNode x) {
		if(x != null) {
			traverseTree(x.left);
			System.out.print(x.key + " ");
			traverseTree(x.right);
		}
	}
	private void printTree() {
		traverseTree(root);
		System.out.println();
	}
	
	//查找鍵值爲key的樹節點
	private TreeNode searchTreeNode(TreeNode x,int key) {
		while(x != null && x.key != key) {
			if(key < x.key) {
				x = x.left;
			} else {
				x = x.right;
			}
		}
		return x;
	}
	
	//獲取最小鍵值節點
	private TreeNode searchMinimum(TreeNode x) {
		while(x.left != null) {
			x = x.left;
		}
		return x;
	}
	
	//獲取最大鍵值節點
	private TreeNode searchMaximum(TreeNode x) {
		while(x.right != null) {
			x = x.right;
		}
		return x;
	}
	
	//插入一個鍵值爲key的樹節點
	private void insertTreeNode(TreeNode z) {
		
		TreeNode y = null;
		TreeNode x = root;
		while(x != null) {
			y = x;					//y是x的parent節點
			if(z.key < x.key) {
				x = x.left;
			} else {
				x = x.right;
			}		
		}
		z.parent = y;
		if(y == null) {
			root = z;
		} else if(z.key < y.key) {
			y.left = z;
		} else {
			y.right = z;
		}
	}
	
	private void deleteTreeNode(TreeNode z) {
		if(z.left == null) {
			transplant(z,z.right);
		} else if(z.right == null) {
			transplant(z,z.left);
		} else {
			TreeNode y = searchMinimum(z.right);
			if(y.parent != z) {
				transplant(y,y.right);
				y.right = z.right;
				z.right.parent = y;
			}
			transplant(z,y);
			y.left = z.left;
			z.left.parent = y;
		}
	}
	
	private void transplant(TreeNode u,TreeNode v) {
		if(u.parent == null) {
			root = v;
		} else if(u == u.parent.left) {
			u.parent.left = v;
		} else {
			u.parent.right = v;
		}
		if(v != null) {
			v.parent = u.parent;
		}
	}
	
	//查找後繼節點
	private TreeNode searchSuccessor(TreeNode x) {
		if(x.right != null) {
			return searchMinimum(x.right);
		}
		TreeNode y = x.parent;
		while(y != null && x == y.right) {
			x = y;
			y = y.parent;
		}
		return y;
	}
	
	//查看前驅節點
	private TreeNode searchPredecessor(TreeNode x) {
		if(x.left != null) {
			return searchMaximum(x.left);
		}
		TreeNode y = x.parent;
		while(y != null && x == y.left) {
			x = y;
			y = y.parent;
		}
		return y;
	}
	
	//樹節點
	private static class TreeNode {	
		TreeNode left = null;
		TreeNode right = null;
		TreeNode parent = null;
		int key = 0;
		
		public TreeNode(int key) {
			this.key = key;
		}	
	}
}


運行的結果爲:

生成的樹爲:2 5 9 12 15 17 18 19 
最大值爲:19
最小值爲:2
插入13後的樹爲:2 5 9 12 13 15 17 18 19 
刪除12後的樹爲:2 5 9 13 15 17 18 19 
17的後繼節點爲:18
17的前驅節點爲:15




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