二叉搜索樹和二叉樹的代碼重構

BST繼承自二叉樹

在這裏插入圖片描述

代碼

二叉樹

import java.util.LinkedList;
import java.util.Queue;
@SuppressWarnings("unchecked")
// 實現BinaryTreeInfo接口是爲了使用打印二叉樹的工具,非必須
public class BinaryTree<E> implements BinaryTreeInfo {
	protected int size; // 元素數量
	protected Node<E> root; // 根節點
	
	/**
	 * 訪問器接口 ——> 訪問器抽象類
	 * 增強遍歷接口
	 */
	/*public static interface Visitor<E>{
		void visit(E element);
	}*/
	public static abstract class Visitor<E> {
		boolean stop;
		// 如果返回true,就代表停止遍歷
		public abstract boolean visit(E element);
	}
	/**
	 * 內部類,節點類
	 */
	public static class Node<E> {
		E element; // 元素值
		Node<E> left; // 左節點
		Node<E> right; // 右節點
		Node<E> parent; // 父節點

		public Node(E element, Node<E> parent) {
			this.element = element;
			this.parent = parent;
		}
		public boolean isLeaf(){ // 是否葉子節點
			return left==null && right==null;
		}
		public boolean hasTwoChildren(){ // 是否有兩個子節點
			return left!=null && right!=null;
		}
	}
	/**
	 * 元素的數量
	 */
	public int size() {
		return size;
	}
	/**
	 * 是否爲空
	 */
	public boolean isEmpty() {
		return size == 0;
	}
	/**
	 * 清空所有的元素
	 */
	public void clear() {
		root = null;
		size = 0;
	}
	/**
	 * 前序遍歷
	 */
	public void preorder(Visitor<E> visitor){
		if (visitor == null) return;
		preorder(root, visitor);
	}
	public void preorder(Node<E> node, Visitor<E> visitor){
		if(node == null || visitor.stop) return;
		// 根
		visitor.stop = visitor.visit(node.element);
		// 左
		preorder(node.left, visitor);
		// 右
		preorder(node.right, visitor);
	}
	/**
	 * 中序遍歷
	 * @param visitor
	 */
	public void inorder(Visitor<E> visitor){
		if (visitor == null) return;
		inorder(root, visitor);
	}
	public void inorder(Node<E> node, Visitor<E> visitor){
		if(node == null || visitor.stop) return;
		// 左
		inorder(node.left, visitor);
		// 根
		if (visitor.stop) return;
		visitor.stop = visitor.visit(node.element);
		// 右
		inorder(node.right, visitor);
	}
	/**
	 * 後序遍歷
	 */
	public void postorder(Visitor<E> visitor){
		if(visitor == null) return;
		postorder(root, visitor);
	}
	public void postorder(Node<E> node, Visitor<E> visitor){
		if(node == null || visitor.stop) return;
		// 左
		postorder(node.left, visitor);
		// 右
		postorder(node.right, visitor);
		// 根
		if(visitor.stop) return;
		visitor.stop = visitor.visit(node.element);
	}
	/**
	 * 層次遍歷
	 */
	public void levelOrder(Visitor<E> visitor){
		if(root == null || visitor.stop) return;
		Queue<Node<E>> queue = new LinkedList<>(); // 隊列
		queue.offer(root);
		
		while(!queue.isEmpty()){
			Node<E> node = queue.poll();
			if(visitor.visit(node.element)) return;
			
			if(node.left != null) {
				queue.offer(node.left);
			}
			if(node.right != null) {
				queue.offer(node.right);
			}
		}
	}
	/**
	 * 高度(遞歸)
	 */
	public int height1(){
		return height1(root);
	}
	public int height1(Node<E> node){
		if(node == null) return 0;
		return 1 + Math.max(height1(node.left), height1(node.right));
	}
	/**
	 * 高度(迭代)
	 */
	public int height(){
		if(root == null) return 0;
		int levelSize = 1; // 存儲每一層的元素數量	
 		int height = 0; // 樹的高度
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(root);
		
		while(!queue.isEmpty()){	
			Node<E> node = queue.poll();
			levelSize--;
			if(node.left != null) {
				queue.offer(node.left);
			}
			if(node.right != null) {
				queue.offer(node.right);
			}
			if(levelSize == 0){ // 即將要訪問下一層
				levelSize = queue.size();
				height++;
			}
		}
		return height;
	}
	/**
	 * 是否是完全二叉樹
	 */
	public boolean isComplete(){
		if(root == null){
			return false;
		}
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(root);
		
		boolean leaf = false;
		while(!queue.isEmpty()){
			Node<E> node = queue.poll();
			if(leaf && !node.isLeaf()){ // 要求是葉子結點,但是當前節點不是葉子結點
				return false;
			}
			if(node.left != null){
				queue.offer(node.left);
			}else if(node.right != null){
				// node.left==null && node.right!=null
				return false;
			}
			if(node.right != null){
				queue.offer(node.right);
			}else{
				// node.left==null && node.right==null
				// node.left!=null && node.right==null
				leaf = true; // 要求後面都是葉子節點
			}
		}
		return true;
	}
	/**
	 * 前驅節點:中序遍歷時的前一個節點
	 * 求前驅節點
	 */
	protected Node<E> predecessor(Node<E> node) {
		if(node == null) return null;
		
		// 前驅節點在左子樹中(left.right.right.right....)
		if(node.left != null ){ // 左子樹不爲空,則找到它的最右節點
			Node<E> p = node.left;	
			while(node.right != null){
				p = p.right;
			}
			return p;
		}
		// 能來到這裏說明左子樹爲空
		// 當父節點不爲空,則順着父節點找,直到找到【某結點爲父節點的右子節點】時
		while(node.parent != null && node.parent.left==node){
			node = node.parent;
		}
		// 來到這裏有以下兩種情況:
		// node.parent == null	無前驅,說明是根結點
		// node.parent.right == node 找到【某結點爲父節點的右子節點】
		return node.parent;
	}
	/**
	 * 後繼節點:中序遍歷時的後一個節點
	 * 求後繼節點
	 */
	protected Node<E> successor(Node<E> node) {
		if(node == null) return null;
		// 後繼節點與前驅節點正好相反
		
		// 後繼節點在右子樹中(node.right.left.left...)
		if(node.right != null){
			Node<E> p = node.right;
			while(p.left != null){
				p = p.left;
			}
			return p;
		}
		// 來到這裏說明沒有右節點
		// 當父節點不爲空,則順着父節點找,直到找到【某結點爲父節點的左子節點】時
		while(node.parent!=null && node.parent.right==node){
			node = node.parent;
		}
		
		// 來到這裏有以下兩種情況:
		// node.parent == null   無前驅,說明是根結點
		// node.parent.left == node  找到【某結點爲父節點的左子節點】
		return node.parent;
	}
	/**
	 * BinaryTreeInfo 工具,用來打印二叉樹
	 */
	@Override
	public Object root() {
		return root;
	}
	@Override
	public Object left(Object node) {
		return ((Node<E>)node).left;
	}
	@Override
	public Object right(Object node) {
		return ((Node<E>)node).right;
	}
	@Override
	public Object string(Object node) {
		Node<E> myNode = (Node<E>)node;
		String parentStr = "null";
		if(myNode.parent != null){
			parentStr = myNode.parent.element.toString();
		}
		return myNode.element + "_p(" + parentStr + ")";
	}
}

