browsing_history = []
browsing_history.append(1)
browsing_history.append(2)
browsing_history.append(3)
browsing_history.pop()
browsing_history[-1]from collections import deque
queue = deque([])
queue.append(1)
queue.append(2)
queue.append(3)
print(queue)
queue.popleft()
print(queue)class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def print(self):
if self.head is None:
print("Linked list is empty")
return
itr = self.head
llstr = ''
while itr:
llstr += str(itr.data)+' --> ' if itr.next else str(itr.data)
itr = itr.next
print(llstr)
def get_length(self):
count = 0
itr = self.head
while itr:
count+=1
itr = itr.next
return count
def insert_at_begining(self, data):
node = Node(data, self.head)
self.head = node
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, None)
def insert_at(self, index, data):
if index<0 or index>self.get_length():
raise Exception("Invalid Index")
if index==0:
self.insert_at_begining(data)
return
count = 0
itr = self.head
while itr:
if count == index - 1:
node = Node(data, itr.next)
itr.next = node
break
itr = itr.next
count += 1
def remove_at(self, index):
if index<0 or index>=self.get_length():
raise Exception("Invalid Index")
if index==0:
self.head = self.head.next
return
count = 0
itr = self.head
while itr:
if count == index - 1:
itr.next = itr.next.next
break
itr = itr.next
count+=1
def insert_values(self, data_list):
self.head = None
for data in data_list:
self.insert_at_end(data)
def get_middel_node(self, key):
count = 0
iter = self.head
while iter:
count += 1
if count == key:
return iter.data
iter = iter.next
if __name__ == '__main__':
ll = LinkedList()
ll.insert_values(["banana","mango","grapes","orange"])
ll.insert_at(1,"blueberry")
ll.remove_at(2)
ll.print()
ll.insert_values([45,7,12,567,99])
ll.insert_at_end(67)
ll.get_middel_node(2) # mango
ll.get_middel_node(4) # orange
ll.print()class TreeNode:
def __init__(self, data):
self.data = data
self.children = []
self.parent = None
def get_level(self):
level = 0
p = self.parent
while p:
level += 1
p = p.parent
return level
def print_tree(self):
spaces = ' ' * self.get_level() * 3
prefix = spaces + "|__" if self.parent else ""
print(prefix + self.data)
if self.children:
for child in self.children:
child.print_tree()
def add_child(self, child):
child.parent = self
self.children.append(child)
def build_product_tree():
root = TreeNode("Electronics")
laptop = TreeNode("Laptop")
laptop.add_child(TreeNode("Mac"))
laptop.add_child(TreeNode("Surface"))
laptop.add_child(TreeNode("Thinkpad"))
cellphone = TreeNode("Cell Phone")
cellphone.add_child(TreeNode("iPhone"))
cellphone.add_child(TreeNode("Google Pixel"))
cellphone.add_child(TreeNode("Vivo"))
tv = TreeNode("TV")
tv.add_child(TreeNode("Samsung"))
tv.add_child(TreeNode("LG"))
root.add_child(laptop)
root.add_child(cellphone)
root.add_child(tv)
root.print_tree()
if __name__ == '__main__':
build_product_tree()Every node has at most 2 child nodes.
Binary Search Tree : left side nodes values less than root node.
Cannot have duplicate values
Every Iteration we reduce search space by 1/2.
n = 8 , 8 > 4 > 2 > 1
Total 3 Iteration so it means,
log28 = 3
Traversal Techniques:
Breadth first Search
Depth first Search
from util import time_it
@time_it
def linear_search(numbers_list, number_to_find):
for index, element in enumerate(numbers_list):
if element == number_to_find:
return index
return -1
@time_it
def binary_search(numbers_list, number_to_find):
left_index = 0
right_index = len(numbers_list) - 1
mid_index = 0
while left_index <= right_index:
mid_index = (left_index + right_index) // 2
mid_number = numbers_list[mid_index]
if mid_number == number_to_find:
return mid_index
if mid_number < number_to_find:
left_index = mid_index + 1
else:
right_index = mid_index - 1
return -1
def binary_search_recursive(numbers_list, number_to_find, left_index, right_index):
if right_index < left_index:
return -1
mid_index = (left_index + right_index) // 2
if mid_index >= len(numbers_list) or mid_index < 0:
return -1
mid_number = numbers_list[mid_index]
if mid_number == number_to_find:
return mid_index
if mid_number < number_to_find:
left_index = mid_index + 1
else:
right_index = mid_index - 1
return binary_search_recursive(numbers_list, number_to_find, left_index, right_index)
if __name__ == '__main__':
numbers_list = [12, 15, 17, 19, 21, 24, 45, 67]
number_to_find = 21
index = binary_search_recursive(numbers_list, number_to_find, 0, len(numbers_list))
print(f"Number found at index {index} using binary search")def bubble_sort(elements):
size = len(elements)
for i in range(size-1):
for j in range(size-1):
if elements[j] > elements[j+1]:
elements[j], elements[j+1] = elements[j+1], elements[j]
if __name__ == '__main__':
elements = [5,9,2,1,67,34,88,34]
elements = ["mona", "dhaval", "aamir", "tina", "chang"]
bubble_sort(elements)
print(elements)