







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








































LECTURE 07: BINARY TREE LECTURE 07: BINARY TREE
Empty structure is an empty tree
Non-empty tree consists of a root and its children, where these children are
also trees
Empty structure is an empty tree
Non-empty tree consists of a root and its children, where these children are
also trees
Definition, Types, Traversal Definition, Types, Traversal
ļ Binary tree: each node has at most two children
Empty tree is a binary tree
Each child may be empty or designated as either left child or right child
ļ Binary tree: each node has at most two children
Empty tree is a binary tree
Each child may be empty or designated as either left child or right child
ļ Tree traversal :
Process of visiting each node in the tree exactly one time
ļ Breadth-first traversal :
Visiting each node starting from lowest (or highest) level and moving down
(or up) level by level
Visiting nodes on each level from left to right (or from right to left)
ļ Tree traversal :
Process of visiting each node in the tree exactly one time
ļ Breadth-first traversal :
Visiting each node starting from lowest (or highest) level and moving down
(or up) level by level
Visiting nodes on each level from left to right (or from right to left)
Bread-first traversal:
A, B, C, D, E, F, G, H, I
ļ Depth-first traversal:
Proceeds as far as possible to the left (or right)
Then backs up until the first crossroad
Goes one step to the right (or left)
Again, as far as possible to the left (or right)
ļ There are three tasks to be done:
V: Visiting a node
L: Traversing the left subtree
R: Traversing the right subtree
ļ Depth-first traversal:
Proceeds as far as possible to the left (or right)
Then backs up until the first crossroad
Goes one step to the right (or left)
Again, as far as possible to the left (or right)
ļ There are three tasks to be done:
V: Visiting a node
L: Traversing the left subtree
R: Traversing the right subtree
ļ Three tasks can be ordered in 3!=6 ways
ļ There are 6 possible ordered depth-first traversals
ļ It can be reduced to 3 traversals
The move is always from left to right and
Attention is focused on the first column.
ļ The 3 traversals are given these standard names
VLR: Preorder traversal (or NLR)
LVR: Inorder traversal (or LNR)
LRV: Postorder (or LRN)
ļ Three tasks can be ordered in 3!=6 ways
ļ There are 6 possible ordered depth-first traversals
ļ It can be reduced to 3 traversals
The move is always from left to right and
Attention is focused on the first column.
ļ The 3 traversals are given these standard names
VLR: Preorder traversal (or NLR)
LVR: Inorder traversal (or LNR)
LRV: Postorder (or LRN)
Preorder traversal: F, B, A, D, C, E, G, I, H
(root, left, right)
Inorder traversal: A, B, C, D, E, F, G, H, I (left,
root, right)
Postorder traversal: A, C, E, D, B, H, I, G, F
(left, right, root)
Level-order traversal (breadth-first): F, B, G,
Preorder traversal: F, B, A, D, C, E, G, I, H
(root, left, right)
Inorder traversal: A, B, C, D, E, F, G, H, I (left,
root, right)
Postorder traversal: A, C, E, D, B, H, I, G, F
(left, right, root)
Level-order traversal (breadth-first): F, B, G,
Binary tree implementationBinary tree implementation