二叉树4种遍历代码实现

二叉树4种遍历代码实现

  1. 前序遍历
  2. 中序遍历
  3. 后序遍历
  4. 层次遍历

前3种遍历空间复杂度是O(h),h:二叉树深度
层次遍历空间复杂度是O(1)+队列

//  二叉树的代码实现

#include <stdio.h>

#include <iostream>


using namespace std;
#define MaxSize 50
typedef struct TNode{
    int data;
    struct TNode *lchild, *rchild;
}TNode, *Tree;

// 蹭序遍历需要用到辅助队列
// 链式队列结点
// 1个队列结点结构体只有两个数据元素,1-二叉树元素指针;2-下一个队列元素指针
typedef struct LinkNode{
    TNode * data;
    struct LinkNode *next;
} LinkNode;
// 整个队列我们需要2个指针,队头指针,队尾指针
typedef struct LinkQueue{
    LinkNode *front, *rear;
} LinkQueue;


// 先序遍历
void pre_order(Tree &tree){
    if (tree!=NULL) {
        cout << "element:" << tree->data << endl;
        pre_order(tree->lchild);
        pre_order(tree->rchild);
    }
    
}

// 中序遍历
void in_order(Tree &tree){
    if (tree!=NULL) {
        in_order(tree->lchild);
        cout << "element:" << tree->data << endl;
        in_order(tree->rchild);
    }
    
}

// 后序遍历
void post_order(Tree &tree){
    if (tree!=NULL) {
        post_order(tree->lchild);
   
        post_order(tree->rchild);
        
        cout << "element:" << tree->data << endl;
    }
    
}



// 带头结点初始化
void init_data(LinkQueue &Q){
    Q.front = Q.rear = (LinkNode *)malloc(sizeof(LinkNode));
    Q.front->next = NULL;
}

// 入队
bool set_data(LinkQueue &Q, TNode *value){
    LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
    // 内存不够,分配失败
    if (p == NULL) {
        return false;
    }
    // 构建好p结点数据
    p->data = value;
    p->next = NULL;
    
    // 入队
    Q.rear->next = p;
    
    // 队尾指针永远指向最后一个元素
    Q.rear = p;
    return true;
}

// value 是一个指针,一个TNode类型的指针,另外需要修改指针本身的值,而不是修改指针指向的地址的值,所以要加引用
bool erase_data(LinkQueue &Q, TNode *&value){
    if (Q.front == Q.rear) {
        return false;
    }
    LinkNode *p = Q.front->next;
    value = p->data;
    Q.front->next = p->next;
    
    // 如果是最后一个数据元素,修改尾指针
    if (p == Q.rear) {
        Q.rear = Q.front;
    }
    free(p);
    return true;
}

void print_data(LinkQueue Q){
    while (Q.front!=NULL) {
        cout << "element:" << Q.front << " " << Q.front->data << endl;
        Q.front = Q.front->next;
    }
}
// 层序遍历

// 算法思想
// 1. 要用到辅助队列
// 2. 根结点入队
// 3. 若队列非空,出队,并访问该结点左右结点入队
// 4. 重复上面操作直到队列为空
void level_order(Tree tree){
    LinkQueue Q;
    
    init_data(Q);
    set_data(Q, tree);
    
    TNode *value = NULL;
    
    while (Q.front->next!=NULL) {
        erase_data(Q, value);
        cout << "address::" << value << endl;
    
        cout << "element::" << value->data << endl;
        if (value->lchild!=NULL) {
            set_data(Q, value->lchild);
        }
        if (value->rchild!=NULL) {
            set_data(Q, value->rchild);
        }
  
        
     
        value = NULL;
        
    }
}


// 仅仅访问二叉树,可以不用引用
int tree_depth(Tree tree){
    if (tree==NULL) {
        return 0;
    }else{
        int depth1 = tree_depth(tree->lchild);
        int depth2 = tree_depth(tree->rchild);
        
        int depth = depth1 > depth2 ? depth1+1 : depth2+1;
        return depth;
    }
}
int main(){
    std::cout << "welcome, to my world!" << std::endl;
    
    Tree tree = NULL;//空指针
    
    cout << "size of:" << sizeof(tree) << endl;
    tree = (TNode *)malloc(sizeof(TNode));//根结点
    tree->data = 999;
    tree->lchild = NULL;
    tree->rchild = NULL;
   
    TNode *p = (TNode *)malloc(sizeof(TNode));//新增结点
    p->lchild = NULL;
    p->rchild = NULL;
    p->data = 2;
    // 如果父亲结点没有左孩子 插入左孩子
    tree->lchild = p;
    
    TNode *q = (TNode *)malloc(sizeof(TNode));//新增结点
    q->lchild = NULL;
    q->rchild = NULL;
    q->data = 3;
    tree->rchild = q;
    
    level_order(tree);
    int depth = tree_depth(tree);
    cout << "depth:" << depth << endl;
    return 0;
}

输出

welcome, to my world!
size of:8
address::0x106064520
element::999
address::0x106064540
element::2
address::0x106064560
element::3
depth:2
Program ended with exit code: 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值