File tree 1 file changed +45
-0
lines changed
1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Author: OMKAR PATHAK
2
+
3
+ class Node (object ):
4
+ def __init__ (self , data = None ):
5
+ self .leftChild = None
6
+ self .rightChild = None
7
+ self .data = data
8
+
9
+ def height (node ):
10
+ if node is None :
11
+ return 0
12
+ else :
13
+ leftHeight = height (node .leftChild )
14
+ rightHeight = height (node .rightChild )
15
+
16
+ if leftHeight > rightHeight :
17
+ return leftHeight + 1
18
+ else :
19
+ return rightHeight + 1
20
+
21
+ def breadthFirstTraversal (root ):
22
+ if root == None :
23
+ return 0
24
+ else :
25
+ h = height (root )
26
+ for i in range (1 , h + 1 ):
27
+ printBFT (root , i )
28
+
29
+ def printBFT (root , level ):
30
+ if root is None :
31
+ return
32
+ else :
33
+ if level == 1 :
34
+ print (root .data , end = ' ' )
35
+ elif level > 1 :
36
+ printBFT (root .leftChild , level - 1 )
37
+ printBFT (root .rightChild , level - 1 )
38
+
39
+ if __name__ == '__main__' :
40
+ root = Node (1 )
41
+ root .leftChild = Node (2 )
42
+ root .rightChild = Node (3 )
43
+ root .leftChild .leftChild = Node (4 )
44
+
45
+ breadthFirstTraversal (root )
You can’t perform that action at this time.
0 commit comments