struktur data Tugas Pertemuan 13 dan 14
Kerjakan tugas terakhir ini Denham sebaik-baiknya sesuai kelompok yang telah terbentuk. Adapun tugas kelompok ini adalah membuat program aplikasi menggunakan pemrograman Phyton untuk kasus:
A. Penyelesaian masalah pada struktur pohon (spt : bobot, ketinggian, derajat, root, dlll)
B. Penyelesaian masalah kunjungan pada pohon biner (preorder, inorder dan postorder)
C. Penyelesaian masalah pada graph (shortest path, minimum spanning tree, critical path)
A. struktur pohon
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
def PrintTree(self):
if self.left:
self.left.PrintTree()
print(self.data),
if self.right:
self.right.PrintTree()
# masukan node di sini
root = Node(12)
root.insert(10)
root.insert(5)
root.insert(9)
root.PrintTree()
B B. PREORDER, INORDER, POSTORDER
from __future__ import print_function
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, items):
self.items.append(items)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)
class Binarytree:
def __init__(self, root):
self.key = root
self.leftChild = None
self.rightChild = None
def insertLeft(self, newNode):
if self.leftChild == None:
self.leftChild = Binarytree(newNode)
else:
t = Binarytree(newNode)
t.leftChild = self.leftChild
self.leftChild = t
def insertRight(self, newNode):
if self.rightChild == None:
self.rightChild = Binarytree(newNode)
else:
t = Binarytree(newNode)
t.rightChild = self.rightChild
self.rightChild = t
def getrightChild(self):
return self.rightChild
def getleftChild(self):
return self.leftChild
def getrootVal(self):
return self.key
def setrootVal(self, obj):
self.key = obj
def size(self):
count = 0
selfleft = self
selfright = self
while selfleft.getleftChild() != None or selfright.getrightChild() != None:
count += 1
if selfleft.getleftChild() != None:
selfleft = selfleft.getleftChild()
else:
selfright = selfright.getrightChild()
return count
def Inorder(root):
if root:
Inorder(root.getleftChild())
print(root.getrootVal(), end=" ")
Inorder(root.getrightChild())
def Preorder(root):
if root:
print(root.getrootVal(), end=" ")
Preorder(root.getleftChild())
Preorder(root.getrightChild())
def Postorder(root):
if root:
Postorder(root.getleftChild())
Postorder(root.getrightChild())
print(root.getrootVal(), end=" ")
#buat masukin node
root = Binarytree('P')
root.insertLeft('F')
root.getleftChild().insertLeft('B')
root.getleftChild().insertRight('H')
root.getleftChild().getleftChild().insertLeft('A')
root.getleftChild().getrightChild().insertRight('M')
root.insertRight('S')
root.getrightChild().insertLeft("R")
root.getrightChild().insertRight("W")
Inorder(root)
print()
Preorder(root)
print()
Postorder(root)
Komentar
Posting Komentar