Presentation is loading. Please wait.

Presentation is loading. Please wait.

5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root.

Similar presentations


Presentation on theme: "5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root."— Presentation transcript:

1 5 -1 Chapter 5 Trees

2 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root left subtree right subtree

3 5 -3 A G FE C D B HI level 0 1 2 3 root: A node: A, B, C, …, H, I father of B: A sons of B: D, E left son of B: D right son of B: E depth: 3 ancestors of E: A, B descendants of B: D, E, G An example of binary tree (1)

4 5 -4 left descendant of B: D right descendant of B: E, G brother: B and C are brothers D and E are brothers leaf: a node that has no sons e.g. D, G, H, I left subtree of A: right subtree of A: B G DE HI C F An example of binary tree (2)

5 5 -5 Not binary trees A BC EDF GHI (a) A BC EDF G (b) A BC EDF G HI (c)

6 5 -6 Strictly binary tree Each nonleaf node has nonempty left and right subtrees. A B C E D FG

7 5 -7 Complete binary tree of depth 3 # of nodes in a complete binary tree of depth d: ** A JIH ED B KNML GF C O

8 5 -8 Each node has 3 fields. 若欲如 doubly linked list, 有雙向的 link, 則加入 "father" 而成為 4 個 field. left soninformationright son A CB DE A nullC B E D Implementation of a binary tree

9 5 -9 #define NUMNODES 500 struct nodetype{ int info; int left; int right; int father; }; struct nodetype node[NUMNODES]; Linked array representation

10 5 -10 struct nodetype{ int info; struct nodetype *left; struct nodetype *right; struct nodetype *father; }; typedef struct nodetype *NODEPTR; Dynamic node representation

11 5 -11 maketree(x): Create a new tree consisting of a single node NODEPTR maketree(int x) { NODEPTR p; p = getnode(); p->info = x; p->left = NULL; p->right = NULL; return(p); } /* end maketree */ X Creation of a new tree

12 5 -12 setleft(p, x): create a new left son of node p with information field x. void setleft(NODEPTR p, int x) { if (p == NULL) printf("void insertion\n"); else if (p->left != NULL) printf("invalid insertion\n"); else p->left = maketree(x); } /* end setleft */ x pp Creation of a new son

13 5 -13 Finding all duplicate numbers in a number series. 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 build a binary search tree: smaller numbers stored in the left subtree. larger numbers stored in the right subtree. Duplicate numbers: ** An application of binary trees 18 16 17 20 14 154 7 39 5

14 5 -14 struct nodetype{ int info; struct nodetype *left; struct nodetype *right; }; typedef struct nodetype *NODEPTR; main() { NODEPTR ptree; NODEPTR p, q; int number; scanf("%d", &number); ptree = maketree(number); Implementation with C

15 5 -15 while (scanf("%d", &number) != EOF){ p = q = ptree; while (number != p->info && q != NULL){ p = q; if (number info) q = p->left; else q = p->right; } /* end while */ if (number == p->info) printf("%d is a duplicate\n", number); else if (number info) setleft(p, number); else setright(p, number); } /* end while */ } /* end main */ 14 154 7 9

16 5 -16 Traversals in a binary tree (1) (1) preorder (depth-first order) 1) root 2) left subtree 3) right subtree (2) inorder (symmetric order) 1) left subtree 2) root 3) right subtree (3) postorder 1) left subtree 2) right subtree 3) root

17 5 -17 preorder : inorder : ** postorder : A D BC FE GIH Traversals in a binary tree (2)

