Professional Documents
Culture Documents
Data Structure Tree - 3
Data Structure Tree - 3
Representation
Array representation
• can easily determine the locations of the parent, left
child, and right child of any node
2
Array representation (Con
3
Linked representation
• thestruct
typedef linkednoderepresentation
*treePointer;
typedef struct node {
int data;
treePointer leftChild,
rightChild;
}; 4
Linked representation (Co
/* global variable */
treePointer root;
5
Binary tree traversal
Introduction
7
Inorder traversal
8
Inorder traversal (Cont
- Left subtree root right subtree
9
Preorder traversal
• visit a node, traverse left, and continue
• if we cannot continue, move right and begin again or
move back
10
Postorder traversal
11
Iterative inorder traversal
• to implement the tree traversal method
without using
recursion by using iterators
12
Level-Order Traversal
• a traversal that requires a queue
– visit the root first, then the root's left child,
followed by the
root's right child
13
Traversal without a stack
• to add a parent field to each node
– can trace way back up to any root and down
again