实现代码如下(详解在下一期博客)博主写累了呜呜呜!
具体实现原理 1.说白了就是分类讨论 2.递归查找删除元素
3. 在删除结点的位置 也需要注意 !!!
4.最重要的 还有一个点 就是 要分清楚值传递和引用传递(地址传递)什么是copy的内容 等等 另外就是 要很清楚 递归!!!复杂的递归的理解 不理解可能很难写的流畅写的简洁 (虽然我也不是十分清晰)
结点数据结构如下:
public class treeNode {
//大小一般比较的是数
public int val;
public treeNode l_child;
public treeNode r_child;
public treeNode(){
this.l_child=this.r_child=null;
}
public treeNode(int date){
this.val=date;
}
}
package 算法and数据结构.数据结构.二叉树.BinarySearchTree.DeleteElement01;
import 算法and数据结构.数据结构.二叉树.binaryTree.Node;
import 算法and数据结构.数据结构.二叉树.treeNode;
public class Delete {
private treeNode Min;
private treeNode Max;
public static treeNode delete(treeNode treeNode,int val){
//特殊情况 :当前树只有一个结点
if(treeNode.l_child==null && treeNode.r_child==null){
//恰好要删除的就是该节点 或者不是
if(treeNode.val==val)return null;
return treeNode;
}
return new Delete().SearchDeleteElement(treeNode,null,val);
}
//============================================寻找=======================================================
private treeNode SearchDeleteElement(treeNode current,treeNode parent,int val){
if(current==null){
return null;
}
if(