求一个集合的全部子集(C语言)

文章目录

  • 1.通过位运算构造哈夫曼编码
  • 2.通过满二叉树构造哈夫曼编码

在这里插入图片描述

输入样例:
3
1 2 3
4
1 2 3 4 
0
输出样例:
[] [3] [2] [2,3] [1] [1,3] [1,2] [1,2,3]
[] [4] [3] [3,4] [2] [2,4] [2,3] [2,3,4] [1] [1,4] [1,3] [1,3,4] [1,2] [1,2,4] [1,2,3] [1,2,3,4]

1.通过位运算构造哈夫曼编码

标志值初始为0x0,每次累加1,得到相应的哈夫曼编码。

#include 
#include 

#define MAX_SIZE 100

void GetSubSet(int *num, int len, int tag);

int main() {
    int len;
    int num[MAX_SIZE];

    while (scanf("%d", &len) && len != 0) {
        for (int i = 0; i < len; i++) {
            scanf("%d", num + i);
        }

        GetSubSet(num, len, 0);
        printf("\n");
    }

    return 0;
}

void GetSubSet(int *num, int len, int tag) {
    int tmp;

    for (int i = 0; i < pow(2, len); i++) {
        tmp = tag;
        tag += 1;

        int is_first = 1;
        printf("[");
        for (int j = 0; j < len; j++) {
            if (tmp & 0x1) {
                if (is_first) {
                    printf("%d", num[j]);
                    is_first--;
                } else {
                    printf(",%d", num[j]);
                }
            }
            tmp >>= 1;
        }
        printf("] ");
    }
}

2.通过满二叉树构造哈夫曼编码

通过层次遍历构建相应高度的满二叉树,再进行先序遍历,每次遍历到叶子结点根据叶子结点的哈夫曼编码进行输出。

#include 
#include 
#include 
#include 

#define MAX_SIZE 100
#define ElemType BiTree

typedef struct BiTNode {
    int data;
    struct BiTNode *left, *right;
} BiTNode, *BiTree;

typedef struct SqQueue {
    ElemType data[MAX_SIZE];
    int front, rear;
} SqQueue;

/*BiTree Part*/
void BiTreeCreat(BiTree *T, int height);

void PreOrderTraverse(BiTree T, int *tag, int level, int *num, int huffman_code, int len);

void BiTreeDestroy(BiTree T);

/*SqQueue Part*/
void QueueInit(SqQueue *Q);

bool IsEmpty(SqQueue Q);

bool EnQueue(SqQueue *Q, ElemType e);

bool DeQueue(SqQueue *Q, ElemType *e);

int main() {
    int len;
    int num[MAX_SIZE];
    int tag[MAX_SIZE] = {0};

    while (scanf("%d", &len) && len != 0) {
        for (int i = 0; i < len; i++) {
            scanf("%d", num + i);
        }

        BiTree T = NULL;
        BiTreeCreat(&T, len + 1);
        PreOrderTraverse(T, tag, -1, num, -1, len);
        BiTreeDestroy(T);
    }
    return 0;
}

void BiTreeCreat(BiTree *T, int height) {
    SqQueue Q;
    BiTree now_node = NULL;

    QueueInit(&Q);
    *T = (BiTree) malloc(sizeof(BiTNode));
    /*(*T)->data = 1;*/
    (*T)->left = (*T)->right = NULL;

    EnQueue(&Q, *T);
    for (int i =  1; i <= pow(2, height - 1) - 1; i++) {
        DeQueue(&Q, &now_node);

        now_node->left = (BiTree) malloc(sizeof(BiTNode));
        /*now_node->left->data = 2 * i;*/
        now_node->left->left= now_node->left->right= NULL;
        EnQueue(&Q, now_node->left);

        now_node->right = (BiTree) malloc(sizeof(BiTNode));
        /*now_node->right->data = 2 * i + 1;*/
        now_node->right->left= now_node->right->right= NULL;
        EnQueue(&Q, now_node->right);
    }
}

void PreOrderTraverse(BiTree T, int *tag, int level, int *num, int huffman_code, int len) {
    if (T) {
        if (level != -1) tag[level] = huffman_code;
        if (T->left == NULL && T->right == NULL) {
            int is_first_num = 1;
            printf("[");
            for (int i = 0; i < len; i++) {
                if (tag[i] == 1) {
                    if (is_first_num == 1) {
                        printf("%d", num[i]);
                        is_first_num--;
                    } else {
                        printf(",%d", num[i]);
                    }
                }
            }
            printf("] ");
        } else {
            PreOrderTraverse(T->left, tag, level + 1, num, 0, len);
            PreOrderTraverse(T->right, tag, level + 1, num, 1, len);
        }
    }
}

void BiTreeDestroy(BiTree T) {
    if (T) {
        BiTreeDestroy(T->left);
        BiTreeDestroy(T->right);
        free(T);
    }
}

void QueueInit(SqQueue *Q) {
    Q->front = Q->rear = 0;
}

bool IsEmpty(SqQueue Q) {
    return (Q.rear == Q.front) ? true : false;
}

bool EnQueue(SqQueue *Q, ElemType e) {
    if ((Q->rear + 1) % MAX_SIZE == Q->front) return false;

    Q->data[Q->rear] = e;
    Q->rear = (Q->rear + 1) % MAX_SIZE;
}

bool DeQueue(SqQueue *Q, ElemType *e) {
    if (IsEmpty(*Q)) return false;

    *e = Q->data[Q->front];
    Q->front = (Q->front + 1) % MAX_SIZE;
}

你可能感兴趣的:(数据结构与算法,c语言,c++,数据结构,二叉树,队列)