java 树 数据结构 xml_二叉查找树的数据结构以及实现(JAVA)

package tree.binarytree;

//二叉查找树

public class BinarySearchTree {

private BinaryNode root;

private T element;

int i=0;

public BinarySearchTree(){

this(null);

}

public BinarySearchTree(T element){

this.root=null;

this.element=element;

}

//判断树是否为空

private boolean isEmpty(){

return this.root==null;

}

//插入数据

public void insert(T t)

{

this.root= insert(t,this.root);

}

private BinaryNode insert(T theelement,BinaryNode root){

i++;

if(root==null){

System.out.println("第"+i+"步:  "+"在插入数据的位置创建一个节点 该节点的值是: "+theelement);

return new BinaryNode(theelement,null,null);

}

int compareResult=((Integer) theelement).compareTo((Integer) root.element);

if(compareResult<0){

System.out.println("第"+i+"步:  "+"将要插入的值为"+theelement+"比节点"+root.element+"小,将该值插入节点的左孩子");

root.left=insert(theelement,root.left);

}

else if(compareResult>0){

System.out.println("第"+i+"步:  "+"将要插入的值为"+theelement+"比节点"+root.element+"大,将该值插入节点的右孩子");

root.right=insert(theelement,root.right);

}

else

System.out.println("第"+i+"步:  "+"存在等值的节点所以没有插入该节点 该节点的数据累加值为"+(root.k+1));

return root;

}

//打印树的节点

private void printTree(){

if(isEmpty()){

System.out.println("空树");

}

else

printTree(root);

}

private void printTree(BinaryNode root){

if(root!=null){

printTree(root.left);

System.out.println(root.element);

printTree(root.right);

}

}

//查找节点的值是否存在

public boolean contains(T t){

return contains(t,root);

}

private boolean contains(T t,BinaryNode root){

if(root==null)

return false;

int compareResult=((Integer) t).compareTo((Integer)root.element);

if(compareResult<0)

return contains(t,root.left);

else if(compareResult>0)

return contains(t,root.right);

else

return true;

}

//找出最小的值

private Integer findMin(){

return (Integer)findMin(root).element;

}

private BinaryNode findMin(BinaryNode root){

if(root==null)return null;

else if(root.left==null)

return root;

return findMin(root.left);

}

//找出最大的值

private Integer findMax(){

return (Integer)findMax(root).element;

}

private BinaryNode findMax(BinaryNode root){

if(root==null)

return null;

else if(root.right==null)

return root;

else

return findMax(root.right);

}

//删除一个节点

private void remove(T t){

this.root=remove(t,root);

}

private BinaryNode remove(T t,BinaryNode root){

if(root==null){

return root;

}

int compareResult=((Integer)t).compareTo((Integer)root.element);

if(compareResult<0){

root.left=remove(t,root.left );

}

else if(compareResult>0){

root.right=remove(t,root.right);

}

else if(root.left!=null&&root.right!=null){

root.element=findMin(root.right).element;//找出右子树最小值

root.right=remove((T)root.element,root.right);

}

else

root=(root.left!=null)?root.left:root.right;

return root;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

BinarySearchTree searchTree=new BinarySearchTree();

searchTree.insert(5);

searchTree.insert(7);

searchTree.insert(9);

searchTree.insert(1);

searchTree.insert(6);

searchTree.insert(50);

searchTree.insert(23);

searchTree.insert(6);

searchTree.printTree();

System.out.println("是否含有6: "+searchTree.contains(6));

System.out.println("查找到最小的是: "+searchTree.findMin());

System.out.println("查找到最大的是: "+searchTree.findMax());

searchTree.remove(9);

searchTree.printTree();

}

}

//定义二叉树的节点

class BinaryNode{

int k=1;//累计相同节点的个数

Object element;

BinaryNode left;

BinaryNode right;

public BinaryNode(T theelement){

this(theelement,null,null);

};

public BinaryNode(T theelement,BinaryNode left,BinaryNode right){

element=theelement;

this.left=left;

this.right=right;

};

}

测试结果:

第1步:  在插入数据的位置创建一个节点 该节点的值是: 5

第2步:  将要插入的值为7比节点5大,将该值插入节点的右孩子

第3步:  在插入数据的位置创建一个节点 该节点的值是: 7

第4步:  将要插入的值为9比节点5大,将该值插入节点的右孩子

第5步:  将要插入的值为9比节点7大,将该值插入节点的右孩子

第6步:  在插入数据的位置创建一个节点 该节点的值是: 9

第7步:  将要插入的值为1比节点5小,将该值插入节点的左孩子

第8步:  在插入数据的位置创建一个节点 该节点的值是: 1

第9步:  将要插入的值为6比节点5大,将该值插入节点的右孩子

第10步:  将要插入的值为6比节点7小,将该值插入节点的左孩子

第11步:  在插入数据的位置创建一个节点 该节点的值是: 6

第12步:  将要插入的值为50比节点5大,将该值插入节点的右孩子

第13步:  将要插入的值为50比节点7大,将该值插入节点的右孩子

第14步:  将要插入的值为50比节点9大,将该值插入节点的右孩子

第15步:  在插入数据的位置创建一个节点 该节点的值是: 50

第16步:  将要插入的值为23比节点5大,将该值插入节点的右孩子

第17步:  将要插入的值为23比节点7大,将该值插入节点的右孩子

第18步:  将要插入的值为23比节点9大,将该值插入节点的右孩子

第19步:  将要插入的值为23比节点50小,将该值插入节点的左孩子

第20步:  在插入数据的位置创建一个节点 该节点的值是: 23

第21步:  将要插入的值为6比节点5大,将该值插入节点的右孩子

第22步:  将要插入的值为6比节点7小,将该值插入节点的左孩子

第23步:  存在等值的节点所以没有插入该节点 该节点的数据累加值为2

1

5

6

7

9

23

50

是否含有6: false

查找到最小的是: 1

查找到最大的是: 50

1

5

6

7

23

50

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值