c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)

来源:互联网 发布:水利软件涌金 编辑:程序博客网 时间:2024/05/16 06:43

这是算法导论中二叉树搜索的一个例题

二叉树为


c++代码为

#include<iostream>#define N 7using namespace std;//二叉树节点类class node{public:    int data;    node *leftChild;    node *rightChild;};typedef node *BiTree;//等价//创建节点node *createNode(int value){    node *q=new node;    q->leftChild=NULL;    q->rightChild=NULL;    q->data=value;    return q;}//创建二叉树BiTree createBiTree(){    node *p[N]={NULL};    int array[6]={6,5,7,2,5,8};    for(int i=0;i<6;++i)        p[i]=createNode(array[i]);    for(int i=0;i<N/2;++i)    {        p[i]->leftChild=p[i*2+1];        p[i]->rightChild=p[i*2+2];    }    return p[0];//返回根节点}//求二叉树的深度int depth(BiTree tree){    int dep=0;    int leftDep,rightDep;    if(!tree)        dep=0;    else    {        leftDep=depth(tree->leftChild);        rightDep=depth(tree->rightChild);        dep=1+(leftDep>rightDep?leftDep:rightDep);    }    return dep;}//度为1的节点数,即只有一个分支int numOfOneDgree(BiTree tree){    int sum=0;    int leftNum,rightNum;    if(tree)    {        if(((tree->leftChild!=NULL)&&(tree->rightChild==NULL))||((tree->leftChild==NULL)&&(tree->rightChild!=NULL)))            sum++;        leftNum=numOfOneDgree(tree->leftChild);//递归        sum+=leftNum;        rightNum=numOfOneDgree(tree->rightChild);        sum+=rightNum;    }    return sum;}//叶子节点的个数int sumOfSubLeft(BiTree tree){    int sum=0;    int leftNum,rightNum;    if(tree)    {        if((!tree->leftChild)&&(!tree->rightChild))//左右节点均为NULL            sum++;        leftNum=sumOfSubLeft(tree->leftChild);        sum+=leftNum;        rightNum=sumOfSubLeft(tree->rightChild);        sum+=rightNum;    }    return sum;}//查找bool search(BiTree tree,int searchNum){    if(tree==NULL)    {        return 0;    }    else    {        if(tree->data==searchNum)        {            return 1;        }        else        {            if(searchNum<tree->data)                return search(tree->leftChild,searchNum);            else                return search(tree->rightChild,searchNum);        }    }}//访问节点中的数据int visit(BiTree tree){    return tree->data;}// 中序遍历void inorderTreeWalk(BiTree tree){    if(tree)    {        inorderTreeWalk(tree->leftChild);        cout << visit(tree) << " ";        inorderTreeWalk(tree->rightChild);    }}//插入void insertNode(node* *tree ,int x){    if(*tree==NULL)//新建一个根节点    {        node *p=new node;        p->data=x;        p->leftChild=NULL;        p->rightChild=NULL;        *tree=p;        return;    }    else if(x<(*tree)->data)        insertNode(&((*tree)->leftChild),x);    else        insertNode(&((*tree)->rightChild),x);}//删除,比较麻烦,分几种情况int deleteNode(node* *tree,int x){    node *temp=*tree;    if(*tree==NULL)        return 0;    if(x<(*tree)->data)        return deleteNode(&((*tree)->leftChild),x);    if(x>(*tree)->data)        return deleteNode(&((*tree)->rightChild),x);    if((*tree)->leftChild==NULL)//左子树为空,且删除元素等于根节点,把右子树作为整个树    {        *tree=(*tree)->rightChild;        free(temp);        return 1;    }    else    {        if((*tree)->leftChild->rightChild==NULL)//前驱节点为空时,把左孩子节点赋给根节点,再从左子树中删除根节点            {                (*tree)->data=(*tree)->leftChild->data;                return deleteNode(&((*tree)->leftChild),(*tree)->data);            }        else        {//找到前驱节点,再把该节点赋给根节点,最后删除该根节点            node *p1=*tree;            node *p2=p1->leftChild;            while(p2->rightChild!=NULL)            {                p1=p2;                p2=p2->rightChild;            }            (*tree)->data=p2->data;            return deleteNode(&(p1->rightChild),p2->data);        }    }}int main(){    BiTree tree;    tree=createBiTree();    cout<<"二叉树的深度为:"<<depth(tree)<<endl;    cout<<"二叉树的叶子节点个数为:"<<sumOfSubLeft(tree)<<endl;    cout<<"二叉树中度为1的个数为:"<<numOfOneDgree(tree)<<endl;    cout<<"请输入要查找的数,输入1000结束查找"<<endl;    int n,m,w;    while(1)    {        cin>>n;        if(n==1000)            break;        else        {            if(search(tree,n))                cout<<"查找到值为"<<n<<"的数"<<endl;            else                cout<<"未查到到值为"<<n<<"的数"<<endl;        }    }    cout << endl<< "中序遍历结果为:";    inorderTreeWalk(tree);    cout<<endl<<"请输入要插入的值:";    cin>>m;    insertNode(&tree,m);    cout << "插入值后中序遍历结果为:";    inorderTreeWalk(tree);    cout<<endl<<"请输入要删除的值:";    cin>>w;    deleteNode(&tree,w);    cout << "删除值后中序遍历结果为:";    inorderTreeWalk(tree);    return 0;}

运行结果为




阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 天魔正统 正统 大明正统 非正统偶像 正统家族 性价比高的正统汉服淘宝店 正统三国98块钱3888元宝 金正统一售后服务电话 正统三国礼包 明正统青花瓷 萝卜平台正统三国 正统三国手游下载 正统三国手游礼包 正统三国平民玩家必要武将 正统三国各章武将详解 基础款jk怎么分正统 枣庄颐正园酒店 临沂奥正诚园 枣庄颐正园 哈尔滨大正臻园 临沂颐正园酒店 正胃胶囊 正胃片 元和正胃片多少钱一盒 正能贷平台 正能贷贷款 宜信贷款好贷不 正能力激励人的好句子 正能量演讲稿三分钟 正能量句子励志简短 早安正能量语录 励志图片正能量 每日正能量语录 鼓励团队的正能量的话 早上正能量 正能量励志经典语录 正能量语录集 正能量段子 正能量的词语 励志正能量句子 一句正能量的话