二叉树的基本操作

简介: c++,先序遍历,中序遍历,后序遍历
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;

const int N=100;
typedef struct edge* node;

struct edge
{
    char data;
    node l;
    node r;
};

//通过先序序列和中序序列构造二叉树 
node insert(char *a, char *b, int n)
{
    int i;
    int n1=0, n2=0;
    int m1=0, m2=0;
    char la[N], ra[N];
    char lb[N], rb[N];
    node BT=NULL;
    if(n==0)
        return NULL;
    BT=(node) malloc(sizeof(struct edge));
    if(!BT)
        return NULL;
    memset(BT,0,sizeof(edge));
    BT->data=a[0];
    for(i=0;i<n;i++)
    {
        if(i<=n1&&(b[i]!=a[0]))
            lb[n1++]=b[i];
        else if(b[i]!=a[0])
            rb[n2++]=b[i];
    }
    for(i=1;i<n;i++)
    {
        if(i<=n1)
            la[m1++]=a[i];
        else
            ra[m2++]=a[i];
    }
    BT->l=insert(la, lb, n1);
    BT->r=insert(ra, rb, n2);
    return BT;
}

//通过后序序列和中序序列构造二叉树 
node push(char *a, char *b, int n)
{
    int i;
    int n1=0, n2=0;
    int m1=0, m2=0;
    char la[N], ra[N];
    char lb[N], rb[N];
    node BT=NULL;
    if(n==0)
        return NULL;
    BT=(node) malloc(sizeof(struct edge));
    if(!BT)
        return NULL;
    memset(BT,0,sizeof(edge));
    BT->data=a[n-1];
    for(int i=0;i<n;i++)
    {
        if(i<=n1&&(b[i]!=a[0]))
            lb[n1++]=b[i];
        else if(b[i]!=a[0])
            rb[n2++]=b[i];
    }
    for(i=0;i<n-1;i++)
    {
        if(i<=n1)
            la[m1++]=a[i];
        else
            ra[m2++]=a[i];
    }
    BT->l=push(la, lb, n1);
    BT->r=push(ra, rb, n2);
    return BT;
}

//先序遍历 
void Preorder(node BT)
{
    if(BT==NULL)
        return;
    printf("%c ", BT->data);
    Preorder(BT->l);
    Preorder(BT->r);
}

//非递归的先序遍历 
//void 

//中序遍历
void Inorder(node BT)
{
    if(BT==NULL)
        return;
    Inorder(BT->l);
    printf("%c ", BT->data);
    Inorder(BT->r);
}

//非递归的中序遍历
//void

//后序遍历
void Postorder(node BT)
{
    if(BT==NULL)
        return;
    Postorder(BT->l);
    Postorder(BT->r);
    printf("%c ", BT->data);
}

//非递归的后序遍历 
//void 

//层序遍历
void  Traversal(node BT)
{
    queue<char> q;
    if(!BT) return;
    q.push(BT->data);
    while(q.size())
    {
        printf("%c ", q.front());
        q.pop();
        if(BT->l) q.push(BT->l->data);
        if(BT->r) q.push(BT->r->data);
    }
}

//计算二叉树树高 
int high(node BT)
{
    if(BT)
    {
        int hl=high(BT->l);
        int hr=high(BT->r);
        int maxh=(hl>hr)?hl:hr;
        return maxh+1;
    }
    else return 0;
}

//输出二叉树的叶子结点 
void yezi(node BT)
{
    if(BT)
    {
        if(!BT->l&&!BT->r) 
            printf("%c ", BT->data);
        yezi(BT->l);
        yezi(BT->r);
    } 
} 


int main()
{
    int n;
    char a[N], b[N];
    node BT;
    BT=NULL;
    scanf("%d", &n);
    getchar();
    for(int i=0;i<n;i++)
        scanf("%c", &a[i]);
    getchar();
    for(int i=0;i<n;i++)
        scanf("%c", &b[i]);
    BT=insert(a, b, n);
    int t=high(BT);
    printf("%d\n", t);
    return 0;
} 
相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
6月前
|
存储 算法
链式二叉树的基本操作实现(建议收藏!!!)(2)
链式二叉树的基本操作实现(建议收藏!!!)
75 0
|
7月前
|
存储 算法
二叉树基本操作实现 && 树和二叉树&& 二叉树进阶oj && 堆的基本概念 && 优先级队列的使用_
二叉树基本操作实现 && 树和二叉树&& 二叉树进阶oj && 堆的基本概念 && 优先级队列的使用_
54 0
|
2天前
|
存储
二叉树的基本概念以及基本操作
二叉树的基本概念以及基本操作
13 2
|
2天前
|
机器学习/深度学习
【二叉树 OJ题】二叉树基础知识 与 OJ题完成(二叉树构建与遍历问题,子树查找问题)
树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合。
18 1
|
6月前
链式二叉树的基本操作实现(建议收藏!!!)(3)
链式二叉树的基本操作实现(建议收藏!!!)
29 0
|
6月前
链式二叉树的基本操作实现(建议收藏!!!)(1)
链式二叉树的基本操作实现(建议收藏!!!)
33 0
构建二叉搜索树及基本操作
构建二叉搜索树及基本操作
《Java数据结构》二叉树的这些基本操作你真的理解了吗
《Java数据结构》二叉树的这些基本操作你真的理解了吗
【Java数据结构】实现二叉树及其基本操作
【Java数据结构】实现二叉树及其基本操作
【Java数据结构】实现二叉树及其基本操作
|
算法 前端开发
理解和默写 二叉树的基本操作
理解和默写 二叉树的基本操作
82 0