You are on page 1of 10

Data Structures and Algorithms

Searching - 2
Tree Traversals
Tree Traversal
• Traversal = visiting every node of a tree
• Three basic alternatives
 Pre-order

• Root
• Left sub-tree 
• Right sub-tree



x A +x+BC xDE F

L R
L R L
Tree Traversal
• Traversal = visiting every node of a tree
• Three basic alternatives
 In-order
• Left sub-tree 
• Root  
• Right sub-tree
 11
 
Ax B+C xDxE +F  
 
L
L R
Tree Traversal
• Traversal = visiting every node of a tree
• Three basic alternatives
 Post-order
• Left sub-tree 11
• Right sub-tree  
• Root


A B C+ D Exx F+x  

L    
L R
Tree Traversal
 Post-order 11
• Left sub-tree
 
• Right sub-tree
• Root 
 Reverse-Polish

 
(A (((BC+)(DEx) x) F +)x )
   
• Normal algebraic form
(A x(((B+C)(DxE))+F))
Trees - Searching
• Binary search tree
• in-order traversal produce sorted list

• In order: ADE G HKL M NOP T V


Trees - Transformations
• Binary search tree
• Preserving the order
• Observe that this transformation preserves the
search tree
Trees - Transformations
• Binary search tree
• Preserving the order
• Observe that this transformation preserves the
search tree

• We’ve performed a rotation of the sub-tree


about the T and O nodes
Trees - Rotations
• Binary search tree
• Rotations can be either left- or right-rotations

• For both trees: the inorder traversal is


AxByC
Trees - Rotations
• Binary search tree
• Rotations can be either left- or right-rotations

• Note that in this rotation, it was necessary to move


B from the right child of x to the left child of y

You might also like