二叉树(数据结构篇)

数据结构之二叉树

二叉树

概念

  • 二叉树(binary tree)是一颗每个节点都不能多于两个子节点的树,左边的子树称为左子树,右边的子树称为右子树

性质

  • 二叉树实际上是图,二叉树相对于树更常用。

  • 平衡二叉树的深度要比节点数小很多,平均深度为O(n1/2)

  • 对于特殊类型的二叉树,即二叉查找树(binary search tree) ,平均深度为O(logN),最坏情况的二叉树深度能达到N-1。

先序遍历

  • 先处理根节点,再处理左子树,最后处理右子树
  • 处理左子树的时候,也是先处理左子树的根节点,再处理剩下的左子节点(或者子树),最后处理右节点(子树)
  • 处理右子树跟处理左子树的操作一样。

中序遍历

  • 先处理左子树,再处理根节点,最后处理右子树
  • 处理左子树时,也是先处理左子树的左子节点(子树),再处理左子树的根节点,最后处理左子树的右子节点(子树)
  • 处理完左子树,处理根节点
  • 处理完根节点,就处理右子树(跟处理左子树的操作一致)

后序遍历

  • 先处理左子树,再处理右子树,最后处理根节点
  • 处理左子树的时候,也是先处理左子树的左子节点(子树),再处理左子树的右子节点(子树),最后处理左子树的根节点。
  • 处理完左子树,再处理右子树(跟处理左子树的操作一致)
  • 处理完右子树,最后处理树的根节点

代码:

cpp 复制代码
//二叉树,用left指向左子树节点,用right指向右子树节点
struct binarytree{
    int data;
    binarytree* left;
    binarytree* right;
};

binarytree* createNode(int data){
    auto p=new binarytree;
    p->data=data;
    p->left=NULL;
    p->right=NULL;
    return p;
}

//插入节点
void addchild(binarytree* &root,int data){
    binarytree* add= createNode(data);
    if(NULL == root) {
        root = add;
    }else{
        if(NULL == root->left){
            root->left=add;
        }else if(NULL==root->right){
            root->right=add;
        }else{
            cout<<"该根节点的子节点已满!"<<endl;
        }
    }
}


//先序查找
binarytree* preorderSearch(binarytree* root,int data){
    if(NULL == root){
        return NULL;
    }
    if(root->data==data){
        return root;
    }
    binarytree* res;
    res=preorderSearch(root->left,data);
    if(NULL!=res){
        return res;
    }
    res=preorderSearch(root->right,data);
    if(NULL!=res){
        return res;
    }
    return NULL;
}

//中序查找
binarytree* inorderSearch(binarytree* root,int data){
    if(NULL == root){
        return NULL;
    }
    binarytree* res;
    res=inorderSearch(root->left,data);
    if(NULL!=res){
        return res;
    }
    if(root->data==data){
        return root;
    }
    res=inorderSearch(root->right,data);
    if(NULL!=res){
        return res;
    }
    return NULL;
}

//后序查找
binarytree* postorderSearch(binarytree* root,int data){
    if(NULL==root){
        return NULL;
    }
    binarytree* res;
    res= postorderSearch(root->left,data);
    if(NULL!=res){
        return res;
    }
    res= postorderSearch(root->right,data);
    if (NULL!=res){
        return res;
    }
    if(root->data==data){
        return root;
    }
    return NULL;
}

//清空树
void destroy(binarytree* &root){
    if(NULL==root){
        return;
    }
    destroy(root->left);
    destroy(root->right);
    delete root;
}

//删除节点
void del(binarytree* &root,int data){
    if(NULL == root){
        return;
    }
    binarytree* p=root;
    binarytree* tail=root->left;
    while (tail!=NULL){
        if(tail->data==data){
            if(NULL!=p->right){
                p->left=p->right;
                p->right=NULL;
                delete tail;
                return;
            }else{
                delete tail;
                p->left=NULL;
                return;
            }
        }
        del(tail,data);
        binarytree* temp=p;
        p=tail;
        tail=temp->right;
    }
    return;
}

//先序遍历
void preorderprint(binarytree* root){
    if(NULL==root){
        return;
    }
    cout<<root->data<<" ";
    preorderprint(root->left);
    preorderprint(root->right);
}

//中序遍历
void inorderprint(binarytree* root){
    if(NULL==root){
        return;
    }
    inorderprint(root->left);
    cout<<root->data<<' ';
    inorderprint(root->right);
}

//后序遍历
void postorderprint(binarytree* root){
    if(NULL==root){
        return;
    }
    postorderprint(root->left);
    postorderprint(root->right);
    cout<<root->data<<' ';
}

尾言

完整版笔记也就是数据结构与算法专栏完整版可到我的博客进行查看,或者在github库中自取(包含源代码)

相关推荐
地平线开发者6 小时前
J6B vio scenario sample
算法
BothSavage18 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn18 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽20 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说1 天前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰2 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术2 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六2 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程