yuku@
yuku
: 2012-05-03 08:50:10 UTC+0000
Pos kode piton deh.
############################### pembuatan node
null = None
class Node:
def __init__(self, l, d, r):
self.d = d
self.l = l
self.r = r
if l != null: l.p = self
if r != null: r.p = self
def __repr__(self):
return str(self.d)
######################################## algo yang kita bikin
def max(n):
if n.r != null:
return max(n.r)
return n
def secondmax(n):
m = max(n)
if m.l == null: return m.p
return max(m.l)
###################### algo eri
def kedua(root):
while root.r != null:
root = root.r
if root.l != null:
root = root.l
if root.r == null:
return root
else:
while root.r != null:
root = root.r
return root
else:
return root.p
################################################ untuk ngetes betul ngga
def coba(t):
print 'max =', max(t), ', secondmax =', secondmax(t)
print 'kedua =', kedua(t)
root1 = Node(Node(null, 3, Node(null, 4, null)), 5, Node(null, 6, Node(null, 7, null)))
coba(root1)
root2 = Node(null, 1, Node(null, 2, Node(null, 3, Node(null, 4, null))))
coba(root2)
root3 = Node(null, 1, Node(null, 2, Node(null, 3, Node(Node(null, 8, null), 9, null))))
coba(root3)
root4 = Node(null, 1, Node(null, 2, Node(null, 3, Node(Node(null, 8, Node(null, 8.5, null)), 9, null))))
coba(root4)