二叉树(数据结构篇)

数据结构之二叉树

二叉树

概念

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

相关推荐
Forever Nore43 分钟前
LeetCode 1 两数之和
算法·leetcode·职场和发展
To_OC1 小时前
LC 3 无重复字符的最长子串:从入门滑动窗口到优化写法,再也不怕面试官追问
javascript·算法·leetcode
(Charon)1 小时前
【C++】多线程死锁:产生原因、复现与解决方法
开发语言·c++·算法
lucas_AI2 小时前
ConfBench:大模型的「我很有把握」,到底能不能信?
人工智能·算法
oier_Asad.Chen2 小时前
【洛谷题解/AcWing题解/真题】洛谷P2330【SCOI2005】繁忙的都市(Kruskal算法的再探究))
c++·算法·贪心算法·图论·最小生成树·kruskal
青 春 记 忆5 小时前
LeetCode 53. 最大子数组和|Python 解法详解
python·算法·leetcode
豆瓣鸡5 小时前
算法日记 - Day10
算法
戴西软件5 小时前
戴西CAxWorks.VPG车辆工程仿真软件技术解析(上)——安全仿真体系的自动化构建
运维·网络·数据库·人工智能·算法·安全·自动化
zander2585 小时前
4. 寻找两个正序数组的中位数:用分割点代替合并
数据结构·算法
Kisorge5 小时前
【电机控制器】 基于STSPIN32G4的FOC控制
stm32·嵌入式硬件·算法