1 / 55

Chapter 5

Chapter 5. Trees. 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. An example of binary tree (1). root : A node : A, B, C, …, H, I father of B: A sons of B: D, E

zasha
Download Presentation

Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Trees

  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. An example of binary tree (1) 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 level 0 A 1 B C 2 D E F 3 G H I

  4. An example of binary tree (2) 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 right subtree of A: left subtree of A: B C D E F G H I

  5. Not binary trees A A B C B C D E F D E F A G H I G B C (a) (b) D E F G H I (c)

  6. Strictly binary tree • Each nonleaf node has nonempty left and right subtrees. A C B E D F G

  7. Complete binary tree of depth 3 • # of nodes in a complete binary tree of depth d: ** A B C D E F G H I J K L M N O

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

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

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

  11. Creation of a new tree • 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

  12. Creation of a new son • 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 */ p p x

  13. 14 4 15 3 9 18 7 16 20 5 17 An application of binary trees • 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: **

  14. Implementation with C 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);

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

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

  17. A B C D E F G H I Traversals in a binary tree (2) • preorder : • inorder : ** • postorder :

  18. Preorder traversal 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 */

  19. Inorder traversal 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 */

  20. Postorder traversal 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 */

  21. Binary search tree • 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 3 9 14 18 7 9 16 20 17 5 • inorder traversal • sorted: 3,4,4,5,5,7,9,9,14,14,15,16,17,18,20 4 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 * B C

  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 0 2 B C 1 3 D E F G 6 5 4 H I J 8 9 7

  24. A B C D E F G Implicit array for an almost-complete binary tree (2) • 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:

  25. Extension to not almost complete binary trees A B C D E F G

  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. /* 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. 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. setleft(p, x): A C B ** F E D setright(p, x): ** G H I

  29. Implementation of a right in-threaded binary tree • 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;

  30. Implementation with C (1) 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 */

  31. Implementation with C (2) 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 */

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

  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. Evaluation of an expression represented by a binary tree(2) 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 */

  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: symbolcode A 0 1 0 B 1 0 0 C 0 0 0 D 1 1 1 010 100 010 000 000 111 010

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

  37. The Huffman code (3) 3) Huffman codes (variable-length codes) symbolcode 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.

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

  39. Construction of a Huffman tree symbol 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 frequency

  40. IHFBDEGCA,91 IHFBD, 38 EGCA, 53 I, 15 HFBD, 23 E, 25 GCA, 28 HFB, 11 D, 12 GC, 13 A, 15 HF, 5 B, 6 G, 6 C, 7 H, 1 F, 4 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. The result of Huffman coding decode • 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

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

  43. Representing lists as binary trees (2) A B C D E F null • nonleaf node: # of leaves in the left subtree • leaf node: contents of a list element 4 k=3 Finding the kth element: k=3 2 1 k=1 1 1 E F A B C D

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

  45. Deleting elements in the list (1) 4 3 tree 1 1 2 2 E F E F 1 1 1 D X A B C D A B X p (a) (b)

  46. 1 1 A E F Deleting elements in the list (2) • Time: O(log2n) 2 1 2 E F 1 A B (d) X (c)

  47. A B C D G E F 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 Fig.1

  48. A A B C D C D B G E F G F E Ordered trees • 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 Fig.1 Fig.2

  49. Representation of trees oldest son information next brother A null B null C D null null E null F null null G null

  50. A B C D oldest son left son next brother right son A G E F B E C D F G Ordered tree and binary tree • An ordered tree can be represented as a binary tree. Binary tree ordered tree

More Related