java语言实现二叉排序树的操作
2011-10-07 10:28
381 查看
public class BiSearchTree {
private BiTree root;
public static void main(String[] args) {
BiSearchTree bst = new BiSearchTree();
bst.root = new BiTree(null, null, 45);
bst.insert(bst.root, 24);
bst.insert(bst.root, 53);
bst.insert(bst.root, 12);
bst.insert(bst.root, 90);
bst.insert(bst.root, 30);
bst.insert(bst.root, 25);
System.out.println("构造完成后:");
System.out.println("中序遍历序列为:");
bst.inTraverse(bst.root);
System.out.println();
System.out.println("前序遍历序列为:");
bst.preTraverse(bst.root);
System.out.println();
System.out.println("查找的数据元素为:" + bst.find(bst.root, 24).data);
System.out.println("删除节点45后:");
bst.delete(bst.root, 45);
System.out.println("中序遍历序列为:");
bst.inTraverse(bst.root);
System.out.println();
System.out.println("前序遍历序列为:");
bst.preTraverse(bst.root);
}
public BiTree find(BiTree root, int key) {
if(root == null)return null;
else if(root.data == key)return root;
else if(root.data > key) return find(root.lchild, key);
else return find(root.rchild, key);
}
public void insert(BiTree root, int key) {
if(key < root.data) {
if(root.lchild == null) {
BiTree p = new BiTree(null, null, key);
root.lchild = p;
}
else insert(root.lchild, key);
}
else if(key > root.data) {
if(root.rchild == null) {
BiTree p = new BiTree(null, null, key);
root.rchild = p;
}
else insert(root.rchild, key);
}
return;
}
//分三种情况:左孩子为空,右孩子为空,左右孩子都为空
//当左右孩子都为空时,将被删除节点的左分支节点的最大节点(即左孩子的最右边孩子)复制到被删除节点
public void delete(BiTree root, int key) {
if(root == null)return;
else if(key == root.data) {
if(root.lchild==null) {
root = root.rchild;
}
else if(root.rchild==null) {
root = root.lchild;
}
else{
//q为s的双亲
//s为左孩子的最右边孩子,将其值赋值给被删除节点
BiTree s = root.lchild;
BiTree q = root;
while(s.rchild != null){
q = s;
s = s.rchild;
}
root.data = s.data;
if(q == root) root.lchild = s.lchild;
else q.rchild = s.lchild;
}
}
else if(key < root.data) {
delete(root.lchild, key);
}
else{
delete(root.rchild, key);
}
}
public void inTraverse(BiTree root) {
if(root != null) {
inTraverse(root.lchild);
System.out.print(root.data + " ");
inTraverse(root.rchild);
}
}
public void preTraverse(BiTree root) {
if(root != null) {
System.out.print(root.data + " ");
preTraverse(root.lchild);
preTraverse(root.rchild);
}
}
}
class BiTree {
public BiTree lchild;
public BiTree rchild;
public int data;
public BiTree(BiTree lchild, BiTree rchild, int data) {
super();
this.lchild = lchild;
this.rchild = rchild;
this.data = data;
}
}
private BiTree root;
public static void main(String[] args) {
BiSearchTree bst = new BiSearchTree();
bst.root = new BiTree(null, null, 45);
bst.insert(bst.root, 24);
bst.insert(bst.root, 53);
bst.insert(bst.root, 12);
bst.insert(bst.root, 90);
bst.insert(bst.root, 30);
bst.insert(bst.root, 25);
System.out.println("构造完成后:");
System.out.println("中序遍历序列为:");
bst.inTraverse(bst.root);
System.out.println();
System.out.println("前序遍历序列为:");
bst.preTraverse(bst.root);
System.out.println();
System.out.println("查找的数据元素为:" + bst.find(bst.root, 24).data);
System.out.println("删除节点45后:");
bst.delete(bst.root, 45);
System.out.println("中序遍历序列为:");
bst.inTraverse(bst.root);
System.out.println();
System.out.println("前序遍历序列为:");
bst.preTraverse(bst.root);
}
public BiTree find(BiTree root, int key) {
if(root == null)return null;
else if(root.data == key)return root;
else if(root.data > key) return find(root.lchild, key);
else return find(root.rchild, key);
}
public void insert(BiTree root, int key) {
if(key < root.data) {
if(root.lchild == null) {
BiTree p = new BiTree(null, null, key);
root.lchild = p;
}
else insert(root.lchild, key);
}
else if(key > root.data) {
if(root.rchild == null) {
BiTree p = new BiTree(null, null, key);
root.rchild = p;
}
else insert(root.rchild, key);
}
return;
}
//分三种情况:左孩子为空,右孩子为空,左右孩子都为空
//当左右孩子都为空时,将被删除节点的左分支节点的最大节点(即左孩子的最右边孩子)复制到被删除节点
public void delete(BiTree root, int key) {
if(root == null)return;
else if(key == root.data) {
if(root.lchild==null) {
root = root.rchild;
}
else if(root.rchild==null) {
root = root.lchild;
}
else{
//q为s的双亲
//s为左孩子的最右边孩子,将其值赋值给被删除节点
BiTree s = root.lchild;
BiTree q = root;
while(s.rchild != null){
q = s;
s = s.rchild;
}
root.data = s.data;
if(q == root) root.lchild = s.lchild;
else q.rchild = s.lchild;
}
}
else if(key < root.data) {
delete(root.lchild, key);
}
else{
delete(root.rchild, key);
}
}
public void inTraverse(BiTree root) {
if(root != null) {
inTraverse(root.lchild);
System.out.print(root.data + " ");
inTraverse(root.rchild);
}
}
public void preTraverse(BiTree root) {
if(root != null) {
System.out.print(root.data + " ");
preTraverse(root.lchild);
preTraverse(root.rchild);
}
}
}
class BiTree {
public BiTree lchild;
public BiTree rchild;
public int data;
public BiTree(BiTree lchild, BiTree rchild, int data) {
super();
this.lchild = lchild;
this.rchild = rchild;
this.data = data;
}
}
相关文章推荐
- 单链表排序操作(java语言实现)
- java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
- Java语言实现对MySql数据库中数据的增删改查操作的代码
- java语言实现简单单链表链式储存结构。插入删除等操作。(有个地方看不出错误来,已经标注,望指正)
- 链表的基本操作与拓展操作(Java语言实现)
- Ruby/Java - 两种语言的二叉排序树的实现和遍历
- 二叉树:实现java操作二叉排序树(生成、插入、遍历、删除)
- Java语言实现简单FTP软件------>连接管理模块的实现:主机与服务器之间的连接与关闭操作(八)
- 用JAVA语言,编写一个链表类(双向链表),实现插入,删除,查找操作
- 链表的基本操作java语言实现
- 使用java语言操作,如何来实现MySQL中Blob字段的存取
- java语言实现二叉排序树(结点的增、删、查、改,递归、非递归遍历)
- java 语言实现二叉树的各种操作
- Java操作另一个Java程序使其重启的简单实现
- java实现二叉树的常见操作
- 【数据结构+Java】Java实现简单队列、循环队列操作
- (转)javabean操作文件正确,但是Jsp调用javabean时文件路径出错问题解决之JavaBean访问本地文件实现路径无关实现方法
- java实现在线预览---poi操作ppt转html及03、07版本兼容问题
- 用java实现对MongoDB的基本操作(增删改查)
- Java 语言实现的随机数生成算法