Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 99a8184

Browse files
Created binary_tree.py & added .gitignore
1 parent c86829c commit 99a8184

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.json

‎binary_tree.py‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#binary tree using LinkedList
2+
class Node:
3+
def __init__(self, key) :
4+
self.val=key
5+
self.left=None
6+
self.right=None
7+
8+
# inorder traversal of binary tree
9+
def print_inorder(root):
10+
if root:
11+
# first recur on left child
12+
print_inorder(root.left)
13+
14+
# print data of node
15+
print(root.val)
16+
17+
# recur on the right child
18+
print_inorder(root.right)
19+
20+
def print_preorder(root):
21+
if root:
22+
# first print data of node
23+
print(root.val)
24+
25+
# recur on left child
26+
print_preorder(root.left)
27+
28+
# recur on right child
29+
print_preorder(root.right)
30+
31+
32+
def print_postorder(root):
33+
if root:
34+
# first recur on left child
35+
print_postorder(root.left)
36+
37+
# recur on right child
38+
print_postorder(root.right)
39+
40+
# print data of node
41+
print(root.val)
42+
43+
44+
root=Node(1)
45+
root.left=Node(2)
46+
root.right=Node(3)
47+
root.left.left=Node(4)
48+
root.left.right=Node(5)
49+
50+
'''
51+
# tree will be like:
52+
1
53+
54+
2 3
55+
56+
4 5
57+
58+
'''
59+
60+
print("Inorder TRaversal:")
61+
print_inorder(root)
62+
63+
print("Preorder Traversal")
64+
print_preorder(root)
65+
66+
print("Postorder Traversal")
67+
print_postorder(root)
68+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /