Tuesday, March 12, 2013

tree traversal

  • tree traversal

tree traversal refers to the process of visiting (examining and/or updating) each node in a tree data structure,
Such traversals are classified by the order in which the nodes are visited.



Although linked lists require more memory space than arrays ( as they have to store address at each node),
they have definite advantages  over arrays.
Insertion and deletion of items can be carried out with out involving considerable movement of data.
http://www.cs.ucf.edu/courses/cop3502h.02/trees1.pdf




  • Tree Traversals

Linked lists are traversed sequentially from first node to the last node.
However, there is no such natural linear order for the nodes of a tree.
Different orderings are possible for traversing a binary tree.
Every node in the tree is a root for the subtree that it points to.

There are three common traversals for binary trees:
• Preorder
• Inorder
• Postorder

These names are chosen according to the sequence in which the root node and its children are visited.

http://www.cs.ucf.edu/courses/cop3502h.02/trees1.pdf





  • Tree Traversals

There are 3 common tree traversals.
1. in-order: left, root, right
2. pre-order: root, left, right
3. post-order: left, right, root

Note "pre" and "post" refer to when we visit the root.
(we always go from left to right throug leaves but when it is in-order root is in the middle,pre-order it's first and post-order it's last.)

The in-order traversal always prints the values in sorted order from smallest to largest.
http://www.math.ucla.edu/~wittman/10b.1.10w/Lectures/Lec18.pdf



  • Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph.

One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.
http://en.wikipedia.org/wiki/Depth-first_search


  • In graph theory, breadth-first search (BFS) is a strategy for searching in a graph when search is limited to essentially two operations: 

(a) visit and inspect a node of a graph; (b) gain access to visit the nodes that neighbor the currently visited node.
The BFS begins at a root node and inspects all the neighboring nodes
http://en.wikipedia.org/wiki/Breadth-first_search




  • There are three types of depth-first traversal: pre-order, in-order and post-order. For a binary tree, they are defined as operations recursively at each node, starting with the root node follows:


Pre-order:
Visit the root.
Traverse the left subtree.
Traverse the right subtree.

In-order (symmetric):
Traverse the left subtree.
Visit the root.
Traverse the right subtree.

Post-order:
Traverse the left subtree.
Traverse the right subtree.
Visit the root.

http://en.wikipedia.org/wiki/Tree_traversal



  • infix traversal gives an infix expression

pre-order traversal gives an pre-order expression
post-order traversal gives an post-order expression

infix,pre-order,post-order traversal example

http://www.cs.nuim.ie/~rosemary/se202/treenotes/sld029.htm



  • infix,pre-order,post-order traversal example

http://www.cs.cuw.edu/csc/csc300/PowerPoint/TREE%20TRAVERSAL%20EXAMPLE.htm




  • D = node, L = left, R =right


• Preorder (DLR) traversal yields: A, H, G, I,F, E, B, C, D

• Postorder (LRD) traversal yields: G, F, E, I,H, D, C, B, A

• In-order (LDR) traversal yields: G, H, F, I,E, A, B, D, C

• Level-order traversal yields: A, H, B, G, I,C, F, E, D

https://docs.google.com/viewer?a=v&q=cache:tUIMUu37Z0oJ:www.cs.siu.edu/~mengxia/Courses%2520PPT/220/finalreview.ppt+&hl=tr&gl=tr&pid=bl&srcid=ADGEEShcYilAqNqch2AN6p2yyA1XFccuhVzGrCzXYljzzrgoZdqR87OAqbgN_osKxdp3B4Jgy_KkgYCHvIImna7DTFXFxruRbv2o2yIOiBqzf6nd-sjahVvFsq0NOpRuZEjA3MrdXXMr&sig=AHIEtbRCSWKbeBnN-GpbuR0Kj_GGrNrgUA





  • Inorder Traversal - LDR


The node’s left subtree is traversed
The node’s data is processed
The node’s right subtree is traversed

Preorder Traversal - DLR

The node’s data is processed
The node’s left subtree is traversed
The node’s right subtree is traversed

Postorder Traversal - LRD

The node’s left subtree is traversed
The node’s right subtree is traversed
The node’s data is processed

https://docs.google.com/viewer?a=v&q=cache:962RB0q-kPkJ:digital.cs.usu.edu/~allan/CS2/Slides/Ch20.pdf+&hl=tr&gl=tr&pid=bl&srcid=ADGEEShPsArt6bX6XAwSHqxzO9ueMEXYOczpYtzLEzcF_Ynxbjqt6S1oSfU6KZShCTWPLazYXtcZsAoDP-iJXq5Ajh_cxtYcXFFOlTGyvNt_1NJ3I4kv2c8oTNYkz372sJqPZFI2kVG0&sig=AHIEtbRfU_jyEsuIyuN6vdEPyzXhoCCEtA




No comments:

Post a Comment