Docstoc

Trees

Document Sample
Trees Powered By Docstoc
					                           Trees
•   trees
•   binary trees
•   tree traversal algorithms
•   data structures for trees




                                   1
                                      Trees


• a tree represents a hierarchy

examples:
  organizational structure of a corporation




        table of contents of a book


                                              2
                Another Example
•   Unix or DOS/Windows file system




                                      3
4
Binary
Trees




         5
             Examples of Binary Trees
• arithmetic expression




  • river




                                        6
             Examples of Binary Trees
• decision trees




                                        7
Properties of Binary Trees




                             8
                  Traversing Trees

There are several different methods for visiting the vertices of a
tree. Each method differs with regard to when the root of
each subtree is visited. The root may be visited first, last,
or somewhere in between the other vertices.

Hence, the three basic methods are referred to as
preorder, inorder, and postorder.




                                                               9
                         Traversing Trees
• preorder traversal – visit the root of each subtree first.
   Algorithm preOrder(v)
     “visit” node v;
     for each child w of v do preOrder(w);




                                                               10
                       Traversing Trees
• postorder traversal – visit the root of each subtree last.
   Algorithm postOrder(v)
     for each child w of v do postOrder(w);
     “visit” node v;




                                                               11
          Evaluating Arithmetic Expressions
• specialization of a postorder traversal
 Algorithm evaluateExpression(v)
         if v is an external node
             then return the variable stored at v
         else
              let op be the operator stored at v
              x  evaluateExpression(leftChild(v))
              y  evaluateExpression(rightChild(v))
              return x op y




                                                      12
                            Traversing Trees
• Inorder traversal of a binary tree
              Algorithm inOrder(v)
                      inOrder(leftChild(v));
                       “visit” node v;
                      inOrder(rightChild(v));


 • printing an arithmetic expression
      // specialization of an inorder traversal
      print “(“ before traversing the left subtree;
      print “)” after traversing the right subtree;




                                                      13
                     Euler Tour Traversal
• generic traversal of a binary tree
• preorder, inorder, and postorder are special cases of the Euler tour
  traversal
• “walk around” the tree and visit each node three times:
   – on the left
   – from below
   – on the right




                                                                 14
Linked Data Structure for Binary Trees




                                         15

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:9/3/2012
language:Unknown
pages:15