编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树
package com.test.tree; import java.util.Iterator; /**
* 编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树。
* 在每个节点上添加一个指向其父节点的链
* @author wyl
* @param <T>
*/
public class MyTreeSet<T extends Comparable<? super T>> { private BinaryNode<T> root; //根节点
int modCount = 0; //记录调整树结构的次数 public MyTreeSet(){
root = null;
}
/**
* 定义二叉查找树的节点
*/
private class BinaryNode<T>{
T data; //节点的值
BinaryNode<T> left; //节点的左节点
BinaryNode<T> right; //节点右节点
BinaryNode<T> parent; //节点的父节点 public BinaryNode(T data){
this(data, null, null, null);
} public BinaryNode(T data, BinaryNode<T> lt, BinaryNode<T> rt, BinaryNode<T> pt) {
this.data = data;
this.left = lt;
this.right = rt;
this.parent = pt;
}
} /**
* 定义TreeSet的迭代器
* @return
*/
public Iterator iterator(){
return new MyTreeSetIterator();
} private class MyTreeSetIterator implements Iterator { private BinaryNode<T> current = findMin(root);
private BinaryNode<T> previous;
private int expectedModCount = modCount;
private boolean okToRemove = false;
private boolean atEnd = false;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return !atEnd;
} @Override
public T next() {
// TODO Auto-generated method stub
if(modCount != expectedModCount){
try {
throw new CurrentModificationException();
} catch (CurrentModificationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(!hasNext()){
try {
throw new NoSuchElementException();
} catch (NoSuchElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} T nextItem = current.data;
previous = current;
if(current.right != null){
current = findMin(current.right);
}else{
BinaryNode<T> child = current;
current = current.parent;
while(current != null && current.left != child){
child = current;
current = current.parent;
}
if(current == null){
atEnd = true;
}
}
okToRemove = true;
return nextItem;
} @Override
public void remove(){
// TODO Auto-generated method stub
if(modCount != expectedModCount){
try {
throw new CurrentModificationException();
} catch (CurrentModificationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(!okToRemove){
throw new IllegalStateException();
} try {
MyTreeSet.this.remove(previous.data);
} catch (UnderflowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
okToRemove = false;
}
} public void makeEmpty(){
modCount++ ;
root = null;
} public boolean isEmpty(){
return root == null;
}
public boolean contains(T x){
return contains(x, root);
} public boolean contains(T x, BinaryNode<T> t){
if(t == null){
return false;
}
int compareResult = x.compareTo(t.data);
if(compareResult < 0){
return contains(x, t.left);
}else if(compareResult > 0){
return contains(x, t.right);
}else{
return true;
}
} public T findMin(){
if(isEmpty()){
try {
throw new UnderflowException();
} catch (UnderflowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return findMin(root).data;
} public T findMax(){
if(isEmpty()){
try {
throw new UnderflowException();
} catch (UnderflowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return findMax(root).data;
} public void insert(T x){
root = insert(x, root, null);
} public void remove(T x) throws UnderflowException{
root = remove(x, root);
} public void printTree(){
if(isEmpty()){
System.out.println("Empty tree");
}else{
printTree(root);
}
} public void printTree(BinaryNode<T> t){
if(t != null){
printTree(t.left);
System.out.println(t.data);
printTree(t.right);
}
} public BinaryNode<T> remove(T x, BinaryNode<T> t){
if(t == null){
try {
throw new UnderflowException();
} catch (UnderflowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int compareResult = x.compareTo(t.data);
if(compareResult < 0){
t = remove(x, t.left);
}else if(compareResult > 0){
t = remove(x, t.right);
}else if(t.left != null && t.right != null){
//要删除的节点是含有左右子树的节点
t.data = findMin(t.right).data;//将右子树的最小值作为根节点
t.right = remove(t.data, t.right);
}else{
modCount++ ;
BinaryNode<T> oneChild;
oneChild = (t.left == null)?t.left:t.right;
oneChild.parent = t.parent;
t = oneChild;
}
return t;
} public BinaryNode<T> insert(T x, BinaryNode<T> t, BinaryNode<T> parent){
if(t == null){
modCount++ ;
//空树
return new BinaryNode(x, null, null, parent);
}
int compareResult = x.compareTo(t.data);
if(compareResult < 0){
//要插入的数小于节点值,插入到左子树
t.left = insert(x, t.left, t);
}else if(compareResult > 0){
//要插入的数小于节点值,插入到左子树
t.right = insert(x, t.right, t);
}else{ }
return t;
} public BinaryNode<T> findMin(BinaryNode<T> t){
// TODO Auto-generated method stub
if(t == null){
try {
throw new UnderflowException();
} catch (UnderflowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(t.left == null){
return t;
}
return findMin(t.left);
} public BinaryNode<T> findMax(BinaryNode<T> t){
// TODO Auto-generated method stub
if(t == null){
try {
throw new UnderflowException();
} catch (UnderflowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(t.right == null){
return t;
}
return findMax(t.right);
} public static void main(String[] args) {
MyTreeSet<Integer> myTreeSet = new MyTreeSet<Integer>();
myTreeSet.insert(24);
myTreeSet.insert(23);
myTreeSet.insert(16);
myTreeSet.insert(20);
myTreeSet.insert(28);
myTreeSet.insert(29);
System.out.println("最小值: "+ myTreeSet.findMin());
System.out.println("最大值: "+ myTreeSet.findMax());
Iterator iter = myTreeSet.iterator();
while(iter.hasNext()){
System.out.print(iter.next() + "、");
} }
}
class UnderflowException extends Exception{}
class CurrentModificationException extends Exception{}
class NoSuchElementException extends Exception{}
编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树的更多相关文章
- 35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n); (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和; (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n
35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n): (2)编写一个类:ClassA来实现接口InterfaceA,实现in ...
- 程序员自己编写的类和JDK类是一种合作关系。
封装类: JAVA为每一个简单数据类型提供了一个封装类,使每个简单数据类型可以被Object来装载. 除了int和char,其余类型首字母大写即成封装类. 转换字符的方式: int I=10; Str ...
- 程序员自己编写的类和JDK类是一种合作关系
封装类: JAVA为每一个简单数据类型提供了一个封装类,使每个简单数据类型可以被Object来装载. 除了int和char,其余类型首字母大写即成封装类. 转换字符的方式: int I=10; Str ...
- 编写一个简单的C++程序
编写一个简单的C++程序 每个C++程序都包含一个或多个函数(function),其中一个必须命名为main.操作系统通过调用main来运行C++程序.下面是一个非常简单的main函数,它什么也不干, ...
- Java API —— TreeSet类
1.TreeSet类 1)TreeSet类概述 使用元素的自然顺序对元素进行排序 或者根据创建 set 时提供的 Comparator 进行排序 ...
- Swift互用性: 使用Objective-C特性编写Swift类(Swift 2.0版)-b
本节包括内容: 继承Objective-C的类(Inheriting from Objective-C Classes) 采用协议(Adopting Protocols) 编写构造器和析构器(Writ ...
- SLAM+语音机器人DIY系列:(二)ROS入门——4.如何编写ROS的第一个程序hello_world
摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序读取相关数据
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第七篇:为ASP.NET MVC应用程序 ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第八篇:为ASP.NET MVC应用程序 ...
随机推荐
- MVC5学习系列
前言 嗷~小弟我又出现了~咳咳..嚎过头了, 先说一说为什么写这个吧,~首先肯定是我自己需要学(废话 - -,)//,之前也写过MVC4的项目,嗯..但是仅限于使用并没有很深入的每个模块去了解, 这段 ...
- coursera 《现代操作系统》 -- 第八周 存储模型(2)
名词解释 页面: 页面大小: 页表: 页表项: 以上名词解释见: coursera <现代操作系统> -- 第七周 存储模型(1) 页表项大小: 问:以上是怎么计算出来的? 32位指什么? ...
- entropy 压缩信息的熵更加高 实际上英文文本的熵大概只有4.7比特
https://en.wikipedia.org/wiki/Entropy_(information_theory) https://zh.wikipedia.org/wiki/熵(信息论) 熵的概念 ...
- geometric mean
w
- MySQL中事务的分类
从事务理论的角度来看,可以把事务分为以下几种类型 扁平事务(Flat Transactions) 带有保存点的扁平事务(Flat Transactions with Savepoints) 链事务(C ...
- Python判断网络是否可以访问
import urllib url = "http://www.baidu.com" try: status = urllib.urlopen(url).code print st ...
- PyNest——part 2: populations of neurons
part 2: populations of neurons introduction 在这篇讲义中,我们着眼于创建和参数化神经元批次,并将它们连接起来. 当你完成这些材料时,你会知道如何: 创建具有 ...
- 爬虫四 selenium模块
一.介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作, ...
- 【转】Python爬虫_示例
爬虫项目:爬取汽车之家新闻资讯 # requests+Beautifulsoup爬取汽车之家新闻 import requests from bs4 import BeautifulSoup res ...
- Python基础(11)_python模块之time模块、rando模块、hashlib、os模块
一.模块 1.什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀 模块的本质:模块的本质是一个py文件 2.模块分为三类:1)内置模块:2)第三方模块: ...