二叉树(数据结构篇)

数据结构之二叉树

二叉树

概念

  • 二叉树(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库中自取(包含源代码)

相关推荐
期待のcode10 小时前
Java虚拟机的垃圾回收器
java·开发语言·jvm·算法
星火开发设计10 小时前
C++ 分支结构:if-else 与 switch-case 的用法与区别
开发语言·c++·学习·算法·switch·知识·分支
txzrxz10 小时前
数据结构有关的题目(栈,队列,set和map)
数据结构·c++·笔记·算法··队列
CoderCodingNo11 小时前
【GESP】C++五级练习题(前缀和) luogu-P1114 “非常男女”计划
数据结构·c++·算法
知乎的哥廷根数学学派11 小时前
基于卷积特征提取和液态神经网络的航空发动机剩余使用寿命预测算法(python)
人工智能·pytorch·python·深度学习·神经网络·算法
我是大咖11 小时前
关于柔性数组的理解
数据结构·算法·柔性数组
叫我:松哥11 小时前
基于神经网络算法的多模态内容分析系统,采用Flask + Bootstrap + ECharts + LSTM-CNN + 注意力机制
前端·神经网络·算法·机器学习·flask·bootstrap·echarts
每天学一点儿11 小时前
【医学图像处理】SimpleITK 图像配准全流程解析
算法
不穿格子的程序员11 小时前
从零开始写算法——回溯篇1:全排列 + 子集
算法·leetcode·深度优先·回溯
wen__xvn11 小时前
代码随想录算法训练营DAY18第六章 二叉树part06
数据结构