搜索树java_二进制搜索树实现和Java

我正在尝试使用Cormen的伪代码来实现BST算法,但仍然存在问题。

这是我的节点代码:

public class Node {

Node left;

Node right;

int value;

Node(int value){

this.value = value;

this.left = null;

this.right = null;

}

}

对于Bstree:

public class Btree {

Node root;

Btree(){

this.root = null;

}

public static void inorderWalk(Node n){

if(n != null){

inorderWalk(n.left);

System.out.print(n.value + " ");

inorderWalk(n.right);

}

}

public static Node getParent(Btree t, Node n){

Node current = t.root;

Node parent = null;

while(true){

if (current == null)

return null;

if( current.value == n.value ){

break;

}

if (current.value > n.value){

parent = current;

current = current.left;

}

else{ //(current.value < n.value)

parent = current;

current = current.right;

}

}

return parent;

}

public static Node search(Node n,int key){

if(n == null || key == n.value ){

return n;

}

if(key < n.value){

return search(n.left,key);

}

else{

return search(n.right,key);

}

}

public static Node treeMinimum(Node x){

if(x == null){

return null;

}

while(x.left != null){

x = x.left;

}

return x;

}

public static Node treeMaximum(Node x){

if(x == null){

return null;

}

while(x.right != null){

x = x.right;

}

return x;

}

public static Node treeSuccessor(Btree t,Node x){

if (x.right == null){

return treeMinimum(x.right);

}

Node y = getParent(t,x);

while(y != null && x == y.right){

x = y;

y = getParent(t,y);

}

return y;

}

public static Btree insert(Btree t,Node z){

Node y = null;

Node x = t.root;

while(x != null){

y = x;

if(z.value < x.value)

x = x.left;

else

x = x.right;

}

Node tmp = getParent(t,z);

tmp = y;

if(y == null){

t.root = z;

}

else if(z.value < y.value)

y.left = z;

else

y.right = z;

return t;

}

public static Btree delete(Btree t,Node z){

Node y,x;

if (z.left == null || z.right == null)

y = z;

else

y = treeSuccessor(t,z);

if (y.left != null)

x = y.left;

else

x = y.right;

if (x != null){

Node tmp = getParent(t,x);

tmp = getParent(t,y);

}

if (getParent(t,y) == null ){

t.root = x;

}

else{

if( y == getParent(t,y).left ){

getParent(t,y).left = x;

}

else{

getParent(t,y).right = x;

}

}

if(y != z){

z.value = y.value;

}

return t;

}

public static void main(String[] args){

Btree test = new Btree();

Node n1 = new Node(6);

Node n2 = new Node(3);

Node n3 = new Node(9);

Node n4 = new Node(1);

Node n5 = new Node(16);

Node n6 = new Node(4);

Node n7 = new Node(2);

Node n8 = new Node(11);

Node n9 = new Node(13);

test = insert(test,n1);

test = insert(test,n2);

test = insert(test,n3);

test = insert(test,n4);

test = insert(test,n5);

test = insert(test,n6);

test = insert(test,n7);

test = insert(test,n8);

test = insert(test,n9);

inorderWalk(test.root);

System.out.println();

test = delete(test,n8);

inorderWalk(test.root);

System.out.println();

test = delete(test,n5);

inorderWalk(test.root);

System.out.println();

test = delete(test,n2);

inorderWalk(test.root);

System.out.println();

test = delete(test,n1);

inorderWalk(test.root);

}

}

主要问题在于删除部分,有时它按预期工作,有时删除错误,有时出现空指针异常。可能是什么问题?

附言:这不是作业

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值