Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5c39220

Browse files
committedJul 5, 2022
Scratch Readable Implementation in Python
1 parent 401d293 commit 5c39220

9 files changed

+845
-0
lines changed
 

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tree/notes/*.DOCX
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
2+
3+
class Node:
4+
def __init__(self, value=None) -> None:
5+
self.value = value
6+
self.next = None
7+
8+
9+
class CircularSinglyLinkedList:
10+
def __init__(self) -> None:
11+
self.head = None
12+
self.tail = None
13+
14+
def __iter__(self):
15+
node = self.head
16+
while node:
17+
yield node
18+
if node.next == self.head:
19+
break
20+
node = node.next
21+
22+
# Creation of circular singly linked list
23+
def createCSLL(self, value):
24+
node = Node(value)
25+
node.next = node
26+
self.head = node
27+
self.tail = node
28+
29+
# Insertion in circular singly linked list
30+
def insertInCSLL(self, value, location):
31+
if self.head is None:
32+
return
33+
else:
34+
newNode = Node(value)
35+
# Insert at first -> TIme: O(1); Space: O(1)
36+
# 1. Update newNode.next = head
37+
# 2. Update lastNode.next -> newNode
38+
# 3. Update tail to newNode
39+
if location == 0:
40+
newNode.next = self.head
41+
self.head = newNode
42+
lastNode = self.tail
43+
lastNode.next = newNode
44+
45+
# Insert into last -> Time: O(1); Space: O(1)
46+
# 1. Update last node next = newNode
47+
# 2. Update newNode.next = reference to first node
48+
# 3. Update tail to newNode
49+
elif location == -1:
50+
prevNode = self.tail
51+
prevNode.next = newNode
52+
newNode.next = self.head
53+
self.tail = newNode
54+
55+
else:
56+
# Insert into kth location -> Time: O(n); Space: O(1)
57+
# 1. Traverse to k-1 node
58+
# 2. Update newNode next to k-1 node.next
59+
# 3. Update k-1 node.next to newNode
60+
61+
# 1. Traverse to k-1 node; where k is the position to insert newNode
62+
tempNode = self.head
63+
index = 0
64+
while index < location - 1:
65+
tempNode = tempNode.next
66+
index += 1
67+
# 2. Update newNode next to k-1 node.next
68+
newNode.next = tempNode.next
69+
# 3. Update k-1 node.next to newNode
70+
tempNode.next = newNode
71+
72+
# Time: O(n)
73+
def traversalCLL(self):
74+
if self.head is None:
75+
print("No any node to traverse")
76+
else:
77+
tempNode = self.head
78+
while tempNode:
79+
print(tempNode.value)
80+
tempNode = tempNode.next
81+
if tempNode == self.tail.next:
82+
break
83+
84+
# Searching for a node in circular singly linked list
85+
def searchCSLL(self, value):
86+
if self.head is None:
87+
print("No any node to search.")
88+
else:
89+
tempNode = self.head
90+
while tempNode:
91+
if tempNode.value == value:
92+
return tempNode.value
93+
tempNode = tempNode.next
94+
if tempNode == self.tail.next:
95+
return "The node does not exist!"
96+
97+
# Delete in circular singly linked list
98+
def deleteNode(self, location):
99+
if self.head is None:
100+
print('No any node exists to delete!')
101+
else:
102+
if location == 0:
103+
if self.head == self.tail:
104+
self.head = None
105+
self.tail = None
106+
self.head.next = None
107+
else:
108+
# If more than one element
109+
# Make head ref to next node
110+
# Then, make last node ref to second node
111+
# Finally, garbage collector delete first node
112+
self.head = self.head.next
113+
self.tail.next = self.head
114+
elif location == -1:
115+
if self.head == self.tail:
116+
self.head = None
117+
self.tail = None
118+
self.head.next = None
119+
else:
120+
tempNode = self.head
121+
while tempNode:
122+
if tempNode.next == self.tail:
123+
break
124+
tempNode = tempNode.next
125+
tempNode.next = self.head
126+
self.tail = tempNode
127+
else:
128+
tempNode = self.head
129+
index = 0
130+
while index < location - 1:
131+
tempNode = tempNode.next
132+
index += 1
133+
delNode = tempNode.next
134+
tempNode.next = delNode.next
135+
136+
# Delete entire circular singly linked list
137+
# Make 3 reference to null
138+
def deleteEntireCLL(self):
139+
self.head = None
140+
self.tail.next = None
141+
self.tail = None
142+
143+
circularSLL = CircularSinglyLinkedList()
144+
circularSLL.createCSLL(-1)
145+
146+
circularSLL.insertInCSLL(0, 0)
147+
circularSLL.insertInCSLL(1, 0)
148+
circularSLL.insertInCSLL(2, 0)
149+
150+
circularSLL.insertInCSLL(10, -1)
151+
circularSLL.insertInCSLL(15, -1)
152+
153+
circularSLL.insertInCSLL(20, 3)
154+
circularSLL.insertInCSLL(25, 4)
155+
print([node.value for node in circularSLL])
156+
# circularSLL.traversalCLL()
157+
# print(circularSLL.searchCSLL(2))
158+
# print(circularSLL.searchCSLL(4))
159+
circularSLL.deleteNode(0)
160+
circularSLL.deleteNode(-1)
161+
circularSLL.deleteNode(2)
162+
print([node.value for node in circularSLL])
163+
164+
circularSLL.deleteEntireCLL()
165+
print([node.value for node in circularSLL])

‎LinkedList/DoublyLinkedList.py

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
2+
from sqlalchemy import null
3+
4+
5+
class Node:
6+
def __init__(self, value=None) -> None:
7+
self.value = value
8+
self.next = None
9+
self.prev = None
10+
11+
class DoublyLinkedList:
12+
def __init__(self) -> None:
13+
self.head = None
14+
self.tail = None
15+
16+
def __iter__(self):
17+
node = self.head
18+
while node:
19+
yield node
20+
node = node.next
21+
22+
# Creation of doubly linkedlist
23+
def createDoublyLL(self, value):
24+
newNode = Node(value)
25+
newNode.prev = None
26+
newNode.next = None
27+
self.head = newNode
28+
self.tail = newNode
29+
return "Creation of Doubly Linked List successful."
30+
31+
# Insertion in Doubly Linked List
32+
def insertInDoublyLL(self, value, location):
33+
if self.head is None:
34+
print("First create a Doubly Linked List, called createDoublyLL() method.")
35+
return
36+
newNode = Node(value)
37+
if location == 0: # Time: O(1); Space: O(1)
38+
# insert at the beginning
39+
# 1. Create a newNode
40+
# 2. Make newNode prev ref to Null
41+
# 3. Make newNode.next to FirstNode
42+
# 4. Update FistNode.prev ref to newNode
43+
# 5. Update head ref to newNode.
44+
newNode.prev = None
45+
newNode.next = self.head
46+
self.head.prev = newNode
47+
self.head = newNode
48+
49+
50+
elif location == -1: # Time: O(1); Space: O(1)
51+
# Insert newNode at last of DoublyLL
52+
# 1. Create a newNode with a given value.
53+
# 2. Make newNode.next to null
54+
# 3. Make newNode.prev to lastNode
55+
# 4. Update lastNode.next to newNode
56+
# 5. Update tail to newNode
57+
newNode.next = None
58+
newNode.prev = self.tail
59+
self.tail.next = newNode
60+
self.tail = newNode
61+
62+
else:
63+
# Insert newNode at specified location -> Time: O(n); Space: O(1)
64+
# 1. Creation of newNOde
65+
# 2. Traverse DoubleLL until index < location -1
66+
# 3. Make newNode.next = prevNode.next
67+
# 4. Update prevNode.next to newNode
68+
# 5. Update nextNode.prev = newNode
69+
# 6. Make newNode.prev = prevNode
70+
tempNode = self.head
71+
index = 0
72+
while index < location - 1:
73+
tempNode = tempNode.next
74+
index += 1
75+
nextNode = tempNode.next
76+
newNode.next = nextNode
77+
tempNode.next = newNode
78+
newNode.prev = tempNode
79+
nextNode.prev = newNode
80+
81+
# Traversal Method in DoublyLL -> Time: O(n); Space: O(1)
82+
def traverseDoublyLL(self):
83+
if self.head is None:
84+
print("There is not any element to traverse")
85+
else:
86+
tempNode = self.head
87+
while tempNode:
88+
print(tempNode.value)
89+
tempNode = tempNode.next
90+
91+
# Reverse Traversal Method in Doubly Linked List -> Time: O(n); Space: O(1)
92+
def reverseTraverseDoublyLL(self):
93+
if self.head is None:
94+
print("There is not any element to traverse.")
95+
else:
96+
tempNode = self.tail
97+
while tempNode:
98+
print(tempNode.value)
99+
tempNode = tempNode.prev
100+
101+
# Search Method in Doubly Linked List -> Time: O(n); Space: O(1)
102+
def searchElementDLL(self, value):
103+
if self.head is None:
104+
return "There is not any element in the Linked List to search."
105+
else:
106+
tempNode = self.head
107+
while tempNode:
108+
if tempNode.value == value:
109+
return tempNode.value
110+
tempNode = tempNode.next
111+
return "The node value does not exist in the linked list"
112+
113+
# Deletion of node in Doubly Linked List
114+
def deleteElementDLL(self, location): # Time: O(n); Space: O(1)
115+
if self.head is None:
116+
return "No any node to delete in List."
117+
else:
118+
# Delete at the beginning of list -> Time: O(1); Space: O(1)
119+
if location == 0:
120+
# Case 1: if there is only one node
121+
# Update head and tail ref to null
122+
if self.head == self.tail:
123+
self.head, self.tail = None, None
124+
else:
125+
# Case 2: more than one node
126+
# 1. Update head to second node
127+
# 2. Updare secondNode.prev to null
128+
self.head = self.head.next
129+
self.head.prev = None
130+
131+
# Delete from the last of List -> Time: O(1); Space: O(1)
132+
elif location == -1:
133+
# Case 1: If there is only one element
134+
# Update head and tail ref to null
135+
if self.head == self.tail:
136+
self.head, self.tail = None, None
137+
else:
138+
# Case 2: more than one node
139+
# 1. Update prevNode.next to null
140+
# 2. Update tail to prevNode
141+
prevNode = self.tail.prev
142+
prevNode.next = None
143+
self.tail = prevNode
144+
145+
# Delete from the given location -> Time: O(n); Space: O(1)
146+
else:
147+
# 1. Traverse untill before given location
148+
# Update prevNode.next to nextNode
149+
# Update nextNode.prev to prevNode
150+
tempNode = self.head
151+
index = 0
152+
while index < location - 1:
153+
tempNode = tempNode.next
154+
index += 1
155+
156+
delNode = tempNode.next
157+
nextNode = delNode.next
158+
tempNode.next = nextNode
159+
nextNode.prev = tempNode
160+
161+
# Delete Entire Double Linked List
162+
def deleteEntireDoubleLL(self): # -> Time: O(n); Space: O(1)
163+
# Steps:
164+
# 1. Traverse through list and make all node prev to null
165+
# 2. Finally Update head and tail ref to null
166+
# 3. By that Garbage collector delete the entire list where there is no
167+
# connection in both ways
168+
node = self.head
169+
while node:
170+
node.prev = None
171+
node = node.next
172+
self.head = None
173+
self.tail = None
174+
175+
doublyLL = DoublyLinkedList()
176+
doublyLL.createDoublyLL(1)
177+
178+
doublyLL.insertInDoublyLL(2, 0)
179+
doublyLL.insertInDoublyLL(5, -1)
180+
doublyLL.insertInDoublyLL(7, 1)
181+
doublyLL.insertInDoublyLL(4, -1)
182+
doublyLL.insertInDoublyLL(8, 2)
183+
print([node.value for node in doublyLL])
184+
# doublyLL.traverseDoublyLL()
185+
# print("#######################")
186+
# doublyLL.reverseTraverseDoublyLL()
187+
# print("###########################")
188+
# print(doublyLL.searchElementDLL(5))
189+
doublyLL.deleteElementDLL(0)
190+
doublyLL.deleteElementDLL(-1)
191+
doublyLL.deleteElementDLL(2)
192+
print([node.value for node in doublyLL])
193+
194+
doublyLL.deleteEntireDoubleLL()
195+
print([node.value for node in doublyLL])
196+
197+

‎LinkedList/LinkedList.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
3+
class Node:
4+
def __init__(self, value=None) -> None:
5+
self.value = value
6+
self.next = None
7+
8+
9+
class SLinkedList:
10+
def __init__(self) -> None:
11+
self.head = None
12+
self.tail = None
13+
14+
def __iter__(self):
15+
node = self.head
16+
while node:
17+
yield node
18+
node = node.next
19+
20+
def __str__(self) -> str:
21+
values = [str(x.value) for x in self]
22+
return ' -> '.join(values)
23+
24+
def __len__(self):
25+
result = 0
26+
node = self.head
27+
while node:
28+
result += 1
29+
node = node.next
30+
return result
31+
32+
# insert in Linked List
33+
def insert_element(self, value, location):
34+
newNode = Node(value)
35+
if self.head is None:
36+
self.head = newNode
37+
self.tail = newNode
38+
else:
39+
# insert at first : Time: O(1)
40+
if location == 0:
41+
newNode.next = self.head
42+
self.head = newNode
43+
# insert at last: Time: O(1)
44+
elif location == -1:
45+
newNode.next = None
46+
self.tail.next = newNode
47+
self.tail = newNode
48+
# insert at nth postion: Time: O(n)
49+
else:
50+
tempNode = self.head
51+
index = 0
52+
while index < location - 1:
53+
tempNode = tempNode.next
54+
index += 1
55+
nextNode = tempNode.next
56+
tempNode.next = newNode
57+
newNode.next = nextNode
58+
59+
def delete_element(self, location):
60+
if self.head is None:
61+
print("Linked List does not exist")
62+
63+
else:
64+
if location == 0:
65+
# if have only one node
66+
if self.head == self.tail:
67+
self.head = None
68+
self.tail = None
69+
else:
70+
temp = self.head
71+
self.head = temp.next
72+
temp = None
73+
74+
# delete from last position : Time: O(n)
75+
elif location == -1:
76+
# #1. if we have only one node
77+
if(self.head == self.tail):
78+
self.head = None
79+
self.tail = None
80+
else:
81+
# 2. Else, traverse to the second last
82+
# element of the list
83+
tempNode = self.head
84+
while(tempNode is not None):
85+
if tempNode.next == self.tail:
86+
break
87+
tempNode = tempNode.next
88+
# 3. Change the next of the second
89+
# last node to null and delete the
90+
# last node
91+
tempNode.next = None
92+
self.tail = tempNode
93+
94+
else:
95+
tempNode = self.head
96+
index = 0
97+
while index < location - 1:
98+
tempNode = tempNode.next
99+
index += 1
100+
delNode = tempNode.next
101+
tempNode.next = delNode.next
102+
103+
# Delete whole SLL
104+
# Time: O(1) ; Space: O(1)
105+
def deleteAllSLL(self):
106+
# Idea is to make reference to head and tail to null
107+
# Garbage collector then destroy all node
108+
if self.head is None:
109+
print("The SLL does not exist")
110+
else:
111+
self.head = None
112+
self.tail = None
113+
114+
115+
singlyLinkedList = SLinkedList()
116+
117+
singlyLinkedList.insert_element(0, 0)
118+
singlyLinkedList.insert_element(1, 0)
119+
singlyLinkedList.insert_element(2, 0)
120+
121+
singlyLinkedList.insert_element(10, -1)
122+
singlyLinkedList.insert_element(15, -1)
123+
124+
singlyLinkedList.insert_element(20, 3)
125+
singlyLinkedList.insert_element(25, 4)
126+
127+
print(singlyLinkedList)
128+
129+
singlyLinkedList.delete_element(4)
130+
singlyLinkedList.delete_element(0)
131+
singlyLinkedList.delete_element(-1)
132+
print(singlyLinkedList)
133+
134+
singlyLinkedList.deleteAllSLL()
135+
print(singlyLinkedList)

‎LinkedList/LinkedList2.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from random import randint
2+
3+
class Node:
4+
def __init__(self, value=None) -> None:
5+
self.value = value
6+
self.next = None
7+
8+
def __str__(self) -> str:
9+
return str(self.value)
10+
11+
12+
class LinkedList:
13+
def __init__(self) -> None:
14+
self.head = None
15+
self.tail = None
16+
17+
def __iter__(self):
18+
node = self.head
19+
while node:
20+
yield node
21+
node = node.next
22+
23+
def __str__(self) -> str:
24+
values = [str(x.value) for x in self]
25+
return ' -> '.join(values)
26+
27+
def __len__(self):
28+
result = 0
29+
node = self.head
30+
while node:
31+
result += 1
32+
node = node.next
33+
return result
34+
35+
def add(self, value):
36+
if self.head is None:
37+
newNode = Node(value)
38+
self.head = newNode
39+
self.tail = newNode
40+
else:
41+
self.tail.next = Node(value)
42+
self.tail = self.tail.next
43+
return self.tail
44+
45+
def generate(self, n, min_value, max_value):
46+
self.head = None
47+
self.tail = None
48+
for i in range(n):
49+
self.add(randint(min_value, max_value))
50+
return self
51+
52+
newLL = LinkedList()
53+
newLL.generate(10, 0, 99)
54+
print(newLL)
55+
print(len(newLL))

‎LinkedList/RemoveDuplicates.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from LinkedList2 import LinkedList
2+
3+
# Remove duplicates elemnent from linked list
4+
def removeDuplicates(ll):
5+
if ll.head is None:
6+
return
7+
else:
8+
curNode = ll.head
9+
visitedNode = set([curNode.value])
10+
while curNode.next:
11+
if curNode.next.value in visitedNode:
12+
curNode.next = curNode.next.next
13+
else:
14+
visitedNode.add(curNode.next.value)
15+
curNode = curNode.next
16+
return ll
17+
18+
# Implement an algorithm to find the nth from the last element of a singly linked list
19+
20+
21+
22+
customLL = LinkedList()
23+
customLL.generate(10, 0, 99)
24+
print(customLL)
25+
removeDuplicates(customLL)
26+
print(customLL)
27+

‎Tree/BinaryTree.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
'''
2+
Creation of Binary Tree using Linked List
3+
- Creation of Tree
4+
- Insertion of a node
5+
- Deletion of a node
6+
- Search for a value
7+
- Traverse all nodes -> 4 ways of traversing
8+
- Deletion of tree
9+
10+
* Traversal of Binary Tree
11+
- Depth First Search
12+
- Preorder traversal
13+
- Inorder traversal
14+
- Post order traversal
15+
- Breadth first search
16+
- Level order traversal
17+
'''
18+
19+
from QueueLinkedList import Queue
20+
21+
22+
class TreeNode:
23+
def __init__(self, data) -> None:
24+
self.data = data
25+
self.leftChild = None
26+
self.rightChild = None
27+
28+
29+
BT = TreeNode("Gadgets")
30+
leftChild = TreeNode("IPad")
31+
leftChild.leftChild = TreeNode("IPadCharger")
32+
leftChild.rightChild = TreeNode("IPadEarphone")
33+
rightChild = TreeNode("Mobile")
34+
rightChild.rightChild = TreeNode("MobileCharger")
35+
BT.leftChild = leftChild
36+
BT.rightChild = rightChild
37+
38+
39+
# Time: O(n) ; Space: O(n)
40+
def preOrderTraversal(rootNode):
41+
if not rootNode:
42+
return
43+
print(rootNode.data)
44+
preOrderTraversal(rootNode.leftChild)
45+
preOrderTraversal(rootNode.rightChild)
46+
47+
# Time: O(n); Space: O(n)
48+
def inOrderTraversal(rootNode):
49+
if not rootNode:
50+
return
51+
inOrderTraversal(rootNode.leftChild)
52+
print(rootNode.data)
53+
inOrderTraversal(rootNode.rightChild)
54+
55+
# Time: O(n); Space: O(n)
56+
def postOrderTraversal(rootNode):
57+
if not rootNode:
58+
return
59+
postOrderTraversal(rootNode.leftChild)
60+
postOrderTraversal(rootNode.rightChild)
61+
print(rootNode.data)
62+
63+
# Time: O(n); Space: O(n)
64+
def levelOrderTraversal(rootNode):
65+
if not rootNode:
66+
return
67+
else:
68+
customQueue = Queue()
69+
customQueue.enqueue(rootNode)
70+
while not customQueue.isEmpty():
71+
root = customQueue.dequeue()
72+
print(root.value.data)
73+
if (root.value.leftChild is not None):
74+
customQueue.enqueue(root.value.leftChild)
75+
76+
if (root.value.rightChild is not None):
77+
customQueue.enqueue(root.value.rightChild)
78+
79+
# Time: O(n); Space: O(n)
80+
def searchValueBT(rootNode, nodeValue):
81+
if not rootNode:
82+
return "The Binary Tree does not exist"
83+
else:
84+
customQueue = Queue()
85+
customQueue.enqueue(rootNode)
86+
while not customQueue.isEmpty():
87+
root = customQueue.dequeue()
88+
if root.value.data == nodeValue:
89+
return "Value found Sucessfully!"
90+
91+
if (root.value.leftChild is not None):
92+
customQueue.enqueue(root.value.leftChild)
93+
94+
if (root.value.rightChild is not None):
95+
customQueue.enqueue(root.value.rightChild)
96+
return "Value not found!"
97+
98+
# Insert a node in Binary Tree
99+
# we do level order traversal to insert node
100+
# Since it is queue and it is efficient than recursion(PreOrder, InOrder, PostOrder)
101+
# Case1: A root node is blank
102+
# Case2: The tree exists and we have to look for a first vacant place
103+
# Time: O(n); Space: O(n)
104+
def insertNodeBT(rootNode, newNode):
105+
if not rootNode:
106+
rootNode = newNode
107+
else:
108+
customQueue = Queue()
109+
customQueue.enqueue(rootNode)
110+
while not customQueue.isEmpty():
111+
root = customQueue.dequeue()
112+
if root.value.leftChild is not None:
113+
customQueue.enqueue(root.value.leftChild)
114+
else:
115+
root.value.leftChild = newNode
116+
return "New Value inserted sucessfully!"
117+
118+
if root.value.rightChild is not None:
119+
customQueue.enqueue(root.value.rightChild)
120+
else:
121+
root.value.rightChild = newNode
122+
return "New Value inserted sucessfully!"
123+
124+
def getFarthestNode(rootNode):
125+
if not rootNode:
126+
return
127+
else:
128+
customQueue = Queue()
129+
customQueue.enqueue(rootNode)
130+
while not customQueue.isEmpty():
131+
root = customQueue.dequeue()
132+
133+
if root.value.leftChild is not None:
134+
customQueue.enqueue(root.value.leftChild)
135+
if root.value.rightChild is not None:
136+
customQueue.enqueue(root.value.rightChild)
137+
# the last node is farthest among all
138+
return root.value
139+
140+
preOrderTraversal(BT)
141+
print("###########")
142+
inOrderTraversal(BT)
143+
print("#######")
144+
postOrderTraversal(BT)
145+
print("############")
146+
levelOrderTraversal(BT)
147+
print("##########")
148+
print(searchValueBT(BT, "MobileCharger"))
149+
print("##########")
150+
# Insert new node in BT
151+
newNode = TreeNode("MobileEarphone")
152+
print(insertNodeBT(BT, newNode))
153+
levelOrderTraversal(BT)
154+
print("#########")
155+
farthestNode = getFarthestNode(BT)
156+
print(farthestNode.data)

‎Tree/QueueLinkedList.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
3+
class Node:
4+
def __init__(self, value=None) -> None:
5+
self.value = value
6+
self.next = None
7+
8+
def __str__(self) -> str:
9+
return str(self.value)
10+
11+
class LinkedList:
12+
def __init__(self) -> None:
13+
self.head = None
14+
self.tail = None
15+
16+
def __iter__(self):
17+
node = self.head
18+
while node:
19+
yield node
20+
node = node.next
21+
22+
23+
class Queue:
24+
def __init__(self) -> None:
25+
self.linkedList = LinkedList()
26+
27+
def __str__(self) -> str:
28+
values = [str(x) for x in self.linkedList]
29+
return ' '.join(values)
30+
31+
def enqueue(self, value):
32+
newNode = Node(value)
33+
if self.linkedList.head == None:
34+
self.linkedList.head = newNode
35+
self.linkedList.tail = newNode
36+
else:
37+
self.linkedList.tail.next = newNode
38+
self.linkedList.tail = newNode
39+
40+
def isEmpty(self):
41+
if self.linkedList.head == None:
42+
return True
43+
else:
44+
return False
45+
46+
def dequeue(self):
47+
if self.isEmpty():
48+
return "Queue Underflow! No any element."
49+
else:
50+
tempNode = self.linkedList.head
51+
if self.linkedList.head == self.linkedList.tail:
52+
self.linkedList.head = None
53+
self.linkedList.tail = None
54+
else:
55+
self.linkedList.head = self.linkedList.head.next
56+
return tempNode
57+
58+
def peek(self):
59+
if self.isEmpty():
60+
return "Queue Underflow! No any element."
61+
else:
62+
return self.linkedList.head
63+
64+
65+
def deleteQueue(self):
66+
self.linkedList.head = None
67+
self.linkedList.tail = None
68+
69+
70+
custQueue = Queue()
71+
custQueue.enqueue(10)
72+
custQueue.enqueue(11)
73+
custQueue.enqueue(12)
74+
print(custQueue)
75+
print(custQueue.dequeue())
76+
print(custQueue)
77+
print(custQueue.peek())
78+
print(custQueue)

‎Tree/Tree.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
4+
5+
class TreeNode:
6+
def __init__(self, data, children=[]) -> None:
7+
self.data = data
8+
self.children = children
9+
10+
def __str__(self, level=0) -> str:
11+
ret = " " * level + str(self.data) + "\n"
12+
for child in self.children:
13+
ret += child.__str__(level + 1)
14+
return ret
15+
16+
def addChild(self, TreeNode):
17+
self.children.append(TreeNode)
18+
19+
20+
tree = TreeNode('Gadgets', [])
21+
Ipad = TreeNode('Ipad', [])
22+
mobile = TreeNode('Mobile', [])
23+
tree.addChild(Ipad)
24+
tree.addChild(mobile)
25+
26+
IpadCharger = TreeNode('IpadCharger', [])
27+
mobileCharger = TreeNode('MobileCharger', [])
28+
Ipad.addChild(IpadCharger)
29+
mobile.addChild(mobileCharger)
30+
print(tree)
31+

0 commit comments

Comments
 (0)
Please sign in to comment.