ADT - Trees - Lab - Ipynb - Colaboratory
ADT - Trees - Lab - Ipynb - Colaboratory
ipynb - Colaboratory
Exercise 1.
STEP1. Create a Stack class using your knowledge of Object Oriented Programming. This class
should have the following methods:
- pop. Remove and return the top element from the stack
- push. Add an element to the top of the stack
- len. Return the number of elements in the stack
- __init__ Allow us to initialize the Stack
- __repr__ Allow us to display the Stack
- is_empty. return True if the stack is empty
- top. return the top element of the stack without removing it
STEP2. Create an instance of that class. After that, follow these instructions:
- Use the top method to display the element that is at the topmost of the Stack.
- Use the pop method to remove and return the top element from the Stack
- Use the len method to get the length of the Stack
- Print the resulting Stack
class Stack:
def __init__(self):
self.items = []
def pop(self):
if not self.is_empty():
return self.items.pop()
def len(self):
return len(self.items)
def __repr__(self):
return repr(self.items)
https://colab.research.google.com/drive/1XJcKx3DBqz_KvrMNfUtHrLYxOFaHhWDx#printMode=true 1/7
8/11/23, 17:07 ADT_Trees_lab.ipynb - Colaboratory
def is_empty(self):
return len(self.items) == 0
def top(self):
if not self.is_empty():
return self.items[-1]
mystack = Stack()
print(mystack.is_empty())
mystack.push(3)
mystack.push(4)
mystack.push(15)
mystack.push(7)
print(mystack.top())
print(mystack.pop())
print(mystack.len())
print(mystack)
True
7
7
3
[3, 4, 15]
Exercise 2.
STEP1. Create a Queue class using your knowledge of Object Oriented Programming. This class
should have the following methods:
STEP2. Create an instance of that class. After that, follow these instructions:
https://colab.research.google.com/drive/1XJcKx3DBqz_KvrMNfUtHrLYxOFaHhWDx#printMode=true 2/7
8/11/23, 17:07 ADT_Trees_lab.ipynb - Colaboratory
- Use the first method to display the element that is the first element of the q
- Use the dequeue method to remove and return the first element from the queue
- Use the len method to get the number of elements of the queue
- Print the resulting Queue
class Queue:
def __init__(self):
self.items = []
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
def len(self):
return len(self.items)
def is_empty(self):
return len(self.items) == 0
def __repr__(self):
return repr(self.items)
def first(self):
if not self.is_empty():
return self.items[0]
myqueue = Queue()
print(myqueue.is_empty())
myqueue.enqueue(3)
myqueue.enqueue(4)
myqueue.enqueue(15)
myqueue.enqueue(7)
print(myqueue.first())
print(myqueue.dequeue())
print(myqueue.len())
print(myqueue)
True
3
3
https://colab.research.google.com/drive/1XJcKx3DBqz_KvrMNfUtHrLYxOFaHhWDx#printMode=true 3/7
8/11/23, 17:07 ADT_Trees_lab.ipynb - Colaboratory
3
[4, 15, 7]
Exercise 3.
STEP 1: Create a Binary Tree class that has the following methods:
- __init__. Allow us to initialize the Binary Tree with the attributes of each node (val
- insert_left. Insert a new node to the left
- insert_right. Insert a new node to the right
- pre_order. preorder traversal algorithm
- post_order. post order traversal algorithm
- in_order. in_order traversal algorithm
IMPORTANT: Notice that we are not using two classes (Node and BinaryTree) for this simple
implementation, just one Binary Tree class. This allows us to manipulate the tree easier. Every
time we create a new node we are creating a new Binary Tree that can be rooted at that node
when calling the different methods.
- initialize the Binary Tree with a node called node_a, whose value is 'a'
- use the insert_left method to insert the value of node_b, which is 'b'
- now: b_node = a_node.left_child
- keep doing this with c_node, d_node, e_node and f_node and their respective values till
- use in_order, pre_order and post_order to traverse and print all the nodes of the binary
[PSEUDOCODE]
#pseudocode
BINARY_TREE:
INPUT = Binary Tree, target node value (TN)
OUTPUT = Result of the operation (e.g., node position, traversal result)
BEGIN
SET current_node = Root of the Binary Tree
INSERT_LEFT(value):
CREATE a new node with the given value
https://colab.research.google.com/drive/1XJcKx3DBqz_KvrMNfUtHrLYxOFaHhWDx#printMode=true 4/7
8/11/23, 17:07 ADT_Trees_lab.ipynb - Colaboratory
SET the new node as the left child of the current node
INSERT_RIGHT(value):
CREATE a new node with the given value
SET the new node as the right child of the current node
PRE_ORDER():
PRINT the value of the current node
IF there is a left child, CALL PRE_ORDER on the left child
IF there is a right child, CALL PRE_ORDER on the right child
POST_ORDER():
IF there is a left child, CALL POST_ORDER on the left child
IF there is a right child, CALL POST_ORDER on the right child
PRINT the value of the current node
IN_ORDER():
IF there is a left child, CALL IN_ORDER on the left child
PRINT the value of the current node
IF there is a right child, CALL IN_ORDER on the right child
RETURN NONE
END
class BinaryTree:
def __init__(self, value):
self.value = value
self.left_child = None
self.right_child = None
def pre_order(self):
print(self.value, end=' ')
if self.left_child:
self.left_child.pre_order()
if self.right_child:
https://colab.research.google.com/drive/1XJcKx3DBqz_KvrMNfUtHrLYxOFaHhWDx#printMode=true 5/7
8/11/23, 17:07 ADT_Trees_lab.ipynb - Colaboratory
self.right_child.pre_order()
def post_order(self):
if self.left_child:
self.left_child.post_order()
if self.right_child:
self.right_child.post_order()
print(self.value, end=' ')
def in_order(self):
if self.left_child:
self.left_child.in_order()
print(self.value, end=' ')
if self.right_child:
self.right_child.in_order()
a_node = BinaryTree('a')
a_node.insert_left('b')
b_node = a_node.left_child
a_node.insert_right('c')
c_node = a_node.right_child
b_node.insert_right('d')
c_node.insert_left('e')
c_node.insert_right('f')
d_node = b_node.right_child
e_node = c_node.left_child
f_node = c_node.right_child
print ('Preorder')
a_node.pre_order()
print('Postorder')
a_node.post_order()
print('Inorder')
a_node.in_order()
Preorder
a b d c e f Postorder
d b e f c a Inorder
b d a e c f
https://colab.research.google.com/drive/1XJcKx3DBqz_KvrMNfUtHrLYxOFaHhWDx#printMode=true 6/7
8/11/23, 17:07 ADT_Trees_lab.ipynb - Colaboratory
https://colab.research.google.com/drive/1XJcKx3DBqz_KvrMNfUtHrLYxOFaHhWDx#printMode=true 7/7