18 5 -18 void pretrav(NODEPTR tree) { if (tree != NULL){ printf("%d\n", tree->info); // visit the root pretrav(tree->left); // traverse left subtree pretrav(tree->right);// traverse right subtree } /* end if */ } /* end pretrav */ Preorder traversal

19 5 -19 void intrav(NODEPTR tree) { if (tree != NULL){ intrav(tree->left); // traverse left subtree printf("%d\n", tree->info); // visit the root intrav(tree->right); // traverse right subtree } /* end if */ } /* end intrav */ Inorder traversal

20 5 -20 void posttrav(NODEPTR tree) { if (tree != NULL){ posttrav(tree->left); //traverse left subtree posttrav(tree->right);//traverse right subtree printf("%d\n", tree->info); // visit the root } /* end if */ } /* end posttrav */ Postorder traversal

21 5 -21 Binary search tree 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 4 39 79 15 18 5 45 14 1620 17 inorder traversal sorted: 3,4,4,5,5,7,9,9,14,14,15,16,17,18,20

22 5 -22 Expressions in binary trees prefix: + A * B C ( preorder traversal ) infix: A + B * C ( inorder traversal ) postfix: A B C * + ( postorder traversal ) + A* CB

23 5 -23 Implicit array for an almost- complete binary tree (1) (1) Each leaf is either at level d or at level d-1. (2) For any node n with a right descendant at level d, all the left descendants of node n that are leaves are also at level d. A CB GFE J D HI 0 1 2 3 4 5 6 7 89 0123456789 ABCDEFGHIJ

24 5 -24 An almost complete binary tree can be implemented by an array. sons of node p : father of node p: ** not an almost complete binary tree: A BC ED GF Implicit array for an almost- complete binary tree (2)

25 5 -25 A BC GF ED 0123456789101112 ABCDEFG Extension to not almost complete binary trees

26 5 -26 Nonrecursive inorder traversal #define MAXSTACK 100 void intrav2(NODEPTR tree) { struct stack{ int top; NODEPTR item[MAXSTACK]; }s; NODEPTR p; s.top = -1; p = tree; do{ /*travel down left branches as far as possible */ /* saving pointers to nodes passed */ while (p != NULL){ push(s, p); p = p->left; } /* end while */

27 5 -27 /* check if finished */ if (!empty(s)){ /* at this point the left subtree is empty */ p = pop(s); printf("%d\n", p->info); /* visit the root */ p = p->right; /* traverse right subtree */ } /* end if */ } while (!empty(s) || p != NULL); } /* end intrav2 */ The recursion stack cannot be eliminated.

28 5 -28 Right in-threaded binary tree A node with an empty right subtree points to its inorder successor. It can be traversed in inorder without a stack. A B C F E D IHG setleft(p, x): setright(p, x): **

29 5 -29 If the array implementation is used, positive pointer: normal right son negative pointer: inorder successor dynamic implementation: struct nodetype{ int info; struct nodetype *left; // pointer to left son struct nodetype *right; // pointer to right son int rthread; // rthread is TRUE if // right is NULL or } // a non-NULL thread typedef struct nodetype *NODEPTR; Implementation of a right in- threaded binary tree

30 5 -30 void intrav3(NODEPTR tree) // No stack is used { NODEPTR p, q; p = tree; do{ q = NULL while (p != NULL){ /* traverse left branch */ q = p; p = p->left; } /* end while */ Implementation with C (1)

31 5 -31 if (q != NULL){ printf("%d\n", q->info); p = q->right; while (q->rthread && p != NULL){ printf("%d\n", p->info); q = p; p = p->right; } /* end while */ } /* end if */ }while (q != NULL) } /* end intrav3 */ Implementation with C (2)

32 5 -32 Heterogeneous binary trees The binary tree represents 3 + 4*(6-7)/5 + 3. '+' 3 '/' '+' 3 5 '*' 4'-' 76

33 5 -33 Evaluation of an expression represented by a binary tree (1) #define OPERATOR 0 #define OPERAND 1 struct nodetype{ short int utype; /* OPERATOR or OPERAND */ union{ char chinfo; float numinfo; }info; struct nodetype *left; struct nodetype *right; }; typedef struct nodetype *NODEPTR; float evalbintree(NODEPTR tree) { float opnd1, opnd2; char symb;

34 5 -34 if (tree->utype == OPERAND) /* expression is a single */ return (tree->numinfo); /* operand */ /* tree->utype == OPERATOR */ /* evaluate the left subtree */ opnd1 = evalbintree(tree->left); /* evaluate the right subtree */ opnd2 = evalbintree(tree->right); symb = tree->chinfo; /* extract the operator */ /* apply the operator and return the result */ return(oper(symb, opnd1, opnd2)); } /* end evalbintree */ Evaluation of an expression represented by a binary tree(2)

35 5 -35 The Huffman code (1) Suppose we have a set of symbols: A, B, C, D 1) Each symbol is encoded by 3 bits (inefficient) Message A B A C C D A would be encoded by 21 bits: symbol code A 0 1 0 B 1 0 0 C 0 0 0 D 1 1 1 010 100 010 000 000 111 010

36 5 -36 2) Each symbol is encoded by 2 bits Message A B A C C D A would be encoded by 14 bits: symbol code A 00 B 01 C 10 D 11 00 01 00 10 10 11 00 The Huffman code (2)