二叉搜索樹

import java.util.Comparator;

@SuppressWarnings("unchecked")
public class BinarySearchTree<E> extends BinaryTree<E> {
	
	// 比較器,根據傳入的比較器實現 compareTo() 方法
	private Comparator<E> comparator;
	
	public BinarySearchTree (Comparator<E> comparator){ // 可以傳一個比較器
		this.comparator = comparator;
	}
	public BinarySearchTree(){ // 不傳比較器,相當於傳入一個 null
		this(null); //
	}
	
	/**
	 * 添加元素
	 */
	public void add(E element) {
		elementNotNullCheck(element); // 不能傳入空節點
		// 傳入第一個節點
		if(root == null){
			root = new Node<>(element, null);
			size++;
			return;
		}
		Node<E> node = root;
		Node<E> parent = root;
		int cmp = 0;
		while(node != null){
			parent = node; // 父節點
			cmp = compareTo(node.element, element); // 方向
			if(cmp < 0){
				node = node.right;
			}else if(cmp > 0){
				node = node.left;
			}else{ // 相等,最好是覆蓋掉,也可以採取其他操作,看具體需求
				node.element = element;
				return;
			}
		}
		Node<E> newNode = new Node<>(element, parent);
		if(cmp < 0){
			parent.right = newNode;
		}else{
			parent.left = newNode;
		}
		size++;
	}
	/**
	 * 是否包含某元素
	 */
	public boolean contains(E element) {
		return node(element) != null;
	}
	/**
	 * 根據傳入的值刪除元素
	 */
	public void remove(E element) {
		remove(node(element));
	}
	// 根據節點刪除元素
	private void remove(Node<E> node) {
		if (node == null) return;
		
		size--;
		
		if (node.hasTwoChildren()) { // 度爲2的節點
			// 找到後繼節點
			Node<E> s = successor(node);
			// 用後繼節點的值覆蓋度爲2的節點的值
			node.element = s.element;
			// 刪除後繼節點
			node = s;
		}
		
		// 刪除node節點(node的度必然是1或者0)
		Node<E> replacement = node.left != null ? node.left : node.right;
		
		if (replacement != null) { // node是度爲1的節點
			// 更改parent
			replacement.parent = node.parent;
			// 更改parent的left、right的指向
			if (node.parent == null) { // node是度爲1的節點並且是根節點
				root = replacement;
			} else if (node == node.parent.left) {
				node.parent.left = replacement;
			} else { // node == node.parent.right
				node.parent.right = replacement;
			}
		} else if (node.parent == null) { // node是葉子節點並且是根節點
			root = null;
		} else { // node是葉子節點,但不是根節點
			if (node == node.parent.left) {
				node.parent.left = null;
			} else { // node == node.parent.right
				node.parent.right = null;
			}
		}
	}
	
