data_CHAP05 -  c / / Data Structures By Hyun-Ju Park /...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview:  c / / Data Structures By Hyun-Ju Park / Hanbat National University / / 55 j / 55 / 66 66 / / 66   6 / / / / 55 5 5  û a   ÷  / / / 555 555 55 / 66   Z / / 6   / / / / 5 5   j   / / / 555 555 55 / 6   6 / / / 5 5    / / 5 5 5 / 6 œ /  / / / 5 55 66 /5 8 6 7 / 5 5 6 / / / / 55 5 6     / / 5 5 5 66 / / / / 5 55 66   ø ø  / / 5 5 5 66 / / / / / / / 55 5 5 55 55 û Z         [   / / / / / 555 555 555 555 55 66 / / / / 55 5 5 û    / / / 555 555 555 /  6     66 66 / Data Structures / / / By Hyun-Ju Park / Hanbat National University  5.1.1  %     (branch)›  o    a} a b ø \ ]  ýª Dusty Dusty Honey Bear Brandy Brunhilde Terry Coyote Nugget Gill Tansey Tweed Zoe Crocus Primrose Nous Belle / / Data Structures By Hyun-Ju Park / Hanbat National University /  \ ` L (node)› `  ñ ,  ó›  (root)“\ L (subtree)“ `  L ó n(≥ 0)[   , T1, ..., Tnó› o . , T1, ..., Tnó `  °,      ª  A B C D E F G H I J K L M / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  L (node) (degree) − “ :    ó›  ó›  § :  L  L (terminal node) ↔ : ø L (child node)  L (non-terminal node) 0 L ›  “   ø  L  ó 1, l  o    § û` \ L   o › ø  L L \  l+1   X\  L :  L X  L (parent node) :  L X û(sibling) (ancestors) (level)  (forest) : ÷ : : : n ≥ 0[  / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.1.2 %  ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) û − ÷    L 1   − − T 0  = k, L =n = n(k – 1) + 1 / / Data Structures  2 ... n By Hyun-Ju Park / Hanbat National University / 6   k  # ⇒        ­ ] ª ]  data left left child     ­ right sibling  ª ]  A B C D E F G H I J K 2  Data Structures L M   c  A  By Hyun-Ju Park / Hanbat National University / 6 / /       ª  A A A B B B A A A B C B C B C  ù     / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   %  ß %   ``  é í ,  “ \r[ o  › o L ñ  / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.2.1       #JOBSZ@5SFF ª BinTree)  Binary_Tree›  structure Binary_Tree (  objects: functions:  bt, bt1, bt2 BinTree Create() Boolean IsEmpty(bt) À ``  L , í Binary_Tree, \ L ñ  BinTree, item element ::= À  § ::= if (bt == À  ) return TRUE else return FALSE ::= í  bt1, bt2,  \ j §  \  § BinTree MakeBT(bt1, item, bt2) BinTree Lchild(bt) element Data(bt) BinTree Rchild(bt) ::= if (IsEmpty(bt)) return  else bt í § ::= if (IsEmpty(bt)) return  else bt   \ j § ::= if (IsEmpty(bt)) return  else bt  § / / Data Structures By Hyun-Ju Park / Hanbat National University /  % À    vs. %  / 0[ §    ª ]  L §  \   \ ø  A A B B / / Data Structures By Hyun-Ju Park / Hanbat National University /  %  %     – ª ]  A A B B C D D H E    I E F G C / / Data Structures By Hyun-Ju Park / Hanbat National University /  5.2.2  0 k   k  ×  i    \  L \ 2i-1, i ≥ 1 L \ 2k - 1 , k ≥ 1 Ù  n1   1 ! 2  T L “  n0   L  n2 2 L n0 = n2 + 1 L \ n = n0 + n1 + n2 / / Data Structures By Hyun-Ju Park / Hanbat National University /   k 0  k, L  ×(full binary tree) 2k -1 (k ≥ 0)    §    ª ]  ] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  ×(complete binary tree) 0  k  L n \      L  ª ]  0 k    1 n § L ÷ ÷› ÷ A B C D E F G H I / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.2.3 5.2.3.1    % 1 L ú Zó   L › ó› ™› § ó  \ í ó› § i L i L § TREE(i)  \ í ø , ø ,  L § À TREE  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6        ª   A (1) (2) (2) (3) A B C D . . . E B (4) (5) (6) (7) (8) C D (9) . . . (16) E / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  A (1) (2) (3) (4) (5) (6) (7) (8) A B C D E F G H I B C D E F G H I (9) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  ns  parent(i)\ i ≠ 1÷ | i / 2 lchild(i)\ 2i ≤ n÷ | 2i %   bo bo  bo rchild(i)\ 2i + 1 ≤ n÷ | 2i + 1    2k - 1 \  k `` û`  | ó` ÷   \    L § L    z“ ó L ã / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.2.3.2        ] ª ]  typedef struct node *tree_pointer; typedef struct node { int data; tree_pointer left_child, right_child; }; left_child data right_child data left_child right_child L parent § à § só›  / / Data Structures By Hyun-Ju Park / Hanbat National University /          ª   root A A B null B null C C null D D null E E null / / Data Structures By Hyun-Ju Park / Hanbat National University /  A B C D E F G A H I B C D null E null null F null null G null null H null null I null / / Data Structures By Hyun-Ju Park / Hanbat National University /   §  L § \  \  §  −  L : moving left, V : visiting the node, R : moving right LVR, LRV, VLR, VRL, RVL, RLV (inorder traversal) - LVR ! (postorder traversal) - LRV (preorder traversal) - VLR / / Data Structures By Hyun-Ju Park / Hanbat National University /       ­  ª  + * E * D / C A B / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  í  §   §   ô §  ª § ]   \ void inorder(tree_pointer ptr) /*  */ { if (ptr) { inorder(ptr->left_child); printf("%d", ptr->data); inorder(ptr->right_child); } } + * E * D / C A B A/B*C*D+E / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    L §  í   §  §  ª § ]   void preorder(tree_pointer ptr) /*  */ { if (ptr) { printf("%d", ptr->data); preorder(ptr->left_child); preorder(ptr->right_child); } } + * E * D / C A B +**/ABCDE / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  í  § ­  _  ô    § ó›  § ª ]    void postorder(tree_pointer ptr) /* !  */ { if (ptr) { postorder(ptr->left_child); postorder(ptr->right_child); printf("%d", ptr->data); } } + * E * D / C A B AB/C*D*E+ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6      § ª  void iter_inorder(tree_pointer node) { int top = -1; /* */ tree_pointer stack[MAX_STACK_SIZE]; for (;;) { for (; node; node = node->left_child) add(&top, node); /*  */ node = delete(&top); /*  û */ if (!node) break; /* À */ printf("%d", node->data); node = node->right_child; } } + * E * D / C A B A/B*C*D+E / / Data Structures By Hyun-Ju Park / Hanbat National University / 6     , í ø ,   _  z L ›   E § § ª ]   ø void level_order(tree_pointer ptr) /*   */ { int front = 0, rear = 0; tree_pointer queue[MAX_QUEUE_SIZE]; if (!ptr) return; /* À  */ addq(front, &rear, ptr); for (;;) { ptr = deleteq(&front, rear); if (ptr) { printf("%d", ptr->data); if (ptr->left_child) addq(front, &rear, ptr->left_child); if (ptr->right_child) addq(front, &rear, ptr->right_child); } else break; } } + * E * D / C A B +*E*D/CAB / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   5.4.1   = –   – QPTUPSEFS ª ]  tree_pointer copy(tree_pointer original) /*  §   o  tree_pointer§ . */ { tree_pointer temp; if (original) { temp = (tree_pointer) malloc(sizeof(node)); if (IS_FULL(temp)) { fprintf(stderr, "The memory is full\n"); exit(1); } temp->left_child = copy(original->left_child); temp->right_child = copy(original->right_child); E temp->data = original->data; return temp; } return NULL; } A B C D F G H E / / Data Structures By Hyun-Ju Park / Hanbat National University /  5.4.2 S ᖠ%   %      S ᖠ§ ª A  A int equal(tree_pointer first, tree_pointer second) { /* r   ÷ TRUE,  ó FALSE§ return ((!first && !second) || (first && second && (first->data == second->data) && equal(first->left_child, second->left_child) && equal(first->right_child, second->right_child)); } E . */ B B C D C D E F G H E / / Data Structures By Hyun-Ju Park / Hanbat National University /  5.4.3  c x1, x2, x3, …, xn  ø ∧(and), ∨(or), ¬(not)ó› \  − − −  x, y  ø ` ÷ | x ∧ y, x ∨ y, ¬x \ ¬, ∧, ∨ ó›  › § ›,   (x1 ∧ x2) ∨ (¬x1 ∧ x3) ∨ ¬ x3  \ \§ \ / / Data Structures By Hyun-Ju Park / Hanbat National University /  %      W ­ª ]  ∨ ∨ ∧ x1 ¬ x2 ¬ x1 ∧ x3 (x1 ∧ ¬x2) ∨ (¬x1 ∧ x3) ∨ ¬ x3 ¬ x3 / / Data Structures By Hyun-Ju Park / Hanbat National University /       c    ª  left_child left_child data val right_child typedef enum { not, and, or, true, false } logical; typedef struct node *tree_pointer; typedef struct node { tree_pointer left_child; logical data; short int value; tree_pointer right_child; }; / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  ns O(g2n)   2n     Ò ª ]  for (all 2n possible combinations) { generate the next combination; replace the variables by their values; evaluate root by traversing it in postorder; if (root->value) { printf(<combination>); return; } } printf("No satisfiable combination\n"); / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       QPTU@PSEFS@FWBM  § ª  void post_order_eval(tree_pointer node) /* û § o! */ { if (node) { post_order_eval(node->left_child); post_order_eval(node->right_child); switch (node->data) { case not: node->value = !node->right_child->value; break; case and: node->value = node->right_child->value && node->left_child->value; break; case or: node->value = node->right_child->value || ∨ node->left_child->value; break; case true: node->value = TRUE; ∨ ¬ case false: node->value = FALSE; } } ∧ ∧ } x3 x1 ¬ x2 ¬ x1 x3 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   E] % û §  L §    \ › ⇒ − (thread) left child §  L §  ø(inorder predecessor) right child − §  L §  ! ø(inorder successor) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       E] ª  A B C D E F G H I / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    %  ]      œ E] ª ]  left_thread left_child left_child data right_child right_thread typedef struct threaded_tree *thread_pointer; typedef struct threaded_tree { short int left_thread; threaded_pointer left_child; char data; threaded_pointer right_child; short int right_thread; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       E]  ª  TRUE TRUE - FALSE f - f f A f f B f f C f f D f t E t t F t t G t t H t t I t / / Data Structures By Hyun-Ju Park / Hanbat National University / 6     %      ] E]  Ñ  § ª ]    _     threaded_pointer insucc(threaded_poninter tree)   ! ø§ \ . */ /* { threaded_pointer temp; temp = tree->right_child; if (!tree->right_thread) while (!temp->left_thread) temp = temp->left_child; return temp; } void tinorder(threaded_pointer tree)  */ /* { threaded_pointer temp = tree; for (;;) { temp = insucc(temp); if (temp = tree) break; printf("%3c", temp->data); } } f - f f A f f B f f C f f D f t E t t F t t G t t H t t I t / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    ª ] %         E]  QBSFOU ­ DIJME — A S A S B T B T C D C D / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  A A S B B T C C D X E F S T X D E F / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    _     E]  — ª  void insert_right(threaded_pointer parent, threaded_pointer child)   child§ parent  ø ó› */ /* { threaded_pointer temp; child->right_child = parent->right_child; child->right_thread = parent->right_thread; child->left_child = parent; child->left_thread = TRUE; parent->right_child = child; parent->right_thread = FALSE; if (!child->right_thread) { temp = insucc(child); temp->left_child = child; } } / / Data Structures By Hyun-Ju Park / Hanbat National University / 66   5.6.1   − L ø  ø ó  −  − L    ø   ó  −    / / Data Structures By Hyun-Ju Park / Hanbat National University / 66        ª  14 9 30 12 7 6 3 25 10 10 8 6 5  À   ›    û / / Data Structures By Hyun-Ju Park / Hanbat National University / 66        ª  2 10 11 7 4 20 83 21 10 10 8 6 50 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66    _     .BY)FBQ ª  structure MaxHeap objects : L   ø  ø › o n > 0  functions :  heap ∈ MaxHeap, item ∈ Element, n, max_size ∈ integer MaxHeap Create(max_size) ::= max_size[  §  \ À § Boolean HeapFull(heap, n) ::= if (n == max_size) return TRUE else return FALSE MaxHeap Insert(heap, item, n) ::= if (!HeapFull(heap, n)) item  § else return  Boolean HeapEmpty(heap, n) ::= if (n > 0) return TRUE else return FALSE Element Delete(heap, n) ::= if (!HeapEmpty(heap, n))    § û` § else return  / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  5.6.2  §    Zó  §   û øb  Zó  ›   ó ›  ó ø Zó  §  û           Á  d  l Á  §   l ª — O(1) O(1) O(n) O(n) O(log2n)  – O(n) O(n) O(1) O(1) O(log2n) / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  5.6.3     —  — ª ]      20 15 2 14 14 10 20 15 5 10 2 14 15 21 20 14 10 2 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    ª  #define MAX_ELEMENTS 200 /* + 1 */ #define HEAP_FULL(n) (n == MAX_ELEMENTS – 1) #define HEAP_EMPTY(n) (!n) typedef struct { int key; */ /* } element; element heap[MAX_ELEMENTS]; int n = 0; / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    _      — ª  void insert_max_heap(element item, int *n) /* n[  § \  item . */ { int i; if (HEAP_FULL(*n)) { fprintf(stderr, "The heap is full. \n"); exit(1); } i = ++(*n); while ((i != 1) && (item.key > heap[i/2].key)) { heap[i] = heap[i/2]; i /= 2; } heap[i] = item; }  − O(log n) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.6.4     –  – ª ]      20 15 14 14 10 2 10 15 2 14 15 2 14 10 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6         – ª  element delete_max_heap(int *n) { /*  §  û */ int parent, child; element item, temp; if (HEAP_EMPTY(*n)) { fprintf(stderr, "The heap is empty\n"); exit(1); } item = heap[1]; /*   */ temp = heap[(*n)--]; /* §   §  */ parent = 1; child = 2; while (child <= *n) { /* parent ø */ if ((child < *n) && (heap[child].key) < heap[child + 1].key) child++; if (temp.key >= heap[child].key) break; /* › */ heap[parent] = heap[child]; parent = child; child *= 2; } heap [parent] = temp; return item; }  − O(log n) / / Data Structures By Hyun-Ju Park / Hanbat National University / 66   5.7.1 } ˜ §  ûã \ ô\    (binary search tree) :  ã › À   \ í  í    À “ § × ô   §    ø   \ °,  r  ÷ ó \  ó   ⇒ , ,  / / Data Structures By Hyun-Ju Park / Hanbat National University / 66       ª  20 20 20 15 25 12 22 12 10 22 10 15 25 %   ó› O(log n) ⇒ % Z (balanced search trees) O(log n) / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  5.7.2  ˜  ˜    ˜ § ˜ª ]  20 20 12 22 10 15 25 tree_pointer search(tree_pointer root, int key) /* key L   .  L { if (!root) return NULL; if (key == root->data) return root; if (key < root->data) return search(root->left_child, key); return search(root->right_child, key); } \ \ NULL */ / / Data Structures By Hyun-Ju Park / Hanbat National University / 66       ˜ ˜ª  20 20 12 22 10 15 25 tree_pointer search2 (tree_pointer tree, int key) /* key L   .  L \ { while (tree) { if (key == tree->data) return tree; if (key < tree->data) tree = tree->left_child; else tree = tree->right_child; return NULL; } \ NULL */  = O(h) = O(log n) / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  5.7.3  ˜     — ˜ — ª ]  30 insert insert 35 5 40 5 30 40 2 80 2 35 80 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66       ˜  — ª  void insert_node(tree_pointer *node, int num) /*  L num  ó ÷  ô; ó data=num L §  */ { tree_pointer ptr, temp = modified_search(*node, num); if (temp || !(*node)) { /* num   ô */ ptr = (tree_pointer)malloc(sizeof(node)) ; if (IS_FULL(ptr)) { fprintf(stderr, "The memory is full\n"); exit(1); } ptr->data = num; ptr->left_child = ptr->right_child = NULL; if (*node) /* temp ø ó› */ if (num < temp->data) temp->left_child = ptr; else temp->right_child = ptr; else *node = ptr: } } \  = O(h) = O(log n) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.7.4  ˜     – ˜ – ª ]  30 delete delete 5 5 40 2 30 40 2 80 80 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       ­ a – ª  40 40 20 10 30 45 52  = O(h) = O(log n) 40 60 50 55 70 delete 60 10 20 30 45 50 52 55 70 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.7.5 ns  ˜  : O(log2n)  : O(n) − 1, 2, 3, ?, n › \   %  % (balanced search tree) O(log2n)   Z AVL tree, 2-3 tree, red-black tree, … / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   ý ks  (run) : key (ordered sequence)  z“ Z ›  ˜o [ › o  % L − − k[   (selection tree) r[ L \ L \ ø L øó L § \    øó L   \j §  ôó› øó § ¿  − % ø  L ø −   L % r[ ø L øó L § ` \   ø  § ñ / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  5.8.1 «        L      W ý W ª ]   1 6 2 6 4 9 8 10 15 16 9 9 20 38 10 20 20 30 6 11 6 15 25 12 8 15 50 9 11 16 5 6 8 13 90 100 110 17 14 17 18 20 15 8 7 3 1 2 3 4 5 6 7 8 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  1 8 2 9 4 9 8 10 15 16 9 9 20 38 10 20 20 30 15 11 15 25 25 12 8 15 50 9 11 16 5 6 8 13 90 100 110 17 14 17 18 20 15 8 7 3 1 2 3 4 5 6 7 8 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66     n[  ké ø ` ›    : O(k) : O(log2k) \  %  ó O(nlog2k) ó O(nlog2k logkm) = O(nlog2m)  ñ | / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  5.8.2  % ­ \ L 11› û L  o  ­  ›§ z“ ø  û L  §  ÷ ⇒ %  L   L \ ó› 11  ­  1 › ø § û\ ú  \ ø   § ó›  § / / Data Structures By Hyun-Ju Park / Hanbat National University / 66       ª  0 6 1 8 2 9 4 10 8 10 1 9 9 2 10 20 3 20 11 6 4 12 8 5 5 6 9  ø 3 17 7 90 13 9 6 90 7 14 17 8 15 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66    (forest)\ n ≥ 0[ (disjoint)        } W ª  A E G B C D F H I / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.9.1 %     §   ›  û §   T1, T2, …, Tn − − − B\ é  › À ó §  ° í , › B(T11, T12, …, T1m)    |   ô\  \ B(T1, T2, …, Tn)ó› n=0 B\ T1  T11, T12, …, T1mó T1 \ B(T2, T3, …, Tn) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6        ª  A A E G B B C D F H I E F G E F H G / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  5.9.2 § (preorder traversal) T F F  F À   ó     \F L  é T  F  ó  ! \ À   (postorder retrieval) \F L ! é÷  !  ! (inorder traversal) T F  À     \F L  é A E G  ó B C D F H I / / Data Structures By Hyun-Ju Park / Hanbat National University / 6     −   \ 1, 2, ..., nó› ó  ›   û (disjoint)§  o j÷   ó  union find(i) : :  i§  \ \ / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  % L ø  ( § û  L \   ) " \   ^ " \  ^ " \  ^        ª ]  0 4 2 6 7 S1 8 1 S2 9 3 S3 5 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  5.10.1 Union Find S1 ∪ S2      §   ª ]     4 ∪ 4 0 4 6 7 8 4 0 1 9 1 S1 ∪ S2 9 6 7 8 S2 ∪ S1 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66          4 4 4  ª  0  S1 S2 S3 1 2 3 5 9 4  6 7 8 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  i parent   [0] -1 [1] 4    VOJPO GJOE  int find1(int i) { for(; parent[i] >= 0; i = parent[i]) ; return i; } void union1(int i, int j) { parent[i] = j; /* or parent[j] = i; */ } − rà  ó  ó   . / / Data Structures       " " "  ª  [2] -1 [3] 2 [4] -1 [5] 2 ª [6] 0 [7] 0  [8] 0 [9] 4 By Hyun-Ju Park / Hanbat National University / 66   Si = { i }, 1 ≤ i ≤ p − −  −  ô \      ª  n parent(i) = 0, 1 ≤ i ≤ p union-find union(0, 1), find(0) union(1, 2), find (0) ? union(n – 2, n – 1), find(0) n-1 M 1  union − − find − − i  \  find find ó O(i)     ó O(n) n – 1[ n–2 n −2 2 ó O ∑ i = O n 1 () / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  s i à  \ L  j    L  ó  j§ i  › ª › ]      a  0 1 ··· n-1 0 2 ··· n-1 0 3 ··· n-1 1 union(0,1), 0=find(0) 0 4 ··· n-1 ··· 1 2 3 1 2 union(0,2), 0=find(0) 0 1 2 3 ··· n-1 union(0,3), ···, 0=find(0) union(0,n-1) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       VOJPO  ª  void union2(int i, int j) /*   ié j(i != j)   parent[i] = -count[i] ° parent[j] = -count[j] */ { int temp; temp = parent[i] + parent[j]; if (parent[i] > parent[j]) { parent[i] = j; /* j§ ›  ›  */ parent[j] = temp; } else { parent[j] = i; /* i§ ›  ›  */ parent[i] = temp; } } . / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  ns  # %  T union2 % T   log2n + 1 : parent(i) = -count(i) = -1, 1 ≤ i ≤ n = 23       ª  ]   union(1, 2), union(3, 4), union(5, 6), union(7, 8), union(1, 3), union(5, 7), union(1, 5)  [-1] 0 [-1] 1 [-1] 2 [-1] 3 [-1] 4 [-1] 5 [-1] 6 [-1] 7 1  [-2] 0 1 [-2] 2 3 [-2] 4 5 [-2] 6 7 1 2 union(0,1) union(2,3) union(4,5) union(6,7) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  [-4] 0 1 2 3 [-8] 0 1 2 3 5 5 [-4] 4 6 7  1 2 3  union(0,2) union(4,6) 1 4 6 7 2 3 4 union(0,4)  − − find n[ § union  m[  find ó ó O(logn)  O(n + mlogn) n – 1[ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  ⇒   ÷ j i  › \ ›  j§  ª ø ó›  ]     GJOE  int find2(int i) /*  i§  \  § ô.   i› › */ { int root, trail, lead; for (root = i; parent[root] >= 0; root = parent[root]) ; for (trail = i; trail != root; trail=lead) { lead = parent[trail]; parent[trail] = root; } return root; } [ find GJOE  − − GJOE  \ 2À GJOE      Ü m GJOE § \  L § find1 find2 : 8 × 3 = 24 : 3 + 3 + 1 × 7 = 13 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  5.10.2 S union{ find `    r[  findé    S  ` union  ª ]   [-1] 0 [-2] 0 [-1] 1 [-1] 2 [-1] 2 [-1] [-1] 3 4 [-1] 5 [-1] 5 [-2] 6 [-1] 6 [-1] 7 [-1] 7 [-1] 8 [-2] 8 [-1] 9 [-1] 11 [-1] 10 [-1] 11 [-2] 3 0≡4 3≡1 6 ≡ 10 8≡9 4 1 10 9 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  [-3] 0 [-4] 6 [-3] 3 [-2] 2 7≡4 6≡8 3≡5 2 ≡ 11 4 7 10 8 1 5 11 [-5] 0 [-4] 6 9 [-3] 3 11 ≡ 0 4 7 2 10 8 1 5 11 9 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66    %     } }    O      O     ª ]  n=2 n=3 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66     \ ñ÷ È  − − I   :ABCDEFGHI :BCAEDGHF    §  ª ]  A B A B, C D, E, F, G, H, I D C A E F G D, E, F, G, H, I I B H C Data Structures By Hyun-Ju Park / Hanbat National University / 66 / /  %    t  1, 2, ..., 9÷| ] ó 2, 3, 1, 5, 4, 7, 8, 6, 9  ª ] .      1 2 4 3 5 6 7 9 8 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  % .  :1 −  n  1, 2, …, n §  û \   ó›  % \    \é  , . n = 3 : (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 2, 1)     } §  ª ]  1 2 3 1 2 3 2 1 3 2 1 2 3 3 1 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  ...
View Full Document

This note was uploaded on 05/10/2011 for the course CS 101 taught by Professor Kks during the Spring '11 term at Seoul National.

Ask a homework question - tutors are online