Skip to content

Commit 527328b

Browse files
Merge pull request #202 from motasimmakki/motasim
feat: Inorder, Preorder, and Postorder Traversal of Tree
2 parents 1b6c944 + 23264b6 commit 527328b

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed
16.4 KB
Loading
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
# Inorder, Preorder, and Postorder Traversal of Tree
3+
4+
## Aim:
5+
The aim is to write a python code to show Inorder, Preorder, and Posrorder Traversal of Tree.
6+
7+
## Purpose:
8+
The purpose is to come up with an recursive solution of traversals.
9+
10+
## Short description of progarm:
11+
The project defines and form a binary tree,
12+
which then is passed to the functions, to print the respective traversal of the tree.
13+
14+
## Workflow of the program:
15+
Description of functions used in the code and their purpose:
16+
17+
Node --> Class, to define node of the binary tree, has data, left, and right as attributes
18+
19+
inorder --> Function, takes in tree root, and prints the Inorder Traversal of the Tree.
20+
21+
preorder --> Function, takes in tree root, and prints the Preorder Traversal of the Tree.
22+
23+
postorder --> Function, takes in tree root, and prints the Postorder Traversal of the Tree.
24+
25+
## Required libraries:
26+
None
27+
28+
## Compilation Steps:
29+
Run the script, after that:
30+
31+
1. The tree is made manually here, to focus on traversal part.
32+
2. The program is then run to print the Inorder, Preorder, and Postorder traversal of the tree.
33+
34+
## Conclusion:
35+
There are three standard traversal for traversing in a Binary tree:
36+
37+
1. Inorder,
38+
2. Preorder, and
39+
3. Postorder
40+
41+
If we consider 'A' as the left child, '+' as the root node, and 'B' as the right child, then
42+
-> In Inorder Traversal, the traversing is done in the A+B fashion.
43+
-> In Preorder Traversal, the traversing is done in the +AB fashion.
44+
-> In Postorder Traversal, the traversing is done in the AB+ fashion.
45+
46+
# Output
47+
![Output](./Images/output.png)
48+
49+
<p align = "center">--- Contributed with 🧡 by <a href = "https://github.com/motasimmakki">Motasim</a> ---</p>
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
'''
2+
Inorder Traversal of Binary Tree:
3+
In the Inorder Traversal first the left node of the tree will be printed,
4+
than the root node, and at end right node will be printed for every Sub-tree,
5+
Just like: (A+B)
6+
7+
Preorder Traversal of Binary Tree:
8+
In the Preorder Traversal first the left node of the tree will be printed,
9+
than the right node, and at end root node will be printed for every Sub-tree,
10+
Just like: (AB+)
11+
12+
Postorder Traversal of Binary Tree:
13+
In the Inorder Traversal first the root node of the tree will be printed,
14+
than the left node, and at end right node will be printed for every Sub-tree,
15+
Just like: (+AB)
16+
'''
17+
# Defination of Class Node.
18+
class Node:
19+
# Defination of Node class constructor.
20+
def __init__(self, data: int):
21+
self.left = None
22+
self.right = None
23+
self.data = data
24+
25+
# inorder() function defination,
26+
# that will print the Inorder Traversal of given tree.
27+
def inorder(self, root: object)->None:
28+
if(root):
29+
root.inorder(root.left)
30+
print("[", root.data,"]", end ="\t")
31+
root.inorder(root.right)
32+
33+
# preorder() function defination,
34+
# that will print the Preorder Traversal of given tree.
35+
def preorder(self, root: object)->None:
36+
if(root):
37+
print("[", root.data,"]", end ="\t")
38+
root.preorder(root.left)
39+
root.preorder(root.right)
40+
41+
# Postorder() function defination,
42+
# that will print the Postorder Traversal of given tree.
43+
def postorder(self, root: object)->None:
44+
if(root):
45+
root.postorder(root.left)
46+
root.postorder(root.right)
47+
print("[", root.data,"]", end ="\t")
48+
49+
50+
'''
51+
Structure of Tree:
52+
53+
Test Case 01:
54+
11
55+
/ \
56+
9 15
57+
/ \ / \
58+
7 10 13 20
59+
/ \
60+
5 8
61+
62+
Test Case 02:
63+
10
64+
/ \
65+
20 30
66+
/ \
67+
40 50
68+
/ \
69+
60 70
70+
'''
71+
# main function or the driver function.
72+
if __name__ == '__main__':
73+
'''
74+
Formation of tree,
75+
as per Test Case 01.
76+
'''
77+
root = Node(11)
78+
root.left = Node(9)
79+
root.right = Node(15)
80+
root.left.left = Node(7)
81+
root.left.right = Node(10)
82+
root.right.left = Node(13)
83+
root.right.right = Node(20)
84+
root.left.left.left = Node(5)
85+
root.left.left.right = Node(8)
86+
87+
print("\n***Running Test Case 01***", end = " ")
88+
# Printing the Inorder Traversal for Test Case 01.
89+
print("\nInorder Traversal :")
90+
root.inorder(root)
91+
# Printing the Preorder Traversal for Test Case 01.
92+
print("\n\nPreorder Traversal :")
93+
root.preorder(root)
94+
# Printing the Postorder Traversal for Test Case 01.
95+
print("\n\nPostorder Traversal :")
96+
root.postorder(root)
97+
98+
'''
99+
Formation of tree,
100+
as per Test Case 02.
101+
'''
102+
root = Node(10)
103+
root.left = Node(20)
104+
root.right = Node(30)
105+
root.right.left = Node(40)
106+
root.right.right = Node(50)
107+
root.right.right.left = Node(60)
108+
root.right.right.right = Node(70)
109+
110+
print("\n\n***Running Test Case 02***", end = " ")
111+
# Printing the Inorder Traversal for Test Case 01.
112+
print("\nInorder Traversal :")
113+
root.inorder(root)
114+
# Printing the Preorder Traversal for Test Case 01.
115+
print("\n\nPreorder Traversal :")
116+
root.preorder(root)
117+
# Printing the Postorder Traversal for Test Case 01.
118+
print("\n\nPostorder Traversal :")
119+
root.postorder(root)
120+
121+
'''
122+
Sample Output:
123+
124+
***Running Test Case 01***
125+
Inorder Traversal :
126+
[ 5 ] [ 7 ] [ 8 ] [ 9 ] [ 10 ] [ 11 ] [ 13 ] [ 15 ] [ 20 ]
127+
128+
Preorder Traversal :
129+
[ 11 ] [ 9 ] [ 7 ] [ 5 ] [ 8 ] [ 10 ] [ 15 ] [ 13 ] [ 20 ]
130+
131+
Postorder Traversal :
132+
[ 5 ] [ 8 ] [ 7 ] [ 10 ] [ 9 ] [ 13 ] [ 20 ] [ 15 ] [ 11 ]
133+
134+
***Running Test Case 02***
135+
Inorder Traversal :
136+
[ 20 ] [ 10 ] [ 40 ] [ 30 ] [ 60 ] [ 50 ] [ 70 ]
137+
138+
Preorder Traversal :
139+
[ 10 ] [ 20 ] [ 30 ] [ 40 ] [ 50 ] [ 60 ] [ 70 ]
140+
141+
Postorder Traversal :
142+
[ 20 ] [ 40 ] [ 60 ] [ 70 ] [ 50 ] [ 30 ] [ 10 ]
143+
'''

Traversals/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
## Check out all the amazing scripts for 'Traversals' here:
12

3+
- [Inorder, Preorder, and Postorder Traversal of Tree](Inorder%20Preorder%20Postorder/inorder_preorder_postorder.py)

0 commit comments

Comments
 (0)