C++实现二叉树存储以及层序遍历

//
// Created by lvhb on 2022/7/20.
//
#include "stdio.h"
#include "malloc.h"

#define MAXSIZE 20


typedef struct TNode {
    int data;
    struct TNode *left, *right;//左子节点与右子节点
} *Tree;

typedef struct Queue {
    int head;//队列头指针,指向队列最先存入的元素
    int tail;//队列尾指针,指向待存入元素的下标
    Tree data[MAXSIZE];//队列容量
    int size;
} Queue;


//初始化,头指针和尾指针都是0
void initQueue(Queue &q) {
    q.head = 0;
    q.tail = 0;
    q.size = 0;
}

//判断一个队列是否为一个空队列
bool isEmpty(Queue q) {
    return (q.tail == q.head) && q.size == 0;
}

//判断一个循环队列是否已满,如果要实现循环队列,则需要牺牲一个存储单元,例如最大10个,现在只能存9个,才能实现循环队列
bool isFullQueue(Queue q) {
    //return ( (queue.tail + 1) % MAXSIZE == queue.head ) && queue.size == MAXSIZE ;
    return (q.tail == q.head) && q.size == MAXSIZE;
}

//入队
bool enQueue(Queue &q, Tree data) {
    if (isFullQueue(q)) {//队列已经满了
        printf(" %s \n", "queue is full");
        return false;
    }
    //插入新元素,在tail处插入,让tail++
    q.data[q.tail] = data;
    //此时让tail等于 ( tail + 1 ) % MAXSIZE,maxsize是10,tail如果现在指向9,那么下标9添加上元素以后tail+1 % 10 = 0,那么此时tail又回到了下标0处的位置,这样就实现了循环队列
    q.tail = (q.tail + 1) % MAXSIZE;
    q.size++;
    return true;
}

//出队操作
bool deQueue(Queue &q, Tree &element) {
    if (isEmpty(q)) {
        printf(" %s \n", "queue is empty");
        return false;
    }
    //将删除的元素返回
    element = q.data[q.head];
    //让head = (head + 1) % MAXSIZE 整个存取都是一个转圈循环的过程,因为队列不能随机插入,只能从队尾删除 对头插入,所以很好想
    q.head = (q.head + 1) % MAXSIZE;
    q.size--;
}

//获取队列的第一个元素
bool getTopData(Queue queue, Tree &data) {
    //返回队列的第一个元素
    if (isEmpty(queue)) {
        printf(" %s \n", "queue is empty");
        return false;
    }
    data = queue.data[queue.head];
}


Tree addData(Tree &root, int num) {
    if (root == NULL) {
        root = (Tree) malloc(sizeof(Tree));
        root->data = num;
        root->left = NULL;
        root->right = NULL;
        return root;
    }
//     printf(" %d \n", 333 );
    //比较要添加的数和当前节点的大小
    int currentData = root->data;
    if (currentData > num) {
        //往左子节点添加
        root->left = addData(root->left, num);
    } else {
        root->right = addData(root->right, num);
    }
    return root;
}
void addNode(Tree &tree, int num) {
    if (tree == NULL) {
//         printf(" %d \n", 111 );
        tree = (Tree) malloc(sizeof(Tree));
        tree->data = num;
        tree->left = NULL;
        tree->right = NULL;
    } else {
//        printf(" %d \n", 222 );
        tree = addData(tree, num);
    }
}


void cengXu(Tree &tree) {
    //层序遍历需要一个辅助队列
    Queue queue;
    initQueue(queue);
    //先将树根节点加入队列
    enQueue(queue, tree);
    //依次从队列中弹出元素
    while (queue.size > 0) {
        Tree item;
        deQueue(queue, item);
        printf(" %d \n", item->data);
        //如果当前节点有左子节点和右边节点
        if(item->left != NULL){
            enQueue(queue,item->left);
        }
        if(item->right !=NULL){
            enQueue(queue,item->right);
        }
    }


}

int main() {

    //定义一个二叉树
    Tree tree;
    tree = NULL;
    //添加元素
    addNode(tree, 20);
    addNode(tree, 30);
    addNode(tree, 10);
    addNode(tree, 110);
    addNode(tree, 8);
    addNode(tree, 4);
    addNode(tree, 2);
    addNode(tree, 1);
    addNode(tree, 23);

    //使用层序遍历树的所有节点
    cengXu(tree);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值