37 5 -37 3) Huffman codes (variable-length codes) symbol code A 0 B 110 C 10 D 111 Message A B A C C D A would be encoded by 13 bits: 0 110 0 10 10 111 0 A frequently used symbol is encoded by a short bit string. The Huffman code (3)

38 5 -38 Huffman tree ACBD,7 B,1 BD,2 CBD,4 A,3 C,2 D,1 0 1 01 0 1

39 5 -39 Construction of a Huffman tree E I A D C G B F H 25 15 15 12 7 6 6 4 1 25 15 15 12 7 6 6 5 25 15 15 12 11 7 6 25 15 15 13 12 11 25 23 15 15 13 28 25 23 15 38 28 25 53 38 91 symbol frequency

40 5 -40 IHFBDEGCA,91 IHFBD, 38EGCA, 53 B, 6HF, 5 D, 12HFB, 11 HFBD, 23I, 15 F, 4H, 1 E, 25GCA, 28 G, 6 A, 15GC, 13 C, 7 Sym Freq Code Sym Freq Code Sym Freq Code A 15 111 D 12 011 G 6 1100 B 6 0101 E 25 10 H 1 01000 C 7 1101 F 4 01001 I 15 00

41 5 -41 The result of Huffman coding A Huffman tree is a strictly binary tree. The Huffman algorithm is an optimal encoding scheme. 111 01000 10 111 011 A H E A D encode decode

42 5 -42 Representing lists as binary trees (1) n: # of elements array linked list binary tree (balanced) insertion or deletion (kth element) finding the kth element O(n-k) O(1) O(k)O(log 2 n) O(1) (inserting an element following a given element)

43 5 -43 nonleaf node: # of leaves in the left subtree leaf node: contents of a list element AFnullBCDE 4 12 11 ABCD FE k=3 k=1 Finding the kth element: k=3 Representing lists as binary trees (2)

44 5 -44 A complete binary tree of depth d has 2 d+1 -1 nodes or 2 d leaf nodes. If an almost complete binary tree is used to represent a list, the kth element can be found by accessing O(log 2 n) nodes. Representing lists as binary trees (3)

45 5 -45 4 1 2 11 ABCD FE 3 1 2 1 AB D FE (a)(b) X X p tree Deleting elements in the list (1)

46 5 -46 Time: O(log 2 n) 2 1 2 1 AB FE 1 1 A FE (c) (d) X Deleting elements in the list (2)

47 5 -47 Trees root of the tree: A B, C, D are brothers. degree of a node: # of sons degree of A : 3 degree of B : 2 degree of C : 0 degree of D : 1 A G DCB FE Fig.1

48 5 -48 ordered tree: the subtrees of each node form an ordered set Fig.1 and Fig.2 are different ordered trees. In Fig.1, oldest son of A : B youngest son of A : D A G BDC FE Fig.2 Ordered trees A G DCB FE Fig.1

49 5 -49 Representation of trees oldest soninformationnext brother Anull BCD EF G

50 5 -50 Ordered tree and binary tree An ordered tree can be represented as a binary tree. A B CE F D G oldest son left son next brother right son ordered tree Binary tree A G DCB FE

51 5 -51 Forest an ordered set of ordered trees H GA BC FED I JK M L

52 5 -52 Representing a forest by a binary tree corresponding binary tree A B C G D E F HI J MK L preorder traversal: A B C D E F G H I J M K L inorder traversal: B D E F C A H G M J K L I postorder traversal: F E D C B H M L K J I G A

53 5 -53 Preorder: + * A B + * C D E Inorder: A * B + C * D + E Postorder:A B * C D * E + + + * A+ * B CE D + * AB + *E CD Preorder: + * A B + * C D E Inorder: A B * C D * E + + Postorder:B A D C E * + * + why same? (a) (b)

54 5 -54 An example —— game trees tic-tac-toe 井字遊戲 evaluation function: 我方成一直線的機會 ( 個數 ) 減 對方成一直線的 機會 ( 個數 ) XO X O XO XX O XO X XO XO X XO XO XX O XOX X O 我方 : X 對方 : O 2 =3-1 221 =3-2

55 5 -55 The minimax method look ahead


Download ppt "5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root."

Similar presentations


Ads by Google