Trees and Graphs

Trees and Graphs

Module 8: Trees and Graphs Theme 1: Basic Properties of Trees A (rooted) tree is a finite set of nodes such that ¯ there is a specially designated node called the root. ¯ d Ì ;Ì ;::: ;Ì ½ ¾ the remaining nodes are partitioned into disjoint sets d such that each of these Ì ;Ì ;::: ;Ì d ½ ¾ sets is a tree. The sets d are called subtrees,and the degree of the root. The above is an example of a recursive definition, as we have already seen in previous modules. A Ì Ì Ì B C ¾ ¿ Example 1: In Figure 1 we show a tree rooted at with three subtrees ½ , and rooted at , and D , respectively. We now introduce some terminology for trees: ¯ A tree consists of nodes or vertices that store information and often are labeled by a number or a letter. In Figure 1 the nodes are labeled as A;B;:::;Å. ¯ An edge is an unordered pair of nodes (usually denoted as a segment connecting two nodes). A; B µ For example, ´ is an edge in Figure 1. A ¯ The number of subtrees of a node is called its degree. For example, node is of degree three, while node E is of degree two. The maximum degree of all nodes is called the degree of the tree. à ; Ä; F ; G; Å ; Á  ¯ A leaf or a terminal node is a node of degree zero. Nodes and are leaves in Figure 1. B ¯ A node that is not a leaf is called an interior node or an internal node (e.g., see nodes and D ). X X X ¯ Roots of subtrees of a node are called children of while is known as the parent of its D A A B; C D children. For example, B; C and are children of , while is the parent of and . B; C; D Ã Ä ¯ Children of the same parent are called siblings. Thus are siblings as well as and are siblings. ¯ The ancestors of a node are all the nodes along the path from the root to that node. For example, À; D A ancestors of Å are and . ¯ The descendants of a node are all the nodes along the path from that node to a terminal node. F; E ; Ã Ä Thus descendants of B are and . 1 A T3 T T2 1 B C D E F G H I J K L M Figure 1: Example of a tree. 1 Ð ¯ The level of a node is defined by letting the root to be at level zero , while a node at level has ·½ A B; C; D children at level Ð . For example, the root in Figure 1 is at level zero, nodes are à ; Ä; Å at level one, nodes E ; F ; G; À ; Á ;  ate level two, and nodes are at level three. ¯ The depth of a node is its level number. The height of a tree is the maximum level of any Å node in this tree. Node G is at depth two, while node at depth three. The height of the tree presented in Figure 1 is three. d d ¯ A tree is called a -ary tree if every internal node has no more than children. A tree is called d a full d-ary tree if every internal node has exactly children. A complete tree isafulltreeup the last but one level, that is, the last level of such a tree is not full. A binary tree is a tree with =¾ ¿ d . The tree in Figure 1 is a -ary tree, which is neither a full tree nor a complete tree. ¯ An ordered rooted tree is a rooted tree where the children of each internal node are ordered. We usually order the subtrees from left to right. Therefore, for a binary (ordered) tree the subtrees are called the left subtree and the right subtree. ¯ A forest is a set of disjoint trees. Now we study some basic properties of trees. We start with a simple one. Ò ½ Theorem 1. A tree with Ò nodes has edges. ½ Proof. Every node except the root has exactly one in-coming edge. Since there are Ò nodes other ½ than the root, there are Ò edges in a tree. 1Some authors prefer to set the root to be on level one. 2 The next result summarizes our basic knowledge about the maximum number of nodes and the height. Theorem 2. Let us consider a binary tree. i ¾ i ¼ (i) The maximum number of nodes at level i is for . h·½ ¾ ½ (ii) The maximum number of all nodes in a tree of height h is . Ò (iii) If a binary tree of height h has nodes then h ÐÓg ´Ò ·½µ ½: ¾ Ð (iv) If a binary tree of height h has leaves, then h ÐÓg Ð: ¾ =¼ Proof. We first proof (i) by induction. It is easy to see that it is true for i since there is only one i node (the root) at level zero. Let now, by the induction hypothesis, assume there are no more ¾ nodes i·½ i ·½ ¾ at level i. We must prove that at level there are no more than nodes. Indeed, every node i ¾ i at level i may have no more than two children. Since there are nodes on level ,wemusthaveno i i·½ ¡ ¾=¾ i ·½ more than ¾ nodes at level . By mathematical induction we prove (i). To prove (ii) we use (i) and the summation formula for the geometric progression. Since every i h level has at most ¾ nodes and there are no more than levels the total number of nodes cannot exceed h X i h·½ ¾ =¾ ½; i=¼ which proves (ii). h·½ ¾ ½ Now we prove (iii). If a tree has height h, then the maximum number of nodes by (ii) is which must be at least be as big as Ò,thatis, h·½ ¾ ½ Ò: ÐÓg ´Ò ·½µ ½ This implies that h , and completes the proof of (iii). Part (iv) can be proved in ¾ exactly the same manner (see also Theorem 3 below). d k Exercise 8A: Consider a d- ary tree (i.e., nodes have degree at most ). Show that at level there are k h·½ h ´d ½µ=´d ½µ at most d nodes. Conclude the total number of nodes in a tree of height is . Finally, we prove one result concerning a relationship between the number of leaves and the number of nodes of higher degrees. 3 A B D C E F Figure 2: Illustration to Theorem 3. Ò Ò ¾ Theorem 3. Let us consider a nonempty binary tree with ¼ leaves and nodes of degree two. Then Ò = Ò ·½: ¼ ¾ Ò Ò Proof.Let be the total number of all nodes, and ½ the number of nodes of degree one. Clearly Ò = Ò · Ò · Ò : ¼ ½ ¾ On the other hand, if b is the number of edges, then — as already observed in Theorem 1 — we have = b ·½ Ò . But also Ò = b ·½ = Ò ·¾Ò ·½ ½ ¾ Comparing the last two displayed equations we prove out theorem. Ò =¾ Ò =½ Ò =¿ Ò = Ò ·½ ½ ¼ ¼ ¾ In Figure 2 the reader can verify that ¾ , and , hence as predicted by Theorem 3. Theme 2: Tree Traversals Trees are often used to store information. In order to retrieve such information we need a procedure to visit all nodes of a tree. We describe here three such procedures called inorder, postorder and preorder traversals. Throughout this section we assume that trees are ordered trees (from left to right). Ì Ì ;Ì ;::: Ì ½ ¾ Definition.Let be an (ordered) rooted tree with d subtrees of the root. Ì 1. If Ì is null, then the empty list is preorder, inorder and postorder traversal of . Ì 2. If Ì consists of a single node, then that node is preorder, inorder and postorder traversal of . Ì ;Ì ;::: Ì ½ ¾ 3. Otherwise, let d be nonempty subtrees of the root. 4 start end T1 T2 T3 T1 T2 T3 T1 T2 T3 end start pre-order in-order post-order Figure 3: Illustration to preorder, inorder and postorder traversals. Ì Ì ¯ The preorder traversal of nodes in is the following: the root of followed by the Ì Ì ::: Ì ½ ¾ nodes of in preorder, then nodes of in preorder traversal, , followed by d in preorder (cf. Figure 3). ¯ Ì Ì The inorder traversal of nodes in is the following: nodes of ½ in inorder, followed Ì Ì ;Ì ;::: ;Ì ¾ ¿ by the root of , followed by the nodes of d in inorder (cf. Figure 3). ¯ Ì Ì The postorder traversal of nodes in is the following: nodes of ½ in postorder, fol- Ì ;::: ;Ì ¾ lowed by d in postorder, followed by the root (cf. Figure 3). Ì A Ì B Ì ¾ Example 2: Let us consider the tree in Figure 1. The root has three subtrees ½ rooted at , C Ì D rooted at , and subtree ¿ rooted at .Thepreorder traversal is fA; B ; E ; à ; Ä; F ; C ; G; D ; À ; Å ; Á ;  g preorder of T = A Ì B Ì Ì ½ ¾ since after the root we visit first ½ (so we list the root and visit subtrees of ), then subtree C Ì D rooted at and its subtrees, and finally we visit the subtree ¿ rooted and its subtrees. The inorder traversal of Ì is fà ; E ; Ä; B ; F ; A; G; C ; Å ; À ; D ; Á ;  g inorder of T = Ì B Ì ¾ since we first must traverse inorder the subtree ½ rooted at . But inorder traversal of starts by à à traversing in inorder the subtree rooted at E , which in turn must start at .Since is a single node, we list it. Then we move backward and list the root, which is E , and move to the right subtree that B A turns out to be a single node Ä.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us