二分搜索树删除任一节点-Hubbard Deletion

二分搜索树删除任一节点-Hubbard Deletion_第1张图片

 

package com.xmg.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 二分搜索树---不一定是完全二叉树
 *
 * BST -- binary search tree
 *
 * 查找表的实现-字典数据结构
 *
 * 高效,查找,插入,删除都可以
 */
public class BinaryTree ,Value>{


	private class Node{
		private Key key;
		private Value value;
		private Node left;
		private Node right;
		private Node(Key key,Value value){
			this.key = key;
			this.value = value;
			this.left = this.right = null;
		}

		private Node(Node node){
			this.right = node.right;
			this.left = node.left;
			this.key = node.key;
			this.value = node.value;
		}

	}
	private Node root;
	private int count;
	public BinaryTree(){
		root = null;
		count = 0;
	}

	public int size(){
		return count;
	}

	boolean isEmpty(){
		return count == 0;
	}

	public void insert(Key key,Value value){
		root = insert(root,key,value);
	}

	public Value search(Key key){
		return search(root,key);
	}

	public void preOrder(){
		preOrder(root);
	}

	public void inOrder(){
		inOrder(root);
	}

	public void postOrder(){
		postOrder(root);
	}



	/**
	 * 是否包含key值
	 * @param key
	 * @return
	 */
	public boolean contain(Key key){
		return contain(root,key);
	}

	/**
	 * 向以node为根的二叉搜索树中,插入节点(key,value)
	 * 返回插入新节点后的二叉搜索树的根
	 * @param node
	 * @param key
	 * @param value
	 * @return
	 */
	private Node insert(Node node, Key key, Value value) {

		if (node == null) {
			count++;
			return new Node(key, value);
		}
		if (key == node.key) {
			node.value = value;
		} else if (key.compareTo(node.key) < 0) {
			node.left = insert(node.left, key, value);
		} else {
			node.right = insert(node.right, key, value);
		}
		return node;
	}

	//非递归插入
	private Node insertNode(Node node,Key key, Value value){
		Node newNode = new Node(key,value);
		if(node == null){
			count++;
			node = newNode;
		}
		while(node.left!=newNode && node.right!= newNode){
			if(key.compareTo(node.key)<0){
				if(node.left == null){
					node.left = newNode;
				}else{
					node = node.left;
				}
			}else{
				if(node.right == null){
					node.right = newNode;
				}else{
					node = node.right;
				}
			}
		}
		return node;
	}


	private boolean contain(Node node,Key key){
		if(node == null){
			return false;
		}
		if(node.key == key){
			return true;
		}else if(key.compareTo(node.key)<0){
			return contain(node.left,key);
		}else{
			return contain(node.right,key);
		}
	}

	/**
	 * 在以node为根的二叉搜索树中查找key所对应的value
	 * @param node
	 * @param key
	 * @return
	 */
	private Value search(Node node,Key key){
		if(node == null){
			return null;
		}
		if(key == node.key){
			return node.value;
		}else if(key.compareTo(node.key)<0){
			return search(node.left,key);
		}else{
			return search(node.right,key);
		}
	}

	/**
	 * 前序遍历
	 * @param node
	 */
	private void preOrder(Node node){
		if(node!= null){
			System.out.println(node.key+"  "+node.value);
			preOrder(node.left);
			preOrder(node.right);
		}
	}

	/**
	 * 中序遍历
	 * @param node
	 */
	private void inOrder(Node node){
		if(node!= null){
			inOrder(node.left);
			System.out.println(node.key+"  "+node.value);
			inOrder(node.right);
		}
	}

	/**
	 * 后序遍历
	 * @param node
	 */
	private void postOrder(Node node){
		if(node!= null){
			postOrder(node.left);
			postOrder(node.right);
			System.out.println(node.key+"  "+node.value);
		}
	}

	/**
	 * 层序遍历,使用queue来作为辅助空间
	 */
	public void levelOrder(){
		Queue queue = new LinkedList<>();
		queue.add(root);
		while(!queue.isEmpty()){
			Node node = queue.remove();
			System.out.println(node.key);
			if(node.left!=null){
				queue.add(node.left);
			}
			if(node.right!=null){
				queue.add(node.right);
			}
		}
	}
	//寻找最小值
	public Key minimum(){
		assert count!=0;
		Node minNode = minimum(root);
		return minNode.key;
	}


	//寻找最大值
	public Key maxmum(){
		assert count!=0;
		Node maxNode = maxmum(root);
		return maxNode.key;
	}

	//删除最小值的节点
	public void removeMin(){
		if(root!=null){
			root = removeMin(root);
		}
	}

	//删除最大值的节点
	public void removeMax(){
		if(root!=null){
			root = removeMax(root);
		}
	}

	/**
	 * 在以node为根的二叉搜索树中,返回最小值的节点
	 * @param node
	 * @return
	 */
	private Node minimum(Node node){
		if(node.left ==null){
			return node;
		}
		return minimum(node.left);
	}

	private Node maxmum(Node node){
		if(node.right == null){
			return node;
		}
		return maxmum(node.right);
	}

	private Node maxmumNotRecursion(Node node){
		while(node!=null){
			if(node.right == null){
				return node;
			}
			node = node.right;
		}
		return null;
	}


	/**
	 * 删除以node为根的二叉搜索树中的最小节点
	 * @param node
	 * @return  删除节点后新的二分搜索树的根
	 */
	private Node removeMin(Node node){
		if(node.left == null){
			Node rightNode = node.right;
			node.right = null;
			count--;
			return rightNode;
		}
		node.left = removeMin(node.left);
		return node;
	}

	/**
	 * 删除以node为根的二叉搜索树中的最大节点
	 * @param node
	 * @return  删除节点后新的二分搜索树的根
	 */
	private Node removeMax(Node node){
		if(node.right == null){
			Node leftNode = node.left;
			node.left = null;
			count--;
			return leftNode;
		}
		node.right = removeMax(node.right);
		return node;
	}
//------------------------------------------------------------------
//-------------------------------------------------------------------
	/**
	 * 删除键值为key的节点
	 * @param key
	 */
	public void delete(Key key){
		root = delete(root,key);
	}

	/**
	 * 删除Key为key的节点
	 * @param node 根节点
	 * @param key  要删除的节点的key
	 * @return
	 */
	private Node delete(Node node,Key key){
		if(node == null){
			return null;
		}
		if( key.compareTo(node.key)<0){
			node.left = delete(node.left,key);
			return node;
		}else if(key.compareTo(node.key)>0){
			node.right = delete(node.right,key);
			return node;
		}else{
			if(node.left == null){
				Node rightNode = node.right;
				node.right = null;
				count --;
				return rightNode;
			}
			if(node.right == null){
				Node leftNode = node.left;
				node.left = null;
				count --;
				return leftNode;
			}
			//node.left!=null && node.right!=null;
			Node successor = new Node(minimum(node.right));
			count++;
			successor.right = removeMin(node.right);
			successor.left  = node.left;

			//删除节点
			node.left = node.right = null;
			count--;
			return successor;
		}

	}
//------------------------------------------------------------------
//-------------------------------------------------------------------
}

 

你可能感兴趣的:(java)