C语言实现树 前中后序遍历 实现广义表生成树

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node { //树的节点
    int val;
    struct Node *left, *right;
} Node;

typedef struct Tree {
    Node *root;
    int len;
} Tree;

Node *initNode(int val) {
    Node *n = (Node *)malloc(sizeof(Node));
    n->val = val;
    n->left = NULL;
    n->right = NULL;
    return n;
}

void freeNode(Node *p) {
    if (!p) return ;
    free(p);
    return ;
}

Tree *initTree() {
    Tree *t = (Tree *)malloc(sizeof(Tree));
    t->root = NULL;
    t->len = 0;
    return t;
}

Node *insertR(Node *root, int val) {
    if (!root) {
        Node *n = initNode(val);
        return n;
    }
    if (val > root->val)
        root->right = insertR(root->right, val);
    else
        root->left = insertR(root->left, val);
    return root;
}

void insertTreeR(Tree *t, int val) {
    if (!t) return ;
    t->root = insertR(t->root, val);
    ++t->len;
    return ;
}

void insert(Node **raddr, int val) {
    if (!(*raddr)) {
        *raddr = initNode(val);
        return ;
    }
    if (val > (*raddr)->val)
        insert(&((*raddr)->right), val);
    else
        insert(&((*raddr)->left), val);
    return ;
}

void insertTree(Tree *t, int val) {
    if (!t) return ;
    insert(&(t->root), val);
    ++t->len;
    return ;
}

///前序遍历
void preorderTrav(Node *root) {
    if (!root) return ;
    printf("%d ", root->val);
    preorderTrav(root->left);
    preorderTrav(root->right);
    return ;
}

void preorderTree(Tree *t) {
    if (!t) return ;
    printf("Pre:[");
    preorderTrav(t->root);
    printf("]\n");
}

///输出广义表
void generalizedTable(Node *root) {
    if (!root) return ;
    printf("%d", root->val);
    if (!root->left && !root->right)
        return ;
    printf("(");
    generalizedTable(root->left);
    printf(",");
    generalizedTable(root->right);
    printf(")");
    return ;
}

void generalizedTableTree(Tree *t) {
    if (!t) return ;
    printf("Table:[");
    generalizedTable(t->root);
    printf("]\n");
}

///中序遍历
void inorderTrav(Node *root) {
    if (!root) return ;
    inorderTrav(root->left);
    printf("%d ", root->val);
    inorderTrav(root->right);
    return ;
}

void inorderTree(Tree *t) {
    if (!t) return ;
    printf("In:[");
    inorderTrav(t->root);
    printf("]\n");
}

///后序遍历
void postorderTrav(Node *root) {
    if (!root) return ;
    postorderTrav(root->left);
    postorderTrav(root->right);
    printf("%d ", root->val);
    return ;
}

void postorderTree(Tree *t) {
    if (!t) return ;
    printf("Post:[");
    postorderTrav(t->root);
    printf("]\n");
}

void freeAll(Node *root) {
    if (!root) return ;
    freeAll(root->left);
    freeAll(root->right);
    freeNode(root);
    return ;
}

void freeTree(Tree *t) {
    if (!t) return ;
    freeAll(t->root);
    free(t);
    return ;
}

Node *findNode(Node *root, int val) {
    if (!root) return NULL;
    if (val == root->val) return root;
    if (val > root->val) return findNode(root->right, val);
    else return findNode(root->left, val);
}

Node *findTree(Tree *t, int val) {
    if (!t) return NULL;
    return findNode(t->root, val);
}

///实现广义表生成树
typedef struct Stack { //栈
    Node **data;
    int size, top;
} Stack;

Stack *initStack(int n) {
    Stack *s = (Stack *)malloc(sizeof(Stack));
    s->data = (Node **)malloc(sizeof(Node *) * n);
    s->top = -1, s->size = n;
    return s;
}

void freeStack(Stack *s) {
    if (!s) return ;
    free(s->data);
    free(s);
    return ;
}

int push(Stack *s, Node *n) { //压栈
    if (!s || s->top == s->size - 1) return 0;
    s->data[++s->top] = n;
    return 1;
}

int isEmpty(Stack *s) {
    return !s || s->top == -1;
}

Node *pop(Stack *s) {
    return s->data[s->top--];
}

Node *builtGeneralizedTable(char *str) { //建立广义表树
    Stack *s = initStack(strlen(str) / 2);
    Node *root, *n;
    int num = 0, flag = 0; //0-左 1-右
    while (str[0]) {
        switch (str[0]) {
        case '(':
            push(s, n);
            flag = 0;
            break;
        case ',':
            flag = 1;
            break;
        case ')':
            root = pop(s);
            break;
        default:
            if (str[0] < '0' || str[0] > '9')
                break;
            num = 0;
            while (str[0] >= '0' && str[0] <= '9')
                num = num * 10 + str[0] - '0', ++str;
            --str;
            n = initNode(num);
            if (!isEmpty(s))
                flag ? (s->data[s->top]->right = n) : (s->data[s->top]->left = n);
        }
        ++str;
    }
    freeStack(s);
    return root;
}

int main() {
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值