二叉树的广义表反序列化

前言

个人小记


一、代码

c 复制代码
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_NODE 10
#define MAX_LEN 100
#define key(n)(n)?(n->key):(-1)
typedef struct Node
{
    int key;
    struct Node* lchild,*rchild;
}Node;

Node* init_node(int key)
{
    Node* node=(Node*)malloc(sizeof(Node));
    node->key=key;
    node->lchild=node->rchild=NULL;
    return node;
}

Node* insert(Node* root,int key)
{
    if(root==NULL)return init_node(key);
    if(rand()%2)root->lchild=insert(root->lchild,key);
    else root->rchild=insert(root->rchild,key);
    return root;
}

Node* getnewtree(Node* root,int n)
{
    for(int i=0;i<n;i++)
    {
        root=insert(root,rand()%100);
    }
    return root;
}

void clear(Node* root)
{
    if(root==NULL)return ;
    clear(root->lchild);
    clear(root->rchild);
    free(root);
    return ;
}

char buff[MAX_LEN];
int len;
void __serial(Node* root)
{
    if(root==NULL)return ;
    len+=snprintf(buff+len,100,"%d",root->key);
    if(root->lchild==NULL&&root->rchild==NULL)return ;
    len+=snprintf(buff+len,100,"(");
    __serial(root->lchild);
    if(root->rchild)
    {
        len+=snprintf(buff+len,100,",");
        __serial(root->rchild);
    }
    len+=snprintf(buff+len,100,")");
    return ;
}

void serial(Node* root)
{
    memset(buff,0,sizeof(buff));
    len=0;
    __serial(root);
    return ;
}

void output(Node* root)
{
    if(root==NULL)return ;
    printf("%d(%d,%d)\n",key(root),key(root->lchild),key(root->rchild));
    output(root->lchild);
    output(root->rchild);
}

Node* diserial(char buff[],int n)
{
    Node* root=NULL;
    Node* p=NULL;
    int top=-1,state=0,fag=0;
    Node** stack=(Node**)malloc(sizeof(Node*)*MAX_NODE);
    for(int i=0;i<n;i++)
    {
        switch(state)
        {
            case 0:
            {
                if(buff[i]<='9'&&buff[i]>='0')state=1;
                if(buff[i]=='(')state=2;
                if(buff[i]==',')state=3;
                if(buff[i]==')')state=4;
                i--;
            }break;
            case 1:
            {
                int key=0;
                while(buff[i]<='9'&&buff[i]>='0')
                {
                    key=key*10+(buff[i]-'0');
                    i++;
                }
                p=init_node(key);
                if(fag==0&&top>=0)stack[top]->lchild=p;
                if(fag==1&&top>=0)stack[top]->rchild=p;
                i--;
                state=0;
            }break;
            case 2:
            {
                stack[++top]=p;
                fag=0;
                state=0;
            }break;
            case 3:
            {
                fag=1;
                state=0;
            }break;
            case 4:
            {
                root=stack[top--];
                state=0;
            }break;
        }
    }
    free(stack);
return root;
}

int main()
{
    srand((unsigned)time(0));
    Node* root=NULL;
    root=getnewtree(root,MAX_NODE);
    serial(root);
    output(root);
    printf("广义表为:\n");
    printf("%s\n",buff);
    Node* new_root=diserial(buff,len);
    printf("反序列化结果为:"\n);
    output(new_root);
    clear(root);
    clear(new_root);
    return 0;
}

二、结果

c 复制代码
10(35,0)
35(41,2)
41(-1,25)
25(-1,-1)
2(-1,-1)
0(94,79)
94(65,-1)
65(-1,-1)
79(44,-1)
44(-1,-1)
广义表为:
10(35(41(,25),2),0(94(65),79(44)))
反序列化结果为:
10(35,0)
35(41,2)
41(-1,25)
25(-1,-1)
2(-1,-1)
0(94,79)
94(65,-1)
65(-1,-1)
79(44,-1)
44(-1,-1)
相关推荐
剑锋所指,所向披靡!11 分钟前
数据结构的基本概念
数据结构
王哈哈^_^32 分钟前
【数据集】【YOLO】【目标检测】口罩数据集,口罩佩戴识别数据集 1971 张,YOLO佩戴口罩检测算法实战训练教程。
人工智能·算法·yolo·目标检测·计算机视觉·ai·视觉检测
dragoooon341 小时前
[优选算法专题四.前缀和——NO.31~32 连续数组、矩阵区域和]
数据结构·算法·leetcode·1024程序员节
py有趣1 小时前
LeetCode算法学习之移除元素
java·数据结构·算法
captain3762 小时前
数据结构易错点
数据结构·数据结构易错点
一念&2 小时前
每日一个C语言知识:C 预处理器
c语言·算法
油泼辣子多加2 小时前
【实战】自然语言处理--长文本分类(2)BERTSplitLSTM算法
算法·自然语言处理·分类
WWZZ20252 小时前
快速上手大模型:深度学习2(实践:深度学习基础、线性回归)
人工智能·深度学习·算法·计算机视觉·机器人·大模型·slam
初级炼丹师(爱说实话版)3 小时前
算法面经常考题整理(1)机器学习
人工智能·算法·机器学习
我不会插花弄玉3 小时前
c语言实现队列【由浅入深-数据结构】
c语言·数据结构