您的位置:首页 > 编程语言 > Java开发

Rhyme/查找树ADT-二叉查找树的简单模拟Java版

2018-01-17 22:16 633 查看

查找树ADT-二叉查找树的简单模拟Java版

package my.binary.search.tree;

/**
* 查找树 ADT 二叉查找树的实现
* 假设元素类型为Integer
*
* @author RhymeChiang
* @date 2018/01/17
**/
public class BinarySearchTree<T extends Comparable<? super T>> {

/**
* 二叉树根节点
*/
private BinaryNode<T> root;

/**
* 静态内部节点类
*
* @param <T>
*/
private static class BinaryNode<T> {

private T element;
private BinaryNode<T> left;
private BinaryNode<T> right;

public BinaryNode() {
}

public BinaryNode(T element) {
this.element = element;
}

public BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right) {
this.element = element;
this.left = left;
this.right = right;
}
}

/**
* 清空二叉树
*/
public void makeEmpty() {
root = null;
}

/**
* 判断二叉树是否为空
*
* @return
*/
public boolean isEmpty() {
return root == null;
}

/**
* 查找元素element是否存在与二叉搜索树中
*
* @param element
* @return
*/
public boolean contains(T element) {
return contains(element, root);
}

/**
* 判断元素element是否存在于二叉查找树root中
*
* @param element
* @param root
* @return
*/
private boolean contains(T element, BinaryNode<T> root) {
if (root == null) {
return false;
}
int compareResult = element.compareTo(root.element);

if (compareResult > 0) {
return contains(element, root.right);
}

if (compareResult < 0) {
return contains(element, root.left);
}

return true;

}

/**
* 查找二叉查找树中最小的元素
*
* @return
*/
public T findMin() {
if (findMin(root) != null) {
return findMin(root).element;
}
return null;
}

/**
* 查找二叉搜索树中最小的节点
*
* @param root
* @return
*/
private BinaryNode<T> findMin(BinaryNode<T> root) {
if (root == null) {
return null;
}
if (root.left != null) {
return findMin(root.left);
}
return root.left;
}

/**
* 查找二叉树中的最大元素
*
* @return
*/
public T findMax() {
BinaryNode<T> maxNode = findMax(root);
if (maxNode != null) {
return maxNode.element;
}
return null;
}

/**
* 查找二叉查找树中的最大节点
*
* @param root
* @return
*/
private BinaryNode<T> findMax(BinaryNode<T> root) {
if (root == null) {
return null;
}
if (root.right != null) {
return findMax(root.right);
}
return root.right;
}

/**
* 插入元素element
*
* @param element
*/
public void insert(T element) {
root = insert(element, root);
}

/**
* 将元素插入到root二叉查找树中
*
* @param element
* @param root
* @return
*/
private BinaryNode<T> insert(T element, BinaryNode<T> root) {
if (root == null) {
return new BinaryNode<>(element);
}
int compareResult = element.compareTo(root.element);
if (compareResult > 0) {
root.right = insert(element, root.right);
} else if (compareResult < 0) {
root.left = insert(element, root.left);
}
return root;
}

/**
* 删除元素一个元素element
*
* @param element
*/
public void remove(T element) {

}

/**
* 在二叉查找树中删除元素element
*
* @param element
* @param root
* @return
*/
private BinaryNode<T> remove(T element, BinaryNode<T> root) {
if (root == null) {
return null;
}

int compareResult = element.compareTo(root.element);

if (compareResult > 0) {
root.right = remove(element, root.right);
} else if (compareResult < 0) {
root.left = remove(element, root.left);
}
// 被删除节点有两个子节点
else if (root.left != null && root.right != null) {
root.element = findMin(root.right).element;
root.right = remove(root.element, root.right);
}
// 叶子节点或单叶节点
else {
root = (root.left != null) ? root.left : root.right;
}
return root;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java ADT 二叉查找树