본문 바로가기
NOTE/Algorithm

자료구조) Binary_Tree

by DevAthena 2015. 8. 25.
#include <stdio.h>
#include <stdlib.h>
 
typedef struct Node
{
    int data;
    Node *left;
    Node *right;
}Node;
 
Node* createNode(int data)
{
    Node* newNode = (Node *)malloc(sizeof(Node));
    newNode->left = NULL;
    newNode->right = NULL;
    newNode->data = data;
 
    return newNode;
}
 
Node* searchNode(Node* Tree, int findData)
{
    if (Tree == NULL)
        return NULL;
    if (Tree->data == findData)
        return Tree;
    else if (Tree->data > findData)
        searchNode(Tree->left, findData);
    else
        searchNode(Tree->right, findData);
}
 
void insertNode(Node* Tree, Node* newNode)
{
    if (newNode->data < Tree->data)
    {
        if (Tree->left != NULL)
            insertNode(Tree->left, newNode);
        else
            Tree->left = newNode;
    }
    else if (newNode->data > Tree->data)
    {
        if (Tree->right != NULL)
            insertNode(Tree->right, newNode);
        else
            Tree->right = newNode;
    }
}
 
Node* findMinNode(Node* Tree)
{
    if (Tree == NULL)
        return NULL;
    if (Tree->left != NULL)
        return findMinNode(Tree->left);
    else
        return Tree;
}
 
Node* removeNode(Node* Tree, int data)
{
    Node *rNode;
 
    if (Tree == NULL)
        printf("해당하는 노드를 찾을 수 없습니다.\n");
    else if (Tree->data > data)
        Tree->left = removeNode(Tree->left, data);
    else if (Tree->data < data)
        Tree->right = removeNode(Tree->right, data);
    else
    {
        if (Tree->left != NULL && Tree->right != NULL)
        {
            rNode = findMinNode(Tree->right);
            Tree->data = rNode->data;
            Tree->right = removeNode(Tree->right, rNode->data);
        }
        else
        {
            rNode = Tree;
            if (Tree->left == NULL)
                Tree = Tree->right;
            else if (Tree->right == NULL)
                Tree = Tree->left;
            free(rNode);
        }
    }
    return Tree;
}
 
void printTree(Node* Tree)
{
    if (Tree == NULL) return;
    printTree(Tree->left);
    printf("%d ", Tree->data);
    printTree(Tree->right);
}
 
int main()
{
    Node* Tree = createNode(10);
    Node* findNode;
    int input;
 
    insertNode(Tree, createNode(5));
    insertNode(Tree, createNode(1));
    insertNode(Tree, createNode(6));
    insertNode(Tree, createNode(17));
    insertNode(Tree, createNode(14));
    insertNode(Tree, createNode(21));
 
    while (1)
    {
        scanf("%d", &input);
        findNode = searchNode(Tree, input);
 
        if (findNode != NULL)
        {
            printf("해당 노드를 찾았습니다! 노드를 제거합니다. 노드의 위치는 %#x입니다.\n", findNode);
            removeNode(Tree, input);
            printf("현재 트리 출력: ");
            printTree(Tree);
            printf("\n");
        }
        else printf("노드를 찾을 수 없었습니다.\n");
    }
 
    return 0;
}
cs


'NOTE > Algorithm' 카테고리의 다른 글

자료구조) 팩토리얼  (0) 2015.08.25
자료구조) 피보나치 수열  (0) 2015.08.25
자료구조) Tree_Traverse  (0) 2015.08.25
자료구조) Graph  (0) 2015.08.25
자료구조) QuickSort  (0) 2015.08.25