好好学数据结构 - 手撕二叉树增删改查

 呵。。写到删除的时候 说实话蒙的一比。充分暴露除了数据结构的不足。下面献上自己写的结构。粗略测过,供大家参考吧,如果有漏洞不足还请指出。。恩。

package binaryTree;

import java.util.ArrayList;
import java.util.List;

public class BinaryTree>{
    
	//成员变量
    private BinaryTreeNode nodeData;
    private int size;
    
    //构造器
    public BinaryTree () {
        this.nodeData = null;
        this.size = 0;
    }
    public BinaryTree(E e) {
        this.nodeData = new BinaryTreeNode(e);
    }
 
    //(空树)增加一个节点
    public boolean add(E e) {
        if(null ==nodeData) {
            nodeData = new BinaryTreeNode(e);
            size++;
            return true;
        }
        if(add(nodeData,e)) {
        	size++;
        	return true;
        }
        return false;
    }
    
    //(非空树)增加一个节点
    private boolean add(BinaryTreeNode nodeData,E e) {
        if(e.compareTo(nodeData.e)<0 ){
            if(null == nodeData.left) {
                nodeData.left = new BinaryTreeNode(e);
                return true;
            }else {
                add(nodeData.left,e);
            }
        }else if(e.compareTo(nodeData.e)>0 ) {
            if(null == nodeData.right) {
                nodeData.right = new BinaryTreeNode(e);
                return true;
            }else {
                add(nodeData.right,e);
            }
        }
        return true;
    }
    
    //先序遍历 【根左右】
    public List preOrderScan(BinaryTreeNode nodeData){
    	List eList = new ArrayList(size); 
    	preOrderScan(nodeData==null?this.nodeData:nodeData,eList);
    	return eList;
    }
    private void preOrderScan(BinaryTreeNode nodeData,List eList) {
    	if(null != nodeData) {
    		eList.add(nodeData.e);
    		preOrderScan(nodeData.left,eList);
    		preOrderScan(nodeData.right,eList);
    	}
    }
    
    //中序遍历 【左根右】
    public List inOrderScan(BinaryTreeNode nodeData){
    	List eList = new ArrayList(size); 
    	inOrderScan(nodeData==null?this.nodeData:nodeData,eList);
    	return eList;
    }
    private void inOrderScan(BinaryTreeNode nodeData,List eList) {
    	if(null != nodeData) {
    		preOrderScan(nodeData.left,eList);
    		eList.add(nodeData.e);
    		preOrderScan(nodeData.right,eList);
    	}
    }
    
    //后续遍历 【左右根】
    public List postOrderScan(BinaryTreeNode nodeData){
    	List eList = new ArrayList(size); 
    	postOrderScan(nodeData==null?this.nodeData:nodeData,eList);
    	return eList;
    }
    private void postOrderScan(BinaryTreeNode nodeData,List eList) {
    	if(null != nodeData) {
    		postOrderScan(nodeData.left,eList);
    		postOrderScan(nodeData.right,eList);
    		eList.add(nodeData.e);
    	}
    }
    
    //获取最大的节点
    public BinaryTreeNode getMaxDataNode(BinaryTreeNode nodeData) {
    	BinaryTreeNode maxDataNode = nodeData==null?this.nodeData:nodeData;
    	while(null!=maxDataNode.right) {
    		maxDataNode = maxDataNode.right;
    	}
    	return maxDataNode;
    }
    
    //获取最小的节点
    public BinaryTreeNode getMinDataNode(BinaryTreeNode nodeData) {
    	BinaryTreeNode minDataNode = nodeData==null?this.nodeData:nodeData;
    	while(null!=minDataNode.left) {
    		minDataNode = minDataNode.left;
    	}
    	return minDataNode;
    }
    
  //获取节点
    public BinaryTreeNode getNodeData(E e) {
    	BinaryTreeNode nodeData = this.nodeData;
    	if(e == nodeData.e) {
    		return nodeData;
    	}
    	while(nodeData!=null) {
    		if(e.compareTo(nodeData.e)>0) {
    			nodeData = nodeData.right;
    			continue;
    		}else if(e.compareTo(nodeData.e)<0) {
    			nodeData = nodeData.left;
    			continue;
    		}else if(e.compareTo(nodeData.e)==0) {
    			return nodeData;
    		}
    	}
    	return null;
    }
    
