CSPC24 Chapter 2 - Data Structures
CSPC24 Chapter 2 - Data Structures
Algorithm and
Complexity
Data Structures
Objectives
• Define and explain Data Structure.
Preorder(root):
1. Follow step 2 to 4 until root != NULL
2. Write root -> data
3. Preorder (root -> left)
4. Preorder (root -> right)
5. End loop
How does Preorder Traversal of Binary
Tree work?
Consider the following tree:
2 3
4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 1: At first the root will be visited, i.e. node 1.
2 3
4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 2: After this, traverse in the left subtree. Now the root of the
left subtree is visited i.e., node 2 is visited.
2 3
4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 3: Again the left subtree of node 2 is traversed and the root
of that subtree i.e., node 4 is visited.
2 3
4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 4: There is no subtree of 4 and the left subtree of node 2 is
visited. So now the right subtree of node 2 will be traversed and
the root of that subtree i.e., node 5 will be visited.
1
2 3
4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 5: The left subtree of node 1 is visited. So now the right
subtree of node 1 will be traversed and the root node i.e., node 3 is
visited.
1
2 3
4 5 6
How does Preorder Traversal of Binary
Tree work?
Step 6: Node 3 has no left subtree. So the right subtree will be traversed and
the root of the subtree i.e., node 6 will be visited. After that there is no node that
is not yet traversed. So the traversal ends.
2 3
4 5 6
Inorder(root):
1. Follow step 2 to 4 until root != NULL
2. Inorder (root -> left)
3. Write root -> data
4. Inorder (root -> right)
5. End loop
How does Inorder Traversal of Binary
Tree work?
Consider the following tree:
2 3
4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its
left subtree root, i.e., 4. Now 4 has no left subtree, so it will be visited. It also
does not have any right subtree. So no more traversal from 4
2 3
4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 2: As the left subtree of 2 is visited completely, now it read data of node 2
before moving to its right subtree.
2 3
4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 3: Now the right subtree of 2 will be traversed i.e., move to node 5. For
node 5 there is no left subtree, so it gets visited and after that, the traversal
comes back because there is no right subtree of node 5.
2 3
4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be
visited.
2 3
4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 5: Left subtree of node 1 and the node itself is visited. So now the right
subtree of 1 will be traversed i.e., move to node 3. As node 3 has no left subtree
so it gets visited.
2 3
4 5 6
How does Inorder Traversal of Binary
Tree work?
Step 6: The left subtree of node 3 and the node itself is visited. So traverse to
the right subtree and visit node 6. Now the traversal ends as all the nodes are
traversed.
2 3
4 5 6
Postorder(root):
1. Follow step 2 to 4 until root != NULL
2. Postorder (root -> left)
3. Postorder (root -> right)
4. Write root -> data
5. End loop
How does Postorder Traversal of Binary
Tree work?
Consider the following tree:
2 3
4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its
left subtree root, i.e., 4. Now 4 has no subtree, so it will be visited.
2 3
4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 2: As the left subtree of 2 is visited completely, now it will traverse the
right subtree of 2 i.e., it will move to 5. As there is no subtree of 5, it will be
visited.
2 3
4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 3: Now both the left and right subtrees of node 2 are visited. So now visit
node 2 itself.
2 3
4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 4: As the left subtree of node 1 is traversed, it will now move to the right
subtree root, i.e., 3. Node 3 does not have any left subtree, so it will traverse the
right subtree i.e., 6. Node 6 has no subtree and so it is visited.
2 3
4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 5: All the subtrees of node 3 are traversed. So now node 3 is visited.
2 3
4 5 6
How does Postorder Traversal of Binary
Tree work?
Step 6: As all the subtrees of node 1 are traversed, now it is time for node 1 to
be visited and the traversal ends after that as the whole tree is traversed.
2 3
4 5 6