二叉树的实现(基于链表)

 二叉树基本操作

 二叉树的结构

struct Tnode{
    Elem data;
    Tnode *lch,*rch;
    Tnode(int val){
        data = val;
        lch=NULL;
        rch=NULL;
    }
}; 

二叉树的建立(先序输入)

 void BinTreeInput(Tnode *&root){
    Elem x;
    cin>>x;
    if(x==-1){
        root=NULL;
    }else{
        root = new Tnode(x);
        BinTreeInput(root->lch);
        BinTreeInput(root->rch);
    }
}

注:如何建立二叉树应根据数据的输入方式变化 

先序遍历输出

void BinTreePreorder(const Tnode *root){//先序遍历输出 
    if(root){
        printf("%d ",root->data);
        BinTreePreorder(root->lch);
        BinTreePreorder(root->rch);
    }
}

 中序遍历输出

void BinTreeInorder(const Tnode *root){//中序遍历输出 
    if(root){
        BinTreeInorder(root->lch);
        printf("%d ",root->data);
        BinTreeInorder(root->rch);
    }
}

后序遍历输出

void BinTreePostorder(const Tnode *root){//后续遍历输出 
    if(root){
        BinTreePostorder(root->lch);
        BinTreePostorder(root->rch);
        printf("%d ",root->data);
    }
}

二叉树的删除操作

void BinTreeDestroy(Tnode *root){//二叉树的删除操作 
    if (root) {
        // 递归释放左子树和右子树
        BinTreeDestroy(root->lch);
        BinTreeDestroy(root->rch);

        // 释放当前节点
        delete root;
        cout<<"释放一个结点"<<endl;
        root = NULL;
    }
}

 

 方法一:用结构体实现

实现代码:

#include<iostream>
using namespace std;
typedef int Elem;
struct Tnode{
    Elem data;
    Tnode *lch,*rch;
    Tnode(int val){
        data = val;
        lch=NULL;
        rch=NULL;
    }
}; 
void BinTreeCreate(Tnode *&root){
    root=NULL;
}
void BinTreeInput(Tnode *&root){
    Elem x;
    cin>>x;
    if(x==-1){
        root=NULL;
    }else{
        root = new Tnode(x);
        BinTreeInput(root->lch);
        BinTreeInput(root->rch);
    }
}
void BinTreePreorder(const Tnode *root){//先序遍历输出 
    if(root){
        printf("%d ",root->data);
        BinTreePreorder(root->lch);
        BinTreePreorder(root->rch);
    }
}
void BinTreeInorder(const Tnode *root){//中序遍历输出 
    if(root){
        BinTreeInorder(root->lch);
        printf("%d ",root->data);
        BinTreeInorder(root->rch);
    }
}
void BinTreePostorder(const Tnode *root){//后续遍历输出 
    if(root){
        BinTreePostorder(root->lch);
        BinTreePostorder(root->rch);
        printf("%d ",root->data);
    }
}
void BinTreeDestroy(Tnode *root){//二叉树的删除操作 
    if (root) {
        // 递归释放左子树和右子树
        BinTreeDestroy(root->lch);
        BinTreeDestroy(root->rch);

        // 释放当前节点
        delete root;
        cout<<"释放一个结点"<<endl;
        root = NULL;
    }
}
int main(){
    Tnode *r;
    BinTreeCreate(r);
    BinTreeInput(r);
    cout<<"先序输出结果:";
    BinTreePreorder(r);
    cout<<endl;
    cout<<"中序输出结果:";
    BinTreeInorder(r);
    cout<<endl;
    cout<<"后序输出结果:";
    BinTreePostorder(r);
    cout<<endl;
    BinTreeDestroy(r);//二叉树的删除操作 
    return 0;
}

方法二:封装在一个类里面

考完再想,懒的写

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会C++的Mr.Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值