File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Node:
2
+ def __init__(self, data):
3
+ self.left = None
4
+ self.right = None
5
+ self.data = data
6
+ # Insert Node
7
+ def insert(self, data):
8
+ if self.data:
9
+ if data < self.data:
10
+ if self.left is None:
11
+ self.left = Node(data)
12
+ else:
13
+ self.left.insert(data)
14
+ else data > self.data:
15
+ if self.right is None:
16
+ self.right = Node(data)
17
+ else:
18
+ self.right.insert(data)
19
+ else:
20
+ self.data = data
21
+ # Print the Tree
22
+ def PrintTree(self):
23
+ if self.left:
24
+ self.left.PrintTree()
25
+ print( self.data),
26
+ if self.right:
27
+ self.right.PrintTree()
28
+ # Inorder traversal
29
+ # Left -> Root -> Right
30
+ def inorderTraversal(self, root):
31
+ res = []
32
+ if root:
33
+ res = self.inorderTraversal(root.left)
34
+ res.append(root.data)
35
+ res = res + self.inorderTraversal(root.right)
36
+ return res
37
+ root = Node(27)
38
+ root.insert(14)
39
+ root.insert(35)
40
+ root.insert(10)
41
+ root.insert(19)
42
+ root.insert(31)
43
+ root.insert(42)
44
+ print(root.inorderTraversal(root))
You can’t perform that action at this time.
0 commit comments