从零开始学数据结构和算法(六)二叉排序树,Android程序员怎么优雅迈过30K+这道坎

本文介绍了如何在Java中实现二叉树的节点插入、中序遍历、查找和删除操作。同时提到了一份针对Android移动开发的全套学习资料,包含了大厂面经、学习笔记等资源,可供自学者参考和获取。
摘要由CSDN通过智能技术生成

//生成一个节点放入
TreeNode newNode=new TreeNode(data);
if(data<parent.data) {
parent.leftChild = newNode;
}else{
parent.rightChild=newNode;
}
newNode.parent=parent;

return newNode;
}
/**

  • 中序遍历
    */
    public void midOrderTraverse(TreeNode root){
    if(root==null){
    return;
    }
    //LDR
    midOrderTraverse(root.leftChild);
    System.out.print(root.data+" ");
    midOrderTraverse(root.rightChild);
    }

/**

  • 查找一个节点
    */
    public TreeNode searchNode(int data){
    if(rootnull){
    return null;
    }
    TreeNode node=root;
    while(node!=null){
    if(node.data
    data){
    return node;
    }else if(data>node.data){
    node=node.rightChild;
    }else if(data<node.data){
    node=node.leftChild;
    }
    }
    return null;
    }

/**

  • 删除节点
  • 要删除的节点在树上是一定存在的才删除
    */
    public void delNode(TreeNode node){
    if(nodenull){
    throw new NoSuchElementException();
    }else{
    //先得到父亲,方便后面的操作
    TreeNode parent=node.parent;
    //1.叶子
    if(node.leftChild
    null && node.rightChildnull){
    //特别的情况:1.树上只有一个节点或是空树
    if(parent
    null){
    root=null;
    }else if(parent.rightChildnode){
    parent.rightChild=null;
    }else if(parent.leftChild
    node){
    parent.leftChild=null;
    }
    node.parent=null;
    }else if(node.leftChild!=null && node.rightChildnull){
    //2.只有左孩子
    if(parent
    null){//如果要删除的是根
    node.parent=null;
    node.leftChild.parent=null;
    root=node.leftChild;
    }else{
    if(parent.leftChild==node){//要删除的节点是父亲的左边
    node.leftChild.parent=parent;
    parent.leftChild=node.leftChild;

}else{//要删除的节点是父亲的右边
node.leftChild.parent=parent;
parent.rightChild=node.leftChild;
}
node.parent=null;
}

}else if(node.leftChildnull && node.rightChild!=null){
//3.只有右孩子
if(parent
null){//如果要删除的是根
node.parent=null;
node.rightChild.parent=null;
root=node.rightChild;
}else{
if(parent.leftChildnode){//要删除的节点是父亲的左边
node.rightChild.parent=parent;
parent.leftChild=node.rightChild;
}else{//要删除的节点是父亲的右边
node.rightChild.parent=parent;
parent.rightChild=node.rightChild;
}
node.parent=null;
}
}else{//4。有左右两个孩子
if(node.rightChild.leftChild
null){//1.如果被删除节点的右子树的左子树为空,就直接补上右子树
node.rightChild.leftChild=node.leftChild;
if(parentnull){
root=node.rightChild;
}else{
if(parent.leftChild
node){
parent.leftChild=node.rightChild;
//
}else{
parent.rightChild=node.rightChild;
//
}
}
node.parent=null;
}else{//2.否则就要补上右子树的左子树上最小的一个
TreeNode leftNode=getMinLeftTreeNode(node.rightChild);
//1
leftNode.leftChild=node.leftChild;
//2
TreeNode leftNodeP=leftNode.parent;
leftNodeP.leftChild=leftNode.rightChild;
//3
leftNode.rightChild=node.rightChild;
//4
if(parentnull){
root=leftNode;
}else{
if(parent.leftChild
node){
parent.leftChild=leftNode;
//
}else{
parent.rightChild=leftNode;
//
}
}
}
}
}
}

private TreeNode getMinLeftTreeNode(TreeNode node) {
TreeNode curRoot=null;
if(node==null){
return null;
}else{
curRoot=node;
while(curRoot.leftChild!=null){
curRoot=curRoot.leftChild;
}
}
return curRoot;
}

public static class TreeNode{
int data;
TreeNode leftChild;
TreeNode rightChild;
TreeNode parent;
public TreeNode(int data){
this.data=data;
this.leftChild=null;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

-2dScr8Lz-1710933544377)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-Z73xuXaM-1710933544377)]

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值