	// 根據元素值獲取節點元素
	private Node<E> node(E element){
		elementNotNullCheck(element);
		
		Node<E> node = root;
		while(node != null){
			int cmp = compareTo(element, node.element);
			if(cmp < 0){
				node = node.left;
			}else if (cmp > 0){
				node = node.right;
			}else{ // cmp == 0
				return node;
			}
		}
		return null;
	}
	// 節點元素比較
	private int compareTo(E e1, E e2) {
		if (comparator != null) { // 傳入比較器則通過比較器比較
			return comparator.compare(e1, e2);
		}
		// 沒傳比較器,元素內部必須自行實現了 Comparable 接口
		return ((Comparable<E>)e1).compareTo(e2);
	}
	// 檢測傳入的節點是否爲空
	private void elementNotNullCheck(E element) {
		if (element == null) { // 不能傳入空節點
			throw new IllegalArgumentException("element must not null");
		}
	}
	
}

Main類main函數測試

import java.util.Comparator;

public class Main {
	
	public static void main(String[] args) {
		test2();
	}
	
	
	public static class PersonComparator implements Comparator<Person> { // 比較器
		@Override
		public int compare(Person e1, Person e2) {
			return e1.getAge() - e2.getAge();
		}
	}
	public static class PersonComparator2 implements Comparator<Person> {
		@Override
		public int compare(Person e1, Person e2) {
			return e2.getAge() - e1.getAge();
		}
	}
	
	// Integer類型的數據
	public static void test1(){
		Integer date[] = new Integer[] { 7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
		for (int i = 0; i < date.length; i++) {
			bst.add(date[i]);
		}
		BinaryTrees.println(bst);
	}

	// Person類型的數據
	public static void test2(){
		// Java,匿名類
		BinarySearchTree<Person> bst = new BinarySearchTree<>(new Comparator<Person>() {
			@Override
			public int compare(Person e1, Person e2) {
				return e2.getAge() - e1.getAge();
			}
		});
		Integer date[] = new Integer[] { 7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
		for (int i = 0; i < date.length; i++) {
			bst.add(new Person(date[i]));
		}
		BinaryTrees.println(bst);
	}
	
	// 保存打印結果到文件
	public static void test3(){
		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
		for(int i = 0; i < 40; i++){
			bst.add((int)(Math.random()*100));
		}
		String string = BinaryTrees.printString(bst);
		string += "\n";
		Files.writeToFile("F:/1.txt", string);
	}
	
	// add() 時值相等的處理
	public static void test4(){
		BinarySearchTree<Person> bst = new BinarySearchTree<>(new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return o1.getAge() - o2.getAge();
			}
		});
		bst.add(new Person(15, "jack"));
		bst.add(new Person(16, "rose"));
		bst.add(new Person(10, "jerry"));
		bst.add(new Person(10, "kali")); // add()時值相等最好覆蓋,否則則不會替換
		BinaryTrees.println(bst);
	}
	
