码迷,mamicode.com
首页 > 其他好文 > 详细

二叉查找树

时间:2014-08-03 18:09:35      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:java   使用   io   for   art   ar   new   amp   

package com.iflytek.tree;

import java.util.Random;

/**
 * 二叉查找树
 * @author fgtian
 *
 */
public class BinaryTree {
	public static class BinaryTreeNode {
		int mValue;		// 数值:以int代替,可以扩展成其他的
		BinaryTreeNode mLeftNode;
		BinaryTreeNode mRightNode;
		BinaryTreeNode mParent;
		
		BinaryTreeNode(int value, BinaryTreeNode parent) {
			mValue = value;
			mParent = parent;
		}
	}
	
	private BinaryTreeNode mHeader;
	
	public BinaryTree() {
		
	}
	
	public void insert(int value) {
		if (null == mHeader) {
			mHeader = new BinaryTreeNode(value, null);
			mHeader.mLeftNode = mHeader.mRightNode = null;
		} else {
			// 找到它的父节点:
			BinaryTreeNode p = mHeader;
			while (true) {
				int curValue = p.mValue;
				if (value <= curValue) {
					BinaryTreeNode left = p.mLeftNode;
					if (null == left) {
						BinaryTreeNode node = new BinaryTreeNode(value, p);
						node.mLeftNode = node.mRightNode = null;
						p.mLeftNode = node;
						return;
					} else {
						p = left;
					}
				} else {
					BinaryTreeNode right = p.mRightNode;
					if (null == right) {
						BinaryTreeNode node = new BinaryTreeNode(value, p);
						node.mLeftNode = node.mRightNode = null;
						p.mRightNode = node;
						return;
					} else {
						p = right;
					}
				}
			}
		}
	}
	
	public BinaryTreeNode find(int value) {
		BinaryTreeNode node = mHeader;
		return findNode(node, value);
	}
	
	private BinaryTreeNode findNode(BinaryTreeNode startNode, int value) {
		BinaryTreeNode node = startNode;
		while (null != node) {
			if (node.mValue == value) {
				return node;
			} else if (value > node.mValue) {
				node = node.mRightNode;
			} else {
				node = node.mLeftNode;
			}
		}
		return null;
	}
	
	public void printLevel() {
		System.out.println("=======================================");
		printLevelNode(mHeader, 0);
		System.out.println("=======================================");
	}
	
	private void printLevelNode(BinaryTreeNode node, int level) {
		if (null != node) {
			for (int i = 0; i < level; i++) {
				System.out.print("    ");
			}
			System.out.println(String.valueOf(node.mValue));
			printLevelNode(node.mLeftNode, level + 1);
			printLevelNode(node.mRightNode, level + 1);
		}
	}
	
	public void print() {
		printNode(mHeader);
	}
	
	private void printNode(BinaryTreeNode node) {
		if (null != node) {
			printNode(node.mLeftNode);
			System.out.print("     " + node.mValue);
			printNode(node.mRightNode);
		}
	}
	
	public int findMin() {
		if (null == mHeader) {
			throw new IllegalStateException("树为空");
		}
		
		BinaryTreeNode node = findMinNode(mHeader);
		return node.mValue;
	}
	
	private BinaryTreeNode findMinNode(BinaryTreeNode startNode) {
		BinaryTreeNode node = startNode;
		while (null != node.mLeftNode) 
			node = node.mLeftNode;
		return node;
	}
	
	public int findMax() {
		if (null == mHeader) {
			throw new IllegalStateException("树为空");
		}
		
		BinaryTreeNode node = findMaxNode(mHeader);
		return node.mValue;
	}
	
	private BinaryTreeNode findMaxNode(BinaryTreeNode startNode) {
		BinaryTreeNode node = startNode;
		while (null != node.mRightNode) 
			node = node.mRightNode;
		return node;
	}
	
	public void remove(int value) {
		BinaryTreeNode node = findNode(mHeader, value);
		if (null == node) {
			return;
		}
		removeNode(node);
	}
	
	private void removeNode(BinaryTreeNode node) {
		if (null == node.mLeftNode || null == node.mRightNode) {
			BinaryTreeNode parent = node.mParent;
			if (null == parent) { // 删除的是根节点
				BinaryTreeNode header = mHeader;
				if (null == node.mLeftNode && null == node.mRightNode) { // 两边为空
					mHeader = null;
				} else if (null == node.mLeftNode) { // 左边为空
					mHeader = node.mRightNode;
				} else {
					mHeader = node.mLeftNode;
				}
				header.mLeftNode = header.mRightNode = null;
			} else {
				if (null == node.mLeftNode && null == node.mRightNode) { // 这是一个叶子
					if (parent.mLeftNode == node) {
						parent.mLeftNode = null;
					} else if (parent.mRightNode == node) {
						parent.mRightNode = null;
					} else {
						throw new IllegalStateException("父节点的两个孩子接口都不等于孩子");
					}
				} else if (null != node.mLeftNode) { // 左边存在
					parent.mLeftNode = node.mLeftNode;
					node.mParent = parent;
				} else {
					parent.mLeftNode = node.mRightNode;
					node.mParent = parent;
				}
			}
		} else { // 使用右边最大的进行替换:
			BinaryTreeNode maxNode = findMaxNode(node);
			int value = maxNode.mValue;
			node.mValue = value;
			removeNode(maxNode);
		}
	}
	
	public static void main(String[] args) {
		BinaryTree tree = new BinaryTree();
		Random r = new Random(10);
		int length = 10;
		int value = 0;
		for (int i = 0; i < length; i++) {
			value = r.nextInt(1000);
			tree.insert(value);
		}
		tree.print();
		
		BinaryTreeNode node = tree.find(value);
		if (null != node) {
			System.out.println("\nFind Value: " + node.mValue);
			// 把value到跟依次打印出来:
			System.out.println("从node到根的路径:");
			while (node != null) {
				System.out.print("     " + node.mValue);
				node = node.mParent;
			}
		} else {
			System.out.println("\nCANNOT Find Value: " + value);
		}
		
		int maxValue = tree.findMax();
		int minValue = tree.findMin();
		System.out.println("\n最大值:" + maxValue + ", 最小值:" + minValue);
		
		tree.printLevel();
		tree.remove(293);
		tree.printLevel();
	}
}

二叉查找树,布布扣,bubuko.com

二叉查找树

标签:java   使用   io   for   art   ar   new   amp   

原文地址:http://blog.csdn.net/buleriver/article/details/38358423

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!