Tree
Tree
C D E F
H I J K L M
P Q
Tree representation
A Root
C D E F
H I J K L M
Subtree with Q
P
single node Subtree
with root F
Subtree with
root D
A Level 0
Root
Parent
Child Level 1
C D E
Siblings B
Leaves
F G H Level 2
I Level 3
Tree Terminology
A Level 0
Root
Parent
Child Level 1
C D E
Siblings B
Leaves
F G H Level 2
A is ________
I Level 3
A is the Parent of __________
_______ are Child of D
F and G are_________
_______________ are leaves
Tree Terminology
The depth of a node is the number of edges from the root to the
node.
The height of a node is the number of edges from the node to
the deepest leaf.
A Level 0
Level 1
C D E
B
F G H Level 2
Depth of H (or level of H) is ______
Height of Tree is _______
I Level 3
The Degree of node D is ____
_________ are External
________ are internal node of Tree
Binary Trees
Binary Tree
Binary Tree:
Ordered tree with all nodes having at most two children
Some examples
A
B C
D E F
G H I
Binary tree
Structures which are not binary trees
A
B C
D E F
G I
Various binary trees
2 B 3 C
4 D 5 E 6 F 7 G
8 H 9 I J 10
B C
D E
F G
A ________ BINARY TREE OF DEPTH ____
B C
D E G F
H I J K L M N O
Complete Binary Tree
• Finding duplicates
• Traversing a binary tree
• Searching
Binary Tree Traversals
Tree Traversal
• Preorder traversal
• Inorder traversal
• Postorder traversal
Steps:
B C
D E F G
H I
Traversing a Tree Preorder
B C
D E F G
H I
Result: A
Traversing a Tree Preorder
B C
D E F G
H I
Result: AB
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABD
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDE
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEH
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHC
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCF
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCFG
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCFGI
Inorder Traversal (symmetric order)
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Result: D
Traversing a Tree Inorder
B C
D E F G
H I
Result: DB
Traversing a Tree Inorder
B C
D E F G
H I
Result: DB
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBH
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHE
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEA
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEA
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEAF
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEAFC
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEAFCG
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEAFCGI
Postorder Traversal
Steps:
B C
D E F G
H I
Traversing a Tree Postorder
B C
D E F G
H I
Result:
Traversing a Tree Postorder
B C
D E F G
H I
Result:
Traversing a Tree Postorder
B C
D E F G
H I
Result: D
Traversing a Tree Postorder
B C
D E F G
H I
Result: D
Traversing a Tree Postorder
B C
D E F G
H I
Result: DH
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHE
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEB
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEB
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBF
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBF
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBFI
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBFIG
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBFIGC
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBFIGCA
Tree Traversal – Example 1
A
B C
D E F
G H I
Preorder: ABDGCEHIF
Inorder: DGBAHEICF
Postorder:
GDBHIEFCA
Tree Traversal – Example 2
A
Preorder: ABCEIFJDGHKL
Inorder: EICFJBGDKHLA
Postorder: IEJFCGKLHDBA
B
C D
G H
E F
K L
I J
Preorder, Postorder and Inorder
Level Order Traversal
B C
D E F G
queue
H I
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
A
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
A
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
BC
A
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
C
A B
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
CDE
A B
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
DEFG
A B C
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
EFGH
A B C D
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
FGH
A B C D E
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
GHI
A B C D EF
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
HI
A B C D E F G
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
I
A B C D E F GH
Level Order Traversal
• Start at the root
• Visit the nodes at each level, from left to right
B C
D E F G
H I
A B C D E F G H I
Level Order Traversal
B C
D E F G
H I
• The right child is pushed before the left child to make sure
that left subtree is processed first.
Non-Recursive In-order
• Inorder sequence: D B E A F C
• Preorder sequence: A B D E C F
Pre order and In order
• Inorder sequence: D B E A F C
Preorder sequence: A B D E C F
Pre order and In order
• Inorder sequence: D B E A F C
Preorder sequence: A B D E C F
• Pre order: 1, 2, 4, 3, 5, 7, 8,6
• In order: 4, 2, 1, 7, 5, 8, 3, 6
• Pre order: 1, 2, 4, 3, 5, 7, 8,6
• In order: 4, 2, 1, 7, 5, 8, 3, 6
• Pre order: 1, 2, 4, 3, 5, 7, 8,6
• In order: 4, 2, 1, 7, 5, 8, 3, 6
Post order and In order
inOrder = { 4, 2, 5, 1, 6, 3, 7 };
postOrder = { 4, 5, 2, 6, 7, 3, 1 };
Level order and In order