1 / 19

Data Structures

Data Structures. Binary Trees. Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST). Visiting and Traversing a Node. Many applications require that all of the nodes of a tree be “ visited ”.

hesper
Download Presentation

Data Structures

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. Data Structures Binary Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)

  2. Visiting and Traversing a Node • Many applications require that all of the nodes of a tree be “visited”. • Visiting a node may mean printing contents, retrieving information, making a calculation, etc. • Traverse: To visit all the nodes in a tree in a systematic fashion. • A traversal can pass through a node without visiting it at that moment.

  3. Depth First and Breadth First Traversal • Depth-first traversals: using the top-down view of the tree structure. Traverse the root, the left subtree and the right subtree. • Breadth-first or level-order traversal: visit nodes in order of increasing depth. For nodes of same depth visit in left-to-right order.

  4. Traversal strategies • Preorder traversal • Work at a node is performed before its children are processed. • Postorder traversal • Work at a node is performed after its children are processed. • Inorder traversal • For each node: • First left child is processed, then the work at the node is performed, and then the right child is processed.

  5. Tree Traversal Types

  6. Preorder traversal • In the preorder traversal, the root node is processed first, followed by the left subtree and then the right subtree. Preorder = root node of each subtreebefore the subsequent left and right subtrees.

  7. Preorder Traversal

  8. Preorder Traversal A B C D G E H J I K • Visit root before traversing subtrees. F

  9. Inorder Traversal • In the inorder traversal, the left subtree is processed first, followed by the root node, and then the right subtree. Inorder = root node in betweenthe left and right subtrees.

  10. Inorder Traversal

  11. Inorder Traversal • In an inorder traversal a node is visited after its left subtree and before its right Subtree • Application: draw a binary tree or Arithmetic expression printing ((2 × (a − 1)) + (3 × b))

  12. Example of Binary Tree (inorder)

  13. Inorder Traversal A B C D G E H J I K • Visit root between left and right subtree. F

  14. y Postorder traversal • In the postorder traversal, the left subtree is processed first, followed by the right subtree, and then the root node. Postorder = root node after the left and right subtrees.

  15. Postorder traversal Postorder Traversal

  16. yyyyyyyy Postorder traversal • In a postorder traversal, a node is visited after its descendants • Application: compute space used by files in a directory and its subdirectories

  17. Postorder traversal A B C D G E H J I K • Visit root after traversing subtrees. F

  18. Expression Trees + + × a × + g b c × f d e Expression tree for ( a + b × c) + ((d ×e + f) × g) There are three notations for a mathematical expression: 1) Infix notation : ( a + (b × c)) + (((d ×e) + f) × c) 2) Postfix notation: a b c × + d e × f + g * + 3) Prefix notation : + + a × b c × + × d e f g

  19. Expression Tree traversals • Depending on how we traverse the expression tree, we can produce one of these notations for the expression represented by the three. • Inorder traversal produces infix notation. • This is a overly parenthesized notation. • Print out the operator, then print put the left subtree inside parentheses, and then print out the right subtree inside parentheses. • Postorder traversal produces postfix notation. • Print out the left subtree, then print out the right subtree, and then printout the operator. • Preorder traversal produces prefix notation. • Print out the operator, then print out the right subtree, and then print out the left subtree.

More Related