-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationsOnBinaryTree.py
More file actions
67 lines (64 loc) · 1.6 KB
/
OperationsOnBinaryTree.py
File metadata and controls
67 lines (64 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from binaryTree import BinaryTreeNode
import queue
from traversal import levelOrderTraversal
maximum=float("-infinity")
def findMaxRecursive(root):
global maximum
if not root:
return maximum
if root.data>maximum:
maximum=root.getData()
findMaxRecursive(root.getLeft())
findMaxRecursive(root.getRight())
return maximum
maximum=float("-infinity")
def findMaxIterative(root):
global maximum
if not root:
return maximum
q=queue.Queue()
q.put(root)
node=None
while not q.empty():
node=q.get()
if maximum<node.getData():
maximum=node.getData()
if node.getLeft() is not None:
q.put(node.getLeft())
if node.getRight() is not None:
q.put(node.getRight())
return maximum
def insertInBinaryTree(root,data):
new=BinaryTreeNode(data)
if not root:
root=new
return root
q=queue.Queue()
q.put(root)
while not q.empty():
node=q.get()
if node.getData()==data:
return root
if node.getLeft() is None:
node.left=new
return root
else:
q.put(node.getLeft())
if node.getRight() is None:
node.right=new
return root
else:
q.put(node.getRight())
a=BinaryTreeNode(3)
b=BinaryTreeNode(4)
c=BinaryTreeNode(5)
d=BinaryTreeNode(0)
a.left=b
a.right=c
b.left=d
print(findMaxRecursive(a))
print(findMaxIterative(a))
insertInBinaryTree(a,7)
l=[]
levelOrderTraversal(a,l)
print(l)