数据结构链式二叉树C语言

#include <iostream>
#include <queue>
// queue是C++语言中的头文件,它包含了队列的实现以及一系列的操作。
// 在这里是为了实现二叉树的层次实现
using namespace std;
#define TRUE 1
#define FALSE 0
#define Elemtype char
typedef struct tNode{
    Elemtype data;
    struct tNode *leftchild, *rightchild;
}tNode, *biTree;

void initBiTree(biTree &T) {
    T = NULL;
}

// 先序创建二叉树
void createBiTree(biTree &T) {
    Elemtype e;
    cin >> e;
    if(e != '#') {
        T = new tNode;
        T->data = e;
        createBiTree(T->leftchild);
        createBiTree(T->rightchild);
    }
    else {
        T = NULL;
    }
}

// 销毁树
void destroyBiTree(biTree &T) {
    if(T == NULL) return ;
    else {
        destroyBiTree(T->leftchild);
        destroyBiTree(T->rightchild);
        delete(T);
    }
}

// 获得一个结点的左子树
bool getLeftChildBiTree(biTree T, biTree &leftChild) {
    if(T == NULL) return false;
    else {
        initBiTree(leftChild);
        leftChild = T->leftchild;
        return true;
    }
}

// 获得一个结点的右子树
bool getRightChildBiTree(biTree T, biTree &rightChild) {
    if(T == NULL) return false;
    else {
        initBiTree(rightChild);
        rightChild = T->rightchild;
        return true;
    }
}

// 先序输出二叉树
void pre_outputBiTree(biTree T) {
    if(T == NULL) return ;
    else {
        cout << T->data << " ";
        pre_outputBiTree(T->leftchild);
        pre_outputBiTree(T->rightchild);
    }
}

// 中序输出二叉树
void med_outputBiTree(biTree T) {
    if(T == NULL) return ;
    else {
        med_outputBiTree(T->leftchild);
        cout << T->data << " ";
        med_outputBiTree(T->rightchild);
    }
}

// 后序输出二叉树
void lat_outputBiTree(biTree T) {
    if(T == NULL) return ;
    else {
        lat_outputBiTree(T->leftchild);
        lat_outputBiTree(T->rightchild);
        cout << T->data << " ";
    }
}

// 层序输出二叉树
void seq_outputBiTree(biTree T) {
    queue<biTree> queue;
    tNode *p;
    queue.push(T);
    while(! queue.empty()) {
        cout << queue.front()->data << " ";
        if(queue.front()->leftchild != NULL)
            queue.push(queue.front()->leftchild);
        if(queue.front()->rightchild != NULL)
            queue.push(queue.front()->rightchild);
        p = queue.front();
        queue.pop();
    }
}

// 获得二叉树的深度
int getDepthBiTree(biTree T) {
    if(T == NULL) return 0;
    else {
        int leftDepth = getDepthBiTree(T->leftchild) + 1;
        int rightDepth = getDepthBiTree(T->rightchild) + 1;
        return max(rightDepth, leftDepth);
    }
}

int main() {
    biTree T;
    initBiTree(T);
    createBiTree(T);
    pre_outputBiTree(T);
    cout << endl;
    med_outputBiTree(T);
    cout << endl;
    lat_outputBiTree(T);
    cout << endl;
    // destroyBiTree(T);
    // pre_outputBiTree(T);
    seq_outputBiTree(T);
    cout << endl;
    int res = getDepthBiTree(T);
    cout << res << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值