二叉树的JAVA实现-二叉树的增删改查CRUD
package org.lyk.interfaces; import java.util.List; public interface IBiTree<T extends Comparable<T>>
{
public void addWithOrder(T data);
public void addFirst(T[] data,T endFlag);
public Object[] toArrayFirst();
public Object[] toArrayMiddle();
public Object[] toArrayLast();
public int size();
public boolean isEmpty();
public boolean contains(T data);
public void delete(T data);
}
package org.lyk.entities; import java.util.HashMap;
import java.util.Map; import org.lyk.interfaces.IBiTree; public class BiTree<T extends Comparable<T>> implements IBiTree<T>
{
private class Node<N extends Comparable<N>>
{
private N data;
private Node<N> left;
private Node<N> right; private Node(N data)
{
if (null == data)
return;
this.data = data;
} public void addWithOrder(N data)
{
// if(this.data.compareTo(data) < 0)
if (data.compareTo(this.data) < )
{
if (null == this.left)
{
this.left = new Node<N>(data);
BiTree.this.count++;
} else
this.left.addWithOrder(data);
} else
{
if (null == this.right)
{
this.right = new Node<N>(data);
BiTree.this.count++;
} else
this.right.addWithOrder(data);
}
} public void toArrayFirst(Object[] retVal)
{
retVal[BiTree.this.foot++] = this.data;
if (null != this.left)
this.left.toArrayFirst(retVal);
if (null != this.right)
this.right.toArrayFirst(retVal);
} public void toArrayMiddle(Object[] retVal)
{
if (null != this.left)
this.left.toArrayMiddle(retVal);
retVal[BiTree.this.foot++] = this.data;
if (null != this.right)
this.right.toArrayMiddle(retVal);
} public void toArrayLast(Object[] retVal)
{
if (null != this.left)
this.left.toArrayLast(retVal);
if (null != this.right)
this.right.toArrayLast(retVal);
retVal[BiTree.this.foot++] = this.data;
}
} private int count = ;
private Node<T> root = null;
private int foot = ; @Override
public void addWithOrder(T data)
{
if (null == data)
return; if (null == this.root)
{
this.root = new Node<T>(data);
this.count++;
} else
{
this.root.addWithOrder(data);
}
} @Override
public void addFirst(T[] data, T endFlag)
{
if (null == data)
{
return;
} this.foot = ;
this.root = this.addFirstInternal(data, endFlag, this.root);
} private Node<T> addFirstInternal(T[] data, T endFlag, Node<T> node)
{
if (data[this.foot].equals(endFlag))
{
this.foot++;
return null;
} else
{
node.data = data[this.foot];
this.foot++;
node.left = this.addFirstInternal(data, endFlag, node.left);
node.right = this.addFirstInternal(data, endFlag, node.right);
return node;
}
} @Override
public Object[] toArrayFirst()
{
if (this.isEmpty())
return null;
Object[] retVal = new Object[this.count];
this.foot = ;
this.root.toArrayFirst(retVal);
return retVal;
} @Override
public Object[] toArrayMiddle()
{
if (this.isEmpty())
return null;
Object[] retVal = new Object[this.count];
this.foot = ;
this.root.toArrayMiddle(retVal);
return retVal;
} @Override
public Object[] toArrayLast()
{
if (this.isEmpty())
return null;
Object[] retVal = new Object[this.count];
this.foot = ;
this.root.toArrayLast(retVal);
return retVal;
} @Override
public int size()
{
return this.count;
} @Override
public boolean isEmpty()
{
if ( == this.count && null == this.root)
return true;
else
return false;
} @Override
public void delete(T data)
{
if (this.isEmpty())
return; Map<String, Object> nodeDel = this.find(data);
if (null == nodeDel)
return; Node<T> node = (Node<T>) nodeDel.get("node");
Node<T> parent = (Node<T>) nodeDel.get("parent");
String direction = (String) nodeDel.get("direction");
if (null == node.left && null == node.right)
{
if (null == direction)
{
this.root = null;
} else
{
if ("left".equals(direction))
{
parent.left = null;
} else if ("right".equals(direction))
{
parent.right = null;
}
}
this.count--;
return;
} if (null != node.left && null == node.right)
{
// 该节点只有左子树
if (null == direction)
{
Node<T> temp = this.root;
this.root = this.root.left;
temp.left = null;
} else
{
if ("left".equals(direction))
{
parent.left = node.left;
node.left = null;
} else if ("right".equals(direction))
{
parent.right = node.left;
node.left = null;
}
}
this.count--;
return;
} if (null == node.left && null != node.right)
{
// 该节点只有右子树
if (null == direction)
{
Node<T> temp = this.root;
this.root = this.root.right;
temp.right = null;
} else
{
if ("left".equals(direction))
{
parent.left = node.right;
node.right = null;
} else if ("right".equals(direction))
{
parent.right = node.right;
node.right = null;
}
}
this.count--;
return;
} Node<T> subsessor = this.getSubsessor(node);
if (null != subsessor)
{
//System.out.println("subsessor:" + subsessor.data);
subsessor.left = node.left;
if (null == direction)
{
this.root = subsessor;
} else if ("left".equals(direction))
{
parent.left = subsessor;
} else if ("right".equals(direction))
{
parent.right = subsessor;
}
this.count--;
} } private Node<T> getSubsessor(Node<T> nodeDel)
{
Node<T> parent = nodeDel;
Node<T> subsessor = nodeDel.right;
subsessor = this.getSubsessorInternal(parent, subsessor, nodeDel);
return subsessor;
} private Node<T> getSubsessorInternal(Node<T> parent, Node<T> subsessor, Node<T> nodeDel)
{
if (null != subsessor.left)
{
parent = subsessor;
subsessor = subsessor.left;
return getSubsessorInternal(parent, subsessor, nodeDel);
} else
{
if (subsessor != nodeDel.right)
{
parent.left = subsessor.right;
subsessor.right = nodeDel.right;
}
return subsessor;
}
} private Map<String, Object> find(T data)
{
return this.findInternal(this.root, null, null, data);
} private Map<String, Object> findInternal(Node<T> node, Node<T> parent, String direction, T data)
{
Map<String, Object> retVal = null;
if (node.data.equals(data))
{
retVal = new HashMap<>();
retVal.put("node", node);
retVal.put("parent", parent);
retVal.put("direction", direction);
return retVal;
} else
{
if (null != node.left)
{
// parent = node;
// node = node.left;
retVal = this.findInternal(node.left, node, "left", data);
if(null != retVal)
return retVal;
} if (null != node.right)
{
// parent = node;
// node = node.right;
retVal = this.findInternal(node.right, node, "right", data);
if(null != retVal)
return retVal;
}
}
return retVal;
} @Override
public boolean contains(T data)
{
// TODO Auto-generated method stub
return false;
} }
二叉树的JAVA实现-二叉树的增删改查CRUD的更多相关文章
- Java连接MySQL数据库增删改查通用方法
版权声明:本文为博主原创文章,未经博主允许不得转载. Java连接MySQL数据库增删改查通用方法 运行环境:eclipse+MySQL 以前我们Java连接MySQL数据库都是一个数据库写一个类,类 ...
- Java实现mongodb原生增删改查语句
Java实现mongodb原生增删改查语句 2018-03-16 自动化测试时,需校验数据库数据,为了快速自动化,在代码中用原生增删改查语句操作mongodb 结构 代码 0 pom.xml < ...
- IDEA SpringBoot-Mybatis-plus 实现增删改查(CRUD)
上一篇: IDEA SpringBoot-Mybatis实现增删改查(CRUD) 下一篇:Intellij IDEA 高效使用教程 (插件,实用技巧) 最好用的idea插件大全 一.前言 Mybati ...
- Java连接MongoDB进行增删改查
1.导入必须的包: 详情看项目:http://pan.baidu.com/s/1cvDAOY 2.通过Myeclipse创建WEB项目 3. 3.bean:创建实体类 package com.bean ...
- java实现xml文件增删改查
java一次删除xml多个节点: 方案1.你直接改动了nodeList,这一般在做循环时是不同意直接这么做的. 你能够尝试在遍历一个list时,在循环体同一时候删除list里的内容,你会得到一个异常. ...
- java对mysql的增删改查
-----连接数据库 package connectdb;import java.sql.*;class Dbcon { // 此处连接数据库,独立开一个类,以后操作数据库的每次连接就不用写这么多 p ...
- java连接mysql以及增删改查操作
java连接数据库的代码基本是固定的,步骤过程觉得繁琐些,代码记起来对我来说是闹挺.直接上代码: (温馨提醒:你的项目提前导入连接数据库的jar包才有的以下操作 ) class DBConnectio ...
- Java连接数据库,及增删改查
自定义连接数据库的util类 package com.shuzf.jdbc; import java.sql.Connection; import java.sql.DriverManager; im ...
- java操作数据库:增删改查
不多bb了直接上. 工具:myeclipse 2016,mysql 5.7 目的:java操作数据库增删改查商品信息 test数据库的goods表 gid主键,自增 1.实体类Goods:封装数据库数 ...
- Java操作数据库实现"增删改查"
本文主要讲解JDBC操作数据库 主要实现对MySql数据库的"增删改查" 综合概述: JDBC的常用类和接口 一 DriverManager类 DriverManage类 ...
随机推荐
- OpenJudge计算概论-单词替换
/*====================================================================== 单词替换 总时间限制: 1000ms 内存限制: 65 ...
- docker 数据共享,数据复制
docker 提供的数据共享的方式有 docker run -it -v:/dataname image 数据复制使用 docker cp containerid:/dataname ...
- mysql 修改字符集
查看mysql数据编码 登录MySQL服务,查看mysql数据库默认的编码 mysql> show variables like 'character%'; +----------------- ...
- ORACLE 表函数实现
1.创建表对象类型. 在Oracle中想要返回表对象,必须自定义一个表类型,如下所示: create or replace type t_table is table of number; 上面的类型 ...
- Load an X509 PEM file into Windows CryptoApi
http://stackoverflow.com/questions/1231178/load-an-x509-pem-file-into-windows-cryptoapi I discovered ...
- SIlverlight外包公司【技术展望】— Silverlight5.1.2最新版本发布,Silverlight 的更新从未停止。
微软发布新版Silverlight 5, 版本号5.1.20913.0 大家可以到微软网站去下载最新版本网址是 http://www.microsoft.com/getsilverlight/get- ...
- LintCode "Longest Increasing Continuous subsequence II" !!
DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...
- 51nod 1348 乘积之和
用(r-l+2)维向量f[l,r]表示区间[l,r]内选i个数(0<=i<=r-l+1)相乘的所有方案之和,可以发现f[l,r]=f[l,m]*f[m+1,r],题目模数100003较小, ...
- Python控制流语句(if,while,for)
if.py number=23 guess=int(input("enter an int:")) if guess==number: print ("congratul ...
- DatagridView 最后编辑状态数据无法自动提交的问题
DataGridView1.CurrentCell = null 从一个帖子上看的,绝招! http://bbs.csdn.net/topics/120020614