1 / 22

Comp 352 – Fall 2011

Comp 352 – Fall 2011. Tutorial Session 6 October 18 & 20, 2011. Prepared by: Jinan El- Hachem. Session Outline. Trees: Definitions Methods Tree Traversal Algorithms Preorder Traversal Postorder Traversal Binary Trees: Definitions Additional Methods Properties and Implementations

charla
Download Presentation

Comp 352 – Fall 2011

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. Comp 352 – Fall 2011 Tutorial Session 6 October 18 & 20, 2011 Prepared by: Jinan El-Hachem

  2. Session Outline • Trees: • Definitions • Methods • Tree Traversal Algorithms • Preorder Traversal • Postorder Traversal • Binary Trees: • Definitions • Additional Methods • Properties and Implementations • Additional Traversal Algorithms • Inorder Traversal • Euler Tour Traversal • Problem Solving

  3. Trees -Definitions (1) A Tree is an ADT that stores its elements hierarchically. Each element in a tree has 1 parent element (with the exception of the root element) and 0 or more children elements. Two nodes that are children of the same parent are siblings. A node v is external if v has no children. A node v is internal if it has one or more children. External nodes are also known as leaves.

  4. Trees -Definitions (2) A node u is an ancestor of a node v if u = v or u is an ancestor of the parent of v. Conversely, we say that a node v is a descendent of a node u if u is an ancestor of v.. An edge of tree T is a pair of nodes (u, v) such that u is the parent of v, or vice versa. A path of T is a sequence of nodes such that any two consecutive nodes in the sequence form an edge.

  5. The Tree ADT -Methods (1) • The position object method: • element(): return the object stored at this position. • The accessor methods: • root(): return the tree's root; an error occurs if the tree is empty. • parent (v): return the parent of v; an error occurs if v is the root. • children(v): return an iterable collection containing the children of node v.

  6. The Tree ADT -Methods (2) • The query methods: • isInternal(v): Test whether node v is internal. • isExternal(v): Test whether node v is external. • isRoot(v): Test whether node v is the root. • The generic methods: • size(), isEmpty() • iterator (): return an iterator of all the elements stored at nodes of the tree. • positions(): return an iterable collection of all the nodes of the tree. • replace(v,e): replace with e and return the element stored at node v

  7. Tree Traversal AlgorithmsDepth Definition and Algorithm The depth of a node v is the number of ancestors of v, excluding v itself. The depth of a node v can also be recursively defined as follows: • If v is the root, then the depth of v is 0 • Otherwise, the depth of v is one plus the depth of the parent of v

  8. Tree Traversal AlgorithmsHeight Definition and Algorithm The height of a node v in a tree can also be recursively defined as follows: • If v is an external node, then the height of v is 0 • Otherwise, the height of v is one plus the maximum height of a child of v

  9. Tree Traversal AlgorithmsPreorder Traversal In a preorder traversal of a tree T, the root of T is visited first and then the subtrees rooted at its children are traversed recursively.

  10. Tree Traversal AlgorithmsPostorder Traversal Apostordertraversal recursively traverses the subtrees rooted at the children of the root first, and then visits the root..

  11. Binary Trees -Definitions A Binary Tree is an ordered tree having the following properties: • Every node has at most 2 children • Each child node is labeled as being either a left child or a right child. • A left child precedes a right child in the ordering of children of a node. A binary tree is proper (also referred to as full binary tree)if each node has either zero or two children. Thus, in a proper binary tree, every internal node has exactly two children. A binary tree that is not proper is improper.

  12. Binary Trees -Additional Methods A Binary Tree supports the following 4 accessor methods in addition to the regular Tree methods: • left(v): Return the left child of v; an error condition occurs if v has no left child. • right(v): Return the right child of v; an error condition occurs if v has no right child. • hasLeft(v): Test whether v has a left child. • hasRight(v): Test whether v has a right child.

  13. Binary Trees –Properties & Implementations In a Binary Tree, every level d has at most 2^d nodes (the rest of the properties on p.303) Linked structure implementation & performance (p.305) Array list representation (p.314)

  14. Binary Trees -Additional Traversal Algorithms Binary Tree-specific traversal algorithms: • The inorder traversal, informally viewed as visiting the nodes from left to right. • The Euler Tour Traversal, informally described as a “walk around” the tree, where every node is traversed 3 times (on the left, from below, and on the right)

  15. Problem Solving -R - 7.11 Draw an arithmetic expression tree that has four external nodes, storing the numbers 1, 5, 6, and 7 (with each number stored in a distinct external node, but not necessarily in this order), and has three internal nodes, each storing an operator from the set { + , − , ×, /}, so that the value of the root is 21. The operators may return and act on fractions, and an operator may be used more than once.

  16. Problem Solving -R - 7.12 Let T be an ordered tree with more than one node. Is it possible that the preorder traversal of T visits the nodes in the same order as the postorder traversal of T? If so, give an example; otherwise, argue why this cannot occur. Likewise, is it possible that the preorder traversal of T visits the nodes in the reverse order of the postorder traversal of T? If so, give an example; otherwise, argue why this cannot occur.

  17. Problem Solving -R - 7.15 Draw a (single) binary tree T such that: • Each internal node of T stores a single character • A preordertraversal of T yields EXAMFUN • An inorder traversal of T yields MAFXUEN.

  18. Problem Solving -C - 7.4 Give an O(n)-time algorithm for computing the depth of all the nodes of a tree T, where n is the number of nodes of T.

  19. Problem Solving -C - 7.5 Design algorithms for the following operations for a binary tree T: • preorderNext(v): return the node visited after node v in a preordertraversal of T • inorderNext(v): return the node visited after node v in an inordertraversal of T • postorderNext(v): return the node visited after node v in a postordertraversal of T.

  20. Problem Solving -C - 7.24 Justify the bounds in Table 7.3 by providing a detailed analysis of the running times of the methods of a binary tree T implemented with an array list, S, where S is realized by means of an array.

  21. Problem Solving -C - 7.29 Describe, in pseudo-code, a nonrecursive method for performing an in-order traversal of a binary tree in linear time.

  22. Problem Solving -C - 7.32 Let T be a binary tree with n nodes (T may be realized with an array list or a linked structure). Give a linear-time algorithm that uses the methods of the Binary Tree interface to traverse the nodes of T by increasing values of the level numbering function p given in Section 7.3.5. This traversal is known as the level order traversal.

More Related