zoukankan      html  css  js  c++  java
  • 二叉树的创建、(中先后序,广义表)遍历

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

    typedef struct Node {
            int data;
            struct Node *lchild, *rchild;
    } Node;

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

    Node *getNewNode(int val) {
            Node *p = (Node * )malloc(sizeof(Node));
            p->data = val;
            p->lchild = p->rchild = NULL;
            return p;
    }

    Tree *getNewTree() {
            Tree *tree = (Tree *)malloc(sizeof(Tree));
            tree->root = NULL;
            tree->n = 0;
            return tree;
    }

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

    void clear(Tree *tree) {
            if (tree == NULL) return;
            clearNode(tree->root);
            free(tree);
            return ;
    }

    Node *insert_node(Node *root, int val, int *flag) {
            if (root == NULL) {
                    *flag = 1;
                    return getNewNode(val);
            }
            if (root->data == val) return root;
            if (val < root->data) root->lchild = insert_node(root->lchild, val, flag);
            else root->rchild = insert_node(root->rchild, val, flag);
            return root;
    }

    void insert(Tree *tree, int val) {
            int flag = 0;
            tree->root = insert_node(tree->root, val, &flag);
            tree->n += flag;
            return ;
    }

    void pre_order_node (Node *node) {
            if (node == NULL) return ;
            printf("%d ", node->data);
            pre_order_node(node->lchild);
            pre_order_node(node->rchild);
            return ;
    }

    void pre_order(Tree *tree) {
            if (tree == NULL) return ;
            printf("pre_order: ");
            pre_order_node(tree->root);
            printf(" ");
            return ;
    }

    void in_order_node (Node *node) {
            if (node == NULL) return ;
            in_order_node(node->lchild);
            printf("%d ", node->data);
            in_order_node(node->rchild);
            return ;
    }

    void in_order(Tree *tree) {
            if (tree == NULL) return ;
            printf("in_order: ");
            in_order_node(tree->root);
            printf(" ");
            return ;
    }

    void post_order_node (Node *node) {
            if (node == NULL) return ;
            post_order_node(node->lchild);
            post_order_node(node->rchild);
            printf("%d ", node->data);
            return ;
    }

    void post_order(Tree *tree) {
            if (tree == NULL) return ;
            printf("post_order: ");
            post_order_node(tree->root);
            printf(" ");
            return ;
    }

    void output_node(Node *root) {
            if (root == NULL) return;
            printf("%d", root->data);
            if (root->lchild == NULL && root->rchild == NULL) return ;
            printf("(");
            output_node(root->lchild);
            printf(",");
            output_node(root->rchild);
            printf(")");
            return ;
    }

    void output(Tree *tree) {
            if (tree == NULL) return ;
            printf("tree(%d) :", tree->n);
            output_node(tree->root);
            printf(" ");
            return ;
    }

    int main () {
            srand(time(0));
            Tree *tree = getNewTree();
    #define max_op 100
            for (int i = 0; i< max_op; i++) {
                    int val = rand() % 10;
                    insert(tree, val);
            }
            pre_order(tree);
            in_order(tree);
            post_order(tree);
            output(tree);
    #undef max_op
            clear(tree);
            return 0;
    }

  • 相关阅读:
    web网络编程
    C++ 多线程*****(看书补充)
    C++信号处理
    预指令
    C++模板*******
    C++ 命名空间
    动态存储
    异常处理**********
    私钥、秘钥详解
    Pod控制器应用进阶
  • 原文地址:https://www.cnblogs.com/hhhahh/p/14008917.html
Copyright © 2011-2022 走看看