	// 遍歷
	public static void test5(){
		Integer date[] = new Integer[] { 7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
		Person persons[] = new Person[10];
		BinarySearchTree<Person> bst = new BinarySearchTree<>(new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return o2.getAge() - o1.getAge();
			}
		});
		
		for(int i = 0; i < persons.length; i++){
			persons[i] = new Person(date[i]);
			bst.add(persons[i]);
		}
		BinaryTrees.println(bst);
		System.out.print("層次遍歷:");
		bst.levelOrder(new Visitor<Person>() {
			@Override
			public boolean visit(Person element) {
				System.out.print(element + " ");
				return false;
			}
		});
	}
	
	// 訪問器遍歷
	public static void test6(){
		Integer date[] = new Integer[] { 7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
		for (int i = 0; i < date.length; i++) {
			bst.add(date[i]);
		}
		BinaryTrees.print(bst);
		
		System.out.print("層次遍歷:");
		bst.levelOrder(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return false;
			}
		});
		System.out.println();
		
		System.out.print("前序遍歷:");
		bst.preorder(new Visitor<Integer>() {
			public boolean visit(Integer element) {
				System.out.print(element + " ");
//				return element == 2 ? true : false;
				return false;
			}
		});
		System.out.println();
		
		System.out.print("中序遍歷:");
		bst.inorder(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return false;
			}
		});
		System.out.println();
		
		System.out.print("後序遍歷:");
		bst.postorder(null);
		bst.postorder(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return false;
			}
		});
		
	}
	
	// 高度
	public static void test7(){
		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
		for(int i = 0; i < 20; i++){
			bst.add((int)(Math.random()*100));
		}
		BinaryTrees.print(bst);
		System.out.println();
//		System.out.println(bst.height()1);//遞歸求高度
		System.out.println(bst.height());
	}
	
	// 是否是完全二叉樹
	public static void test8(){
		/* 
		 *    7
		 *  4   8
		 *1   5 
		 * 
		 */
		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
		Integer date[] = new Integer[] { 7, 4, 8, 1, 5};
		for (int i = 0; i < date.length; i++) {
			bst.add(date[i]);
		}
		BinaryTrees.println(bst);
		System.out.println(bst.isComplete());
	}

	// 是否包含某個結點
	public static void test9(){
		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
		Integer date[] = new Integer[] { 7, 4, 8, 1, 5};
		for (int i = 0; i < date.length; i++) {
			bst.add(date[i]);
		}
		BinaryTrees.println(bst);
		System.out.println(bst.contains(6));
	}
	
	// 刪除節點
	public static void test10(){
		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
		Integer date[] = new Integer[] { 7, 4, 8, 1, 5};
		for (int i = 0; i < date.length; i++) {
			bst.add(date[i]);
		}
		BinaryTrees.println(bst);
		bst.remove(8);
		BinaryTrees.println(bst);
	}
	
}

如果是用普通二叉樹類用來添加,不知道添加規則,所以沒有意義用二叉搜索樹類的規則添加元素纔有意義,在實際應用中,可自定義規則,比如當前後插入兩個相同的元素時,是選擇放在原來節點的右邊還是左邊或者是覆蓋,都可自定義

二叉搜索樹的複雜度

BST的添加刪除搜索,比較的次數與樹的高度有關,但與元素個數無關
如果二叉樹是滿二叉樹或完全二叉樹 ,複雜度=O(h)=O(logn)(其中n是節點個數,logn是比較次數)
二叉搜索樹只適合於隨機添加的數字,如果按順序添加就會退化成鏈表。
在這裏插入圖片描述
刪除2之後,原來根節點的左孩子也就變成了鏈表
在這裏插入圖片描述
平衡二叉樹就是通過平衡因子來使二叉排序樹維持平衡,從而防止退化成鏈表

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