Download - 第 6 章樹狀結構

Transcript
  • 6

  • 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9

  • 6.1

    1

    2

    3

    4

  • 6.1 (con.t)(node)(edge)(ancestor)(descendant)(parent node)(children node)(sibling node)(terminal node)(leaf node)

  • 6.1 (con.t)(degree)(level)(height)(depth)(forest)

  • 6.2 (right subtree)(left subtree)ABAB

  • 6.2 (con.t) 2/(left/right skewed tree)(a)(fully binary tree) (b)(complete binary tree) (c)

  • 6.2 (con.t)

  • 6.2 (con.t) i 2i-1i1()k2k-1k1n0n2 2 n0=n2+1Ex6.3

  • 6.3 (c)

    (c)(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)(12)(13)(14)(15)1234567891011121314151st level2nd level3rd level4th level

  • 6.3 (con.t) (left link)(data)(right link)

  • 6.4 (traversal)(inorder) (preorder) (postorder)

  • 6.4 (con.t)(inorder) void inorder(Node type *tree){ if (tree != NULL){ inorder(tree->llink); printf("%d", tree->data); inorder(tree->rlink); }}

  • 6.4 (con.t)(preorder) void preorder(Node type *tree){ if (tree != NULL){ printf("%d", tree->data); preorder(tree->llink); preorder(tree->rlink); } }

  • 6.4 (con.t)(postorder)void postorder(Node type *tree){ if (tree != NULL){ postorder(tree->llink); postorder(tree->rlink); printf("%d", tree->data); } }

  • 6.5 LINKn+1LINK(n)LINK(thread)LINK(head node)

  • 6.5 (con.t) LINKLINK

  • 6.5 (con.t)LBIT = 1LLINKLBIT = 0LLINKRBIT = 1RLINKRBIT = 0RLINK

  • 6.5.1 (inorder successor)Node_type *insucc(Node_type *ptr){ Node_type *this_n; this_n = ptr->rlink; if (ptr->rbit == 1) while (this_n->lbit == 1) this_n = this_n->llink; return this_n;}

  • 6.5.1 (con.t)void tinorder(Node_type *tree, Node_type *head){ tree = head; printf(%d, tree->data); for(;;){ tree = insuc(tree); if(tree == head) return; printf(%d,tree->data); } }

  • 6.5.1 (con.t)(inorder predecessor)Node_type *pred(Node_type *ptr){ Node_type *this_n; this_n = ptr->llink; if(ptr->lbit == 1) while(this_n->rbit == 1) this_n = this_n->rlink; return this_n;}

  • 6.5.2 void insert_right(Node_type *node_parent, Node_type *node){ Node_type *w; node->rchild = node_parent->rchild; node->rbit = node_parent->rbit; node->lchild = node_parent; node->lbit = 0; node_parent->rchild = node; node_parent->rbit = 1; if ( node->rbit == 1 ){ /*nodetree*/ w = insucc( node ); w->lchild = node; }} TS

  • 6.5.2 (con.t)void insert_left(Node_type *node_parent, Node_type *node){ Node_type *w; node->lchild = node_parent->lchild; node->lbit = node_parent->lbit; node->rchild = node_parent; node->rbit = 0; node_parent->lchild = node; node_parent->lbit = 1; if ( node->lbit == 1 ){ /*node tree*/ w = inpred( node ); w->rchild = node; }} TS

  • 6.5.3

  • 6.5.3 (con.t)

    prev1child = this1child;prev1bit = 0

  • 6.5.3 (con.t) 4prev1child = this1child;prev1bit = 0

  • 6.6 (binary search tree)(key value)

  • 6.6.1 Ex

  • 6.6.1 (con.t) 48

  • 6.6.1 (con.t) 90

  • 6.6.2 (Ex 50)

  • 6.7 (Heap)()

  • 6.7 (con.t) Heap Sort Heap()()

  • 6.7 (con.t)

  • 6.7 (con.t) n n/2 n/2()

  • 6.7.1 Ex6-47

  • 6.7.1 (con.t)Ex 50(Heap)

  • 6.7.1 (con.t)

  • 6.7.2 Ex

  • 6.7.2 (con.t) 4010

  • 6.7.3 Min-HeapHeapMax-heapMin-HeapMin-Max-HeapdeapMax-HeapMin-HeapMax-HeapEx

  • 6.8 (Set)110S1={1, 3, 5, 7} S2={2, 4, 6} S3={8, 9,10}S1 S2 S3(disjoint set)S1 S2 S3S1 S2 S3

  • 6.8.1

    i p(i) i parent (p(i)-1parent)

    i12345678 910p(i)3411343188

  • 6.9 (forest)(1) (2) (3) 45Ex

  • 6.9 (con.t)(1) (45)(2) (3) 45Ex(1)(1):

  • 6.9 (con.t)(2)

    (3)(3):(2):

  • 6.9 (con.t)

  • 6.9.2 Ex FDHGIBEAC ABDFGHIEC

  • 6.9.2 (con.t)ACA

  • 6.9.2 (con.t)BFDHGIEEB

  • 6.9.2 (con.t)DFHGIFDHGID

  • 6.9.2 (con.t)GHIHGIG