基于C++的二叉树的基本操作

代码仅供参考(手动滑稽)

#include <iostream>

using namespace std;
typedef struct BitNode{
    char data;
    BitNode *lchild;
    BitNode *rchild;
}BitNode;

class bitt{
    private:
    BitNode * bt;
    void create(BitNode *&t) ;
    int countleaf(BitNode *t);
    int counthigh(BitNode *t);
    BitNode *search(BitNode *t,char x);
    
    public:
void recreate();
int countleafbit();
int counthighbit();
BitNode *search();
};
 
void bitt::create(BitNode *&t){
    char ch;
    cin >> ch;
    if (ch == '.') t = NULL;//是否等于空树
    else{
        t = new BitNode;
        t->data = ch;
        create(t->lchild);
        create(t->rchild); //递归输入
    }
}//创建二叉树

void bitt::recreate(){
    BitNode *t;
    create(t);
    bt = t;
}

int bitt::countleafbit(){
    BitNode *t = bt; //保护私有成员
    return countleaf(t);   
}

int bitt::countleaf(BitNode *t){
    if(t == NULL)return 0;
    else{
        int m = countleaf(t->lchild);
        int n = countleaf(t->rchild);
        if(m+n==0)return 1;
        else return m+n;
    }
}
int bitt :: counthigh(BitNode *t){
if(t == NULL)return 0;
else{
    int m = 1 + counthigh(t->lchild);
    int n = 1 + counthigh(t->rchild);
    if (m >= n )return m;
    else n;
}

int bit::counthighbit(){
    BitNode *t = bt;
    return countleaf(t);
}
}


BitNode* bitt::search(BitNode *t,char x){
    if (t== NULL)return t;
    else {
        if (t->data == x) return t;
        BitNode *p = search(t->lchild,x);
        if(p)return p;
        return search(t->rchild ,x);
    }
}

BitNode * bitt :: searchbit(char x){
    BitNode *t = bt;
    return search(t , x);
}
//iiiiiiiiiii
int countone(bitnode *t){
    if(!t)return 0;
    if(t->lchild && !t->rchild == 0 || !t->lchild && t->rchild)return 1 + countone(t->child)+counto(t->rchild);
    else return countone(t->child)+counto(t->rchild) ;
}
//计算结点数量

//判定两个二叉树是否相识
//形式相同,长的一样,节点的值不一样
//相似返回1,否则返回0

int alike(bitnode *t,bitnode *s){
    if(!t && !s)return 1;
    if(t && !s || !t && s)return 0;
    else return alike(t->lchild , s->lchild) && alike(t->rchild , s->rchild)
}

//


int main(){
    bitt bbt;
    bbt.recreate();
    cout << bbt.countleafbit << endl;
    cout << bbt.search('a')->data << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值