-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinorder_succesor.py
More file actions
39 lines (34 loc) · 849 Bytes
/
inorder_succesor.py
File metadata and controls
39 lines (34 loc) · 849 Bytes
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
class Node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def minvalue(node):
current=node
while current is not None:
if current.left==None:
break
current=current.left
return current.data
def inorderSuccesor(root,n):
if n.right is not None:
return minvalue(n.right)
succ=None
while root!=None:
if n.data<root.data:
succ=root
root=root.left
elif(n.data>root.data):
root=root.right
else:
break
return succ
root=Node(20)
root.left=Node(8)
root.left.left=Node(4)
root.left.right=Node(12)
root.left.rig.ht.left=Node(10)
root.left.right.right=Node(14)
root.right=Node(22)
temp=root.right
print(inorderSuccesor(root,temp))