|
| 1 | +class Node: |
| 2 | + """ |
| 3 | + A Node has data variable and pointers toits left and right nodes. |
| 4 | + """ |
| 5 | + def __init__(self,data): |
| 6 | + self.left=None |
| 7 | + self.right=None |
| 8 | + self.data=data |
| 9 | + |
| 10 | +def preOrder(root): |
| 11 | + """ |
| 12 | + PreOrder traversal: visit root node then its left subtree followed by right subtree. |
| 13 | + """ |
| 14 | + if(root!=None): |
| 15 | + print(root.data,end=" ") |
| 16 | + preOrder(root.left) |
| 17 | + preOrder(root.right) |
| 18 | + |
| 19 | +def postOrder(root): |
| 20 | + """ |
| 21 | + PostOrder traversal: visit left subtree followed by right subtree and then root node. |
| 22 | + """ |
| 23 | + if(root!=None): |
| 24 | + postOrder(root.left) |
| 25 | + postOrder(root.right) |
| 26 | + print(root.data,end=" ") |
| 27 | + |
| 28 | +def inOrder(root): |
| 29 | + """ |
| 30 | + InOrder traversal: visit its left subtree followed by root node and then right subtree. |
| 31 | + """ |
| 32 | + if(root!=None): |
| 33 | + inOrder(root.left) |
| 34 | + print(root.data,end=" ") |
| 35 | + inOrder(root.right) |
| 36 | + |
| 37 | +def Height(root): |
| 38 | + """ |
| 39 | + Recursive function for calculating height of the binary tree. |
| 40 | + """ |
| 41 | + if(root==None): |
| 42 | + return 0 |
| 43 | + leftHeight=Height(root.left) |
| 44 | + rightHeight=Height(root.right) |
| 45 | + if leftHeight>rightHeight: |
| 46 | + return leftHeight+1 |
| 47 | + else: |
| 48 | + return rightHeight+1 |
| 49 | + |
| 50 | +def levelOrder1(root): |
| 51 | + """ |
| 52 | + Print whole binary tree in Level Order Traverse. |
| 53 | + Level Order traverse: Visit nodes of the tree level-by-level. |
| 54 | + """ |
| 55 | + if root==None: |
| 56 | + return |
| 57 | + temp=root |
| 58 | + que=[temp] |
| 59 | + while(len(que)>0): |
| 60 | + print(que[0].data,end=" ") |
| 61 | + temp=que.pop(0) |
| 62 | + if temp.left!=None: |
| 63 | + que.append(temp.left) |
| 64 | + if temp.right!=None: |
| 65 | + que.append(temp.right) |
| 66 | + |
| 67 | +def levelOrder2(root,level): |
| 68 | + """ |
| 69 | + Level-wise traversal: |
| 70 | + Print all nodes present at the given level of the binary tree. |
| 71 | + """ |
| 72 | + if root==None: |
| 73 | + return root |
| 74 | + if level==1: |
| 75 | + print(root.data,end=" ") |
| 76 | + elif level>1: |
| 77 | + levelOrder2(root.left,level-1) |
| 78 | + levelOrder2(root.right,level-1) |
| 79 | + |
| 80 | +def printLeftToRight(root,level): |
| 81 | + """ |
| 82 | + Print elements on particular level from left to right direction of the binary tree. |
| 83 | + """ |
| 84 | + if root==None: |
| 85 | + return |
| 86 | + if level==1: |
| 87 | + print(root.data,end=" ") |
| 88 | + elif level>1: |
| 89 | + printLeftToRight(root.left,level-1) |
| 90 | + printLeftToRight(root.right,level-1) |
| 91 | + |
| 92 | +def printRightToLeft(root,level): |
| 93 | + """ |
| 94 | + Print elements on particular level from right to left direction of the binary tree. |
| 95 | + """ |
| 96 | + if root==None: |
| 97 | + return |
| 98 | + if level==1: |
| 99 | + print(root.data,end=" ") |
| 100 | + elif level>1: |
| 101 | + printRightToLeft(root.right,level-1) |
| 102 | + printRightToLeft(root.left,level-1) |
| 103 | + |
| 104 | +def ZigZag(root): |
| 105 | + """ |
| 106 | + ZigZag traverse: Print node left to right and right to left, alternatively. |
| 107 | + """ |
| 108 | + flag=0 |
| 109 | + height=Height(root) |
| 110 | + for h in range(1,height+1): |
| 111 | + if flag==0: |
| 112 | + printLeftToRight(root,h) |
| 113 | + flag=1 |
| 114 | + else: |
| 115 | + printRightToLeft(root,h) |
| 116 | + flag=0 |
| 117 | + |
| 118 | +def main(): # Main function for testing. |
| 119 | + """ |
| 120 | + Create binary tree. |
| 121 | + """ |
| 122 | + root=Node(1) |
| 123 | + root.left=Node(2) |
| 124 | + root.right=Node(3) |
| 125 | + root.left.left=Node(4) |
| 126 | + root.left.right=Node(5) |
| 127 | + |
| 128 | + """ |
| 129 | + All Traversals of the binary are as follows: |
| 130 | + """ |
| 131 | + print("In order Traversal is : ") |
| 132 | + inOrder(root) |
| 133 | + print("\nPre order Traversal is : ") |
| 134 | + preOrder(root) |
| 135 | + print("\nPost order Traversal is : ") |
| 136 | + postOrder(root) |
| 137 | + print("\nHeight of Tree is : ") |
| 138 | + height=Height(root) |
| 139 | + print(height) |
| 140 | + print("\nComplete Level Order Traversal is : ") |
| 141 | + levelOrder1(root) |
| 142 | + print("\nLevel-wise order Traversal is : ") |
| 143 | + for h in range(1,height+1): |
| 144 | + levelOrder2(root,h) |
| 145 | + print("\nZigZag order Traversal is : ") |
| 146 | + ZigZag(root) |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
0 commit comments