(数据结构(2) )------------------ 二叉树(代码实现)

代码

package com.test213;
public class BinaryTree {
	int data;
	BinaryTree left;
	BinaryTree right;
	public BinaryTree(int data) {
		super();
		this.data = data;
		left = null;
		right = null;
	}
	public BinaryTree() {
		super();
	}
	public BinaryTree[] search(BinaryTree root, int data){          //查
		BinaryTree point = new BinaryTree();                        //用来记录当前结点
		BinaryTree point1 = new BinaryTree();                       //用来记住前一个结点
		point = root;
		int i = 0;

			while(point != null){
				i++;
				if(data == point.data) {
					if(i == 1){
						BinaryTree[] binarytree = new BinaryTree[] {point,null};
						return binarytree;
					}
					else{
						BinaryTree[] binarytree = new BinaryTree[] {point,point1};
						return binarytree;
					}
				}
				else if(data > point.data) {
					if(point.right == null) {
						System.out.println("您查找的节点不存在");
						return null;}
					else {
						point1 = point;
						point = point.right;}}
				else {
					if(point.left == null) {
						System.out.println("您查找的节点不存在");
						return null;}
					else {
						point1 = point;
						point = point.left;}
				}
			}
			return null;
	
	}
	public void insert( BinaryTree root, int data ) {                //增
		if(data>root.data) {
			if(root.right== null) {
				root.right = new BinaryTree(data);
			}else {
				this.insert(root.right, data);
			}
		}
		else {
			if(root.left== null) {
				root.left = new BinaryTree(data);
			}else {
				this.insert(root.left, data);
			}
		}
	}
    public void delete(BinaryTree root,int data){                    //删除
    	BinaryTree temp = search(root, data)[0];
    	BinaryTree tempbefore = search(root, data)[1];
    	BinaryTree linshi = new BinaryTree();
    	BinaryTree linshi1 = new BinaryTree();
    	if( temp.right == null && temp.left == null ){               //当前节点为叶子节点
    		if(tempbefore == null) {
    			temp = temp.left;
    			System.out.println(temp);
    		}
    		else {
	    		if( tempbefore.left != null){
		    		if( tempbefore.left.data == temp.data ){             //判断删除的节点是上个节点的左子树还是右子树 	
		    			tempbefore.left = null;
		    		}
	    		}
	    		if(  tempbefore.right != null ){
	    			if( tempbefore.right.data == temp.data ){        //判断删除的节点是上个节点的左子树还是右子树
		        			tempbefore.right = null;
		       			}			
	    		}
	    		System.out.println(tempbefore);
    		}
    	}
    	else {                                                       //只有左子树、只有右子树 、左右子树都有                                   
    		linshi = temp;
    		if(temp.left == null){                                   //只有右子树
    			while(linshi.right != null){
    				linshi1 = linshi;
    				linshi = linshi.right;
    			}
    			if(tempbefore != null){                              //不为头结点的时候
    				if( tempbefore.left == null && tempbefore.right != null){
    					tempbefore.right = temp.right;
    				}
    				if( tempbefore.left != null && tempbefore.right == null){
    					tempbefore.left = temp.right;
    				}
    				if( tempbefore.left != null && tempbefore.right != null){
    					if( tempbefore.left.data == temp.data )      //判断删除的节点是上个节点的左子树还是右子树
    	    			{	
    	    				tempbefore.left = temp.right;
    	    			}
    					if( tempbefore.left.data == temp.data )      //判断删除的节点是上个节点的左子树还是右子树
    	    			{	
    	    				tempbefore.left = temp.right;
    	    			}
    				}
    			}
    			if(tempbefore == null){
    				temp.data = temp.right.data;
    				temp.left = temp.right.left;
    				temp.right = temp.right.right;
    			}
    		}
    		else {                                                   //有左子树的时候
		    		while(linshi.left != null){                      //一直左找
		    			linshi = linshi.left;
		    			if(linshi.right ==null){                     //寻找途中右子树为空
		        			if(linshi.left == null){                 //直到找到左边为空为止(一直找到最下面)
			        				temp.data = temp.left.data;
			        				temp.left = temp.left.left;
		        				}
		        			}
		    			if(linshi.right !=null){                     //寻找途中找到了右子树不为空得时候
		    				while(linshi.right != null) {            //沿着右子树寻找
		    	    			linshi1 = linshi;
		    	    			linshi = linshi.right;}
		    				temp.data = linshi.data;
		    				linshi1.right = linshi.left;
		    				return;
		    				}
		    			}
	    		}
    	}			
    }
    public void change(BinaryTree root,int data,int data1){
    	delete(root,data);
    	insert(root, data1);
    	System.out.println(root);
    }
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("BinaryTree [data=");
		builder.append(data);
		builder.append(", left=");
		builder.append(left);
		builder.append(", right=");
		builder.append(right);
		builder.append("]");
		return builder.toString();
	}
}
package com.test213;


public class Node {
	public static void main(String[] args) {
		int[] array = {15};
		BinaryTree root = new BinaryTree(array[0]);
		for(int i=1;i<array.length;i++){
			   root.insert(root, array[i]); 
			  };
	    System.out.println(root);
	    root.delete(root,15);
	/*	System.out.println(root);*/
		/*root.change(root,66,55);*/
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值