Professional Documents
Culture Documents
Alva
Alva
Disusun oleh:
Alvarizqky putra K.R (2206109)
TEKNIK INFORMATIKA
INSTITUT TEKNOLOGI GARUT
2022
1. CODE PROGRAM
class Tree:
def __init__(self, info):
self.info = info
self.left = None
self.right = None
class BinaryTree:
def __init__(self):
self.root = None
2
self.root = self._delete(self.root, key)
temp = self._find_min(node.right)
node.info = temp.info
node.right = self._delete(node.right, temp.info)
return node
def preorder_traversal(self):
self._preorder_traversal(self.root)
3
def _preorder_traversal(self, node):
if node is not None:
print(node.info, end=" ")
self._preorder_traversal(node.left)
self._preorder_traversal(node.right)
def inorder_traversal(self):
self._inorder_traversal(self.root)
def postorder_traversal(self):
self._postorder_traversal(self.root)
if __name__ == '__main__':
bt = BinaryTree()
while True:
print("1. Insert")
print("2. Delete")
4
print("3. Preorder Traversal")
print("4. Inorder Traversal")
print("5. Postorder Traversal")
print("6. Exit")
if choice == 1:
n = int(input("Enter item: "))
bt.insert(n)
elif choice == 2:
key = int(input("Enter element to delete: "))
bt.delete(key)
elif choice == 3:
print()
bt.preorder_traversal()
print()
elif choice == 4:
print()
bt.inorder_traversal()
print()
elif choice == 5:
print()
bt.postorder_traversal()
print()
elif choice == 6:
break
5
2. CONSOLE