算法导论-SearchTree二叉搜索树(完全)insert插入delete删除successor后继最大最小元素-java实现...

本文介绍了一个简单的二叉搜索树的Java实现,包括插入、查找、遍历等基本操作,并展示了如何通过递归和迭代方法完成这些任务。

package datastructure;

public class SearchTree {
public static void main(String args[]){
SearchTree tree=new SearchTree();
tree.inOrderTreeWalk();
}
private TreeNode root;
public SearchTree(){
TreeNode t15=new TreeNode(15);
TreeNode t6=new TreeNode(6);
TreeNode t18=new TreeNode(18);
TreeNode t3=new TreeNode(3);
TreeNode t7=new TreeNode(7);
TreeNode t17=new TreeNode(17);
TreeNode t20=new TreeNode(20);
TreeNode t2=new TreeNode(2);
TreeNode t4=new TreeNode(4);
TreeNode t13=new TreeNode(13);
TreeNode t9=new TreeNode(9);
this.root=t15;
t15.left=t6;
t6.p=t15;
t15.right=t18;
t18.p=t15;
t6.left=t3;
t3.p=t6;
t6.right=t7;
t7.p=t6;
t18.left=t17;
t17.p=t18;
t18.right=t20;
t20.p=t18;
t3.left=t2;
t2.p=t3;
t3.right=t4;
t4.p=t3;
t7.right=t13;
t13.p=t7;
t13.left=t9;
t9.p=t13;
}
public void inOrderTreeWalk(){
inOrderTreeWalk(this.root);
}
public void inOrderTreeWalk(TreeNode x){
if(x!=null){
inOrderTreeWalk(x.left);
System.out.println(x.key);
inOrderTreeWalk(x.right);
}
}
public TreeNode recursiveTreeSearch(int k){
return recursiveTreeSearch(this.root,k);
}
public TreeNode recursiveTreeSearch(TreeNode x,int k){
if(x==null || k==x.key)
return x;
if(k<x.key)
return recursiveTreeSearch(x.left,k);
else
return recursiveTreeSearch(x.right,k);
}
public TreeNode iterativeTreeSearch(int k){
return iterativeTreeSearch(this.root,k);
}
public TreeNode iterativeTreeSearch(TreeNode x,int k){
while(x!=null && k!=x.key){
if(k<x.key)
x=x.left;
else
x=x.right;
}
return x;
}
public TreeNode minimum(){
return minimum(this.root);
}
public TreeNode minimum(TreeNode x){
while(x.left!=null)
x=x.left;
return x;
}
public TreeNode maxmum(){
return maxmum(this.root);
}
public TreeNode maxmum(TreeNode x){
while(x.right!=null)
x=x.right;
return x;
}
public TreeNode treeSuccessor(TreeNode x){
if(x.right!=null)
return minimum(x.right);
TreeNode y=x.p;
while(y!=null && x==y.right){
x=y;
y=y.p;
}
return y;
}
public void treeInsert(TreeNode z){
TreeNode y=null;
TreeNode x=this.root;
while(x!=null){
y=x;
if(z.key<x.key)
x=x.left;
else
x=x.right;
}
z.p=y;
if(y==null){
this.root=z;
}
else{
if(z.key<y.key)
y.left=z;
else
y.right=z;
}
}
public TreeNode treeDelete(TreeNode z){
TreeNode y,x;
if(z.left==null || z.right==null){
y=z;
}
else{
y=treeSuccessor(z);
}
if(y.left!=null){
x=y.left;
}
else
x=y.right;
if(x!=null)
x.p=y.p;
if(y.p==null){
this.root=x;
}
else if(y==y.p.left){
y.p.left=x;
}
else{
y.p.right=x;
}
if (y!=z){
z.key=y.key;
}
return y;
}
}
class TreeNode{
public int key;
public TreeNode p;
public TreeNode left;
public TreeNode right;
public TreeNode(int key){
this.key=key;
}
public TreeNode(int key,TreeNode p,TreeNode left,TreeNode right){
this.key=key;
this.p=p;
this.left=left;
this.right=right;
}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值