java实现二叉排序树的生成、查找、打印
2018-03-10 16:35
381 查看
直接上代码:
public class Test {
public static void main(String args[]) {
int[] data = {6,4,9,1,8,5,3,7}; //初始化data数组备用
TreeNode[] TreeNodeList = new TreeNode[data.length]; //建立TreeNode数组存储树的结点
int i;
for(i=0; i<data.length; i++) { //初始化一系列结点
TreeNodeList[i] = new TreeNode(data[i]);
}
TreeNode root = new TreeNode(); //声明一个根节点
root.data = data[0]; //将data数组的第一个元素设置为根结点的值
try { //下面时捕获异常,debug用
for(i=1; i<TreeNodeList.length; i++) {
root.BstInsert(TreeNodeList[i].data);
System.out.println("number: " + i + " data: " + TreeNodeList[i].data);
}
} catch(NullPointerException e) {
e.printStackTrace();
}
root.print(); //先序遍历打印二叉树
root.BstSearch(9); //查询元素9
}
}
//创建树的结点的类
class TreeNode {
public int data ;
public TreeNode lchild = null;
public TreeNode rchild = null;
public TreeNode() {
}
public TreeNode(int data) {
this.data = data;
}
public void print() {
if(this == null) System.out.println("end");
else {
System.out.println(this.data);
if(this.lchild!=null) this.lchild.print(); //递归打印左孩子,注意一定要进行null判断,否则会出现NulPointerException异常
if(this.rchild!=null) this.rchild.print(); //递归打印右孩子
}
}
public TreeNode BstSearch(int n) {
if(this.data == n) {
System.out.println("you have find it");
return this;
}
else if(n < this.data)
return this.lchild.BstSearch(n); //递归调用
else return this.rchild.BstSearch(n);
}
public int BstInsert(int n) {
if(this.data == n) return 0;
else if(n < this.data) {
System.out.println("left");
if(this.lchild == null) { //这里一定要判断,否则会出现NullPointer异常
this.lchild = new TreeNode();
this.lchild.data = n;
}
else return this.lchild.BstInsert(n);
}
else {
System.out.println("right");
if(this.rchild == null) {
this.rchild = new TreeNode();
this.rchild.data = n;
}
else return this.rchild.BstInsert(n);
}
return 0;
}
}
public class Test {
public static void main(String args[]) {
int[] data = {6,4,9,1,8,5,3,7}; //初始化data数组备用
TreeNode[] TreeNodeList = new TreeNode[data.length]; //建立TreeNode数组存储树的结点
int i;
for(i=0; i<data.length; i++) { //初始化一系列结点
TreeNodeList[i] = new TreeNode(data[i]);
}
TreeNode root = new TreeNode(); //声明一个根节点
root.data = data[0]; //将data数组的第一个元素设置为根结点的值
try { //下面时捕获异常,debug用
for(i=1; i<TreeNodeList.length; i++) {
root.BstInsert(TreeNodeList[i].data);
System.out.println("number: " + i + " data: " + TreeNodeList[i].data);
}
} catch(NullPointerException e) {
e.printStackTrace();
}
root.print(); //先序遍历打印二叉树
root.BstSearch(9); //查询元素9
}
}
//创建树的结点的类
class TreeNode {
public int data ;
public TreeNode lchild = null;
public TreeNode rchild = null;
public TreeNode() {
}
public TreeNode(int data) {
this.data = data;
}
public void print() {
if(this == null) System.out.println("end");
else {
System.out.println(this.data);
if(this.lchild!=null) this.lchild.print(); //递归打印左孩子,注意一定要进行null判断,否则会出现NulPointerException异常
if(this.rchild!=null) this.rchild.print(); //递归打印右孩子
}
}
public TreeNode BstSearch(int n) {
if(this.data == n) {
System.out.println("you have find it");
return this;
}
else if(n < this.data)
return this.lchild.BstSearch(n); //递归调用
else return this.rchild.BstSearch(n);
}
public int BstInsert(int n) {
if(this.data == n) return 0;
else if(n < this.data) {
System.out.println("left");
if(this.lchild == null) { //这里一定要判断,否则会出现NullPointer异常
this.lchild = new TreeNode();
this.lchild.data = n;
}
else return this.lchild.BstInsert(n);
}
else {
System.out.println("right");
if(this.rchild == null) {
this.rchild = new TreeNode();
this.rchild.data = n;
}
else return this.rchild.BstInsert(n);
}
return 0;
}
}
相关文章推荐
- Java实现动态表查找--二叉排序树
- 借助POI实现Java生成并打印报表(Excel)
- 二叉排序树(Binary Sort Tree)查找、插入、删除 Java实现
- 11、借助POI实现Java生成并打印excel报表(2)
- 二叉树:实现java操作二叉排序树(生成、插入、遍历、删除)
- java实现pdf的生成下载打印,java生成pdf电子账单,java生成pdf合同模板
- java实现单链表的增加,删除,查找,打印
- Java实现二叉排序树的插入、查找、删除
- Java实现二叉排序树的插入、查找、删除
- java实现顺序表的增加,删除,查找,打印
- 用JAVA实现二叉查找树(二叉排序树)的打印
- java实现二叉排序树的建立,查找,插入,删除
- Java绘制图片并实现打印前生成图片文件进行预览
- 10、借助POI实现Java生成并打印excel报表(1)
- (学习java)写一个完整的程序,实现随机生成20个元素的链表,快速查找中间结点的值并显示
- java实现二维码生成及调用打印机打印
- C语言实现单链表的生成打印查找删除逆置排序
- JAVA实现打印源代码
- Web开发中的验证码图片的生成-基于Java的实现
- 使用java反射优化Spring自动生成的DAO实现类!