편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2020.05.21

    이진트리 레벨 순회 실행 안됨 오류


    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 10
    
    typedef struct node {
        char data;
        struct node* left;
        struct node* right;
    }Node;
    
    Node* arr[MAX];
    int top;
    
    Node* pop() {
        Node* node = NULL;
        if (top == -1) {
            return node;
        }
        node = arr[top];
        top--;
        return node;
    }
    
    void push(Node* p) {
        // top을 1증가 시키고, top이 가리키는 칸에 data 저장
        if (top == MAX - 1) {
            return;
        }
        arr[++top] = p;
    }
    
    
    //전위
    void preorder(Node* node) {
        Node* tmp;
        top = -1;
        tmp = node;
        while (1) {
            while (tmp != NULL) {
                printf("%c ", tmp->data);
                push(tmp);
                tmp = tmp->left;
            }
    
            tmp = pop();
            if (tmp == NULL) break;
            tmp = tmp->right;
    
        }
    }
    
    //중위
    void inorder(Node* node) {
        Node* tmp;
        top = -1;
        tmp = node;
        while (1) {
            while (tmp != NULL) { // 우선순위
                push(tmp);
                tmp = tmp->left;
            }
    
            tmp = pop();
            if (tmp == NULL) break;
            printf("%c ", tmp->data);
            tmp = tmp->right;
        }
    }
    
    //후위
    void postorder(Node* node) {
        Node* tmp;
        top = -1;
        tmp = node;
        while (1) {
            do {
                if (tmp->data != NULL && tmp != NULL) {
                    push(tmp);
                    tmp = tmp->left;
                }
                else break;
    
            } while (tmp != NULL);
            tmp = pop();
            if (!tmp) break;
            if (tmp->right != NULL) {
                if (tmp->right->data == NULL) {
                    printf("%c ", tmp->data);
                    tmp->data = NULL;
                }
                else {
                    push(tmp);
                    tmp = tmp->right;
                }
            }
            else {
                printf("%c ", tmp->data);
                tmp->data = NULL;
            }
        }
    }
    
    Node* queue[MAX];
    int front = -1;
    int rear = -1;
    
    void enqueue(Node* node) {
        if (rear == MAX - 1) {
            return;
        }
        if (front == -1) {
            front++;
        }
        rear++;
        queue[rear] = node;
    }
    
    Node* dequeue() {
        Node* node = NULL;
    
        if (front == rear) {
            if (front != -1) {
                node = queue[front];
                front = rear = -1;
            }
            return node;
        }
        node = queue[front];
        front++;
        return node;
    }
    //레벨
    void levelOrder(Node* root) { 
        Node* tmp;
        enqueue(root);
    
        while (1) {
            tmp = dequeue();
            if (tmp == NULL) {
                break;
            }
            printf("%c ", tmp->data);
            if (tmp->left) {
                enqueue(tmp->left);
            }
            if (tmp->right) {
                enqueue(tmp->right);
            }
        }
    }
    
    //이진 트리 생성
    Node* makeBinaryTree(char data, Node* left, Node* right) {
        Node* tmp = (Node*)malloc(sizeof(Node));
        tmp->data = data;
        tmp->left = left;
        tmp->right = right;
    
        return tmp;
    }
    
    void main() {
        Node* f11 = makeBinaryTree('K', NULL, NULL);
        Node* f10 = makeBinaryTree('J', NULL, NULL);
        Node* f9 = makeBinaryTree('I', NULL, NULL);
        Node* f8 = makeBinaryTree('H', NULL, NULL);
        Node* f7 = makeBinaryTree('G', NULL, f11);
        Node* f6 = makeBinaryTree('F', NULL, NULL);
        Node* f5 = makeBinaryTree('E', f9, f10);
        Node* f4 = makeBinaryTree('D', f8, NULL);
        Node* f3 = makeBinaryTree('C', f6, f7);
        Node* f2 = makeBinaryTree('B', f4, f5);
        Node* f1 = makeBinaryTree('A', f2, f3);
    
        printf(" preorder : ");
        preorder(f1);
    
        printf("\n\n inorder : ");
        inorder(f1);
    
        printf("\n\n postorder : ");
        postorder(f1);
    
        printf("\n\n levelorder : ");
        levelOrder(f1);
    
    
    }
    

    위는 제가 작성한 이진트리의 네 가지 순회(전위, 중위, 후위, 레벨 순회)의 코드입니다. 이 중에서 레벨 순회만 공백으로 실행이 안되는데 이유가 무엇인지 정말 궁금하네요. 제가 생각하기론 enqueue와 dequeue함수에서의 문제 같습니다.

  • 프로필 알 수 없는 사용자님의 편집
    날짜2020.05.20

    이진트리 레벨 순회 실행 안됨 오류


    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 10
    
    typedef struct node {
        char data;
        struct node* left;
        struct node* right;
    }Node;
    
    Node* arr[MAX];
    int top;
    
    Node* pop() {
        Node* node = NULL;
        if (top == -1) {
            return node;
        }
        node = arr[top];
        top--;
        return node;
    }
    
    void push(Node* p) {
        // top을 1증가 시키고, top이 가리키는 칸에 data 저장
        if (top == MAX - 1) {
            return;
        }
        arr[++top] = p;
    }
    
    
    //전위
    void preorder(Node* node) {
        Node* tmp;
        top = -1;
        tmp = node;
        while (1) {
            while (tmp != NULL) {
                printf("%c ", tmp->data);
                push(tmp);
                tmp = tmp->left;
            }
    
            tmp = pop();
            if (tmp == NULL) break;
            tmp = tmp->right;
    
        }
    }
    
    //중위
    void inorder(Node* node) {
        Node* tmp;
        top = -1;
        tmp = node;
        while (1) {
            while (tmp != NULL) { // 우선순위
                push(tmp);
                tmp = tmp->left;
            }
    
            tmp = pop();
            if (tmp == NULL) break;
            printf("%c ", tmp->data);
            tmp = tmp->right;
        }
    }
    
    //후위
    void postorder(Node* node) {
        Node* tmp;
        top = -1;
        tmp = node;
        while (1) {
            do {
                if (tmp->data != NULL && tmp != NULL) {
                    push(tmp);
                    tmp = tmp->left;
                }
                else break;
    
            } while (tmp != NULL);
            tmp = pop();
            if (!tmp) break;
            if (tmp->right != NULL) {
                if (tmp->right->data == NULL) {
                    printf("%c ", tmp->data);
                    tmp->data = NULL;
                }
                else {
                    push(tmp);
                    tmp = tmp->right;
                }
            }
            else {
                printf("%c ", tmp->data);
                tmp->data = NULL;
            }
        }
    }
    
    Node* queue[MAX];
    int front = -1;
    int rear = -1;
    
    void enqueue(Node* node) {
        if (rear == MAX - 1) {
            return;
        }
        if (front == -1) {
            front++;
        }
        rear++;
        queue[rear] = node;
    }
    
    Node* dequeue() {
        Node* node = NULL;
    
        if (front == rear) {
            if (front != -1) {
                node = queue[front];
                front = rear = -1;
            }
            return node;
        }
        node = queue[front];
        front++;
        return node;
    }
    //레벨
    void levelOrder(Node* root) { 
        Node* tmp;
        enqueue(root);
    
        while (1) {
            tmp = dequeue();
            if (tmp == NULL) {
                break;
            }
            printf("%c ", tmp->data);
            if (tmp->left) {
                enqueue(tmp->left);
            }
            if (tmp->right) {
                enqueue(tmp->right);
            }
        }
    }
    
    //이진 트리 생성
    Node* makeBinaryTree(char data, Node* left, Node* right) {
        Node* tmp = (Node*)malloc(sizeof(Node));
        tmp->data = data;
        tmp->left = left;
        tmp->right = right;
    
        return tmp;
    }
    
    void main() {
        Node* f11 = makeBinaryTree('K', NULL, NULL);
        Node* f10 = makeBinaryTree('J', NULL, NULL);
        Node* f9 = makeBinaryTree('I', NULL, NULL);
        Node* f8 = makeBinaryTree('H', NULL, NULL);
        Node* f7 = makeBinaryTree('G', NULL, f11);
        Node* f6 = makeBinaryTree('F', NULL, NULL);
        Node* f5 = makeBinaryTree('E', f9, f10);
        Node* f4 = makeBinaryTree('D', f8, NULL);
        Node* f3 = makeBinaryTree('C', f6, f7);
        Node* f2 = makeBinaryTree('B', f4, f5);
        Node* f1 = makeBinaryTree('A', f2, f3);
    
        printf(" preorder : ");
        preorder(f1);
    
        printf("\n\n inorder : ");
        inorder(f1);
    
        printf("\n\n postorder : ");
        postorder(f1);
    
        printf("\n\n levelorder : ");
        levelOrder(f1);
    
    
    }
    

    위는 제가 작성한 이진트리의 네 가지 순회(전위, 중위, 후위, 레벨 순회)의 코드입니다. 이 중에서 레벨 순회만 공백으로 실행이 안되는데 이유가 무엇인지 정말 궁금하네요 ㅠㅠ 제가 생각하기론 enqueue와 dequeue함수에서의 문제 같은데