├── Graph ├── .DS_Store ├── QueuesLinked.py ├── Graphs.py ├── DFS.py ├── GraphsWeightedUndirected.py ├── GraphsUndirected.py ├── GraphDirected.py ├── GraphWeightedDirected.py └── BFS.py ├── Hashing ├── .DS_Store ├── Chaining.py ├── BucketSort.py ├── LinearProbe.py └── LinkedList.py ├── PQ & Heap ├── .DS_Store ├── __pycache__ │ └── Heaps.cpython-310.pyc ├── HeapSort.py ├── PythonsHeapq.py ├── HeapInsert.py ├── Heaps.py └── HeapDelete.py ├── Queues ├── .DS_Store ├── QueuesArrays.py ├── DEQueArrays.py ├── QueuesLinked.py └── DEQueLinked.py ├── Stacks ├── .DS_Store ├── StacksArrays.py └── StacksLinked.py ├── searching ├── .DS_Store ├── linearsearch.py ├── binarysearchiterative.py └── binarysearchrecursive.py ├── sorting ├── .DS_Store ├── BubbleSort.py ├── PythonBuiltinSorting.py ├── SelectionSort.py ├── InsertionSort.py ├── ShellSort.py ├── CountSort.py ├── RadixSort.py ├── QuickSort.py └── MergeSort.py ├── Binary Tree ├── .DS_Store ├── QueuesLinked.py ├── BinaryTree (1).py ├── BinaryTree (2).py ├── BinaryTree.py ├── BinaryTreeCountNodes.py ├── BinaryTreeHeight.py └── LevelOrderTraversal.py ├── linked list ├── .DS_Store ├── LLCreateDisplay.py ├── LLSearchElement.py ├── DLLCreateDisplay.py ├── CLLCreateDisplay.py ├── DLLAddFirstElement.py ├── LLAddFirstElement.py ├── CLLAddFirstElement.py ├── LLAddAnyElement.py ├── DLLAddAnyElement.py ├── CLLAddAnyElement.py ├── LLRemoveFirstElement.py ├── DLLRemoveFirstElement.py ├── CLLRemoveFirstElement.py ├── LLRemoveLastElement.py ├── DLLRemoveLastElement.py ├── LLRemoveAnyElement.py ├── CLLRemoveLastElement.py ├── DLLRemoveAnyElement.py └── CLLRemoveAnyElement.py ├── Binary Search Tree ├── .DS_Store ├── InsertRecursive.py ├── SearchIterative.py ├── SearchRecursive.py └── DeleteBinarySearchTree.py ├── README.md └── LICENSE /Graph/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/Graph/.DS_Store -------------------------------------------------------------------------------- /Hashing/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/Hashing/.DS_Store -------------------------------------------------------------------------------- /PQ & Heap/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/PQ & Heap/.DS_Store -------------------------------------------------------------------------------- /Queues /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/Queues /.DS_Store -------------------------------------------------------------------------------- /Stacks /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/Stacks /.DS_Store -------------------------------------------------------------------------------- /searching/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/searching/.DS_Store -------------------------------------------------------------------------------- /sorting/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/sorting/.DS_Store -------------------------------------------------------------------------------- /Binary Tree/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/Binary Tree/.DS_Store -------------------------------------------------------------------------------- /linked list/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/linked list/.DS_Store -------------------------------------------------------------------------------- /Binary Search Tree/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/Binary Search Tree/.DS_Store -------------------------------------------------------------------------------- /PQ & Heap/__pycache__/Heaps.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codehariom/DSA-in-Python/HEAD/PQ & Heap/__pycache__/Heaps.cpython-310.pyc -------------------------------------------------------------------------------- /searching/linearsearch.py: -------------------------------------------------------------------------------- 1 | def linearsearch(A, key): 2 | index = 0 3 | while index < len(A): 4 | if A[index] == key : 5 | return index 6 | index = index + 1 7 | return -1 8 | 9 | A = [84, 21, 47, 96, 15] 10 | found = linearsearch(A,96) 11 | print('Result:',found) 12 | -------------------------------------------------------------------------------- /sorting/BubbleSort.py: -------------------------------------------------------------------------------- 1 | def bubblesort(A): 2 | n = len(A) 3 | for passes in range(n-1,0,-1): 4 | for i in range(passes): 5 | if A[i] > A[i+1]: 6 | temp = A[i] 7 | A[i] = A[i+1] 8 | A[i+1] = temp 9 | 10 | 11 | A = [3, 5, 8, 9, 6, 2] 12 | print('Original Array:',A) 13 | bubblesort(A) 14 | print('Sorted Array:',A) 15 | -------------------------------------------------------------------------------- /sorting/PythonBuiltinSorting.py: -------------------------------------------------------------------------------- 1 | A = [63,250,835,947,651,28] 2 | print('Original List:', A) 3 | A.sort() 4 | print('Sorted List:', A) 5 | 6 | A = [63,250,835,947,651,28] 7 | print('Original List:', A) 8 | B = sorted(A) 9 | print('List-A:', A) 10 | print('List-B:', B) 11 | 12 | A = [63,250,835,947,651,28] 13 | print('Original List:', A) 14 | A.sort(reverse=True) 15 | print('Sorted List:', A) 16 | 17 | 18 | -------------------------------------------------------------------------------- /PQ & Heap/HeapSort.py: -------------------------------------------------------------------------------- 1 | from Heaps import Heap 2 | 3 | def heapsort(A): 4 | H = Heap() 5 | n = len(A) 6 | for i in range(n): 7 | H.insert(A[i]) 8 | k = n-1 9 | for i in range(H._csize): 10 | A[k] = H.deletemax() 11 | k = k - 1 12 | 13 | 14 | A = [63, 250, 835, 947, 651, 28] 15 | print('Original Array:',A) 16 | heapsort(A) 17 | print('Sorted Array:',A) 18 | 19 | 20 | -------------------------------------------------------------------------------- /sorting/SelectionSort.py: -------------------------------------------------------------------------------- 1 | def selectionsort(A): 2 | n = len(A) 3 | for i in range(n-1): 4 | position = i 5 | for j in range(i+1, n): 6 | if A[j] < A[position]: 7 | position = j 8 | temp = A[i] 9 | A[i] = A[position] 10 | A[position] = temp 11 | 12 | 13 | A = [3, 5, 8, 9, 6, 2] 14 | print('Original Array:',A) 15 | selectionsort(A) 16 | print('Sorted Array:',A) 17 | -------------------------------------------------------------------------------- /sorting/InsertionSort.py: -------------------------------------------------------------------------------- 1 | def insertionsort(A): 2 | n = len(A) 3 | for i in range(1,n): 4 | cvalue = A[i] 5 | position = i 6 | while position > 0 and A[position-1] > cvalue: 7 | A[position] = A[position-1] 8 | position = position - 1 9 | A[position] = cvalue 10 | 11 | 12 | A = [3,5,8,9,6,2] 13 | print('Original Array:',A) 14 | insertionsort(A) 15 | print('Sorted Array:',A) 16 | -------------------------------------------------------------------------------- /searching/binarysearchiterative.py: -------------------------------------------------------------------------------- 1 | def binarysearch_iterative(A, key): 2 | l = 0 3 | r = len(A)-1 4 | while l <= r: 5 | mid = (l + r) // 2 6 | if key == A[mid]: 7 | return mid 8 | elif key < A[mid]: 9 | r = mid - 1 10 | elif key > A[mid]: 11 | l = mid + 1 12 | return -1 13 | 14 | A = [15,21,47,84,96] 15 | found = binarysearch_iterative(A,84) 16 | print('Result: ',found) 17 | -------------------------------------------------------------------------------- /sorting/ShellSort.py: -------------------------------------------------------------------------------- 1 | def shellsort(A): 2 | n = len(A) 3 | gap = n//2 4 | while gap > 0: 5 | i = gap 6 | while i < n: 7 | temp = A[i] 8 | j = i - gap 9 | while j >= 0 and A[j] > temp: 10 | A[j+gap] = A[j] 11 | j = j - gap 12 | A[j+gap] = temp 13 | i = i + 1 14 | gap = gap // 2 15 | 16 | 17 | A = [3, 5, 8, 9, 6, 2] 18 | print('Original Array:',A) 19 | shellsort(A) 20 | print('Sorted Array:',A) 21 | -------------------------------------------------------------------------------- /searching/binarysearchrecursive.py: -------------------------------------------------------------------------------- 1 | def binaryseaarch_recursive(A, key, l, r): 2 | if l > r: 3 | return -1 4 | else: 5 | mid = (l + r) // 2 6 | if key == A[mid]: 7 | return mid 8 | elif key < A[mid]: 9 | return binaryseaarch_recursive(A,key,l,mid-1) 10 | elif key > A[mid]: 11 | return binaryseaarch_recursive(A,key,mid+1,r) 12 | 13 | A = [15, 21, 47, 84, 96] 14 | found = binaryseaarch_recursive(A,17,0,4) 15 | print('Result:', found) 16 | 17 | 18 | -------------------------------------------------------------------------------- /sorting/CountSort.py: -------------------------------------------------------------------------------- 1 | def countsort(A): 2 | n = len(A) 3 | maxsize = max(A) 4 | carray = [0] * (maxsize + 1) 5 | for i in range(n): 6 | carray[A[i]] = carray[A[i]] + 1 7 | i = 0 8 | j = 0 9 | while i < maxsize + 1: 10 | if carray[i] > 0: 11 | A[j] = i 12 | j = j + 1 13 | carray[i] = carray[i] - 1 14 | else: 15 | i = i + 1 16 | 17 | 18 | A = [3, 5, 8, 9, 6, 2, 3, 5, 5] 19 | print('Original Array:',A) 20 | countsort(A) 21 | print('Sorted Array:',A) 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /sorting/RadixSort.py: -------------------------------------------------------------------------------- 1 | def radixsort(A): 2 | n = len(A) 3 | maxelement = max(A) 4 | digits = len(str(maxelement)) 5 | l = [] 6 | bins = [l] * 10 7 | for i in range(digits): 8 | for j in range(n): 9 | e = int((A[j] / pow(10, i)) % 10) 10 | if len(bins[e]) > 0: 11 | bins[e].append(A[j]) 12 | else: 13 | bins[e] = [A[j]] 14 | k = 0 15 | for x in range(10): 16 | if len(bins[x]) > 0: 17 | for y in range(len(bins[x])): 18 | A[k] = bins[x].pop(0) 19 | k = k + 1 20 | A = [63, 250, 835, 947, 651, 28] 21 | print('Original Array:',A) 22 | radixsort(A) 23 | print('Sorted Array:',A) 24 | -------------------------------------------------------------------------------- /sorting/QuickSort.py: -------------------------------------------------------------------------------- 1 | def partition(A, low, high): 2 | pivot = A[low] 3 | i = low + 1 4 | j = high 5 | while True: 6 | while i <= j and A[i] <= pivot: 7 | i = i + 1 8 | while i <=j and A[j] > pivot: 9 | j = j - 1 10 | if i <= j: 11 | A[i], A[j] = A[j], A[i] 12 | else: 13 | break 14 | A[low], A[j] = A[j], A[low] 15 | return j 16 | 17 | 18 | def quicksort(A, low, high): 19 | if low < high: 20 | pi = partition(A, low, high) 21 | quicksort(A, low, pi - 1) 22 | quicksort(A, pi + 1, high) 23 | 24 | 25 | 26 | A = [3, 5, 8, 9, 6, 2] 27 | print('Original Array:',A) 28 | quicksort(A, 0, len(A)-1) 29 | print('Sorted Array:',A) 30 | 31 | -------------------------------------------------------------------------------- /sorting/MergeSort.py: -------------------------------------------------------------------------------- 1 | def mergesort(A, left, right): 2 | if left < right: 3 | mid = (left + right) // 2 4 | mergesort(A, left, mid) 5 | mergesort(A, mid+1, right) 6 | merge(A, left, mid, right) 7 | 8 | def merge(A, left, mid, right): 9 | i = left 10 | j = mid+1 11 | k = left 12 | B = [0] * (right+1) 13 | while i <= mid and j <= right: 14 | if A[i] < A[j]: 15 | B[k] = A[i] 16 | i = i + 1 17 | else: 18 | B[k] = A[j] 19 | j = j + 1 20 | k = k + 1 21 | 22 | while i <= mid: 23 | B[k] = A[i] 24 | i = i + 1 25 | k = k + 1 26 | 27 | while j <= right: 28 | B[k] = A[j] 29 | j = j + 1 30 | k = k + 1 31 | for x in range(left,right+1): 32 | A[x] = B[x] 33 | 34 | 35 | A = [3, 5, 8, 9, 6, 2] 36 | print('Original Array:',A) 37 | mergesort(A,0,len(A)-1) 38 | print('Sorted Array:',A) 39 | -------------------------------------------------------------------------------- /PQ & Heap/PythonsHeapq.py: -------------------------------------------------------------------------------- 1 | import heapq as heap 2 | 3 | L1 = [] 4 | heap.heappush(L1, 25) 5 | heap.heappush(L1, 14) 6 | heap.heappush(L1, 2) 7 | heap.heappush(L1, 20) 8 | heap.heappush(L1, 10) 9 | heap.heappush(L1, 12) 10 | heap.heappush(L1, 18) 11 | 12 | print(L1) 13 | e = heap.heappop(L1) 14 | print('Removed Element:',e) 15 | print(L1) 16 | e = heap.heappop(L1) 17 | print('Removed Element:',e) 18 | print(L1) 19 | 20 | heap.heappush(L1, 5) 21 | print(L1) 22 | e = heap.heappop(L1) 23 | print('Removed Element:',e) 24 | print(L1) 25 | 26 | e = heap.heappushpop(L1, 35) 27 | print('Removed Element:',e) 28 | print(L1) 29 | 30 | e = heap.heapreplace(L1, 8) 31 | print('Removed Element:',e) 32 | print(L1) 33 | 34 | L2 = [20, 14, 2, 15, 10, 21] 35 | print('Original List:',L2) 36 | heap.heapify(L2) 37 | print('List after Heapify:',L2) 38 | 39 | L3 = heap.nsmallest(3, L2) 40 | print('Smallest Elements:',L3) 41 | L3 = heap.nlargest(3, L2) 42 | print('Largest Elements:',L3) 43 | -------------------------------------------------------------------------------- /Stacks /StacksArrays.py: -------------------------------------------------------------------------------- 1 | class StacksArray: 2 | 3 | def __init__(self): 4 | self._data = [] 5 | 6 | def __len__(self): 7 | return len(self._data) 8 | 9 | def isempty(self): 10 | return len(self._data) == 0 11 | 12 | def push(self, e): 13 | self._data.append(e) 14 | 15 | def pop(self): 16 | if self.isempty(): 17 | print('Stack is empty') 18 | return 19 | return self._data.pop() 20 | 21 | def top(self): 22 | if self.isempty(): 23 | print('Stack is empty') 24 | return 25 | return self._data[-1] 26 | 27 | 28 | S = StacksArray() 29 | S.push(5) 30 | S.push(3) 31 | print(S._data) 32 | print(len(S)) 33 | print(S.pop()) 34 | print(S.isempty()) 35 | print(S.pop()) 36 | print(S.isempty()) 37 | S.push(7) 38 | S.push(9) 39 | print(S.top()) 40 | S.push(4) 41 | print(len(S)) 42 | print(S.pop()) 43 | S.push(6) 44 | S.push(8) 45 | print(S.pop()) 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Hashing/Chaining.py: -------------------------------------------------------------------------------- 1 | from LinkedList import LinkedList 2 | 3 | class HashChain: 4 | def __init__(self): 5 | self.hashtable_size = 10 6 | self.hashtable = [0] * self.hashtable_size 7 | for i in range(self.hashtable_size): 8 | self.hashtable[i] = LinkedList() 9 | 10 | def hashcode(self, key): 11 | return key % self.hashtable_size 12 | 13 | def insert(self, element): 14 | i = self.hashcode(element) 15 | self.hashtable[i].insertsorted(element) 16 | 17 | def search(self, key): 18 | i = self.hashcode(key) 19 | return self.hashtable[i].search(key) != -1 20 | 21 | def display(self): 22 | for i in range(self.hashtable_size): 23 | print('[',i,']',end=' ') 24 | self.hashtable[i].display() 25 | print() 26 | 27 | H = HashChain() 28 | H.insert(54) 29 | H.insert(78) 30 | H.insert(64) 31 | H.insert(92) 32 | H.insert(34) 33 | H.insert(86) 34 | H.insert(28) 35 | H.display() 36 | print('Result:',H.search(74)) -------------------------------------------------------------------------------- /PQ & Heap/HeapInsert.py: -------------------------------------------------------------------------------- 1 | class Heap: 2 | def __init__(self): 3 | self._maxsize = 10 4 | self._data = [-1] * self._maxsize 5 | self._csize = 0 6 | 7 | def __len__(self): 8 | return len(self._data) 9 | 10 | def isempty(self): 11 | return len(self._data) == 0 12 | 13 | def insert(self, e): 14 | if self._csize == self._maxsize: 15 | print('No Space in Heap') 16 | return 17 | self._csize = self._csize + 1 18 | hi = self._csize 19 | while hi > 1 and e > self._data[hi // 2]: 20 | self._data[hi] = self._data[hi // 2] 21 | hi = hi // 2 22 | self._data[hi] = e 23 | 24 | def max(self): 25 | if self._csize == 0: 26 | print('Heap is Empty') 27 | return 28 | return self._data[1] 29 | 30 | S = Heap() 31 | S.insert(25) 32 | S.insert(14) 33 | S.insert(2) 34 | S.insert(20) 35 | S.insert(10) 36 | print(S._data) 37 | S.insert(40) 38 | print(S._data) 39 | 40 | 41 | -------------------------------------------------------------------------------- /Hashing/BucketSort.py: -------------------------------------------------------------------------------- 1 | def bucketsort(A): 2 | n = len(A) 3 | maxelement = max(A) 4 | l = [] 5 | buckets = [l] * 10 6 | for i in range(n): 7 | j = int(n * A[i] / (maxelement + 1) ) 8 | if len(buckets[j]) == 0: 9 | buckets[j] = [A[i]] 10 | else: 11 | buckets[j].append(A[i]) 12 | for i in range(10): 13 | insertionsort(buckets[i]) 14 | k = 0 15 | for i in range(10): 16 | for j in range(len(buckets[i])): 17 | A[k] = buckets[i].pop(0) 18 | k = k + 1 19 | 20 | def insertionsort(A): 21 | n = len(A) 22 | for i in range(1,n): 23 | cvalue = A[i] 24 | position = i 25 | while position > 0 and A[position-1] > cvalue: 26 | A[position] = A[position-1] 27 | position = position - 1 28 | A[position] = cvalue 29 | 30 | #A = [63, 250, 835, 947, 651, 28] 31 | A = [54, 78, 64, 92, 34, 86, 28] 32 | #A = [3,5,8,9,6,2] 33 | print('Original Array:',A) 34 | bucketsort(A) 35 | print('Sorted Array:',A) 36 | -------------------------------------------------------------------------------- /linked list/LLCreateDisplay.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class LinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._head = newest 24 | else: 25 | self._tail._next = newest 26 | self._tail = newest 27 | self._size += 1 28 | 29 | def display(self): 30 | p = self._head 31 | while p: 32 | print(p._element,end=' --> ') 33 | p = p._next 34 | print() 35 | 36 | 37 | L = LinkedList() 38 | L.addlast(7) 39 | L.addlast(4) 40 | L.addlast(12) 41 | L.addlast(8) 42 | L.addlast(3) 43 | L.display() 44 | print('Size:',len(L)) -------------------------------------------------------------------------------- /Binary Search Tree/InsertRecursive.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinarySearchTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def rinsert(self,troot,e): 14 | if troot: 15 | if e < troot._element: 16 | troot._left = self.rinsert(troot._left,e) 17 | elif e > troot._element: 18 | troot._right = self.rinsert(troot._right,e) 19 | else: 20 | n = _Node(e) 21 | troot = n 22 | return troot 23 | 24 | def inorder(self,troot): 25 | if troot: 26 | self.inorder(troot._left) 27 | print(troot._element,end=' ') 28 | self.inorder(troot._right) 29 | 30 | B = BinarySearchTree() 31 | B._root= B.rinsert(B._root,50) 32 | B.rinsert(B._root,30) 33 | B.rinsert(B._root,80) 34 | B.rinsert(B._root,10) 35 | B.rinsert(B._root,40) 36 | B.rinsert(B._root,60) 37 | B.rinsert(B._root,90) 38 | B.inorder(B._root) 39 | 40 | -------------------------------------------------------------------------------- /Queues /QueuesArrays.py: -------------------------------------------------------------------------------- 1 | class QueuesArray: 2 | 3 | def __init__(self): 4 | self._data = [] 5 | 6 | def __len__(self): 7 | return len(self._data) 8 | 9 | def isempty(self): 10 | return len(self._data) == 0 11 | 12 | def enqueue(self, e): 13 | self._data.append(e) 14 | 15 | def dequeue(self): 16 | if self.isempty(): 17 | print('Queue is empty') 18 | return 19 | return self._data.pop(0) 20 | 21 | def first(self): 22 | if self.isempty(): 23 | print('Queue is empty') 24 | return 25 | return self._data[0] 26 | 27 | 28 | Q = QueuesArray() 29 | Q.enqueue(5) 30 | Q.enqueue(3) 31 | print('Queue:',Q._data) 32 | print('Queue Length:', len(Q)) 33 | ele = Q.dequeue() 34 | print('Queue:',Q._data) 35 | print('Queue Length:', len(Q)) 36 | print('Removed Element:',ele) 37 | print('Is Queue Empty:',Q.isempty()) 38 | Q.enqueue(7) 39 | print('Queue:',Q._data) 40 | print('Queue Length:', len(Q)) 41 | Q.enqueue(12) 42 | print('Queue:',Q._data) 43 | print('Queue Length:', len(Q)) 44 | ele = Q.dequeue() 45 | print('Queue:',Q._data) 46 | print('Queue Length:', len(Q)) 47 | print('Removed Element:',ele) 48 | print('First Element:',Q.first()) 49 | -------------------------------------------------------------------------------- /Hashing/LinearProbe.py: -------------------------------------------------------------------------------- 1 | class HashLinearProbe: 2 | def __init__(self): 3 | self.hashtable_size = 10 4 | self.hashtable = [0] * self.hashtable_size 5 | 6 | def hashcode(self, key): 7 | return key % self.hashtable_size 8 | 9 | def lprobe(self, element): 10 | i = self.hashcode(element) 11 | j = 0 12 | while self.hashtable[(i+j) % self.hashtable_size] != 0: 13 | j = j + 1 14 | return (i + j) % self.hashtable_size 15 | 16 | def insert(self, element): 17 | i = self.hashcode(element) 18 | if self.hashtable[i] == 0: 19 | self.hashtable[i] = element 20 | else: 21 | i = self.lprobe(element) 22 | self.hashtable[i] = element 23 | 24 | def search(self, key): 25 | i = self.hashcode(key) 26 | j = 0 27 | while self.hashtable[(i+j) % self.hashtable_size] != key: 28 | if self.hashtable[(i+j) % self.hashtable_size] == 0: 29 | return False 30 | j = j + 1 31 | return True 32 | 33 | def display(self): 34 | print(self.hashtable) 35 | 36 | HC = HashLinearProbe() 37 | #HC.display() 38 | HC.insert(54) 39 | HC.insert(78) 40 | HC.insert(64) 41 | HC.insert(92) 42 | HC.insert(34) 43 | HC.insert(86) 44 | HC.insert(28) 45 | HC.display() 46 | print(HC.search(34)) -------------------------------------------------------------------------------- /Queues /DEQueArrays.py: -------------------------------------------------------------------------------- 1 | class DEQueArray: 2 | 3 | def __init__(self): 4 | self._data = [] 5 | 6 | def __len__(self): 7 | return len(self._data) 8 | 9 | def isempty(self): 10 | return len(self._data) == 0 11 | 12 | def addfirst(self, e): 13 | self._data.insert(0,e) 14 | 15 | def addlast(self, e): 16 | self._data.append(e) 17 | 18 | def removefirst(self): 19 | if self.isempty(): 20 | print('DEQue is empty') 21 | return 22 | return self._data.pop(0) 23 | 24 | def removelast(self): 25 | if self.isempty(): 26 | print('DEQue is empty') 27 | return 28 | return self._data.pop() 29 | 30 | def first(self): 31 | if self.isempty(): 32 | print('DEQue is empty') 33 | return 34 | return self._data[0] 35 | 36 | def last(self): 37 | if self.isempty(): 38 | print('DEQue is empty') 39 | return 40 | return self._data[-1] 41 | 42 | 43 | D = DEQueArray() 44 | D.addfirst(5) 45 | D.addfirst(3) 46 | D.addlast(7) 47 | D.addlast(12) 48 | print(D._data) 49 | print('Length:',len(D)) 50 | print(D.removefirst()) 51 | print(D.removelast()) 52 | print(D._data) 53 | print('First Element:',D.first()) 54 | print('Last Element:',D.last()) 55 | 56 | -------------------------------------------------------------------------------- /Binary Tree/QueuesLinked.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class QueuesLinked: 9 | def __init__(self): 10 | self._front = None 11 | self._rear = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def enqueue(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._front = newest 24 | else: 25 | self._rear._next = newest 26 | self._rear = newest 27 | self._size += 1 28 | 29 | def dequeue(self): 30 | if self.isempty(): 31 | print('Queue is empty') 32 | return 33 | e = self._front._element 34 | self._front = self._front._next 35 | self._size -= 1 36 | if self.isempty(): 37 | self._rear = None 38 | return e 39 | 40 | def first(self): 41 | if self.isempty(): 42 | print('Queue is empty') 43 | return 44 | return self._front._element 45 | 46 | def display(self): 47 | p = self._front 48 | while p: 49 | print(p._element,end=' <-- ') 50 | p = p._next 51 | print() 52 | -------------------------------------------------------------------------------- /Graph/QueuesLinked.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class QueuesLinked: 9 | def __init__(self): 10 | self._front = None 11 | self._rear = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def enqueue(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._front = newest 24 | else: 25 | self._rear._next = newest 26 | self._rear = newest 27 | self._size += 1 28 | 29 | def dequeue(self): 30 | if self.isempty(): 31 | print('Queue is empty') 32 | return 33 | e = self._front._element 34 | self._front = self._front._next 35 | self._size -= 1 36 | if self.isempty(): 37 | self._rear = None 38 | return e 39 | 40 | def first(self): 41 | if self.isempty(): 42 | print('Queue is empty') 43 | return 44 | return self._front._element 45 | 46 | def display(self): 47 | p = self._front 48 | while p: 49 | print(p._element,end=' <-- ') 50 | p = p._next 51 | print() 52 | 53 | -------------------------------------------------------------------------------- /linked list/LLSearchElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class LinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._head = newest 24 | else: 25 | self._tail._next = newest 26 | self._tail = newest 27 | self._size += 1 28 | 29 | def display(self): 30 | p = self._head 31 | while p: 32 | print(p._element,end='-->') 33 | p = p._next 34 | print() 35 | 36 | def search(self,key): 37 | p = self._head 38 | index = 0 39 | while p: 40 | if p._element == key: 41 | return index 42 | p = p._next 43 | index += 1 44 | return -1 45 | 46 | L = LinkedList() 47 | L.addlast(7) 48 | L.addlast(4) 49 | L.addlast(12) 50 | L.addlast(8) 51 | L.addlast(3) 52 | L.display() 53 | i = L.search(8) 54 | print('Result:',i) 55 | index = L.search(20) 56 | print('Result:',i) 57 | 58 | -------------------------------------------------------------------------------- /linked list/DLLCreateDisplay.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next', '_prev' 3 | 4 | def __init__(self, element, next, prev): 5 | self._element = element 6 | self._next = next 7 | self._prev = prev 8 | 9 | class DoublyLinkedList: 10 | def __init__(self): 11 | self._head = None 12 | self._tail = None 13 | self._size = 0 14 | 15 | def __len__(self): 16 | return self._size 17 | 18 | def isempty(self): 19 | return self._size == 0 20 | 21 | def addlast(self, e): 22 | newest = _Node(e, None, None) 23 | if self.isempty(): 24 | self._head = newest 25 | self._tail = newest 26 | else: 27 | self._tail._next = newest 28 | newest._prev = self._tail 29 | self._tail = newest 30 | self._size += 1 31 | 32 | def display(self): 33 | p = self._head 34 | while p: 35 | print(p._element,end=' --> ') 36 | p = p._next 37 | print() 38 | 39 | def displayrev(self): 40 | p = self._tail 41 | while p: 42 | print(p._element,end=' <-- ') 43 | p = p._prev 44 | print() 45 | 46 | 47 | L = DoublyLinkedList() 48 | L.addlast(7) 49 | L.addlast(4) 50 | L.addlast(12) 51 | L.addlast(8) 52 | L.addlast(3) 53 | L.display() 54 | print('Size:',len(L)) 55 | L.displayrev() 56 | -------------------------------------------------------------------------------- /Binary Tree/BinaryTree (1).py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinaryTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def maketree(self, e, left, right): 14 | self._root = _Node(e,left._root,right._root) 15 | 16 | def inorder(self,troot): 17 | if troot: 18 | self.inorder(troot._left) 19 | print(troot._element,end=' ') 20 | self.inorder(troot._right) 21 | 22 | def preorder(self,troot): 23 | if troot: 24 | print(troot._element,end=' ') 25 | self.preorder(troot._left) 26 | self.preorder(troot._right) 27 | 28 | def postorder(self,troot): 29 | if troot: 30 | self.postorder(troot._left) 31 | self.postorder(troot._right) 32 | print(troot._element, end=' ') 33 | 34 | 35 | x = BinaryTree() 36 | y = BinaryTree() 37 | z = BinaryTree() 38 | r = BinaryTree() 39 | s = BinaryTree() 40 | t = BinaryTree() 41 | a = BinaryTree() 42 | x.maketree(40,a,a) 43 | y.maketree(60,a,a) 44 | z.maketree(20,x,a) 45 | r.maketree(50,a,y) 46 | s.maketree(30,r,a) 47 | t.maketree(10,z,s) 48 | print('Inorder Traversal') 49 | t.inorder(t._root) 50 | print() 51 | print('Preorder Traversal') 52 | t.preorder(t._root) 53 | print() 54 | print('Postorder Traversal') 55 | t.postorder(t._root) 56 | 57 | -------------------------------------------------------------------------------- /Binary Tree/BinaryTree (2).py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinaryTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def maketree(self, e, left, right): 14 | self._root = _Node(e,left._root,right._root) 15 | 16 | def inorder(self,troot): 17 | if troot: 18 | self.inorder(troot._left) 19 | print(troot._element,end=' ') 20 | self.inorder(troot._right) 21 | 22 | def preorder(self,troot): 23 | if troot: 24 | print(troot._element,end=' ') 25 | self.preorder(troot._left) 26 | self.preorder(troot._right) 27 | 28 | def postorder(self,troot): 29 | if troot: 30 | self.postorder(troot._left) 31 | self.postorder(troot._right) 32 | print(troot._element, end=' ') 33 | 34 | 35 | x = BinaryTree() 36 | y = BinaryTree() 37 | z = BinaryTree() 38 | r = BinaryTree() 39 | s = BinaryTree() 40 | t = BinaryTree() 41 | a = BinaryTree() 42 | x.maketree(40,a,a) 43 | y.maketree(60,a,a) 44 | z.maketree(20,x,a) 45 | r.maketree(50,a,y) 46 | s.maketree(30,r,a) 47 | t.maketree(10,z,s) 48 | print('Inorder Traversal') 49 | t.inorder(t._root) 50 | print() 51 | print('Preorder Traversal') 52 | t.preorder(t._root) 53 | print() 54 | print('Postorder Traversal') 55 | t.postorder(t._root) 56 | 57 | -------------------------------------------------------------------------------- /Binary Tree/BinaryTree.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinaryTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def maketree(self, e, left, right): 14 | self._root = _Node(e,left._root,right._root) 15 | 16 | def inorder(self,troot): 17 | if troot: 18 | self.inorder(troot._left) 19 | print(troot._element,end=' ') 20 | self.inorder(troot._right) 21 | 22 | def preorder(self,troot): 23 | if troot: 24 | print(troot._element,end=' ') 25 | self.preorder(troot._left) 26 | self.preorder(troot._right) 27 | 28 | def postorder(self,troot): 29 | if troot: 30 | self.postorder(troot._left) 31 | self.postorder(troot._right) 32 | print(troot._element, end=' ') 33 | 34 | 35 | x = BinaryTree() 36 | y = BinaryTree() 37 | z = BinaryTree() 38 | r = BinaryTree() 39 | s = BinaryTree() 40 | t = BinaryTree() 41 | a = BinaryTree() 42 | x.maketree(40,a,a) 43 | y.maketree(60,a,a) 44 | z.maketree(20,x,a) 45 | r.maketree(50,a,y) 46 | s.maketree(30,r,a) 47 | t.maketree(10,z,s) 48 | print('Inorder Traversal') 49 | t.inorder(t._root) 50 | print() 51 | print('Preorder Traversal') 52 | t.preorder(t._root) 53 | print() 54 | print('Postorder Traversal') 55 | t.postorder(t._root) 56 | 57 | -------------------------------------------------------------------------------- /linked list/CLLCreateDisplay.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class CircularLinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | newest._next = newest 24 | self._head = newest 25 | else: 26 | newest._next = self._tail._next 27 | self._tail._next = newest 28 | self._tail = newest 29 | self._size += 1 30 | 31 | def display(self): 32 | p = self._head 33 | i = 0 34 | while i < len(self): 35 | print(p._element,end='-->') 36 | p = p._next 37 | i += 1 38 | print() 39 | 40 | def search(self,key): 41 | p = self._head 42 | index = 0 43 | while index < len(self): 44 | if p._element == key: 45 | return index 46 | p = p._next 47 | index = index + 1 48 | return -1 49 | 50 | C = CircularLinkedList() 51 | C.addlast(7) 52 | C.addlast(4) 53 | C.addlast(12) 54 | C.display() 55 | print('Size:',len(C)) 56 | C.addlast(8) 57 | C.addlast(3) 58 | C.display() 59 | print('Size:',len(C)) 60 | -------------------------------------------------------------------------------- /PQ & Heap/Heaps.py: -------------------------------------------------------------------------------- 1 | class Heap: 2 | def __init__(self): 3 | self._maxsize = 10 4 | self._data = [-1] * self._maxsize 5 | self._csize = 0 6 | 7 | def __len__(self): 8 | return len(self._data) 9 | 10 | def isempty(self): 11 | return len(self._data) == 0 12 | 13 | def insert(self, e): 14 | if self._csize == self._maxsize: 15 | print('No Space in Heap') 16 | return 17 | self._csize = self._csize + 1 18 | hi = self._csize 19 | while hi > 1 and e > self._data[hi // 2]: 20 | self._data[hi] = self._data[hi // 2] 21 | hi = hi // 2 22 | self._data[hi] = e 23 | 24 | def max(self): 25 | if self._csize == 0: 26 | print('Heap is Empty') 27 | return 28 | return self._data[1] 29 | 30 | def deletemax(self): 31 | if self._csize == 0: 32 | print('Heap is Empty') 33 | return 34 | e = self._data[1] 35 | self._data[1] = self._data[self._csize] 36 | self._data[self._csize] = -1 37 | self._csize = self._csize - 1 38 | i = 1 39 | j = i * 2 40 | while j <= self._csize: 41 | if self._data[j] < self._data[j+1]: 42 | j = j + 1 43 | if self._data[i] < self._data[j]: 44 | temp = self._data[i] 45 | self._data[i] = self._data[j] 46 | self._data[j] = temp 47 | i = j 48 | j = i * 2 49 | else: 50 | break 51 | return e 52 | 53 | -------------------------------------------------------------------------------- /Graph/Graphs.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class Graph: 4 | 5 | def __init__(self, vertices): 6 | self._vertices = vertices 7 | self._adjMat = np.zeros((vertices, vertices)) 8 | 9 | def insert_edge(self, u, v, x=1): 10 | self._adjMat[u][v] = x 11 | 12 | def remove_edge(self, u, v): 13 | self._adjMat[u][v] = 0 14 | 15 | def exist_edge(self, u, v): 16 | return self._adjMat[u][v] != 0 17 | 18 | def vertex_count(self): 19 | return self._vertices 20 | 21 | def edge_count(self): 22 | count = 0 23 | for i in range(self._vertices): 24 | for j in range(self._vertices): 25 | if self._adjMat[i][j] != 0: 26 | count = count + 1 27 | return count 28 | 29 | def vertices(self): 30 | for i in range(self._vertices): 31 | print(i,end=' ') 32 | print() 33 | 34 | def edges(self): 35 | for i in range(self._vertices): 36 | for j in range(self._vertices): 37 | if self._adjMat[i][j] != 0: 38 | print(i,'--',j) 39 | 40 | def outdegree(self, v): 41 | count = 0 42 | for j in range(self._vertices): 43 | if self._adjMat[v][j] != 0: 44 | count = count + 1 45 | return count 46 | 47 | def indegree(self, v): 48 | count = 0 49 | for i in range(self._vertices): 50 | if self._adjMat[i][v] != 0: 51 | count = count + 1 52 | return count 53 | 54 | def display_adjMat(self): 55 | print(self._adjMat) 56 | 57 | 58 | -------------------------------------------------------------------------------- /Stacks /StacksLinked.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class StacksLinked: 9 | def __init__(self): 10 | self._top = None 11 | self._size = 0 12 | 13 | def __len__(self): 14 | return self._size 15 | 16 | def isempty(self): 17 | return self._size == 0 18 | 19 | def push(self, e): 20 | newest = _Node(e, None) 21 | if self.isempty(): 22 | self._top = newest 23 | else: 24 | newest._next = self._top 25 | self._top = newest 26 | self._size += 1 27 | 28 | def pop(self): 29 | if self.isempty(): 30 | print('Stack is empty') 31 | return 32 | e = self._top._element 33 | self._top = self._top._next 34 | self._size -= 1 35 | return e 36 | 37 | def top(self): 38 | if self.isempty(): 39 | print('Stack is empty') 40 | return 41 | return self._top._element 42 | 43 | def display(self): 44 | p = self._top 45 | while p: 46 | print(p._element,end='-->') 47 | p = p._next 48 | print() 49 | 50 | 51 | S = StacksLinked() 52 | S.push(5) 53 | S.push(3) 54 | print(len(S)) 55 | S.display() 56 | print(S.pop()) 57 | print(S.isempty()) 58 | 59 | print(S.pop()) 60 | print(S.isempty()) 61 | S.push(7) 62 | S.push(9) 63 | print(S.top()) 64 | S.push(4) 65 | print(len(S)) 66 | print(S.pop()) 67 | S.push(6) 68 | S.push(8) 69 | print(S.pop()) -------------------------------------------------------------------------------- /Binary Search Tree/SearchIterative.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinarySearchTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def insert(self,troot,e): 14 | temp = None 15 | while troot: 16 | temp = troot 17 | if e < troot._element: 18 | troot = troot._left 19 | elif e > troot._element: 20 | troot = troot._right 21 | n = _Node(e) 22 | if self._root: 23 | if e < temp._element: 24 | temp._left = n 25 | else: 26 | temp._right = n 27 | else: 28 | self._root = n 29 | 30 | def search(self,key): 31 | troot = self._root 32 | while troot: 33 | if key == troot._element: 34 | return True 35 | elif key < troot._element: 36 | troot = troot._left 37 | elif key > troot._element: 38 | troot = troot._right 39 | return False 40 | 41 | def inorder(self,troot): 42 | if troot: 43 | self.inorder(troot._left) 44 | print(troot._element,end=' ') 45 | self.inorder(troot._right) 46 | 47 | B = BinarySearchTree() 48 | B.insert(B._root,50) 49 | B.insert(B._root,30) 50 | B.insert(B._root,80) 51 | B.insert(B._root,10) 52 | B.insert(B._root,40) 53 | B.insert(B._root,60) 54 | B.insert(B._root,70) 55 | B.insert(B._root,90) 56 | B.inorder(B._root) 57 | print() 58 | print(B.search(60)) 59 | print(B.search(30)) 60 | print(B.search(70)) 61 | 62 | -------------------------------------------------------------------------------- /Binary Tree/BinaryTreeCountNodes.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinaryTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def maketree(self, e, left, right): 14 | self._root = _Node(e,left._root,right._root) 15 | 16 | def inorder(self,troot): 17 | if troot: 18 | self.inorder(troot._left) 19 | print(troot._element,end=' ') 20 | self.inorder(troot._right) 21 | 22 | def preorder(self,troot): 23 | if troot: 24 | print(troot._element,end=' ') 25 | self.preorder(troot._left) 26 | self.preorder(troot._right) 27 | 28 | def postorder(self,troot): 29 | if troot: 30 | self.postorder(troot._left) 31 | self.postorder(troot._right) 32 | print(troot._element, end=' ') 33 | 34 | def count(self,troot): 35 | if troot: 36 | x = self.count(troot._left) 37 | y = self.count(troot._right) 38 | return x + y + 1 39 | return 0 40 | 41 | 42 | x = BinaryTree() 43 | y = BinaryTree() 44 | z = BinaryTree() 45 | r = BinaryTree() 46 | s = BinaryTree() 47 | t = BinaryTree() 48 | a = BinaryTree() 49 | x.maketree(40,a,a) 50 | y.maketree(60,a,a) 51 | z.maketree(20,x,a) 52 | r.maketree(50,a,y) 53 | s.maketree(30,r,a) 54 | t.maketree(10,z,s) 55 | print('Inorder Traversal') 56 | t.inorder(t._root) 57 | print() 58 | print('Preorder Traversal') 59 | t.preorder(t._root) 60 | print() 61 | print('Postorder Traversal') 62 | t.postorder(t._root) 63 | print() 64 | print('Number of Nodes') 65 | print(t.count(t._root)) 66 | 67 | -------------------------------------------------------------------------------- /linked list/DLLAddFirstElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next', '_prev' 3 | 4 | def __init__(self, element, next, prev): 5 | self._element = element 6 | self._next = next 7 | self._prev = prev 8 | 9 | class DoublyLinkedList: 10 | def __init__(self): 11 | self._head = None 12 | self._tail = None 13 | self._size = 0 14 | 15 | def __len__(self): 16 | return self._size 17 | 18 | def isempty(self): 19 | return self._size == 0 20 | 21 | def addlast(self, e): 22 | newest = _Node(e, None, None) 23 | if self.isempty(): 24 | self._head = newest 25 | self._tail = newest 26 | else: 27 | self._tail._next = newest 28 | newest._prev = self._tail 29 | self._tail = newest 30 | self._size += 1 31 | 32 | def addfirst(self, e): 33 | newest = _Node(e, None, None) 34 | if self.isempty(): 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | newest._next = self._head 39 | self._head._prev = newest 40 | self._head = newest 41 | self._size += 1 42 | 43 | def display(self): 44 | p = self._head 45 | while p: 46 | print(p._element,end=' --> ') 47 | p = p._next 48 | print() 49 | 50 | def displayrev(self): 51 | p = self._tail 52 | while p: 53 | print(p._element,end=' <-- ') 54 | p = p._prev 55 | print() 56 | 57 | L = DoublyLinkedList() 58 | L.addlast(7) 59 | L.addlast(4) 60 | L.addlast(12) 61 | L.addlast(8) 62 | L.addlast(3) 63 | L.display() 64 | print('Size:',len(L)) 65 | L.addfirst(25) 66 | L.display() 67 | print('Size:',len(L)) 68 | L.displayrev() 69 | -------------------------------------------------------------------------------- /linked list/LLAddFirstElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class LinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._head = newest 24 | else: 25 | self._tail._next = newest 26 | self._tail = newest 27 | self._size += 1 28 | 29 | def display(self): 30 | p = self._head 31 | while p: 32 | print(p._element,end='-->') 33 | p = p._next 34 | print() 35 | 36 | def search(self,key): 37 | p = self._head 38 | index = 0 39 | while p: 40 | if p._element == key: 41 | return index 42 | p = p._next 43 | index = index + 1 44 | return -1 45 | 46 | def addfirst(self, e): 47 | newest = _Node(e, None) 48 | if self.isempty(): 49 | self._head = newest 50 | self._tail = newest 51 | else: 52 | newest._next = self._head 53 | self._head = newest 54 | self._size += 1 55 | 56 | L = LinkedList() 57 | L.addlast(7) 58 | L.addlast(4) 59 | L.addlast(12) 60 | L.addlast(8) 61 | L.addlast(3) 62 | L.display() 63 | print('Size:',len(L)) 64 | L.addfirst(15) 65 | L.display() 66 | print('Size:',len(L)) 67 | L.addfirst(25) 68 | L.display() 69 | print('Size:',len(L)) 70 | L.addlast(35) 71 | L.display() 72 | print('Size:',len(L)) 73 | -------------------------------------------------------------------------------- /Binary Search Tree/SearchRecursive.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinarySearchTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def insert(self,troot,e): 14 | temp = None 15 | while troot: 16 | temp = troot 17 | if e == troot._element: 18 | return 19 | elif e < troot._element: 20 | troot = troot._left 21 | elif e > troot._element: 22 | troot = troot._right 23 | n = _Node(e) 24 | if self._root: 25 | if e < temp._element: 26 | temp._left = n 27 | else: 28 | temp._right = n 29 | else: 30 | self._root = n 31 | 32 | def rsearch(self,troot,key): 33 | if troot: 34 | if key == troot._element: 35 | return True 36 | elif key < troot._element: 37 | return self.rsearch(troot._left,key) 38 | elif key > troot._element: 39 | return self.rsearch(troot._right,key) 40 | else: 41 | return False 42 | 43 | def inorder(self,troot): 44 | if troot: 45 | self.inorder(troot._left) 46 | print(troot._element,end=' ') 47 | self.inorder(troot._right) 48 | 49 | B = BinarySearchTree() 50 | B.insert(B._root,50) 51 | B.insert(B._root,30) 52 | B.insert(B._root,80) 53 | B.insert(B._root,10) 54 | B.insert(B._root,40) 55 | B.insert(B._root,60) 56 | B.insert(B._root,90) 57 | B.inorder(B._root) 58 | print() 59 | print(B.rsearch(B._root,60)) 60 | print(B.rsearch(B._root,30)) 61 | print(B.rsearch(B._root,70)) 62 | 63 | -------------------------------------------------------------------------------- /PQ & Heap/HeapDelete.py: -------------------------------------------------------------------------------- 1 | class Heap: 2 | def __init__(self): 3 | self._maxsize = 10 4 | self._data = [-1] * self._maxsize 5 | self._csize = 0 6 | 7 | def __len__(self): 8 | return len(self._data) 9 | 10 | def isempty(self): 11 | return len(self._data) == 0 12 | 13 | 14 | def insert(self, e): 15 | if self._csize == self._maxsize: 16 | print('No Space in Heap') 17 | return 18 | self._csize = self._csize + 1 19 | hi = self._csize 20 | while hi > 1 and e > self._data[hi // 2]: 21 | self._data[hi] = self._data[hi // 2] 22 | hi = hi // 2 23 | self._data[hi] = e 24 | 25 | def max(self): 26 | if self._csize == 0: 27 | print('Heap is Empty') 28 | return 29 | return self._data[1] 30 | 31 | def deletemax(self): 32 | if self._csize == 0: 33 | print('Heap is Empty') 34 | return 35 | e = self._data[1] 36 | self._data[1] = self._data[self._csize] 37 | self._data[self._csize] = -1 38 | self._csize = self._csize - 1 39 | i = 1 40 | j = i * 2 41 | while j <= self._csize: 42 | if self._data[j] < self._data[j+1]: 43 | j = j + 1 44 | if self._data[i] < self._data[j]: 45 | temp = self._data[i] 46 | self._data[i] = self._data[j] 47 | self._data[j] = temp 48 | i = j 49 | j = i * 2 50 | else: 51 | break 52 | return e 53 | 54 | 55 | S = Heap() 56 | S.insert(25) 57 | S.insert(14) 58 | S.insert(2) 59 | S.insert(20) 60 | S.insert(10) 61 | S.insert(40) 62 | print(S._data) 63 | print(S.deletemax()) 64 | print(S._data) 65 | print(S.deletemax()) 66 | print(S._data) 67 | 68 | -------------------------------------------------------------------------------- /Binary Tree/BinaryTreeHeight.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinaryTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def maketree(self, e, left, right): 14 | self._root = _Node(e,left._root,right._root) 15 | 16 | def inorder(self,troot): 17 | if troot: 18 | self.inorder(troot._left) 19 | print(troot._element,end=' ') 20 | self.inorder(troot._right) 21 | 22 | def preorder(self,troot): 23 | if troot: 24 | print(troot._element,end=' ') 25 | self.preorder(troot._left) 26 | self.preorder(troot._right) 27 | 28 | def postorder(self,troot): 29 | if troot: 30 | self.postorder(troot._left) 31 | self.postorder(troot._right) 32 | print(troot._element, end=' ') 33 | 34 | def height(self,troot): 35 | if troot: 36 | x = self.height(troot._left) 37 | y = self.height(troot._right) 38 | if x > y: 39 | return x + 1 40 | else: 41 | return y + 1 42 | return 0 43 | 44 | x = BinaryTree() 45 | y = BinaryTree() 46 | z = BinaryTree() 47 | r = BinaryTree() 48 | s = BinaryTree() 49 | t = BinaryTree() 50 | a = BinaryTree() 51 | x.maketree(40,a,a) 52 | y.maketree(60,a,a) 53 | z.maketree(20,x,a) 54 | r.maketree(50,a,y) 55 | s.maketree(30,r,a) 56 | t.maketree(10,z,s) 57 | print('Inorder Traversal') 58 | t.inorder(t._root) 59 | print() 60 | print('Preorder Traversal') 61 | t.preorder(t._root) 62 | print() 63 | print('Postorder Traversal') 64 | t.postorder(t._root) 65 | print('Height') 66 | print(t.height(t._root)-1) 67 | print() 68 | -------------------------------------------------------------------------------- /linked list/CLLAddFirstElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class CircularLinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | newest._next = newest 24 | self._head = newest 25 | else: 26 | newest._next = self._tail._next 27 | self._tail._next = newest 28 | self._tail = newest 29 | self._size += 1 30 | 31 | def addfirst(self,e): 32 | newest = _Node(e, None) 33 | if self.isempty(): 34 | newest._next = newest 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | self._tail._next = newest 39 | newest._next = self._head 40 | self._head = newest 41 | self._size += 1 42 | 43 | def display(self): 44 | p = self._head 45 | i = 0 46 | while i < len(self): 47 | print(p._element,end='-->') 48 | p = p._next 49 | i += 1 50 | print() 51 | 52 | def search(self,key): 53 | p = self._head 54 | index = 0 55 | while index < len(self): 56 | if p._element == key: 57 | return index 58 | p = p._next 59 | index = index + 1 60 | return -1 61 | 62 | C = CircularLinkedList() 63 | C.addlast(7) 64 | C.addlast(4) 65 | C.addlast(12) 66 | C.addlast(8) 67 | C.addlast(3) 68 | C.display() 69 | print('Size:',len(C)) 70 | C.addfirst(25) 71 | C.display() 72 | print('Size:',len(C)) 73 | -------------------------------------------------------------------------------- /Queues /QueuesLinked.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class QueuesLinked: 9 | def __init__(self): 10 | self._front = None 11 | self._rear = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def enqueue(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._front = newest 24 | else: 25 | self._rear._next = newest 26 | self._rear = newest 27 | self._size += 1 28 | 29 | def dequeue(self): 30 | if self.isempty(): 31 | print('Queue is empty') 32 | return 33 | e = self._front._element 34 | self._front = self._front._next 35 | self._size -= 1 36 | if self.isempty(): 37 | self._rear = None 38 | return e 39 | 40 | def first(self): 41 | if self.isempty(): 42 | print('Queue is empty') 43 | return 44 | return self._front._element 45 | 46 | def display(self): 47 | p = self._front 48 | while p: 49 | print(p._element,end=' <-- ') 50 | p = p._next 51 | print() 52 | 53 | 54 | Q = QueuesLinked() 55 | Q.enqueue(5) 56 | Q.enqueue(3) 57 | print('Queue:') 58 | Q.display() 59 | print('Queue Length:', len(Q)) 60 | ele = Q.dequeue() 61 | print('Queue:') 62 | Q.display() 63 | print('Queue Length:', len(Q)) 64 | print('Removed Element:',ele) 65 | print('Is Queue Empty:',Q.isempty()) 66 | Q.enqueue(7) 67 | print('Queue:') 68 | Q.display() 69 | print('Queue Length:', len(Q)) 70 | Q.enqueue(12) 71 | print('Queue:') 72 | Q.display() 73 | print('Queue Length:', len(Q)) 74 | ele = Q.dequeue() 75 | print('Queue:') 76 | Q.display() 77 | print('Queue Length:', len(Q)) 78 | print('Removed Element:',ele) 79 | print('First Element:',Q.first()) 80 | -------------------------------------------------------------------------------- /linked list/LLAddAnyElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class LinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._head = newest 24 | else: 25 | self._tail._next = newest 26 | self._tail = newest 27 | self._size += 1 28 | 29 | def addfirst(self, e): 30 | newest = _Node(e, None) 31 | if self.isempty(): 32 | self._head = newest 33 | self._tail = newest 34 | else: 35 | newest._next = self._head 36 | self._head = newest 37 | self._size += 1 38 | 39 | def addany(self, e, position): 40 | newest = _Node(e, None) 41 | p = self._head 42 | i = 1 43 | while i < position-1: 44 | p = p._next 45 | i = i + 1 46 | newest._next = p._next 47 | p._next = newest 48 | self._size += 1 49 | 50 | def display(self): 51 | p = self._head 52 | while p: 53 | print(p._element,end='-->') 54 | p = p._next 55 | print() 56 | 57 | def search(self,key): 58 | p = self._head 59 | index = 0 60 | while p: 61 | if p._element == key: 62 | return index 63 | p = p._next 64 | index = index + 1 65 | return -1 66 | 67 | L = LinkedList() 68 | L.addlast(7) 69 | L.addlast(4) 70 | L.addlast(12) 71 | L.addlast(8) 72 | L.addlast(3) 73 | L.display() 74 | print('Size:',len(L)) 75 | L.addany(20,3) 76 | L.display() 77 | print('Size:',len(L)) 78 | L.addany(40,5) 79 | L.display() 80 | print('Size:',len(L)) 81 | -------------------------------------------------------------------------------- /Binary Tree/LevelOrderTraversal.py: -------------------------------------------------------------------------------- 1 | from QueuesLinked import QueuesLinked 2 | 3 | class _Node: 4 | __slots__ = '_element', '_left', '_right' 5 | 6 | def __init__(self, element, left=None, right=None): 7 | self._element = element 8 | self._left = left 9 | self._right = right 10 | 11 | class BinaryTree: 12 | def __init__(self): 13 | self._root = None 14 | 15 | def maketree(self, e, left, right): 16 | self._root = _Node(e,left._root,right._root) 17 | 18 | def inorder(self,troot): 19 | if troot: 20 | self.inorder(troot._left) 21 | print(troot._element,end=' ') 22 | self.inorder(troot._right) 23 | 24 | def preorder(self,troot): 25 | if troot: 26 | print(troot._element,end=' ') 27 | self.preorder(troot._left) 28 | self.preorder(troot._right) 29 | 30 | def postorder(self,troot): 31 | if troot: 32 | self.postorder(troot._left) 33 | self.postorder(troot._right) 34 | print(troot._element, end=' ') 35 | 36 | def levelorder(self): 37 | Q = QueuesLinked() 38 | t = self._root 39 | print(t._element,end=' ') 40 | Q.enqueue(t) 41 | while not Q.isempty(): 42 | t = Q.dequeue() 43 | if t._left: 44 | print(t._left._element,end=' ') 45 | Q.enqueue(t._left) 46 | if t._right: 47 | print(t._right._element,end=' ') 48 | Q.enqueue(t._right) 49 | 50 | 51 | x = BinaryTree() 52 | y = BinaryTree() 53 | z = BinaryTree() 54 | r = BinaryTree() 55 | s = BinaryTree() 56 | t = BinaryTree() 57 | a = BinaryTree() 58 | x.maketree(40,a,a) 59 | y.maketree(60,a,a) 60 | z.maketree(20,x,a) 61 | r.maketree(50,a,y) 62 | s.maketree(30,r,a) 63 | t.maketree(10,z,s) 64 | print('Inorder Traversal') 65 | t.inorder(t._root) 66 | print() 67 | print('Preorder Traversal') 68 | t.preorder(t._root) 69 | print() 70 | print('Postorder Traversal') 71 | t.postorder(t._root) 72 | print() 73 | t.levelorder() 74 | print() 75 | -------------------------------------------------------------------------------- /linked list/DLLAddAnyElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next', '_prev' 3 | 4 | def __init__(self, element, next, prev): 5 | self._element = element 6 | self._next = next 7 | self._prev = prev 8 | 9 | class DoublyLinkedList: 10 | def __init__(self): 11 | self._head = None 12 | self._tail = None 13 | self._size = 0 14 | 15 | def __len__(self): 16 | return self._size 17 | 18 | def isempty(self): 19 | return self._size == 0 20 | 21 | def addlast(self, e): 22 | newest = _Node(e, None, None) 23 | if self.isempty(): 24 | self._head = newest 25 | self._tail = newest 26 | else: 27 | self._tail._next = newest 28 | newest._prev = self._tail 29 | self._tail = newest 30 | self._size += 1 31 | 32 | def addfirst(self, e): 33 | newest = _Node(e, None, None) 34 | if self.isempty(): 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | newest._next = self._head 39 | self._head._prev = newest 40 | self._head = newest 41 | self._size += 1 42 | 43 | def addany(self, e, position): 44 | newest = _Node(e, None, None) 45 | p = self._head 46 | i = 1 47 | while i < position-1: 48 | p = p._next 49 | i = i + 1 50 | newest._next = p._next 51 | p._next._prev = newest 52 | p._next = newest 53 | newest._prev = p 54 | self._size += 1 55 | 56 | def display(self): 57 | p = self._head 58 | while p: 59 | print(p._element,end=' --> ') 60 | p = p._next 61 | print() 62 | 63 | def displayrev(self): 64 | p = self._tail 65 | while p: 66 | print(p._element,end=' <-- ') 67 | p = p._prev 68 | print() 69 | 70 | L = DoublyLinkedList() 71 | L.addlast(7) 72 | L.addlast(4) 73 | L.addlast(12) 74 | L.addlast(8) 75 | L.addlast(3) 76 | L.display() 77 | print('Size:',len(L)) 78 | L.addany(20,3) 79 | L.display() 80 | print('Size:',len(L)) 81 | L.displayrev() 82 | -------------------------------------------------------------------------------- /linked list/CLLAddAnyElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class CircularLinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | newest._next = newest 24 | self._head = newest 25 | else: 26 | newest._next = self._tail._next 27 | self._tail._next = newest 28 | self._tail = newest 29 | self._size += 1 30 | 31 | def addfirst(self,e): 32 | newest = _Node(e, None) 33 | if self.isempty(): 34 | newest._next = newest 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | self._tail._next = newest 39 | newest._next = self._head 40 | self._head = newest 41 | self._size += 1 42 | 43 | def addany(self, e, position): 44 | newest = _Node(e, None) 45 | p = self._head 46 | i = 1 47 | while i < position - 1: 48 | p = p._next 49 | i = i + 1 50 | newest._next = p._next 51 | p._next = newest 52 | self._size += 1 53 | 54 | def display(self): 55 | p = self._head 56 | i = 0 57 | while i < len(self): 58 | print(p._element,end='-->') 59 | p = p._next 60 | i += 1 61 | print() 62 | 63 | def search(self,key): 64 | p = self._head 65 | index = 0 66 | while index < len(self): 67 | if p._element == key: 68 | return index 69 | p = p._next 70 | index = index + 1 71 | return -1 72 | 73 | C = CircularLinkedList() 74 | C.addlast(7) 75 | C.addlast(4) 76 | C.addlast(12) 77 | C.addlast(8) 78 | C.addlast(3) 79 | C.display() 80 | print('Size:',len(C)) 81 | C.addany(25,3) 82 | C.display() 83 | print('Size:',len(C)) 84 | -------------------------------------------------------------------------------- /Binary Search Tree/DeleteBinarySearchTree.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_left', '_right' 3 | 4 | def __init__(self, element, left=None, right=None): 5 | self._element = element 6 | self._left = left 7 | self._right = right 8 | 9 | class BinarySearchTree: 10 | def __init__(self): 11 | self._root = None 12 | 13 | def insert(self,troot,e): 14 | temp = None 15 | while troot: 16 | temp = troot 17 | if e == troot._element: 18 | return 19 | elif e < troot._element: 20 | troot = troot._left 21 | elif e > troot._element: 22 | troot = troot._right 23 | n = _Node(e) 24 | if self._root: 25 | if e < temp._element: 26 | temp._left = n 27 | else: 28 | temp._right = n 29 | else: 30 | self._root = n 31 | 32 | def delete(self,e): 33 | p = self._root 34 | pp = None 35 | while p and p._element != e: 36 | pp = p 37 | if e < p._element: 38 | p = p._left 39 | else: 40 | p = p._right 41 | if not p: 42 | return False 43 | if p._left and p._right: 44 | s = p._left 45 | ps = p 46 | while s._right: 47 | ps = s 48 | s = s._right 49 | p._element = s._element 50 | p = s 51 | pp = ps 52 | c = None 53 | if p._left: 54 | c = p._left 55 | else: 56 | c = p._right 57 | if p == self._root: 58 | self._root = c 59 | else: 60 | if p == pp._left: 61 | pp._left = c 62 | else: 63 | pp._right = c 64 | 65 | def inorder(self,troot): 66 | if troot: 67 | self.inorder(troot._left) 68 | print(troot._element,end=' ') 69 | self.inorder(troot._right) 70 | 71 | B = BinarySearchTree() 72 | B.insert(B._root,50) 73 | B.insert(B._root,30) 74 | B.insert(B._root,80) 75 | B.insert(B._root,10) 76 | B.insert(B._root,40) 77 | B.insert(B._root,60) 78 | B.insert(B._root,90) 79 | B.inorder(B._root) 80 | B.delete(50) 81 | print() 82 | B.inorder(B._root) 83 | 84 | -------------------------------------------------------------------------------- /linked list/LLRemoveFirstElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class LinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._head = newest 24 | else: 25 | self._tail._next = newest 26 | self._tail = newest 27 | self._size += 1 28 | 29 | def addfirst(self, e): 30 | newest = _Node(e, None) 31 | if self.isempty(): 32 | self._head = newest 33 | self._tail = newest 34 | else: 35 | newest._next = self._head 36 | self._head = newest 37 | self._size += 1 38 | 39 | def addany(self, e, position): 40 | newest = _Node(e, None) 41 | p = self._head 42 | i = 1 43 | while i < position-1: 44 | p = p._next 45 | i = i + 1 46 | newest._next = p._next 47 | p._next = newest 48 | self._size += 1 49 | 50 | def removefirst(self): 51 | if self.isempty(): 52 | print('List is empty') 53 | return 54 | e = self._head._element 55 | self._head = self._head._next 56 | self._size -= 1 57 | if self.isempty(): 58 | self._tail = None 59 | return e 60 | 61 | def display(self): 62 | p = self._head 63 | while p: 64 | print(p._element,end='-->') 65 | p = p._next 66 | print() 67 | 68 | def search(self,key): 69 | p = self._head 70 | index = 0 71 | while p: 72 | if p._element == key: 73 | return index 74 | p = p._next 75 | index = index + 1 76 | return -1 77 | 78 | L = LinkedList() 79 | L.addlast(7) 80 | L.addlast(4) 81 | L.addlast(12) 82 | L.addlast(8) 83 | L.addlast(3) 84 | L.display() 85 | print('Size:',len(L)) 86 | ele = L.removefirst() 87 | L.display() 88 | print('Size:',len(L)) 89 | print('Element Removed:',ele) 90 | -------------------------------------------------------------------------------- /Graph/DFS.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class Graph: 4 | 5 | def __init__(self, vertices): 6 | self._adjMat = np.zeros((vertices, vertices)) 7 | self._vertices = vertices 8 | self._visited = [0] * vertices 9 | 10 | def insert_edge(self, u, v, x=1): 11 | self._adjMat[u][v] = x 12 | 13 | def remove_edge(self, u, v): 14 | self._adjMat[u][v] = 0 15 | 16 | def exist_edge(self, u, v): 17 | return self._adjMat[u][v] != 0 18 | 19 | def vertex_count(self): 20 | return self._vertices 21 | 22 | def edge_count(self): 23 | count = 0 24 | for i in range(self._vertices): 25 | for j in range(self._vertices): 26 | if self._adjMat[i][j] != 0: 27 | count += 1 28 | return count 29 | 30 | def vertices(self): 31 | for i in range(self._vertices): 32 | print(i,end=' ') 33 | print() 34 | 35 | def edges(self): 36 | for i in range(self._vertices): 37 | for j in range(self._vertices): 38 | if self._adjMat[i][j] != 0: 39 | print(i,'--',j) 40 | 41 | def outdegree(self, v): 42 | count = 0 43 | for j in range(self._vertices): 44 | if not self._adjMat[v][j] == 0: 45 | count += 1 46 | return count 47 | 48 | def indegree(self, v): 49 | count = 0 50 | for j in range(self._vertices): 51 | if not self._adjMat[j][v] == 0: 52 | count += 1 53 | return count 54 | 55 | def display_adjMat(self): 56 | print(self._adjMat) 57 | 58 | def DFS(self,s): 59 | if self._visited[s]==0: 60 | print(s,end=' ') 61 | self._visited[s]=1 62 | for j in range(self._vertices): 63 | if self._adjMat[s][j]==1 and self._visited[j]==0: 64 | self.DFS(j) 65 | 66 | 67 | G = Graph(7) 68 | G.insert_edge(0, 1) 69 | G.insert_edge(0, 5) 70 | G.insert_edge(0, 6) 71 | G.insert_edge(1, 0) 72 | G.insert_edge(1, 2) 73 | G.insert_edge(1, 5) 74 | G.insert_edge(1, 6) 75 | G.insert_edge(2, 3) 76 | G.insert_edge(2, 4) 77 | G.insert_edge(2, 6) 78 | G.insert_edge(3, 4) 79 | G.insert_edge(4, 2) 80 | G.insert_edge(4, 5) 81 | G.insert_edge(5, 2) 82 | G.insert_edge(5, 3) 83 | G.insert_edge(6, 3) 84 | print('Edges:') 85 | G.edges() 86 | print('DFS:') 87 | G.DFS(0) 88 | -------------------------------------------------------------------------------- /Graph/GraphsWeightedUndirected.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class Graph: 4 | 5 | def __init__(self, vertices): 6 | self._adjMat = np.zeros((vertices, vertices)) 7 | self._vertices = vertices 8 | 9 | def insert_edge(self, u, v, x=1): 10 | self._adjMat[u][v] = x 11 | 12 | def remove_edge(self, u, v): 13 | self._adjMat[u][v] = 0 14 | 15 | def exist_edge(self, u, v): 16 | return self._adjMat[u][v] != 0 17 | 18 | def vertex_count(self): 19 | return self._vertices 20 | 21 | def edge_count(self): 22 | count = 0 23 | for i in range(self._vertices): 24 | for j in range(self._vertices): 25 | if self._adjMat[i][j] != 0: 26 | count = count + 1 27 | return count 28 | 29 | def vertices(self): 30 | for i in range(self._vertices): 31 | print(i,end=' ') 32 | print() 33 | 34 | def edges(self): 35 | for i in range(self._vertices): 36 | for j in range(self._vertices): 37 | if self._adjMat[i][j] != 0: 38 | print(i,'--',j) 39 | 40 | def outdegree(self, v): 41 | count = 0 42 | for j in range(self._vertices): 43 | if not self._adjMat[v][j] == 0: 44 | count = count + 1 45 | return count 46 | 47 | def indegree(self, v): 48 | count = 0 49 | for i in range(self._vertices): 50 | if not self._adjMat[i][v] == 0: 51 | count = count + 1 52 | return count 53 | 54 | def display_adjMat(self): 55 | print(self._adjMat) 56 | 57 | 58 | G = Graph(4) 59 | print('Graph Adjacency Matrix') 60 | G.display_adjMat() 61 | print('Vertices: ', G.vertex_count()) 62 | G.insert_edge(0, 1, 26) 63 | G.insert_edge(0, 2, 16) 64 | G.insert_edge(1, 0, 26) 65 | G.insert_edge(1, 2, 12) 66 | G.insert_edge(2, 0, 16) 67 | G.insert_edge(2, 1, 12) 68 | G.insert_edge(2, 3, 8) 69 | G.insert_edge(3, 2, 8) 70 | print('Graph Adjacency Matrix') 71 | G.display_adjMat() 72 | print('Edges Count:', G.edge_count()) 73 | print('Vertices:') 74 | G.vertices() 75 | print('Edges:') 76 | G.edges() 77 | print('Edges between 1--3:', G.exist_edge(1,3)) 78 | print('Edges between 1--2:', G.exist_edge(1,2)) 79 | print('Degree of vertex 2:',G.indegree(2)) 80 | print('Graph Adjacency Matrix') 81 | G.display_adjMat() 82 | G.remove_edge(1,2) 83 | print('Graph Adjacency Matrix') 84 | G.display_adjMat() 85 | print('Edges between 1--2:', G.exist_edge(1,2)) 86 | 87 | -------------------------------------------------------------------------------- /Graph/GraphsUndirected.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class Graph: 4 | 5 | def __init__(self, vertices): 6 | self._vertices = vertices 7 | self._adjMat = np.zeros((vertices, vertices)) 8 | 9 | def insert_edge(self, u, v, x=1): 10 | self._adjMat[u][v] = x 11 | 12 | def remove_edge(self, u, v): 13 | self._adjMat[u][v] = 0 14 | 15 | def exist_edge(self, u, v): 16 | return self._adjMat[u][v] != 0 17 | 18 | def vertex_count(self): 19 | return self._vertices 20 | 21 | def edge_count(self): 22 | count = 0 23 | for i in range(self._vertices): 24 | for j in range(self._vertices): 25 | if self._adjMat[i][j] != 0: 26 | count = count + 1 27 | return count 28 | 29 | def vertices(self): 30 | for i in range(self._vertices): 31 | print(i,end=' ') 32 | print() 33 | 34 | def edges(self): 35 | for i in range(self._vertices): 36 | for j in range(self._vertices): 37 | if self._adjMat[i][j] != 0: 38 | print(i,'--',j) 39 | 40 | def outdegree(self, v): 41 | count = 0 42 | for j in range(self._vertices): 43 | if self._adjMat[v][j] != 0: 44 | count = count + 1 45 | return count 46 | 47 | def indegree(self, v): 48 | count = 0 49 | for i in range(self._vertices): 50 | if self._adjMat[i][v] != 0: 51 | count = count + 1 52 | return count 53 | 54 | def display_adjMat(self): 55 | print(self._adjMat) 56 | 57 | 58 | G = Graph(4) 59 | print('Graph Adjacency Matrix') 60 | G.display_adjMat() 61 | print('Vertices: ', G.vertex_count()) 62 | print('Edges Count:', G.edge_count()) 63 | G.insert_edge(0, 1) 64 | G.insert_edge(0, 2) 65 | G.insert_edge(1, 0) 66 | G.insert_edge(1, 2) 67 | G.insert_edge(2, 0) 68 | G.insert_edge(2, 1) 69 | G.insert_edge(2, 3) 70 | G.insert_edge(3, 2) 71 | print('Graph Adjacency Matrix') 72 | G.display_adjMat() 73 | print('Edges Count:', G.edge_count()) 74 | print('Vertices:') 75 | G.vertices() 76 | print('Edges:') 77 | G.edges() 78 | print('Edges between 1--3:', G.exist_edge(1,3)) 79 | print('Edges between 1--2:', G.exist_edge(1,2)) 80 | print('Degree of vertex 2:',G.indegree(2)) 81 | print('Graph Adjacency Matrix') 82 | G.display_adjMat() 83 | G.remove_edge(1,2) 84 | print('Graph Adjacency Matrix') 85 | G.display_adjMat() 86 | print('Edges between 1--2:', G.exist_edge(1,2)) 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /Queues /DEQueLinked.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class DEQueLinked: 9 | def __init__(self): 10 | self._front = None 11 | self._rear = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._front = newest 24 | else: 25 | self._rear._next = newest 26 | self._rear = newest 27 | self._size += 1 28 | 29 | def addfirst(self, e): 30 | newest = _Node(e, None) 31 | if self.isempty(): 32 | self._front = newest 33 | self._rear = newest 34 | else: 35 | newest._next = self._front 36 | self._front = newest 37 | self._size += 1 38 | 39 | def removefirst(self): 40 | if self.isempty(): 41 | print('DEQue is empty') 42 | return 43 | e = self._front._element 44 | self._front = self._front._next 45 | self._size -= 1 46 | if self.isempty(): 47 | self._rear = None 48 | return e 49 | 50 | def removelast(self): 51 | if self.isempty(): 52 | print('DEQue is empty') 53 | return 54 | p = self._front 55 | i = 1 56 | while i < len(self) - 1: 57 | p = p._next 58 | i = i + 1 59 | self._rear = p 60 | p = p._next 61 | e = p._element 62 | self._rear._next = None 63 | self._size -= 1 64 | return e 65 | 66 | def first(self): 67 | if self.isempty(): 68 | print('DEQue is empty') 69 | return 70 | return self._front._element 71 | 72 | def last(self): 73 | if self.isempty(): 74 | print('DEQue is empty') 75 | return 76 | return self._rear._element 77 | 78 | def display(self): 79 | p = self._front 80 | while p: 81 | print(p._element,end=' --> ') 82 | p = p._next 83 | print() 84 | 85 | 86 | D = DEQueLinked() 87 | D.addfirst(5) 88 | D.addfirst(3) 89 | D.addlast(7) 90 | D.addlast(12) 91 | D.display() 92 | print('Length:',len(D)) 93 | print(D.removefirst()) 94 | print(D.removelast()) 95 | D.display() 96 | print(D.first()) 97 | print(D.last()) 98 | -------------------------------------------------------------------------------- /Graph/GraphDirected.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class Graph: 4 | 5 | def __init__(self, vertices): 6 | self._adjMat = np.zeros((vertices, vertices)) 7 | self._vertices = vertices 8 | 9 | def insert_edge(self, u, v, x=1): 10 | self._adjMat[u][v] = x 11 | 12 | def remove_edge(self, u, v): 13 | self._adjMat[u][v] = 0 14 | 15 | def exist_edge(self, u, v): 16 | return self._adjMat[u][v] != 0 17 | 18 | def vertex_count(self): 19 | return self._vertices 20 | 21 | def edge_count(self): 22 | count = 0 23 | for i in range(self._vertices): 24 | for j in range(self._vertices): 25 | if self._adjMat[i][j] != 0: 26 | count = count + 1 27 | return count 28 | 29 | def vertices(self): 30 | for i in range(self._vertices): 31 | print(i,end=' ') 32 | print() 33 | 34 | def edges(self): 35 | for i in range(self._vertices): 36 | for j in range(self._vertices): 37 | if self._adjMat[i][j] != 0: 38 | print(i,'--',j) 39 | 40 | def outdegree(self, v): 41 | count = 0 42 | for j in range(self._vertices): 43 | if not self._adjMat[v][j] == 0: 44 | count = count + 1 45 | return count 46 | 47 | def indegree(self, v): 48 | count = 0 49 | for i in range(self._vertices): 50 | if not self._adjMat[i][v] == 0: 51 | count = count + 1 52 | return count 53 | 54 | def display_adjMat(self): 55 | print(self._adjMat) 56 | 57 | 58 | G = Graph(4) 59 | print('Graph Adjacency Matrix') 60 | G.display_adjMat() 61 | print('Vertices: ', G.vertex_count()) 62 | G.insert_edge(0, 1) 63 | G.insert_edge(0, 2) 64 | G.insert_edge(1, 2) 65 | G.insert_edge(2, 3) 66 | print('Graph Adjacency Matrix') 67 | G.display_adjMat() 68 | print('Edges Count:', G.edge_count()) 69 | print('Vertices:') 70 | G.vertices() 71 | print('Edges:') 72 | G.edges() 73 | print('Edges between 1--3:', G.exist_edge(1,3)) 74 | print('Edges between 1--2:', G.exist_edge(1,2)) 75 | print('Edges between 2--1:', G.exist_edge(2,1)) 76 | print('Degree of vertex 2:',G.indegree(2)+G.outdegree(2)) 77 | print('In-Degree of vertex 2:',G.indegree(2)) 78 | print('Out-Degree of vertex 2:',G.outdegree(2)) 79 | print('Graph Adjacency Matrix') 80 | G.display_adjMat() 81 | G.remove_edge(1,2) 82 | print('Graph Adjacency Matrix') 83 | G.display_adjMat() 84 | print('Edges between 1--2:', G.exist_edge(1,2)) 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /Graph/GraphWeightedDirected.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class Graph: 4 | 5 | def __init__(self, vertices): 6 | self._adjMat = np.zeros((vertices, vertices)) 7 | self._vertices = vertices 8 | 9 | def insert_edge(self, u, v, x=1): 10 | self._adjMat[u][v] = x 11 | 12 | def remove_edge(self, u, v): 13 | self._adjMat[u][v] = 0 14 | 15 | def exist_edge(self, u, v): 16 | return self._adjMat[u][v] != 0 17 | 18 | def vertex_count(self): 19 | return self._vertices 20 | 21 | def edge_count(self): 22 | count = 0 23 | for i in range(self._vertices): 24 | for j in range(self._vertices): 25 | if self._adjMat[i][j] != 0: 26 | count = count + 1 27 | return count 28 | 29 | def vertices(self): 30 | for i in range(self._vertices): 31 | print(i,end=' ') 32 | print() 33 | 34 | def edges(self): 35 | for i in range(self._vertices): 36 | for j in range(self._vertices): 37 | if self._adjMat[i][j] != 0: 38 | print(i,'--',j) 39 | 40 | def outdegree(self, v): 41 | count = 0 42 | for j in range(self._vertices): 43 | if not self._adjMat[v][j] == 0: 44 | count = count + 1 45 | return count 46 | 47 | def indegree(self, v): 48 | count = 0 49 | for i in range(self._vertices): 50 | if not self._adjMat[i][v] == 0: 51 | count = count + 1 52 | return count 53 | 54 | def display_adjMat(self): 55 | print(self._adjMat) 56 | 57 | 58 | G = Graph(4) 59 | print('Graph Adjacency Matrix') 60 | G.display_adjMat() 61 | print('Vertices: ', G.vertex_count()) 62 | G.insert_edge(0, 1, 26) 63 | G.insert_edge(0, 2, 16) 64 | G.insert_edge(1, 2, 12) 65 | G.insert_edge(2, 3, 8) 66 | print('Graph Adjacency Matrix') 67 | G.display_adjMat() 68 | print('Edges Count:', G.edge_count()) 69 | print('Vertices:') 70 | G.vertices() 71 | print('Edges:') 72 | G.edges() 73 | print('Edges between 1--3:', G.exist_edge(1,3)) 74 | print('Edges between 1--2:', G.exist_edge(1,2)) 75 | print('Edges between 2--1:', G.exist_edge(2,1)) 76 | print('Degree of vertex 2:',G.indegree(2)+G.outdegree(2)) 77 | print('In-Degree of vertex 2:',G.indegree(2)) 78 | print('Out-Degree of vertex 2:',G.outdegree(2)) 79 | print('Graph Adjacency Matrix') 80 | G.display_adjMat() 81 | G.remove_edge(1,2) 82 | print('Graph Adjacency Matrix') 83 | G.display_adjMat() 84 | print('Edges between 1--2:', G.exist_edge(1,2)) 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /Graph/BFS.py: -------------------------------------------------------------------------------- 1 | from QueuesLinked import QueuesLinked 2 | import numpy as np 3 | 4 | class Graph: 5 | 6 | def __init__(self, vertices): 7 | self._adjMat = np.zeros((vertices, vertices)) 8 | self._vertices = vertices 9 | 10 | def insert_edge(self, u, v, x=1): 11 | self._adjMat[u][v] = x 12 | 13 | def remove_edge(self, u, v): 14 | self._adjMat[u][v] = 0 15 | 16 | def exist_edge(self, u, v): 17 | return self._adjMat[u][v] != 0 18 | 19 | def vertex_count(self): 20 | return self._vertices 21 | 22 | def edge_count(self): 23 | count = 0 24 | for i in range(self._vertices): 25 | for j in range(self._vertices): 26 | if self._adjMat[i][j] != 0: 27 | count += 1 28 | return count 29 | 30 | def vertices(self): 31 | for i in range(self._vertices): 32 | print(i,end=' ') 33 | print() 34 | 35 | def edges(self): 36 | for i in range(self._vertices): 37 | for j in range(self._vertices): 38 | if self._adjMat[i][j] != 0: 39 | print(i,'--',j) 40 | 41 | def outdegree(self, v): 42 | count = 0 43 | for j in range(self._vertices): 44 | if not self._adjMat[v][j] == 0: 45 | count += 1 46 | return count 47 | 48 | def indegree(self, v): 49 | count = 0 50 | for j in range(self._vertices): 51 | if not self._adjMat[j][v] == 0: 52 | count += 1 53 | return count 54 | 55 | def display_adjMat(self): 56 | print(self._adjMat) 57 | 58 | def BFS(self, s): 59 | i = s 60 | q = QueuesLinked() 61 | visited = [0] * self._vertices 62 | print(i,end=' ') 63 | visited[i] = 1 64 | q.enqueue(i) 65 | while not q.isempty(): 66 | i = q.dequeue() 67 | for j in range(self._vertices): 68 | if self._adjMat[i][j] == 1 and visited[j] == 0: 69 | print(j,end=' ') 70 | visited[j] = 1 71 | q.enqueue(j) 72 | 73 | G = Graph(7) 74 | G.insert_edge(0, 1) 75 | G.insert_edge(0, 5) 76 | G.insert_edge(0, 6) 77 | G.insert_edge(1, 0) 78 | G.insert_edge(1, 2) 79 | G.insert_edge(1, 5) 80 | G.insert_edge(1, 6) 81 | G.insert_edge(2, 3) 82 | G.insert_edge(2, 4) 83 | G.insert_edge(2, 6) 84 | G.insert_edge(3, 4) 85 | G.insert_edge(4, 2) 86 | G.insert_edge(4, 5) 87 | G.insert_edge(5, 2) 88 | G.insert_edge(5, 3) 89 | G.insert_edge(6, 3) 90 | print('Edges:') 91 | G.edges() 92 | print('BFS:') 93 | G.BFS(0) 94 | -------------------------------------------------------------------------------- /linked list/DLLRemoveFirstElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next', '_prev' 3 | 4 | def __init__(self, element, next, prev): 5 | self._element = element 6 | self._next = next 7 | self._prev = prev 8 | 9 | class DoublyLinkedList: 10 | def __init__(self): 11 | self._head = None 12 | self._tail = None 13 | self._size = 0 14 | 15 | def __len__(self): 16 | return self._size 17 | 18 | def isempty(self): 19 | return self._size == 0 20 | 21 | def addlast(self, e): 22 | newest = _Node(e, None, None) 23 | if self.isempty(): 24 | self._head = newest 25 | self._tail = newest 26 | else: 27 | self._tail._next = newest 28 | newest._prev = self._tail 29 | self._tail = newest 30 | self._size += 1 31 | 32 | def addfirst(self, e): 33 | newest = _Node(e, None, None) 34 | if self.isempty(): 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | newest._next = self._head 39 | self._head._prev = newest 40 | self._head = newest 41 | self._size += 1 42 | 43 | def addany(self, e, position): 44 | newest = _Node(e, None, None) 45 | p = self._head 46 | i = 1 47 | while i < position-1: 48 | p = p._next 49 | i = i + 1 50 | newest._next = p._next 51 | p._next = newest 52 | p._next._prev = newest 53 | newest._prev = p 54 | self._size += 1 55 | 56 | def removefirst(self): 57 | if self.isempty(): 58 | print('List is empty') 59 | return 60 | e = self._head._element 61 | self._head = self._head._next 62 | self._size -= 1 63 | if self.isempty(): 64 | self._tail = None 65 | else: 66 | self._head._prev = None 67 | return e 68 | 69 | def display(self): 70 | p = self._head 71 | while p: 72 | print(p._element,end='-->') 73 | p = p._next 74 | print() 75 | 76 | def displayrev(self): 77 | p = self._tail 78 | while p: 79 | print(p._element,end=' <-- ') 80 | p = p._prev 81 | print() 82 | 83 | 84 | L = DoublyLinkedList() 85 | L.addlast(7) 86 | L.addlast(4) 87 | L.addlast(12) 88 | L.addlast(8) 89 | L.addlast(3) 90 | L.display() 91 | print('Size:',len(L)) 92 | ele = L.removefirst() 93 | L.display() 94 | print('Size:',len(L)) 95 | print('Removed Element:',ele) 96 | L.displayrev() -------------------------------------------------------------------------------- /linked list/CLLRemoveFirstElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class CircularLinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | newest._next = newest 24 | self._head = newest 25 | else: 26 | newest._next = self._tail._next 27 | self._tail._next = newest 28 | self._tail = newest 29 | self._size += 1 30 | 31 | def addfirst(self,e): 32 | newest = _Node(e, None) 33 | if self.isempty(): 34 | newest._next = newest 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | self._tail._next = newest 39 | newest._next = self._head 40 | self._head = newest 41 | self._size += 1 42 | 43 | def addany(self, e, position): 44 | newest = _Node(e, None) 45 | p = self._head 46 | i = 1 47 | while i < position - 1: 48 | p = p._next 49 | i = i + 1 50 | newest._next = p._next 51 | p._next = newest 52 | self._size += 1 53 | 54 | def removefirst(self): 55 | if self.isempty(): 56 | print('List is empty') 57 | return 58 | e = self._head._element 59 | self._tail._next = self._head._next 60 | self._head = self._head._next 61 | self._size -= 1 62 | if self.isempty(): 63 | self._head = None 64 | self._tail = None 65 | return e 66 | 67 | def display(self): 68 | p = self._head 69 | i = 0 70 | while i < len(self): 71 | print(p._element,end='-->') 72 | p = p._next 73 | i += 1 74 | print() 75 | 76 | def search(self,key): 77 | p = self._head 78 | index = 0 79 | while index < len(self): 80 | if p._element == key: 81 | return index 82 | p = p._next 83 | index = index + 1 84 | return -1 85 | 86 | C = CircularLinkedList() 87 | C.addlast(7) 88 | C.addlast(4) 89 | C.addlast(12) 90 | C.addlast(8) 91 | C.addlast(3) 92 | C.display() 93 | print('Size:',len(C)) 94 | ele = C.removefirst() 95 | C.display() 96 | print('Size:',len(C)) 97 | print('Removed Element:',ele) 98 | -------------------------------------------------------------------------------- /linked list/LLRemoveLastElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class LinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._head = newest 24 | else: 25 | self._tail._next = newest 26 | self._tail = newest 27 | self._size += 1 28 | 29 | def addfirst(self, e): 30 | newest = _Node(e, None) 31 | if self.isempty(): 32 | self._head = newest 33 | self._tail = newest 34 | else: 35 | newest._next = self._head 36 | self._head = newest 37 | self._size += 1 38 | 39 | def addany(self, e, position): 40 | newest = _Node(e, None) 41 | p = self._head 42 | i = 1 43 | while i < position-1: 44 | p = p._next 45 | i = i + 1 46 | newest._next = p._next 47 | p._next = newest 48 | self._size += 1 49 | 50 | def removefirst(self): 51 | if self.isempty(): 52 | print('List is empty') 53 | return 54 | e = self._head._element 55 | self._head = self._head._next 56 | self._size -= 1 57 | if self.isempty(): 58 | self._tail = None 59 | return e 60 | 61 | def removelast(self): 62 | if self.isempty(): 63 | print('List is empty') 64 | return 65 | p = self._head 66 | i = 1 67 | while i < len(self) - 1: 68 | p = p._next 69 | i = i + 1 70 | self._tail = p 71 | p = p._next 72 | e = p._element 73 | self._tail._next = None 74 | self._size -= 1 75 | return e 76 | 77 | def display(self): 78 | p = self._head 79 | while p: 80 | print(p._element,end='-->') 81 | p = p._next 82 | print() 83 | 84 | def search(self,key): 85 | p = self._head 86 | index = 0 87 | while p: 88 | if p._element == key: 89 | return index 90 | p = p._next 91 | index = index + 1 92 | return -1 93 | 94 | L = LinkedList() 95 | L.addlast(7) 96 | L.addlast(4) 97 | L.addlast(12) 98 | L.addlast(8) 99 | L.addlast(3) 100 | L.display() 101 | print('Size:',len(L)) 102 | ele = L.removelast() 103 | L.display() 104 | print('Size:',len(L)) 105 | print('Element Removed',ele) 106 | -------------------------------------------------------------------------------- /linked list/DLLRemoveLastElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next', '_prev' 3 | 4 | def __init__(self, element, next, prev): 5 | self._element = element 6 | self._next = next 7 | self._prev = prev 8 | 9 | class DoublyLinkedList: 10 | def __init__(self): 11 | self._head = None 12 | self._tail = None 13 | self._size = 0 14 | 15 | def __len__(self): 16 | return self._size 17 | 18 | def isempty(self): 19 | return self._size == 0 20 | 21 | def addlast(self, e): 22 | newest = _Node(e, None, None) 23 | if self.isempty(): 24 | self._head = newest 25 | self._tail = newest 26 | else: 27 | self._tail._next = newest 28 | newest._prev = self._tail 29 | self._tail = newest 30 | self._size += 1 31 | 32 | def addfirst(self, e): 33 | newest = _Node(e, None, None) 34 | if self.isempty(): 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | newest._next = self._head 39 | self._head._prev = newest 40 | self._head = newest 41 | self._size += 1 42 | 43 | def addany(self, e, position): 44 | newest = _Node(e, None, None) 45 | p = self._head 46 | i = 1 47 | while i < position-1: 48 | p = p._next 49 | i = i + 1 50 | newest._next = p._next 51 | p._next = newest 52 | p._next._prev = newest 53 | newest._prev = p 54 | self._size += 1 55 | 56 | def removefirst(self): 57 | if self.isempty(): 58 | print('List is empty') 59 | return 60 | e = self._head._element 61 | self._head = self._head._next 62 | self._size -= 1 63 | if self.isempty(): 64 | self._tail = None 65 | else: 66 | self._head._prev = None 67 | return e 68 | 69 | def removelast(self): 70 | if self.isempty(): 71 | print('List is empty') 72 | return 73 | e = self._tail._element 74 | self._tail = self._tail._prev 75 | self._tail._next = None 76 | self._size -= 1 77 | return e 78 | 79 | def display(self): 80 | p = self._head 81 | while p: 82 | print(p._element,end=' --> ') 83 | p = p._next 84 | print() 85 | 86 | def displayrev(self): 87 | p = self._tail 88 | while p: 89 | print(p._element,end=' <-- ') 90 | p = p._prev 91 | print() 92 | 93 | L = DoublyLinkedList() 94 | L.addlast(7) 95 | L.addlast(4) 96 | L.addlast(12) 97 | L.addlast(8) 98 | L.addlast(3) 99 | L.display() 100 | print('Size:',len(L)) 101 | ele = L.removelast() 102 | L.display() 103 | print('Size:',len(L)) 104 | print('Removed Element:',ele) 105 | L.displayrev() 106 | -------------------------------------------------------------------------------- /linked list/LLRemoveAnyElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | 7 | self._next = next 8 | class LinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._head = newest 24 | else: 25 | self._tail._next = newest 26 | self._tail = newest 27 | self._size += 1 28 | 29 | def addfirst(self, e): 30 | newest = _Node(e, None) 31 | if self.isempty(): 32 | self._head = newest 33 | self._tail = newest 34 | else: 35 | newest._next = self._head 36 | self._head = newest 37 | self._size += 1 38 | 39 | def addany(self, e, position): 40 | newest = _Node(e, None) 41 | p = self._head 42 | i = 1 43 | while i < position-1: 44 | p = p._next 45 | i = i + 1 46 | newest._next = p._next 47 | p._next = newest 48 | self._size += 1 49 | 50 | def removefirst(self): 51 | if self.isempty(): 52 | print('List is empty') 53 | return 54 | e = self._head._element 55 | self._head = self._head._next 56 | self._size -= 1 57 | if self.isempty(): 58 | self._tail = None 59 | return e 60 | 61 | def removelast(self): 62 | if self.isempty(): 63 | print('List is empty') 64 | return 65 | p = self._head 66 | i = 1 67 | while i < len(self) - 1: 68 | p = p._next 69 | i = i + 1 70 | self._tail = p 71 | p = p._next 72 | e = p._element 73 | self._tail._next = None 74 | self._size -= 1 75 | return e 76 | 77 | def removeany(self, position): 78 | p = self._head 79 | i = 1 80 | while i < position - 1: 81 | p = p._next 82 | i = i + 1 83 | e = p._next._element 84 | p._next = p._next._next 85 | self._size -= 1 86 | return e 87 | 88 | def display(self): 89 | p = self._head 90 | while p: 91 | print(p._element,end='-->') 92 | p = p._next 93 | print() 94 | 95 | def search(self,key): 96 | p = self._head 97 | index = 0 98 | while p: 99 | if p._element == key: 100 | return index 101 | p = p._next 102 | index = index + 1 103 | return -1 104 | 105 | L = LinkedList() 106 | L.addlast(7) 107 | L.addlast(4) 108 | L.addlast(12) 109 | L.addlast(8) 110 | L.addlast(3) 111 | L.display() 112 | print('Size:',len(L)) 113 | ele = L.removeany(3) 114 | L.display() 115 | print('Size:',len(L)) 116 | print('Removed Element:',ele) 117 | -------------------------------------------------------------------------------- /linked list/CLLRemoveLastElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class CircularLinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | newest._next = newest 24 | self._head = newest 25 | else: 26 | newest._next = self._tail._next 27 | self._tail._next = newest 28 | self._tail = newest 29 | self._size += 1 30 | 31 | def addfirst(self,e): 32 | newest = _Node(e, None) 33 | if self.isempty(): 34 | newest._next = newest 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | self._tail._next = newest 39 | newest._next = self._head 40 | self._head = newest 41 | self._size += 1 42 | 43 | def addany(self, e, position): 44 | newest = _Node(e, None) 45 | p = self._head 46 | i = 1 47 | while i < position - 1: 48 | p = p._next 49 | i = i + 1 50 | newest._next = p._next 51 | p._next = newest 52 | self._size += 1 53 | 54 | def removefirst(self): 55 | if self.isempty(): 56 | print('List is empty') 57 | return 58 | e = self._head._element 59 | self._tail._next = self._head._next 60 | self._head = self._head._next 61 | self._size -= 1 62 | if self.is_empty(): 63 | self._head = None 64 | self._tail = None 65 | return e 66 | 67 | def removelast(self): 68 | if self.isempty(): 69 | print('List is empty') 70 | return 71 | p = self._head 72 | i = 1 73 | while i < len(self) - 1: 74 | p = p._next 75 | i = i + 1 76 | self._tail = p 77 | p = p._next 78 | self._tail._next = self._head 79 | e = p._element 80 | self._size -= 1 81 | return e 82 | 83 | def display(self): 84 | p = self._head 85 | i = 0 86 | while i < len(self): 87 | print(p._element,end='-->') 88 | p = p._next 89 | i += 1 90 | print() 91 | 92 | def search(self,key): 93 | p = self._head 94 | index = 0 95 | while index < len(self): 96 | if p._element == key: 97 | return index 98 | p = p._next 99 | index = index + 1 100 | return -1 101 | 102 | C = CircularLinkedList() 103 | C.addlast(7) 104 | C.addlast(4) 105 | C.addlast(12) 106 | C.addlast(8) 107 | C.addlast(3) 108 | C.display() 109 | print('Size:',len(C)) 110 | ele = C.removelast() 111 | C.display() 112 | print('Size:',len(C)) 113 | print('Removed Element:',ele) 114 | -------------------------------------------------------------------------------- /linked list/DLLRemoveAnyElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next', '_prev' 3 | 4 | def __init__(self, element, next, prev): 5 | self._element = element 6 | self._next = next 7 | self._prev = prev 8 | 9 | class DoublyLinkedList: 10 | def __init__(self): 11 | self._head = None 12 | self._tail = None 13 | self._size = 0 14 | 15 | def __len__(self): 16 | return self._size 17 | 18 | def isempty(self): 19 | return self._size == 0 20 | 21 | def addlast(self, e): 22 | newest = _Node(e, None, None) 23 | if self.isempty(): 24 | self._head = newest 25 | self._tail = newest 26 | else: 27 | self._tail._next = newest 28 | newest._prev = self._tail 29 | self._tail = newest 30 | self._size += 1 31 | 32 | def addfirst(self, e): 33 | newest = _Node(e, None, None) 34 | if self.isempty(): 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | newest._next = self._head 39 | self._head._prev = newest 40 | self._head = newest 41 | self._size += 1 42 | 43 | def addany(self, e, position): 44 | newest = _Node(e, None, None) 45 | p = self._head 46 | i = 1 47 | while i < position-1: 48 | p = p._next 49 | i = i + 1 50 | newest._next = p._next 51 | p._next = newest 52 | p._next._prev = newest 53 | newest._prev = p 54 | self._size += 1 55 | 56 | def removefirst(self): 57 | if self.isempty(): 58 | print('List is empty') 59 | return 60 | e = self._head._element 61 | self._head = self._head._next 62 | self._size -= 1 63 | if self.isempty(): 64 | self._tail = None 65 | else: 66 | self._head._prev = None 67 | return e 68 | 69 | def removelast(self): 70 | if self.isempty(): 71 | print('List is empty') 72 | return 73 | e = self._tail._element 74 | self._tail = self._tail._prev 75 | self._tail._next = None 76 | self._size -= 1 77 | return e 78 | 79 | def removeany(self, position): 80 | if self.isempty(): 81 | print('List is empty') 82 | return 83 | p = self._head 84 | i = 1 85 | while i < position - 1: 86 | p = p._next 87 | i = i + 1 88 | e = p._next._element 89 | p._next = p._next._next 90 | p._next._prev = p 91 | self._size -= 1 92 | return e 93 | 94 | def display(self): 95 | p = self._head 96 | while p: 97 | print(p._element,end=' --> ') 98 | p = p._next 99 | print() 100 | 101 | def displayrev(self): 102 | p = self._tail 103 | while p: 104 | print(p._element,end=' <-- ') 105 | p = p._prev 106 | print() 107 | 108 | 109 | L = DoublyLinkedList() 110 | L.addlast(7) 111 | L.addlast(4) 112 | L.addlast(12) 113 | L.addlast(8) 114 | L.addlast(3) 115 | L.display() 116 | print('Size:',len(L)) 117 | ele = L.removeany(3) 118 | L.display() 119 | print('Size:',len(L)) 120 | print('Removed Element:',ele) 121 | L.displayrev() 122 | 123 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /Hashing/LinkedList.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class LinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | self._head = newest 24 | else: 25 | self._tail._next = newest 26 | self._tail = newest 27 | self._size += 1 28 | 29 | def addfirst(self, e): 30 | newest = _Node(e, None) 31 | if self.isempty(): 32 | self._head = newest 33 | self._tail = newest 34 | else: 35 | newest._next = self._head 36 | self._head = newest 37 | self._size += 1 38 | 39 | def addany(self, e, position): 40 | newest = _Node(e, None) 41 | p = self._head 42 | i = 1 43 | while i < position-1: 44 | p = p._next 45 | i = i + 1 46 | newest._next = p._next 47 | p._next = newest 48 | self._size += 1 49 | 50 | def removefirst(self): 51 | if self.isempty(): 52 | print('List is empty') 53 | return 54 | e = self._head._element 55 | self._head = self._head._next 56 | self._size -= 1 57 | if self.isempty(): 58 | self._tail = None 59 | return e 60 | 61 | def removelast(self): 62 | if self.isempty(): 63 | print('List is empty') 64 | return 65 | p = self._head 66 | i = 1 67 | while i < len(self) - 1: 68 | p = p._next 69 | i = i + 1 70 | self._tail = p 71 | p = p._next 72 | e = p._element 73 | self._tail._next = None 74 | self._size -= 1 75 | return e 76 | 77 | def removeany(self, position): 78 | p = self._head 79 | i = 1 80 | while i < position - 1: 81 | p = p._next 82 | i = i + 1 83 | e = p._next._element 84 | p._next = p._next._next 85 | self._size -= 1 86 | return e 87 | 88 | def display(self): 89 | p = self._head 90 | while p: 91 | print(p._element,end='-->') 92 | p = p._next 93 | print() 94 | 95 | def search(self,key): 96 | p = self._head 97 | index = 0 98 | while p: 99 | if p._element == key: 100 | return index 101 | p = p._next 102 | index = index + 1 103 | return -1 104 | 105 | def insertsorted(self,e): 106 | newest = _Node(e, None) 107 | if self.isempty(): 108 | self._head = newest 109 | else: 110 | p = self._head 111 | q = self._head 112 | while p and p._element < e: 113 | q = p 114 | p = p._next 115 | if p == self._head: 116 | newest._next = self._head 117 | self._head = newest 118 | else: 119 | newest._next = q._next 120 | q._next = newest 121 | self._size += 1 122 | 123 | -------------------------------------------------------------------------------- /linked list/CLLRemoveAnyElement.py: -------------------------------------------------------------------------------- 1 | class _Node: 2 | __slots__ = '_element', '_next' 3 | 4 | def __init__(self, element, next): 5 | self._element = element 6 | self._next = next 7 | 8 | class CircularLinkedList: 9 | def __init__(self): 10 | self._head = None 11 | self._tail = None 12 | self._size = 0 13 | 14 | def __len__(self): 15 | return self._size 16 | 17 | def isempty(self): 18 | return self._size == 0 19 | 20 | def addlast(self, e): 21 | newest = _Node(e, None) 22 | if self.isempty(): 23 | newest._next = newest 24 | self._head = newest 25 | else: 26 | newest._next = self._tail._next 27 | self._tail._next = newest 28 | self._tail = newest 29 | self._size += 1 30 | 31 | def addfirst(self,e): 32 | newest = _Node(e, None) 33 | if self.isempty(): 34 | newest._next = newest 35 | self._head = newest 36 | self._tail = newest 37 | else: 38 | self._tail._next = newest 39 | newest._next = self._head 40 | self._head = newest 41 | self._size += 1 42 | 43 | def addany(self, e, position): 44 | newest = _Node(e, None) 45 | p = self._head 46 | i = 1 47 | while i < position - 1: 48 | p = p._next 49 | i = i + 1 50 | newest._next = p._next 51 | p._next = newest 52 | self._size += 1 53 | 54 | def removefirst(self): 55 | if self.isempty(): 56 | print('List is empty') 57 | return 58 | e = self._head._element 59 | self._tail._next = self._head._next 60 | self._head = self._head._next 61 | self._size -= 1 62 | if self.is_empty(): 63 | self._head = None 64 | self._tail = None 65 | return e 66 | 67 | def removelast(self): 68 | if self.isempty(): 69 | print('List is empty') 70 | return 71 | p = self._head 72 | i = 1 73 | while i < len(self) - 1: 74 | p = p._next 75 | i = i + 1 76 | self._tail = p 77 | self._tail._next = self._head 78 | p = p._next 79 | e = p._element 80 | self._size -= 1 81 | return e 82 | 83 | def removeany(self, position): 84 | p = self._head 85 | i = 1 86 | while i < position - 1: 87 | p = p._next 88 | i = i + 1 89 | e = p._next._element 90 | p._next = p._next._next 91 | self._size -= 1 92 | return e 93 | 94 | def display(self): 95 | p = self._head 96 | i = 0 97 | while i < len(self): 98 | print(p._element,end='-->') 99 | p = p._next 100 | i += 1 101 | print() 102 | 103 | def search(self,key): 104 | p = self._head 105 | index = 0 106 | while index < len(self): 107 | if p._element == key: 108 | return index 109 | p = p._next 110 | index = index + 1 111 | return -1 112 | 113 | C = CircularLinkedList() 114 | C.addlast(7) 115 | C.addlast(4) 116 | C.addlast(12) 117 | C.addlast(8) 118 | C.addlast(3) 119 | C.display() 120 | print('Size:',len(C)) 121 | ele = C.removeany(3) 122 | C.display() 123 | print('Size:',len(C)) 124 | print('Removed Element:',ele) 125 | 126 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DSA in Python 2 | 3 | ## I. Introduction 4 | - A brief overview of DSA in Python 5 | - Importance of Data Structures and Algorithms 6 | 7 | ## II. Basics of Data Structures 8 | - Definition and significance 9 | - Common types: Lists, Stacks, Queues 10 | 11 | ## III. DSA Implementation in Python 12 | - Python's built-in data structures 13 | - Custom implementation of data structures 14 | 15 | ## IV. Algorithms in Python 16 | - Overview of algorithms 17 | - Pythonic ways of algorithmic implementation 18 | 19 | ## V. Optimization Techniques 20 | - Strategies for optimizing code 21 | - Importance of time and space complexity in Python 22 | 23 | ## VI. Real-world Applications 24 | - Practical uses of DSA in Python 25 | - Examples from different domains 26 | 27 | ## VII. Challenges and Solutions 28 | - Common challenges faced in DSA 29 | - Solutions and best practices in Python 30 | 31 | ## VIII. DSA Libraries in Python 32 | - Overview of popular libraries 33 | - How to leverage them for efficient coding 34 | 35 | ## IX. Learning Resources 36 | - Books, websites, and courses for DSA in Python 37 | - Tips for continuous learning 38 | 39 | ## X. Python Community and DSA 40 | - Connecting with like-minded Python enthusiasts 41 | - Engaging in open-source DSA projects 42 | 43 | ## XI. Future Trends 44 | - Emerging technologies in DSA with Python 45 | - Preparing for the future 46 | 47 | ## XII. Case Studies 48 | - Analyzing successful implementations 49 | - Learning from real-world examples 50 | 51 | ## XIII. Tips for Interview Preparation 52 | - Common DSA questions in Python interviews 53 | - Strategies for effective preparation 54 | 55 | ## XIV. Frequently Asked Questions 56 | - Answering common queries related to DSA in Python 57 | 58 | ## XV. Conclusion 59 | - Summarizing the importance of DSA in Python 60 | - Encouraging continuous learning and exploration 61 | 62 | --- 63 | 64 | DSA, or Data Structures and Algorithms, are fundamental concepts in computer science and programming. They are crucial for organizing and processing data efficiently in various applications. There are several types of data structures and algorithms, each serving a specific purpose. Here's a brief overview: 65 | 66 | ### **Types of Data Structures:** 67 | 68 | 1. **Arrays:** 69 | - A collection of elements stored in contiguous memory locations. 70 | 71 | 2. **Linked Lists:** 72 | - Elements are linked through pointers, allowing dynamic memory allocation. 73 | 74 | 3. **Stacks:** 75 | - Follows the Last In, First Out (LIFO) principle, used for managing function calls and recursion. 76 | 77 | 4. **Queues:** 78 | - Follows the First In, First Out (FIFO) principle, often used for task scheduling. 79 | 80 | 5. **Trees:** 81 | - Hierarchical structure with nodes connected by edges, common in hierarchical data representation. 82 | 83 | 6. **Graphs:** 84 | - A collection of nodes and edges, representing relationships between various elements. 85 | 86 | 7. **Hash Tables:** 87 | - Uses a hash function to map keys to values, enabling fast data retrieval. 88 | 89 | 8. **Heaps:** 90 | - Specialized tree-based structure, commonly used for implementing priority queues. 91 | 92 | ### **Types of Algorithms:** 93 | 94 | 1. **Searching Algorithms:** 95 | - Techniques to find a particular item in a collection. 96 | 97 | 2. **Sorting Algorithms:** 98 | - Arranging elements in a specific order, such as ascending or descending. 99 | 100 | 3. **Graph Algorithms:** 101 | - Solving problems related to graphs, like finding the shortest path or detecting cycles. 102 | 103 | 4. **Dynamic Programming:** 104 | - Solves problems by breaking them down into smaller overlapping subproblems. 105 | 106 | 5. **Greedy Algorithms:** 107 | - Makes locally optimal choices at each stage with the hope of finding a global optimum. 108 | 109 | 6. **Divide and Conquer:** 110 | - Breaks down a problem into subproblems, solves them independently, and combines solutions. 111 | 112 | 7. **Backtracking:** 113 | - Systematic trial and error approach used to find all possible solutions. 114 | 115 | 8. **Hashing Algorithms:** 116 | - Utilizes hash functions to map data to fixed-size arrays, ensuring efficient data retrieval. 117 | 118 | Understanding these types of data structures and algorithms is essential for writing efficient and scalable code in various programming languages, including Python. 119 | 120 | # **DSA in Python: Mastering Data Structures and Algorithms** 121 | 122 | ## Introduction 123 | 124 | In the vast landscape of programming languages, Python stands out as a versatile and powerful tool. One of its key strengths lies in its ability to seamlessly integrate Data Structures and Algorithms (DSA). In this article, we'll delve into the world of DSA in Python, exploring its basics, implementation, real-world applications, and much more. 125 | 126 | ## Basics of Data Structures 127 | 128 | Before diving into Python-specifics, let's understand the fundamentals of Data Structures. These are the building blocks of any efficient algorithm, facilitating the storage and retrieval of data. Common types include lists, stacks, and queues, each serving a unique purpose in problem-solving. 129 | 130 | ## DSA Implementation in Python 131 | 132 | Python provides a rich set of built-in data structures, simplifying the implementation of DSA. Additionally, creating custom data structures tailored to specific needs is straightforward. We'll explore both aspects, showcasing the versatility Python offers. 133 | 134 | ## Algorithms in Python 135 | 136 | Algorithms are the heart of efficient coding. We'll discuss the basics of algorithms and how Pythonic they can be. With its readability and expressiveness, Python provides an excellent platform for algorithmic implementations. 137 | 138 | ## Optimization Techniques 139 | 140 | Efficiency is paramount in coding. We'll explore strategies for optimizing code, emphasizing the significance of time and space complexity in Python programming. 141 | 142 | ## Real-world Applications 143 | 144 | From web development to machine learning, DSA plays a crucial role in various domains. We'll examine practical applications, showcasing Python's prowess in solving real-world problems. 145 | 146 | ## Challenges and Solutions 147 | 148 | Coding isn't without its challenges. We'll address common stumbling blocks in DSA and provide practical solutions, ensuring smoother coding experiences in Python. 149 | 150 | ## DSA Libraries in Python 151 | 152 | Python boasts powerful libraries for DSA. We'll take a closer look at these libraries, demonstrating how they can be leveraged to streamline coding and enhance efficiency. 153 | 154 | ## Learning Resources 155 | 156 | Continuous learning is key. We'll guide you to valuable books, websites, and courses that will aid your journey in mastering DSA with Python. 157 | 158 | ## Python Community and DSA 159 | 160 | Connecting with like-minded individuals enhances the learning experience. We'll explore how you can engage with the Python community, participate in open-source projects, and grow as a programmer. 161 | 162 | ## Future Trends 163 | 164 | The tech landscape evolves rapidly. We'll discuss emerging trends in DSA with Python, equipping you with knowledge for the future. 165 | 166 | ## Case Studies 167 | 168 | Analyzing successful implementations is a great way to learn. We'll delve into case studies, unraveling the secrets behind effective DSA in Python. 169 | 170 | ## Tips for Interview Preparation 171 | 172 | Preparing for interviews involves understanding common DSA questions. We'll provide insights and strategies to help you excel in Python-focused interviews. 173 | 174 | ## Frequently Asked Questions 175 | 176 | 1. **Is Python suitable for DSA?** 177 | - Absolutely! Python's simplicity and readability make it an excellent choice for DSA. 178 | 179 | 2. **Which is better, built-in or custom data structures?** 180 | - It depends on the context. Built-in structures are convenient, but custom ones offer tailored solutions. 181 | 182 | 3. **How can I enhance time complexity in Python?** 183 | - Explore Python's built-in functions and optimize loops for better time complexity. 184 | 185 | 4. **Are there DSA challenges specific to Python?** 186 | - While challenges exist, Python's community often provides solutions and workarounds. 187 | 188 | 5. **Where can I find Python DSA projects to contribute to?** 189 | - Platforms like GitHub host numerous open-source DSA projects. Explore and contribute! 190 | 191 | ## Conclusion 192 | 193 | In conclusion, mastering DSA in Python opens doors to endless possibilities in the coding world. Embrace the challenges, engage with the community, and keep learning. Python's elegance combined with solid DSA skills will undoubtedly set you on a path to success. 194 | 195 | # Share your thoughts with Us and Send This Repo to Your Coder Friends 196 | 197 | ## Our Linkedin Profile link [Follow on Linkedin ](https://www.linkedin.com/in/realhariom/). 198 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------