0% found this document useful (0 votes)
26 views

ADT - Trees - Lab - Ipynb - Colaboratory

The document describes exercises to practice creating and using abstract data types (ADTs) like stacks, queues, and binary trees in Python. It includes instructions to: 1) Create Stack and Queue classes with common methods like push, pop, enqueue, dequeue. 2) Create a BinaryTree class with methods for insertion, preorder, inorder and postorder traversal. 3) Provide pseudocode for the BinaryTree methods and traversal algorithms. 4) Demonstrate usage of the classes by creating instances, adding elements, and printing results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

ADT - Trees - Lab - Ipynb - Colaboratory

The document describes exercises to practice creating and using abstract data types (ADTs) like stacks, queues, and binary trees in Python. It includes instructions to: 1) Create Stack and Queue classes with common methods like push, pop, enqueue, dequeue. 2) Create a BinaryTree class with methods for insertion, preorder, inorder and postorder traversal. 3) Provide pseudocode for the BinaryTree methods and traversal algorithms. 4) Demonstrate usage of the classes by creating instances, adding elements, and printing results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

8/11/23, 17:07 ADT_Trees_lab.

ipynb - Colaboratory

Abstract Data Types and Trees Lab


Use your new programming tools (AND THE SLIDES) to complete the following exercises.

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 is_empty method to check if the Stack is empty


- Use push method to push the following numbers into the Stack one by one:
3 4 15 7

- 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 push(self, item):


self.items.append(item)

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:

- enqueue. add an element to the back of the queue


- dequeue. remove and return the first element from the queue
- len. return the number of elements in the queue
- is_empty. return True if the queue is empty
- __repr__ Allow us to display the queue
- __init__ Allow us to initialize the queue class
- first. return the first element of the list without removing it

STEP2. Create an instance of that class. After that, follow these instructions:

- Use is_empty method to check if the Queue is empty


- Use enqueue method to add the following numbers to the back of the queue one b
3 4 15 7

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 enqueue(self, item):


self.items.append(item)

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.

STEP 2: Create an instance of that Binary tree following these instructions:

- 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

Before you start, create the pseudocode of insert_left , insert_right , pre_order ,


post_order and in_order traversal algorithms:

[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

WHILE current_node is not None DO


IF TN > current_node.value THEN
SET current_node = current_node.right_child
ELSE IF TN < current_node.value THEN
SET current_node = current_node.left_child
ELSE IF TN == current_node.value THEN
RETURN Result (e.g., position or traversal result)
END IF
END WHILE

RETURN NONE

END

class BinaryTree:
def __init__(self, value):
self.value = value
self.left_child = None
self.right_child = None

def insert_left(self, value):


new_node = BinaryTree(value)
self.left_child = new_node

def insert_right(self, value):


new_node = BinaryTree(value)
self.right_child = new_node

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

You might also like