File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ class ListNode:
2
+ def __init__(self, data):
3
+ self.data = data
4
+ self.next = None
5
+ class BinaryTreeNode:
6
+ def __init__(self, data):
7
+ self.data = data
8
+ self.left = None
9
+ self.right = None
10
+ class Conversion:
11
+ def __init__(self, data = None):
12
+ self.head = None
13
+ self.root = None
14
+
15
+ def push(self, new_data):
16
+ new_node = ListNode(new_data)
17
+ new_node.next = self.head
18
+ self.head = new_node
19
+
20
+ def convertList2Binary(self):
21
+ q = []
22
+ if self.head is None:
23
+ self.root = None
24
+ return
25
+ self.root = BinaryTreeNode(self.head.data)
26
+ q.append(self.root)
27
+ self.head = self.head.next
28
+ parent = q.pop(0)
29
+ leftChild= None
30
+ rightChild = None
31
+
32
+ leftChild = BinaryTreeNode(self.head.data)
33
+ q.append(leftChild)
34
+ self.head = self.head.next
35
+ if(self.head):
36
+ rightChild = BinaryTreeNode(self.head.data)
37
+ q.append(rightChild)
38
+ self.head = self.head.next
39
+ parent.left = leftChild
40
+ parent.right = rightChild
41
+
42
+ def inorderTraversal(self, root):
43
+ if(root):
44
+ self.inorderTraversal(root.left)
45
+ print root.data,
46
+ self.inorderTraversal(root.right)
47
+
48
+ conv = Conversion()
49
+ conv.push(36)
50
+ conv.push(30)
51
+ conv.push(25)
52
+ conv.push(15)
53
+ conv.push(12)
54
+ conv.push(10)
55
+
56
+ conv.convertList2Binary()
57
+
58
+ print "Inorder Traversal of the contructed Binary Tree is:"
59
+ conv.inorderTraversal(conv.root)
You can’t perform that action at this time.
0 commit comments