1 / 12

Session 5

Session 5. Trees - Definitions (1). An 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.

tannar
Download Presentation

Session 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. Session 5

  2. Trees -Definitions (1) An 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.

  3. 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.

  4. 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.

  5. 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

  6. 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

  7. 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

  8. 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.

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

  10. 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.

  11. 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.

  12. 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.

More Related