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

初学菜鸟的学习日记——二叉树(JAVA)

2014-05-07 19:18 381 查看
这几天看了看JAVA数据结构,其中链表之类的结构之前看过JDK源码,但是树之类的是第一次接触。最近封闭开发挺忙的,抽了点时间自己实现一个二叉树,实现了最基本的增删改查和截取,挺简陋的,冗余代码比较多。而且有的地方设计的不太合理。暂且记下这些,晚上再好好改改。不怕被喷,就怕不懂装懂。骚年,Go ahead!!!

package duty;

import java.util.TreeMap;

/**
* @author Administrator
*
* @param <T>
* @param <K>
*/
public class Tree<T,K> implements Cloneable{
public static final String DEFAULT_CLASS_NAME = Integer.class.getName();
private static final Integer DEFAULT_KEY = 0;
private static final Object DEFAULT_VALUE = null;
private Node root;
private Node currentNode;
private transient int size = 0;

public Tree(){}

public Tree(Class type){
try {
K obj = (K) type.newInstance();
init((Comparable) obj,null);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public Tree(Tree tree){
if(tree==null||tree.root==null){
return;
}
root = (Node) tree.root;
currentNode = root;
size = tree.size();
}

Tree(Node node){
this.root = node;
}

public Tree(boolean forceInit){
if(!forceInit){
return;
}
init((Comparable) DEFAULT_KEY,(K) DEFAULT_VALUE);
}

public Tree(Comparable key,K value){
init(key,value);
}

private void init(Comparable key,K value){
root = new Node(key,value);
currentNode = root;
}

public K put(Comparable key,K value) throws IllegalArgumentException{
if(key==null){
throw new IllegalArgumentException("The key of tree can't be null...");
}
size ++;
if(root == null){
root = new Node(key,value);
return null;
}
K oldValue = checkPosition(key,value);
currentNode = root;
return oldValue;
}

/**
* 遍历插入
* @param key
* @param value
* @return
*/
@SuppressWarnings("unchecked")
private K checkPosition(Comparable key,K value){
if(key.compareTo((T) currentNode.key)==0){//如果找到一样的节点,返回初始值
K oldValue = currentNode.value;
currentNode.value = value;
size--;
return oldValue;
}else if(key.compareTo((T)currentNode.key)<0){//如果小于当前节点,左转寻径
if(currentNode.leftChild == null){
currentNode.leftChild = new Node(key,value);
currentNode.leftChild.parent = currentNode;
return null;
}
currentNode = currentNode.leftChild;
}else{
if(currentNode.rigthChild == null){
currentNode.rigthChild = new Node(key,value);
currentNode.rigthChild.parent = currentNode;
return null;
}
currentNode = currentNode.rigthChild;
}
return checkPosition(key,value);
}

public K get(Comparable key){
if(key == null){
return null;
}
K value = checkGet(key);
currentNode = root;
return value;
}

/**
* @param key
* @return
*/
@SuppressWarnings("unchecked")
private K checkGet(Comparable key){
if(key.compareTo(currentNode.key)==0){
return currentNode.value;
}else if(key.compareTo(currentNode.key)<0){
if(currentNode.leftChild==null){
return null;
}
currentNode = currentNode.leftChild;
}else if(key.compareTo(currentNode.key)>0){
if(currentNode.rigthChild==null){
return null;
}
currentNode = currentNode.rigthChild;
}
return checkGet(key);
}

public K remove(Comparable key){
K obj = checkRemove(key);
currentNode = root;
size--;
return obj;
}

/**
* 递归遍历树
* @param key
* @return
*/
private K checkRemove(Comparable key){
if(key.compareTo(currentNode.key)==0){
K value = currentNode.value;
if(currentNode == root){
clear();
return value;
}
Node parent = currentNode.parent;
int compare =currentNode.key.compareTo((T) parent.key);
if(currentNode.leftChild==null&¤tNode.rigthChild==null){
if(compare<0)
parent.leftChild=null;
else
parent.rigthChild=null;
}else if(currentNode.leftChild!=null&¤tNode.rigthChild==null){
if(compare<0)
parent.leftChild = currentNode.leftChild;
else
parent.rigthChild = currentNode.leftChild;
}else if(currentNode.rigthChild!=null&¤tNode.leftChild==null){
if(compare<0)
parent.leftChild = currentNode.rigthChild;
else
parent.rigthChild = currentNode.rigthChild;

//忘了这里是怎么规定的了。。。就先优先最大或最小吧
}else if(currentNode.rigthChild!=null && currentNode.rigthChild!=null){
if(compare<0){
parent.leftChild = currentNode.leftChild;//断开当前节点,接上下面的节点
currentNode.leftChild.rigthChild = currentNode.rigthChild;
}else{
parent.rigthChild = currentNode.rigthChild;
currentNode.rigthChild.leftChild = currentNode.leftChild;
}
}
return value;
}else if(key.compareTo(currentNode.key)<0){
if(currentNode.leftChild==null){
return null;
}
currentNode = currentNode.leftChild;
}else if(key.compareTo(currentNode.key)>0){
if(currentNode.rigthChild==null){
return null;
}
currentNode = currentNode.rigthChild;
}
return checkRemove(key);
}

public void clear(){
root = null;
}

public int size(){
return size;
}

public Tree subTree(Comparable key){
if(key==null)
return null;
Tree tree =checkSubTree(key);
currentNode = root;
return tree;
}

@SuppressWarnings("rawtypes")
public Tree checkSubTree(Comparable key){
if(key.compareTo(currentNode.key)==0){
Node store = root;
root = currentNode;
Tree tree;
try {
tree = (Tree) this.clone();
root = store;
return tree;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}else if(key.compareTo(currentNode.key)<0){
if(currentNode.leftChild==null){
return null;
}
currentNode = currentNode.leftChild;
}else if(key.compareTo(currentNode.key)>0){
if(currentNode.rigthChild==null){
return null;
}
currentNode = currentNode.rigthChild;
}
return checkSubTree(key);
}

@Override
public String toString() {
return "Tree [root=" + root + "]";
}

/**
* @author zhq
* 树节点类
*
*/
private class Node{
Node leftChild; //左节点
Node rigthChild;
Node parent;//父节点
private Comparable<T> key;
private K value;
public Node(Comparable key,K value){
this.key = key;
this.value = value;
}
@Override
public String toString() {
return "Node [leftChild=" + leftChild + ", rigthChild="
+ rigthChild + ", key=" + key + ", value=" + value + "]";
}

}
public static void main(String[] args) {
Tree tree= new Tree("2",2);
tree.put("1", 1);
tree.put("6", 32);
tree.put("1a", 3);
Tree tree1 = tree.subTree("1");
System.out.println(tree);
System.out.println(tree1);
tree.remove("1a");
System.out.println(tree);
System.out.println(tree.get("1"));
System.out.println(tree.get("1a"));
System.out.println(tree.get("555"));
tree.clear();
System.out.println(tree.get("6"));

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: