二叉树分析及设计
来源:互联网 发布:手机日语注音软件 编辑:程序博客网 时间:2024/06/10 18:20
自己设计一颗二叉树,实现insert,findMin,findMax,remove,contains,printTree方法
package com.org.Tree;
//二叉查找树
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {
private BinaryNode<AnyType> root;
//初始化二叉树
public BinarySearchTree(){
root = null;
}
//使树为空
public void makeEmpty(){
root = null;
}
//判断是否为空
public boolean isEmpty(){
return root == null;
}
//查找树中是否含有x
public boolean contains(AnyType x){
return contains(x,root);
}
private boolean contains(AnyType x,BinaryNode<AnyType> t){
if(t == null)
return false;
int compareResult = x.compareTo(t.element);
if(compareResult < 0)
return contains(x,t.left);
else if(compareResult > 0)
return contains(x, t.right);
else
return true;
}
//查找最小元素
public AnyType findMin(){
if(isEmpty())
throw new ArrayIndexOutOfBoundsException();
return findMin(root).element;
}
private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t){
if(t == null)
return null;
else if(t.left == null)
return t;
return findMin(t.left);
}
//查找最大元素
public AnyType findMax(){
if(isEmpty())
throw new ArrayIndexOutOfBoundsException();
return findMax(root).element;
}
private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t){
if(t != null)
while(t.right != null)
t = t.right;
return t;
}
//插入元素
public void insert(AnyType x){
root = insert(x,root);
}
private BinaryNode<AnyType>insert(AnyType x,BinaryNode<AnyType> t){
if(t == null)
return new BinaryNode<AnyType>(x, null, null);
int compareResult = x.compareTo(t.element);
if(compareResult < 0)
t.left = insert(x,t.left);
else if(compareResult >0)
t.right = insert(x,t.right);
else
;
return t;
}
//删除元素
public void remove(AnyType x){
root = remove(x,root);
}
private BinaryNode<AnyType> remove(AnyType x,BinaryNode<AnyType>t){
if(t == null)
return t;
int compareResult = x.compareTo(t.element);
if(compareResult < 0)
t.left = remove(x,t.left);
else if(compareResult > 0)
t.right = remove(x,t.right);
else if(t.left != null && t.right != null){
t.element = findMin(t.right).element;
t.right = remove(t.element,t.right);
}else
t = (t.left != null)?t.left:t.right;
return root;
}
//打印树
public void printTree(){
printTree(root);
}
private void printTree(BinaryNode<AnyType> t){
if(t!=null){
System.out.println(t.element);
printTree(t.left);
printTree(t.right);
}
}
//二叉树节点类
@SuppressWarnings("unused")
private static class BinaryNode<AnyType>{
AnyType element;
BinaryNode<AnyType> left;
BinaryNode<AnyType> right;
BinaryNode(AnyType theElement){
this(theElement,null,null);
}
BinaryNode(AnyType theElement,BinaryNode<AnyType> lt,BinaryNode<AnyType> rt){
element = theElement;
left = lt;
right = rt;
}
}}
- 二叉树分析及设计
- 二叉树的结构分析及实现
- 算法设计与分析基础-8.3、最优二叉查找树
- 算法设计与分析 最优二叉搜索树 动态规划
- [Java算法分析与设计]二叉树的遍历
- 红黑二叉树详解及理论分析
- 红黑二叉树详解及理论分析
- 二叉树的非递归遍历及算法分析
- 红黑二叉树详解及理论分析
- 二叉节点及二叉树
- 二叉查找树(二叉排序树、有序二叉树)算法分析及实现
- 二叉树类型设计
- 二叉树算法设计
- 3.9重建二叉树(各种方案的分析比较及扩展问题的分析)
- 树及二叉树
- 树及二叉树
- 树及二叉树
- 树及二叉树
- Uncaught Error: Syntax error, unrecognized expression: <!DOCTYPE html>
- 用PLsql连接oracle数据库
- PHP include 路径
- uva 532(bfs)
- Struck 跟踪算法(二)
- 二叉树分析及设计
- 设计模式——观察者模式
- Spring学习初体验
- 基于Ubuntu 14.04 LTS编译Android4.4.2源代码
- 帮助了解青春期的女孩
- UVA 11044 (暑假-数学(3) -B-Searching for Nessy)
- c 面试经典
- #ifdef __cplusplus
- hdu4870(高斯消元解概率DP)