0% found this document useful (0 votes)
26 views10 pages

Binary Trees 2

The document discusses tree traversal algorithms and tree transformations. It describes three tree traversal algorithms: preorder, inorder, and postorder. It also discusses binary search trees and how rotations can be used to transform a binary search tree while preserving the search tree property and inorder traversal. Rotations can be either left or right rotations about nodes in the tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Binary Trees 2

The document discusses tree traversal algorithms and tree transformations. It describes three tree traversal algorithms: preorder, inorder, and postorder. It also discusses binary search trees and how rotations can be used to transform a binary search tree while preserving the search tree property and inorder traversal. Rotations can be either left or right rotations about nodes in the tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 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