Hello Ocean! ๐ŸŒผ

[์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋””] ์ž๋ฃŒ๊ตฌ์กฐ_2 ๋ณธ๋ฌธ

Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋””] ์ž๋ฃŒ๊ตฌ์กฐ_2

bba_dda 2020. 7. 28. 19:43
๋ฐ˜์‘ํ˜•

1.ํŠธ๋ฆฌ, ๊ทธ๋ž˜ํ”„

๋น„์„ ํ˜• ๊ตฌ์กฐ
์„ ํ˜•๊ตฌ์กฐ๋Š” ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊บผ๋‚ด๋Š” ๊ฒƒ์— ์ดˆ์ ์ด ๋งž์ถฐ์ ธ ์žˆ๊ณ , ๋น„์„ ํ˜•๊ตฌ์กฐ๋Š” ํ‘œํ˜„์— ์ดˆ์ ์ด ๋งž์ถฐ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŠธ๋ฆฌ

  • ํŠธ๋ฆฌ์˜ ๊ตฌ์„ฑ ์š”์†Œ

    • Root Node : ํŠธ๋ฆฌ ๊ตฌ์กฐ์—์„œ ์ตœ์ƒ์œ„์— ์กด์žฌํ•˜๋Š” ๋…ธ๋“œ (A)
    • Node : ํŠธ๋ฆฌ์˜ ๊ตฌ์„ฑ์š”์†Œ์— ํ•ด๋‹นํ•˜๋Š” ์š”์†Œ (A,B,C,D,E,F,G)
    • Edge : ๋…ธ๋“œ์™€ ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์„ 
    • Terminal Node(Leaf Node) : ํŠธ๋ฆฌ์˜ ๋งจ ๋ฐ‘์— ์žˆ๋Š” ์ž์‹์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ๋…ธ๋“œ
    • Sub-Tree : ์ „์ฒด ํŠธ๋ฆฌ์— ์†ํ•˜๋Š” ์ž‘์€ ํŠธ๋ฆฌ
    • Level : ํŠธ๋ฆฌ์—์„œ ๊ฐ ์ธต๋ณ„๋กœ ์ˆซ์ž๋ฅผ ๋งค๊น€ (root node = level 0)
    • Height : ํŠธ๋ฆฌ์˜ ์ตœ๊ณ  ๋ ˆ๋ฒจ
  • ํŠธ๋ฆฌ์˜ ์ข…๋ฅ˜

    • ์ด์ง„ํŠธ๋ฆฌ (binary tree)

      ์–ด๋–ค ํ•œ ๋…ธ๋“œ๊ฐ€ 2๊ฐœ ์ดํ•˜์˜ ์ž์‹๋งŒ์„ ๊ฐ€์ง€๋Š” ํŠธ๋ฆฌ

    • ํฌํ™”์ด์ง„ํŠธ๋ฆฌ (perfect binary tree)

      leaf๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ 2๊ฐœ์˜ ์ž์‹์„ ๊ฐ€์ง€๋Š” ํŠธ๋ฆฌ

      • ๋†’์ด๊ฐ€ d์ธ ํฌํ™”์ด์ง„ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์ˆ˜
        2^(d+1)-1
    • ์™„์ „์ด์ง„ํŠธ๋ฆฌ (complete binary tree)

      ํฌํ™”์ด์ง„ํŠธ๋ฆฌ์—์„œ leaf๋ฅผ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์ œ๊ฑฐํ•˜์—ฌ ์–ป๋Š” ํŠธ๋ฆฌ
      ์œ„์—์„œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๊ฝ‰ ์ฑ„์›Œ์ง„ ๋ชจ์–‘์ด๋‹ค

      • ๋†’์ด๊ฐ€ h์ธ ์™„์ „์ด์ง„ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์ˆ˜๋Š” 2^h ์ด์ƒ 2^(h+1)์ดํ•˜์ด๋‹ค.
  • ๊ตฌํ˜„

    • ๋…ธ๋“œ์˜ ๊ตฌ์กฐ

  • ์ฃผ๋กœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ํ˜น์€ ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ํ•„์š”ํ•œ ํ•จ์ˆ˜
    • Create: ์ƒˆ๋กœ์šด ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ธฐ
    • Destroy: ์‚ฌ์šฉ๋˜๋˜ ํŠธ๋ฆฌ๋ฅผ ํŒŒ๊ธฐํ•˜๊ธฐ
    • Insert: ํŠธ๋ฆฌ์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ธฐ
    • Delete: ํŠธ๋ฆฌ์˜ ํŠน์ • ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ
    • Search: ์ฃผ์–ด์ง„ ํ‚ค๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ธฐ
    • IsEmpty: ๋นˆ ํŠธ๋ฆฌ์ธ์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ
    • CopyTree: ์ฃผ์–ด์ง„ ํŠธ๋ฆฌ๋ฅผ ๋ณต์‚ฌํ•˜๊ธฐ
    • Traverse: ํŠธ๋ฆฌ ๋‚ด๋ถ€ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๊ธฐ