    //获取父节点
    public BinaryTreeNode getSuperNodeData(BinaryTreeNode nodeData) {
    	BinaryTreeNode parentDataNode = this.nodeData;
    	if(null == nodeData || nodeData == parentDataNode) {
    		return null;
    	}
    	BinaryTreeNode tempDataNode = this.nodeData;
    	while(tempDataNode!=null) {
    		if(nodeData.e.compareTo(tempDataNode.e)>0) {
    			parentDataNode = tempDataNode;
    			tempDataNode = tempDataNode.right;
    			continue;
    		}else if(nodeData.e.compareTo(tempDataNode.e)<0) {
    			parentDataNode = tempDataNode;
    			tempDataNode = tempDataNode.left;
    			continue;
    		}else if(nodeData.e.compareTo(tempDataNode.e)==0) {
    			return parentDataNode;
    		}
    	}
    	return null;
    }
    
    //获取父节点
    public BinaryTreeNode getSuperNodeData(E e) {
    	BinaryTreeNode parentDataNode = this.nodeData;
    	if(null == e || e == parentDataNode.e) {
    		return null;
    	}
    	BinaryTreeNode tempDataNode = this.nodeData;
    	while(tempDataNode!=null) {
    		if(e.compareTo(tempDataNode.e)>0) {
    			parentDataNode = tempDataNode;
    			tempDataNode = tempDataNode.right;
    			continue;
    		}else if(e.compareTo(tempDataNode.e)<0) {
    			parentDataNode = tempDataNode;
    			tempDataNode = tempDataNode.left;
    			continue;
    		}else if(e.compareTo(tempDataNode.e)==0) {
    			return parentDataNode;
    		}
    	}
    	return null;
    }
    
    //获取二叉树节点个数
    public int getSize() {
        return size;
    }
    //判断二叉树是否为空
    public boolean isEmpty() {
        return size == 0;
    }
 
    /**
     * 删除节点
     * 四种情况
     * 1、待删除节点的左右子都是null
     * 2、待删除节点的左子为null
     * 3、待删除节点的右子为null
     * 4、待删除节点的左右子都不是null
     * @param e
     * @return
     */
    
	public boolean remove(E e) {
		BinaryTreeNode parentNode = getSuperNodeData(e);
		BinaryTreeNode deleteNode = getNodeData(e);
        if(this.remove(parentNode,deleteNode) !=null) {
        	size--;	
        	return true;
        }
        return false;
    }
    
    private BinaryTreeNode remove(BinaryTreeNode parentNode, BinaryTreeNode deleteNode) {
        //1、待删除节点的左右子都是null,此时直接父节点的左或右子节点为空。
    	if(parentNode != null && deleteNode.left == null && deleteNode.right == null) {
            if(null != parentNode.left && parentNode.left.e == deleteNode.e) {
                parentNode.left = null;
            }else {
                parentNode.right = null;
            }
            return deleteNode;
        }else if(parentNode != null && deleteNode.left == null && deleteNode.right != null) {
            if(null != parentNode.left && parentNode.left.e == deleteNode.e) {
                parentNode.left = deleteNode.right;
            }else {
                parentNode.right = deleteNode.right;
            }
            return deleteNode;
        }else if(parentNode != null && deleteNode.left != null && deleteNode.right == null) {
            if(null != parentNode.left && parentNode.left.e == deleteNode.e) {
                parentNode.left = deleteNode.left;
            }else {
                parentNode.right = deleteNode.left;
            }
            return deleteNode;
        }else if(parentNode != null && deleteNode.left != null && deleteNode.right != null) {
        	BinaryTreeNode deletedNode = deleteNode;
            if(null != parentNode.left && parentNode.left.e == deleteNode.e) {
                //取待删除节点左子树最大值,替换待删除节点
            	//parentNode.left = getMaxDataNode(deleteNode.left);
            	BinaryTreeNode maxNode = getMaxDataNode(deleteNode.left);
            	deleteNode.e = remove(getSuperNodeData(maxNode),maxNode).e;
            }else {
                //取待删除节点左子树最大值,替换待删除节点
            	BinaryTreeNode minNode = getMinDataNode(deleteNode.left);
            	deleteNode.e = remove(getSuperNodeData(minNode),minNode).e;
            }
            return deletedNode;
        //如果是根节点
        }else if(parentNode == null){
        	BinaryTreeNode deletedNode = deleteNode;
        	BinaryTreeNode maxNode = getMaxDataNode(deleteNode.left);
        	deleteNode.e = remove(getSuperNodeData(maxNode),maxNode).e;
        	return deletedNode;
        }
		return null;
    }
   
    //清空
    public void clear() {
        this.nodeData = new BinaryTreeNode();
    }
 
}

 

你可能感兴趣的:(java数据结构)