์ด์ง„ํƒ์ƒ‰ํŠธ๋ฆฌ์˜ ๊ตฌํ˜„

  • ์›์†Œ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋…ธ๋“œ ์œ„์น˜๋ฅผ ์ •์˜ํ•œ ์ด์ง„ํŠธ๋ฆฌ
#include <stdio.h>
#include <stdlib.h>
//๋ถ€๋ชจ๋…ธ๋“œ ์ธ๋ฑ์Šค i/2
//์™ผ์ชฝ์ž์‹ ์ธ๋ฑ์Šค 2i
//์˜ค๋ฅธ์ชฝ ์ž์‹ ์ธ๋ฑ์Šค 2i+1

typedef struct TreeNode { //๊ตฌ์กฐ์ฒด  
    int key;
    struct TreeNode *left,*right;
}TreeNode; 

//์ „์œ„ ์ˆœํšŒ
preorder(TreeNode *root) {
    if(root){
        printf("%d ",root->key);
        preorder(root->left);
        preorder(root->right);
    }

}
//์ค‘์œ„ ์ˆœํšŒ
inorder(TreeNode *root) {
    if(root){
        inorder(root->left);
        printf("%d ",root->key);
        inorder(root->right);
    }
} 
//ํ›„์œ„ ์ˆœํšŒ
postorder (TreeNode *root) {
    if(root){
        postorder(root->left);
        postorder(root->right);
        printf("%d ",root->key);
    }
} 
//ํƒ์ƒ‰  
TreeNode *search(TreeNode *node,int key) {
    while(node!=NULL){
        if(key==node->key) return node;
        else if(key<node->key)
            node=node->left;
        else 
            node=node->right;
    }
    return NULL; // ํƒ์ƒ‰ ์‹คํŒจํ•œ ๊ฒฝ์šฐ  
}
//๋…ธ๋“œ ์‚ฝ์ž…  
void insert_node(TreeNode **root, int key) {
   TreeNode *parent, *t;
   TreeNode *new;

   t = *root;
   parent = NULL;
   while (t != NULL) {
      if (key == t->key)
         return;
      parent = t;
      if (key < t->key)
         t = t->left;
      else
         t = t->right;
   }
   new = (TreeNode *)malloc(sizeof(TreeNode));
   if (new == NULL)
      return;
   new->key = key;
   new->left = new->right = NULL;
   if (parent != NULL)
      if (key < parent->key)
         parent->left = new;
      else
         parent->right = new;
   else
      *root = new;
}
//๋…ธ๋“œ ์‚ญ์ œ  
void delete_node(TreeNode **root,int key) {
    TreeNode *parent,*child,*suc,*suc_parent,*t;

    parent = NULL;
    t=*root;
    while(t != NULL && t->key != key) {
        parent = t;
        t=(key<parent->key) ? parent->left : parent->right ;
    }
    if (t==NULL) {
        printf("ํƒ์ƒ‰ ์‹คํŒจ\n");
        return;
    }
    //๋‹จ๋ง ๋…ธ๋“œ์ธ ๊ฒฝ์šฐ 
    if((t->left==NULL)&&(t->right==NULL))
        if(parent != NULL)
            if(parent->left==t)
                parent->left=NULL;
            else
                parent->right=NULL;
        else
            *root=NULL;

    //ํ•˜๋‚˜์˜ ์ž์‹๋งŒ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ 
    else if((t->left==NULL)||(t->right==NULL)) {
        child = (t->left != NULL) ? t->left : t->right;
        if(parent != NULL)
            if(parent->left==t)
                parent->left=child;
            else
                parent->right=child;
        else
            *root=child;
    }
    //๋‘ ๊ฐœ์˜ ์ž์‹์„ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ 
    else {
        //์˜ค๋ฅธ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ์—์„œ ๋งจ ์™ผ์ชฝ ๋…ธ๋“œ๊ฐ€ ํ›„๊ณ„์ž 
        suc_parent=t;
        suc=t->right;
        while(suc->left != NULL) {
            suc_parent = suc;
            suc=suc->left;
        } 
        //ํ›„๊ณ„์ž์˜ ๋ถ€๋ชจ์™€ ์ž์‹์„ ์—ฐ๊ฒฐ
        if(suc_parent->left==suc)
            suc_parent->left=suc->right;
        else
            suc_parent->right=suc->right;
        //ํ›„์†์ž๊ฐ€ ๊ฐ€์ง„ ํ‚ค ๊ฐ’์„ ํ˜„์žฌ ๋…ธ๋“œ์— ๋ณต์‚ฌ
        t->key=suc->key;
        t=suc; 
    } 
    free(t);            
}

int main(void) {
    TreeNode *root=NULL;
    insert_node(&root,30);
    inorder(root);
    printf("\n");
    insert_node(&root,20);
    insert_node(&root,40);
    inorder(root);
    printf("\n");
    insert_node(&root,15); 
    insert_node(&root,25);
    insert_node(&root,35); 
    insert_node(&root,45);
    inorder(root);
    printf("\n");
    delete_node(&root,30);
    inorder(root);
    printf("\n");  

    return 0;
} 
๋ฐ˜์‘ํ˜•