├── README.md ├── hashing ├── README.md ├── open-addressing-1.py ├── open-addressing.py └── separate-chaining.py ├── linked-list ├── README.md ├── circular-linked-list.py ├── concatenate-circular-lists.py ├── concatenate-single-lists.py ├── double-linked-list.py ├── header-linked-list.py ├── merging-linked-lists.py ├── single-linked-list-1.py ├── single-linked-list.py └── sorted-linked-list.py ├── searching ├── README.md ├── binary-search-recursive.py ├── binary-search.py ├── linear-search-sentinel.py ├── linear-search-sorted.py └── linear-search.py ├── sorting ├── README.md ├── binary-tree-sort.py ├── bubble-sort-improvement.py ├── bubble-sort.py ├── heap-sort.py ├── insertion-sort.py ├── merge-sort-iterative.py ├── merge-sort-recursive.py ├── merging.py ├── merging1.py ├── quick-sort.py ├── radix-sort.py ├── selection-sort.py └── shell-sort.py ├── stack-and-queue ├── README.md ├── circular-queue.py ├── deque-circular.py ├── deque-linked.py ├── deque.py ├── infix-to-postfix.py ├── parentheses.py ├── priority-queue.py ├── queue-array-1.py ├── queue-array.py ├── queue-circular-linked.py ├── queue-linked.py ├── stack-array-1.py ├── stack-array.py └── stack-linked.py └── tree ├── README.md ├── binary-search-tree.py ├── binary-tree.py ├── build-heap.py └── heap.py /README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithmsin Python ( DSA ) 2 | 3 | This [Data Structures and Algorithms In Python (DSA)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Instructor - Deepali Srivastava, Author of [Ultimate Python Programming](https://www.amazon.in/Ultimate-Python-Programming-programs-questions/dp/935551655X) 7 | * Thoroughly detailed course with complete working programs 8 | * Contains lots of animations to help you visualize the concepts 9 | * Includes over 100 Quiz questions 10 | * Builds a solid foundation in Data Structures and Algorithms 11 | * Prepares you for coding interviews 12 | * Lifetime Access 13 | 14 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 15 | 16 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 19 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 20 | 21 | 31 | 32 | ## Copyright 33 | © Copyright Deepali Srivastava : All rights reserved. 34 | Not to be used for commercial purposes. 35 | -------------------------------------------------------------------------------- /hashing/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Python 2 | 3 | This [“Data Structures and Algorithms in Python”]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 17 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 18 | [![exception-handling-in-python](https://user-images.githubusercontent.com/98641125/153196201-83c1a210-9c4f-4ba5-a56f-6aa4d4c551b2.png)]( https://www.udemy.com/course/exception-handling-in-python/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /hashing/open-addressing-1.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class InvalidOperationException(Exception): 5 | pass 6 | 7 | class studentRecord: 8 | def __init__(self,i,Name): 9 | self.studentId = i 10 | self.studentName = Name 11 | 12 | def get_student_id(self): 13 | return self.studentId 14 | 15 | def set_student_id(self,i): 16 | self.studentId = i 17 | 18 | def __str__(self): 19 | return str(self.studentId) + " " + self.studentName 20 | 21 | 22 | class HashTable: 23 | 24 | def __init__(self,tableSize=11): 25 | self.m = tableSize 26 | self.array = [None] * self.m 27 | self.n = 0 28 | 29 | def hash1(self, key): 30 | return (key % self.m) 31 | 32 | def rehash(self, new_size): 33 | temp = HashTable(new_size) 34 | 35 | for i in range(self.m): 36 | if self.array[i] != None and self.array[i].get_student_id() != -1: 37 | temp.insert(self.array[i]) 38 | self.array = temp.array 39 | self.m = new_size 40 | 41 | def insert1(self, newRecord): 42 | if self.n >= self.m//2: 43 | self.rehash( self.next_prime(2*self.m) ) 44 | print("New Table Size is : " , self.m ) 45 | self.insert(newRecord) 46 | 47 | def insert(self, newRecord): 48 | key = newRecord.get_student_id() 49 | h = self.hash1(key) 50 | 51 | location = h 52 | 53 | for i in range(1,self.m): 54 | if self.array[location] is None or self.array[location].get_student_id() == -1: 55 | self.array[location] = newRecord 56 | self.n+=1 57 | return 58 | 59 | if self.array[location].get_student_id() == key: 60 | raise InvalidOperationException("Duplicate key") 61 | 62 | location = (h + i) % self.m 63 | 64 | 65 | def next_prime(self,x): 66 | while self.is_prime(x) is not True: 67 | x=x+1 68 | return x 69 | 70 | def is_prime(self, x): 71 | for i in range(2,x): 72 | if x % i == 0: 73 | return False 74 | return True 75 | 76 | def search(self,key): 77 | h = self.hash1(key) 78 | location = h 79 | 80 | for i in range(1,self.m): 81 | if self.array[location] is None : 82 | return None 83 | if self.array[location].get_student_id() == key: 84 | return self.array[location] 85 | location = (h + i) % self.m 86 | return None 87 | 88 | def display_table(self): 89 | 90 | for i in range(self.m): 91 | print("[",end=''); print(i,end=''); print("]",end=''); 92 | 93 | if self.array[i] is not None and self.array[i].get_student_id() != -1: 94 | print(self.array[i]) 95 | else: 96 | print("___") 97 | 98 | def delete(self,key): 99 | h = self.hash1(key) 100 | location = h 101 | 102 | for i in range(1,self.m): 103 | if self.array[location] is None: 104 | return 105 | 106 | if self.array[location].get_student_id() == key: 107 | self.array[location].set_student_id(-1) 108 | self.n-=1 109 | if self.n > 0 and self.n <= self.m/8: 110 | self.rehash(self.next_prime(self.m//2)) 111 | print( "New Table Size is : " ,self.m ) 112 | 113 | location = (h + i) % self.m 114 | 115 | 116 | ################################################################################# 117 | 118 | 119 | size = int(input("Enter initial size of table : ")) 120 | table = HashTable(size) 121 | 122 | while True: 123 | print("1.Insert a record") 124 | print("2.search a record") 125 | print("3.delete a record") 126 | print("4.Display table") 127 | print("5.Exit") 128 | option = int(input("Enter your option : ")) 129 | 130 | if option == 1: 131 | id = int(input("Enter student id : ")) 132 | name = input("Enter student name : ") 133 | aRecord = studentRecord(id,name) 134 | table.insert1(aRecord) 135 | elif option == 2: 136 | idd = int(input("Enter a key to be searched : ")) 137 | aRecord = table.search(id) 138 | if aRecord is None: 139 | print("Key not found") 140 | else: 141 | print(aRecord) 142 | elif option == 3: 143 | id = int(input("Enter a key to be deleted :")) 144 | table.delete(id) 145 | elif option == 4: 146 | table.display_table() 147 | elif option == 5: 148 | break 149 | else: 150 | print("Wrong option") 151 | print() 152 | -------------------------------------------------------------------------------- /hashing/open-addressing.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class InvalidOperationException(Exception): 5 | pass 6 | 7 | class studentRecord: 8 | def __init__(self,i,Name): 9 | self.studentId = i 10 | self.studentName = Name 11 | 12 | def get_student_id(self): 13 | return self.studentId 14 | 15 | def set_student_id(self,i): 16 | self.studentId = i 17 | 18 | def __str__(self): 19 | return str(self.studentId) + " " + self.studentName 20 | 21 | 22 | class HashTable: 23 | 24 | def __init__(self,tableSize=11): 25 | self.m = tableSize 26 | self.array = [None] * self.m 27 | self.n = 0 28 | 29 | def hash1(self, key): 30 | return (key % self.m) 31 | 32 | def insert(self, newRecord): 33 | key = newRecord.get_student_id() 34 | h = self.hash1(key) 35 | 36 | location = h 37 | 38 | for i in range(1,self.m): 39 | if self.array[location] is None or self.array[location].get_student_id() == -1: 40 | self.array[location] = newRecord 41 | self.n+=1 42 | return 43 | 44 | if self.array[location].get_student_id() == key: 45 | raise InvalidOperationException("Duplicate key") 46 | 47 | location = (h + i) % self.m 48 | 49 | print("Table is full : Record can't be inserted ") 50 | 51 | 52 | def search(self,key): 53 | h = self.hash1(key) 54 | location = h 55 | 56 | for i in range(1,self.m): 57 | if self.array[location] is None : 58 | return None 59 | if self.array[location].get_student_id() == key: 60 | return self.array[location] 61 | location = (h + i) % self.m 62 | return None 63 | 64 | 65 | def display_table(self): 66 | 67 | for i in range(self.m): 68 | print("[",end=''); print(i,end=''); print("]",end=''); 69 | 70 | if self.array[i] is not None and self.array[i].get_student_id() != -1: 71 | print(self.array[i]) 72 | else: 73 | print("___") 74 | 75 | def delete(self,key): 76 | h = self.hash1(key) 77 | location = h 78 | 79 | for i in range(1,self.m): 80 | if self.array[location] is None: 81 | return None 82 | 83 | if self.array[location].get_student_id() == key: 84 | temp = self.array[location] 85 | self.array[location].set_student_id(-1) 86 | self.n-=1 87 | return temp 88 | 89 | location = (h + i) % self.m 90 | 91 | return None 92 | 93 | ################################################################################## 94 | 95 | 96 | size = int(input("Enter initial size of table : ")) 97 | table = HashTable(size) 98 | 99 | while True: 100 | print("1.Insert a record") 101 | print("2.Search a record") 102 | print("3.Delete a record") 103 | print("4.Display table") 104 | print("5.Exit") 105 | option = int(input("Enter your option : ")) 106 | 107 | if option == 1: 108 | id = int(input("Enter student id : ")) 109 | name = input("Enter student name : ") 110 | aRecord = studentRecord(id,name) 111 | table.insert(aRecord) 112 | elif option == 2: 113 | id = int(input("Enter a key to be searched : ")) 114 | aRecord = table.search(id) 115 | if aRecord is None: 116 | print("Key not found") 117 | else: 118 | print(aRecord) 119 | elif option == 3: 120 | id = int(input("Enter a key to be deleted :")) 121 | table.delete(id) 122 | elif option == 4: 123 | table.display_table() 124 | elif option == 5: 125 | break 126 | else: 127 | print("Wrong option") 128 | print() 129 | -------------------------------------------------------------------------------- /hashing/separate-chaining.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | ################################################################## 5 | 6 | class studentRecord: 7 | def __init__(self,i,name): 8 | self.studentId = i 9 | self.studentName = name 10 | 11 | def get_student_id(self): 12 | return self.studentId 13 | 14 | def set_student_id(self,i): 15 | self.studentId = i 16 | 17 | def __str__(self): 18 | return str(self.studentId) + " " + self.studentName 19 | 20 | ################################################################## 21 | 22 | class Node: 23 | def __init__(self,value): 24 | self.info = value 25 | self.link = None 26 | 27 | ################################################################## 28 | 29 | class SingleLinkedList: 30 | def __init__(self): 31 | self.start = None 32 | 33 | def display_list(self): 34 | if self.start is None: 35 | print("___") 36 | return 37 | p = self.start 38 | while p is not None: 39 | print(p.info , " ", end='') 40 | p = p.link 41 | print() 42 | 43 | def search(self,x): 44 | p = self.start 45 | while p is not None: 46 | if p.info.get_student_id() == x: 47 | return p.info 48 | p = p.link 49 | else: 50 | return None 51 | 52 | def insert_in_beginning(self, data): 53 | temp = Node(data) 54 | temp.link = self.start 55 | self.start = temp 56 | 57 | def delete_node(self,x): 58 | if self.start is None: 59 | print("List is empty") 60 | return 61 | 62 | # Deletion of first node 63 | if self.start.info.get_student_id() == x: 64 | self.start = self.start.link 65 | return 66 | 67 | # Deletion in between or at the end 68 | p = self.start 69 | while p.link is not None: 70 | if p.link.info.get_student_id() == x: 71 | break 72 | p = p.link 73 | 74 | if p.link is None: 75 | print("Element ", x ,"not in list") 76 | else: 77 | p.link = p.link.link 78 | 79 | ################################################################## 80 | 81 | 82 | class HashTable: 83 | 84 | def __init__(self,tableSize): 85 | self.m = tableSize 86 | self.array = [None] * self.m 87 | self.n = 0 88 | 89 | def hash(self, key): 90 | return (key % self.m) 91 | 92 | def display_table(self): 93 | for i in range(self.m): 94 | print( "[" , i , "] --> ", end ='' ) 95 | if self.array[i]!= None: 96 | self.array[i].display_list() 97 | else: 98 | print("___") 99 | 100 | 101 | def search(self, key): 102 | h = self.hash(key) 103 | if self.array[h] != None: 104 | return self.array[h].search(key) 105 | return None 106 | 107 | def insert(self, newRecord): 108 | key = newRecord.get_student_id() 109 | h = self.hash(key) 110 | 111 | if self.array[h] == None: 112 | self.array[h] = SingleLinkedList() 113 | self.array[h].insert_in_beginning(newRecord) 114 | self.n+=1 115 | 116 | 117 | 118 | def delete(self, key): 119 | h = self.hash(key) 120 | if self.array[h] != None: 121 | self.array[h].delete_node(key) 122 | self.n-=1 123 | else: 124 | print("Value " , key , " not present") 125 | 126 | 127 | 128 | ################################################################## 129 | 130 | size = int(input("Enter size of table : ")) 131 | table = HashTable(size) 132 | 133 | while True: 134 | print("1.Insert a record") 135 | print("2.Search a record") 136 | print("3.Delete a record") 137 | print("4.Display table") 138 | print("5.Exit") 139 | 140 | choice = int(input("Enter your choice : ")) 141 | if choice == 1: 142 | id = int(input("Enter student id : ")) 143 | name = input("Enter student name : ") 144 | aRecord = studentRecord(id,name) 145 | table.insert(aRecord) 146 | elif choice == 2: 147 | id = int(input("Enter a key to be searched : ")) 148 | aRecord = table.search(id) 149 | if aRecord is None: 150 | print("Key not found") 151 | else: 152 | print(aRecord) 153 | elif choice == 3: 154 | id = int(input("Enter a key to be deleted :")) 155 | table.delete(id) 156 | elif choice == 4: 157 | table.display_table() 158 | elif choice == 5: 159 | break 160 | else: 161 | print("Wrong option") 162 | print() 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /linked-list/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Python 2 | 3 | This [“Data Structures and Algorithms in Python”]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 17 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 18 | [![exception-handling-in-python](https://user-images.githubusercontent.com/98641125/153196201-83c1a210-9c4f-4ba5-a56f-6aa4d4c551b2.png)]( https://www.udemy.com/course/exception-handling-in-python/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 23 | 24 | 25 | ## Copyright 26 | © Copyright Deepali Srivastava : All rights reserved. 27 | Not to be used for commercial purposes. 28 | -------------------------------------------------------------------------------- /linked-list/circular-linked-list.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node(object): 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.link = None 9 | 10 | 11 | class CircularLinkedList(object): 12 | 13 | def __init__(self): 14 | self.last = None 15 | 16 | def display_list(self): 17 | if self.last == None: 18 | print("List is empty\n") 19 | return 20 | 21 | p = self.last.link 22 | 23 | while True: 24 | print(p.info , " ",end='') 25 | p = p.link 26 | if p == self.last.link: 27 | break 28 | print() 29 | 30 | def insert_in_beginning(self,data): 31 | temp = Node(data) 32 | temp.link = self.last.link 33 | self.last.link = temp 34 | 35 | def insert_in_empty_list(self,data): 36 | temp = Node(data) 37 | self.last = temp 38 | self.last.link = self.last 39 | 40 | def insert_at_end(self,data): 41 | temp = Node(data) 42 | temp.link = self.last.link 43 | self.last.link = temp 44 | self.last = temp 45 | 46 | def create_list(self): 47 | n = int(input("Enter the number of nodes : ")) 48 | if n == 0: 49 | return 50 | data = int(input("Enter the element to be inserted : ")) 51 | self.insert_in_empty_list(data) 52 | 53 | for i in range(n-1): 54 | data = int(input("Enter the element to be inserted : ")) 55 | self.insert_at_end(data) 56 | 57 | def insert_after(self,data,x): 58 | p = self.last.link 59 | 60 | while True: 61 | if p.info == x: 62 | break 63 | p = p.link 64 | if p == self.last.link: 65 | break 66 | 67 | if p == self.last.link and p.info != x: 68 | print(x , " not present in the list") 69 | else: 70 | temp = Node(data) 71 | temp.link = p.link 72 | p.link = temp 73 | if p == self.last: 74 | self.last = temp 75 | 76 | def delete_first_node(self): 77 | if self.last is None: # List is empty 78 | return 79 | if self.last.link == self.last: # List has only one node 80 | self.last = None 81 | return 82 | self.last.link = self.last.link.link 83 | 84 | 85 | def delete_last_node(self): 86 | if self.last is None: # List is empty 87 | return 88 | if self.last.link == self.last: # List has only one node 89 | self.last = None 90 | return 91 | 92 | p = self.last.link 93 | while p.link != self.last: 94 | p = p.link 95 | p.link = self.last.link 96 | self.last = p 97 | 98 | def delete_node(self, x): 99 | 100 | if self.last is None: # List is empty 101 | return 102 | if self.last.link == self.last and self.last.info == x: # Deletion of only node 103 | self.last = None 104 | return 105 | 106 | if self.last.link.info == x: # Deletion of first node 107 | self.last.link = self.last.link.link 108 | return 109 | 110 | p = self.last.link 111 | while p.link != self.last.link: 112 | if p.link.info == x : 113 | break 114 | p = p.link 115 | 116 | if p.link == self.last.link: 117 | print(x , " not found in the list") 118 | else: 119 | p.link = p.link.link 120 | if self.last.info == x: 121 | self.last = p 122 | 123 | 124 | ############################################################## 125 | 126 | list = CircularLinkedList() 127 | list.create_list() 128 | 129 | while True: 130 | print("1.Display list") 131 | print("2.Insert in empty list") 132 | print("3.Insert a node in the beginning of the list") 133 | print("4.Insert a node at the end of the list") 134 | print("5.Insert a node after a specified node") 135 | print("6.Delete first node") 136 | print("7.Delete last node") 137 | print("8.Delete any node") 138 | print("9.Quit") 139 | 140 | option = int(input("Enter your choice : " )) 141 | 142 | if option == 1: 143 | list.display_list() 144 | elif option == 2: 145 | data = int(input("Enter the element to be inserted : ")) 146 | list.insert_in_empty_list(data) 147 | elif option == 3: 148 | data = int(input("Enter the element to be inserted : ")) 149 | list.insert_in_beginning(data) 150 | elif option == 4: 151 | data = int(input("Enter the element to be inserted : ")) 152 | list.insert_at_end(data) 153 | elif option == 5: 154 | data = int(input("Enter the element to be inserted : ")) 155 | x = int(input("Enter the element after which to insert : ")) 156 | list.insert_after(data,x) 157 | elif option == 6: 158 | list.delete_first_node() 159 | elif option == 7: 160 | list.delete_last_node() 161 | elif option == 8: 162 | data = int(input("Enter the element to be deleted : ")) 163 | list.delete_node(data) 164 | elif option == 9: 165 | break 166 | else: 167 | print("Wrong option") 168 | print() 169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /linked-list/concatenate-circular-lists.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node(object): 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.link = None 9 | 10 | 11 | class CircularLinkedList(object): 12 | 13 | def __init__(self): 14 | self.last = None 15 | 16 | def display_list(self): 17 | if self.last == None: 18 | print("List is empty\n") 19 | return 20 | 21 | p = self.last.link 22 | 23 | while True: 24 | print(p.info , " ",end='') 25 | p = p.link 26 | if p == self.last.link: 27 | break 28 | print() 29 | 30 | def insert_in_empty_list(self,data): 31 | temp = Node(data) 32 | self.last = temp 33 | self.last.link = self.last 34 | 35 | 36 | def insert_at_end(self,data): 37 | temp = Node(data) 38 | temp.link = self.last.link 39 | self.last.link = temp 40 | self.last = temp 41 | 42 | 43 | def create_list(self): 44 | n = int(input("Enter the number of nodes : ")) 45 | if n == 0: 46 | return 47 | data = int(input("Enter the element to be inserted : ")) 48 | self.insert_in_empty_list(data) 49 | 50 | # for(i = 2 i <= n i++) 51 | for i in range(n-1): 52 | data = int(input("Enter the element to be inserted : ")) 53 | self.insert_at_end(data) 54 | 55 | def concatenate(self,list2): 56 | if self.last is None: 57 | self.last = list.last; 58 | return 59 | 60 | if list2.last is None: 61 | return 62 | 63 | p = self.last.link; 64 | self.last.link = list2.last.link; 65 | list2.last.link = p; 66 | self.last = list2.last; 67 | 68 | 69 | ############################################################### 70 | list1 = CircularLinkedList() 71 | list2 = CircularLinkedList() 72 | 73 | print("Enter first list :- ") 74 | list1.create_list() 75 | 76 | print("Enter second list :- ") 77 | list2.create_list() 78 | 79 | print("First "); list1.display_list() 80 | print("Second "); list2.display_list() 81 | 82 | list1.concatenate(list2) 83 | print("First ") 84 | list1.display_list() 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /linked-list/concatenate-single-lists.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node: 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.link = None 9 | 10 | 11 | class SingleLinkedList: 12 | 13 | def __init__(self): 14 | self.start = None 15 | 16 | def display_list(self): 17 | if self.start is None: 18 | print("List is empty") 19 | return 20 | else: 21 | print("List is : ") 22 | p = self.start 23 | while p is not None: 24 | print(p.info , " ", end='') 25 | p = p.link 26 | print() 27 | 28 | def insert_at_end(self, data): 29 | temp = Node(data) 30 | if self.start is None: 31 | self.start = temp 32 | return 33 | 34 | p = self.start 35 | while p.link is not None: 36 | p = p.link 37 | p.link = temp 38 | 39 | 40 | def create_list(self): 41 | n = int(input("Enter the number of nodes : ")) 42 | if n == 0: 43 | return 44 | for i in range(n): 45 | data = int(input("Enter the element to be inserted : ")) 46 | self.insert_at_end(data) 47 | 48 | def concatenate(self, list2): 49 | if self.start is None: 50 | self.start = list2.start 51 | return 52 | 53 | if list2.start is None: 54 | return 55 | 56 | p = self.start 57 | while p.link is not None: 58 | p = p.link 59 | 60 | p.link = list2.start 61 | 62 | ######################################################################################### 63 | 64 | list1 = SingleLinkedList() 65 | list2 = SingleLinkedList() 66 | 67 | print("Enter first list :- ") 68 | list1.create_list() 69 | print("Enter second list :- ") 70 | list2.create_list() 71 | 72 | print("First "); list1.display_list() 73 | print("Second "); list2.display_list() 74 | 75 | list1.concatenate(list2) 76 | print("First "); list1.display_list() 77 | 78 | -------------------------------------------------------------------------------- /linked-list/double-linked-list.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node(object): 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.prev = None 9 | self.next = None 10 | 11 | 12 | class DoubleLinkedList(object): 13 | 14 | def __init__(self): 15 | self.start = None 16 | 17 | def display_list(self): 18 | if self.start is None: 19 | print("List is empty") 20 | return 21 | 22 | print("List is : ") 23 | p = self.start 24 | while p is not None: 25 | print(p.info, " ", end='') 26 | p = p.next 27 | print() 28 | 29 | 30 | def insert_in_beginning(self,data): 31 | temp = Node(data) 32 | temp.next = self.start 33 | self.start.prev = temp 34 | self.start = temp 35 | 36 | def insert_in_empty_list(self,data): 37 | temp = Node(data) 38 | self.start = temp 39 | 40 | def insert_at_end(self,data): 41 | temp = Node(data) 42 | p = self.start 43 | while p.next is not None: 44 | p = p.next 45 | p.next = temp 46 | temp.prev = p 47 | 48 | 49 | def create_list(self): 50 | n = int(input("Enter the number of nodes : ")) 51 | if n == 0: 52 | return 53 | data = int(input("Enter the first element to be inserted : ")) 54 | self.insert_in_empty_list(data) 55 | 56 | for i in range(n-1): 57 | data = int(input("Enter the next element to be inserted : ")) 58 | self.insert_at_end(data) 59 | 60 | 61 | def insert_after(self,data, x): 62 | temp = Node(data) 63 | p = self.start 64 | while p is not None: 65 | if p.info == x: 66 | break 67 | p = p.next 68 | 69 | if p is None: 70 | print(x," not present in the list") 71 | else: 72 | temp.prev = p 73 | temp.next = p.next 74 | if p.next is not None: 75 | p.next.prev = temp # should not be done when p refers to last node 76 | p.next = temp; 77 | 78 | def insert_before(self,data,x): 79 | if self.start is None: 80 | print("List is empty") 81 | return 82 | 83 | if self.start.info == x: 84 | temp = Node(data) 85 | temp.next = self.start 86 | self.start.prev = temp 87 | self.start = temp 88 | return 89 | 90 | p = self.start; 91 | while p is not None: 92 | if p.info == x: 93 | break 94 | p = p.next 95 | 96 | if p is None: 97 | print(x, " not present in the list") 98 | else: 99 | temp = Node(data) 100 | temp.prev = p.prev 101 | temp.next = p 102 | p.prev.next = temp 103 | p.prev = temp 104 | 105 | def delete_first_node(self): 106 | if self.start is None: # list is empty 107 | return 108 | if self.start.next is None: # list has only one node 109 | self.start = None 110 | return 111 | self.start = self.start.next 112 | self.start.prev = None 113 | 114 | def delete_last_node(self): 115 | if self.start is None: # list is empty 116 | return 117 | if self.start.next is None: # list has only one node 118 | self.start = None 119 | return 120 | 121 | p = self.start; 122 | while p.next != None: 123 | p = p.next; 124 | p.prev.next = None 125 | 126 | 127 | def delete_node(self,x): 128 | if self.start is None: # list is empty 129 | return 130 | if self.start.next is None: # list has only one node 131 | if self.start.info == x: 132 | self.start = None; 133 | else: 134 | print(x," not found") 135 | return 136 | 137 | # Deletion of first node 138 | if self.start.info == x: 139 | self.start = self.start.next 140 | self.start.prev = None 141 | return 142 | 143 | p = self.start.next 144 | while p.next is not None: 145 | if p.info == x: 146 | break 147 | p = p.next 148 | 149 | 150 | if p.next is not None : # node to be deleted is in between 151 | p.prev.next = p.next 152 | p.next.prev = p.prev; 153 | else: # p refers to last node 154 | if p.info == x: # node to be deleted is last node 155 | p.prev.next = None 156 | else: 157 | print(x," not found") 158 | 159 | def reverse_list(self): 160 | if self.start is None: 161 | return 162 | 163 | p1 = self.start 164 | p2 = p1.next 165 | p1.next = None 166 | p1.prev = p2 167 | while p2 is not None: 168 | p2.prev = p2.next 169 | p2.next = p1 170 | p1 = p2 171 | p2 = p2.prev 172 | self.start = p1 173 | 174 | ######################################################################### 175 | 176 | list = DoubleLinkedList() 177 | list.create_list() 178 | 179 | while True: 180 | print("1.Display list") 181 | print("2.Insert in empty list") 182 | print("3.Insert a node in the beginning of the list") 183 | print("4.Insert a node at the end of the list") 184 | print("5.Insert a node after a specified node") 185 | print("6.Insert a node before a specified node") 186 | print("7.Delete first node") 187 | print("8.Delete last node") 188 | print("9.Delete any node") 189 | print("10.Reverse the list") 190 | print("11.Quit") 191 | 192 | option = int(input("Enter your choice : " )) 193 | 194 | if option == 1: 195 | list.display_list() 196 | elif option == 2: 197 | data = int(input("Enter the element to be inserted : ")) 198 | list.insert_in_empty_list(data) 199 | elif option == 3: 200 | data = int(input("Enter the element to be inserted : ")) 201 | list.insert_in_beginning(data) 202 | elif option == 4: 203 | data = int(input("Enter the element to be inserted : ")) 204 | list.insert_at_end(data) 205 | elif option == 5: 206 | data = int(input("Enter the element to be inserted : ")) 207 | x = int(input("Enter the element after which to insert : ")) 208 | list.insert_after(data,x) 209 | elif option == 6: 210 | data = int(input("Enter the element to be inserted : ")) 211 | x = int(input("Enter the element before which to insert : ")) 212 | list.insert_before(data,x) 213 | elif option == 7: 214 | list.delete_first_node() 215 | elif option == 8: 216 | list.delete_last_node() 217 | elif option == 9: 218 | data = int(input("Enter the element to be deleted : ")) 219 | list.delete_node(data) 220 | elif option == 10: 221 | list.reverse_list() 222 | elif option == 11: 223 | break 224 | else: 225 | print("Wrong option") 226 | print() 227 | 228 | 229 | 230 | -------------------------------------------------------------------------------- /linked-list/header-linked-list.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node(object): 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.link = None 9 | 10 | class HeaderLinkedList(object): 11 | 12 | def __init__(self): 13 | self.head = Node(0) 14 | 15 | def display_list(self): 16 | if self.head.link == None: 17 | print("List is empty") 18 | return 19 | 20 | p = self.head.link 21 | print("List is : ") 22 | while p is not None: 23 | print(p.info, " ", end='') 24 | p = p.link 25 | print() 26 | 27 | 28 | 29 | def create_list(self): 30 | n = int(input("Enter the number of nodes : ")) 31 | for i in range(n): 32 | data = int(input("Enter the element to be inserted : ")) 33 | self.insert_at_end(data) 34 | 35 | 36 | def insert_at_end(self,data): 37 | 38 | temp = Node(data) 39 | 40 | p = self.head 41 | while p.link is not None: 42 | p = p.link 43 | 44 | p.link = temp 45 | temp.link = None 46 | 47 | def insert_before(self, data, x): 48 | # Find pointer to predecessor of node containing x 49 | p = self.head 50 | while p.link is not None: 51 | if p.link.info == x: 52 | break 53 | p = p.link 54 | 55 | if p.link is None: 56 | print(x , " not present in the list") 57 | else: 58 | temp = Node(data) 59 | temp.link = p.link 60 | p.link = temp 61 | 62 | def insert_at_position(self,data,k): 63 | p = self.head 64 | i = 1 65 | while i <= k-1 and p is not None: 66 | p = p.link 67 | i+=1 68 | 69 | if p is None: 70 | print("You can insert only upto " , (i-1) , "th position ") 71 | else: 72 | temp = Node(data) 73 | temp.link = p.link 74 | p.link = temp 75 | 76 | def delete_node(self,data): 77 | p = self.head 78 | while p.link is not None: 79 | if p.link.info == data: 80 | break 81 | p = p.link 82 | 83 | if p.link == None: 84 | print(data + "Element %d not found") 85 | else: 86 | p.link = p.link.link 87 | 88 | def reverse_list(self): 89 | prev = None 90 | p = self.head.link 91 | while p is not None: 92 | next = p.link 93 | p.link = prev 94 | prev = p 95 | p = next 96 | 97 | self.head.link = prev 98 | 99 | list = HeaderLinkedList() 100 | 101 | list.create_list() 102 | 103 | while True: 104 | print("1.Display list") 105 | print("2.Insert a node at the end of the list") 106 | print("3.Insert a node before a specified node") 107 | print("4.Insert a node at a given position") 108 | print("5.Delete a node") 109 | print("6.Reverse the list") 110 | print("7.Quit") 111 | 112 | option = int(input("Enter your choice : " )) 113 | 114 | if option == 1: 115 | list.display_list() 116 | elif option == 2: 117 | data = int(input("Enter the element to be inserted : ")) 118 | list.insert_at_end(data) 119 | elif option == 3: 120 | data = int(input("Enter the element to be inserted : ")) 121 | x = int(input("Enter the element before which to insert : ")) 122 | list.insert_before(data,x) 123 | elif option == 4: 124 | data = int(input("Enter the element to be inserted : ")) 125 | k = int(input("Enter the position at which to insert : ")) 126 | list.insert_at_position(data,k) 127 | elif option == 5: 128 | data = int(input("Enter the element to be deleted : ")) 129 | list.delete_node(data) 130 | elif option == 6: 131 | list.reverse_list() 132 | elif option == 7: 133 | break 134 | else: 135 | print("Wrong option") 136 | print() 137 | -------------------------------------------------------------------------------- /linked-list/merging-linked-lists.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node: 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.link = None 9 | 10 | 11 | class SingleLinkedList: 12 | 13 | def __init__(self): 14 | self.start = None 15 | 16 | def display_list(self): 17 | if self.start is None: 18 | print("List is empty") 19 | return 20 | else: 21 | print("List is : ") 22 | p = self.start 23 | while p is not None: 24 | print(p.info , " ", end='') 25 | p = p.link 26 | print() 27 | 28 | def insert_at_end(self, data): 29 | temp = Node(data) 30 | if self.start is None: 31 | self.start = temp 32 | return 33 | 34 | p = self.start 35 | while p.link is not None: 36 | p = p.link 37 | p.link = temp 38 | 39 | 40 | def create_list(self): 41 | n = int(input("Enter the number of nodes : ")) 42 | if n == 0: 43 | return 44 | for i in range(n): 45 | data = int(input("Enter the element to be inserted : ")) 46 | self.insert_at_end(data) 47 | 48 | 49 | def bubble_sort_exdata(self): 50 | end = None 51 | while end != self.start.link: 52 | 53 | p = self.start 54 | while p.link != end: 55 | q = p.link 56 | if p.info > q.info: 57 | p.info,q.info = q.info,p.info 58 | p = p.link 59 | end = p 60 | 61 | def merge1(self, list2): 62 | merge_list = SingleLinkedList() 63 | merge_list.start = self._merge1(self.start, list2.start) 64 | return merge_list 65 | 66 | def _merge1(self, p1, p2): 67 | if p1.info <= p2.info: 68 | startM = Node(p1.info) 69 | p1 = p1.link 70 | else: 71 | startM = Node(p2.info) 72 | p2 = p2.link; 73 | 74 | pM = startM 75 | 76 | while p1 is not None and p2 is not None: 77 | if p1.info <= p2.info : 78 | pM.link = Node(p1.info) 79 | p1 = p1.link 80 | else: 81 | pM.link = Node(p2.info); 82 | p2 = p2.link; 83 | pM = pM.link; 84 | 85 | 86 | #If second list has finished and elements left in first list 87 | while p1 is not None: 88 | pM.link = Node(p1.info) 89 | p1 = p1.link 90 | pM = pM.link 91 | 92 | #If first list has finished and elements left in second list 93 | while p2 is not None: 94 | pM.link = Node(p2.info) 95 | p2 = p2.link 96 | pM = pM.link 97 | 98 | return startM 99 | 100 | 101 | def merge2(self,list2): 102 | merge_list = SingleLinkedList() 103 | merge_list.start = self._merge2(self.start, list2.start) 104 | return merge_list 105 | 106 | def _merge2(self, p1, p2): 107 | 108 | if p1.info <= p2.info: 109 | startM = p1 110 | p1 = p1.link 111 | else: 112 | startM = p2 113 | p2 = p2.link 114 | 115 | pM = startM 116 | 117 | while p1 is not None and p2 is not None: 118 | if p1.info <= p2.info : 119 | pM.link = p1 120 | pM = pM.link 121 | p1 = p1.link 122 | else: 123 | pM.link = p2 124 | pM = pM.link 125 | p2 = p2.link 126 | 127 | if p1 is None: 128 | pM.link = p2 129 | else: 130 | pM.link = p1 131 | 132 | return startM 133 | 134 | 135 | ######################################################################################### 136 | 137 | list1 = SingleLinkedList() 138 | list2 = SingleLinkedList() 139 | 140 | list1.create_list() 141 | list2.create_list() 142 | 143 | list1.bubble_sort_exdata() 144 | list2.bubble_sort_exdata() 145 | 146 | print("First List - "); list1.display_list() 147 | print("Second List - "); list2.display_list() 148 | 149 | list3 = list1.merge1(list2) 150 | print("Merged List - "); list3.display_list() 151 | 152 | print("First List - "); list1.display_list() 153 | print("Second List - "); list2.display_list() 154 | 155 | list3 = list1.merge2(list2) 156 | print("Merged List - "); list3.display_list() 157 | print("First List - "); list1.display_list() 158 | print("Second List - "); list2.display_list() 159 | 160 | -------------------------------------------------------------------------------- /linked-list/single-linked-list-1.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node: 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.link = None 9 | 10 | 11 | class SingleLinkedList: 12 | 13 | def __init__(self): 14 | self.start = None 15 | 16 | 17 | def display_list(self): 18 | if self.start is None: 19 | print("List is empty") 20 | return 21 | else: 22 | print("List is : ") 23 | p = self.start 24 | while p is not None: 25 | print(p.info , " ", end='') 26 | p = p.link 27 | print() 28 | 29 | 30 | def count_nodes(self): 31 | p = self.start 32 | n = 0 33 | while p is not None: 34 | n+=1 35 | p = p.link 36 | print("Number of nodes in the list = " ,n) 37 | 38 | def search(self,x): 39 | position = 1 40 | p = self.start 41 | while p is not None: 42 | if p.info == x: 43 | print(x , " is at position ", position) 44 | return True 45 | position+=1 46 | p = p.link 47 | else: 48 | print(x," not found in list") 49 | return False 50 | 51 | def insert_in_beginning(self, data): 52 | temp = Node(data) 53 | temp.link = self.start 54 | self.start = temp 55 | 56 | 57 | def insert_at_end(self, data): 58 | temp = Node(data) 59 | if self.start is None: 60 | self.start = temp 61 | return 62 | 63 | p = self.start 64 | while p.link is not None: 65 | p = p.link 66 | p.link = temp 67 | 68 | 69 | def create_list(self): 70 | n = int(input("Enter the number of nodes : ")) 71 | if n == 0: 72 | return 73 | for i in range(n): 74 | data = int(input("Enter the element to be inserted : ")) 75 | self.insert_at_end(data) 76 | 77 | def insert_after(self,data,x): 78 | p = self.start 79 | while p is not None: 80 | if p.info == x: 81 | break 82 | p = p.link 83 | 84 | if p is None: 85 | print(x, "not present in the list") 86 | else: 87 | temp = Node(data) 88 | temp.link = p.link 89 | p.link = temp 90 | 91 | 92 | 93 | def insert_before(self, data, x): 94 | # If list is empty 95 | if self.start is None: 96 | print("List is empty") 97 | return 98 | 99 | # x is in first node,new node is to be inserted before first node 100 | if x == self.start.info: 101 | temp = Node(data) 102 | temp.link = self.start 103 | self.start = temp 104 | return 105 | 106 | # Find reference to predecessor of node containing x 107 | p = self.start 108 | while p.link is not None: 109 | if p.link.info == x: 110 | break 111 | p = p.link 112 | 113 | 114 | if p.link is None: 115 | print(x , " not present in the list") 116 | else: 117 | temp = Node(data) 118 | temp.link = p.link 119 | p.link = temp 120 | 121 | 122 | 123 | def insert_at_position(self, data, k): 124 | if k == 1: 125 | temp = Node(data) 126 | temp.link = self.start 127 | self.start = temp 128 | return 129 | 130 | p = self.start 131 | i = 1 132 | while i q.info: 211 | p.info,q.info = q.info,p.info 212 | p = p.link 213 | end = p 214 | 215 | 216 | def bubble_sort_exlinks(self): 217 | end = None 218 | while end != self.start.link: 219 | r = p = self.start 220 | while p.link != end: 221 | q = p.link 222 | if p.info > q.info : 223 | p.link = q.link 224 | q.link = p 225 | if p!=self.start: 226 | r.link = q 227 | else: 228 | self.start = q 229 | p,q = q,p 230 | r = p 231 | p = p.link 232 | end = p 233 | 234 | 235 | def has_cycle(self): 236 | 237 | if self.find_cycle() is None: 238 | return False 239 | else: 240 | return True 241 | 242 | 243 | def find_cycle(self): 244 | 245 | if self.start is None or self.start.link is None: 246 | return None 247 | 248 | slowR = self.start 249 | fastR = self.start 250 | 251 | while fastR is not None and fastR.link is not None: 252 | slowR = slowR.link 253 | fastR = fastR.link.link 254 | if slowR == fastR: 255 | return slowR 256 | return None 257 | 258 | 259 | def remove_cycle(self): 260 | 261 | c = self.find_cycle() 262 | if c is None: 263 | return 264 | print("Node at which the cycle was detected is " , c.info) 265 | 266 | p = c 267 | q = c 268 | lenCycle = 0 269 | 270 | while True: 271 | lenCycle+=1 272 | q = q.link 273 | if p == q: 274 | break; 275 | 276 | print("Length of cycle is :", lenCycle) 277 | 278 | lenRemList = 0 279 | p = self.start 280 | while p != q: 281 | lenRemList+=1 282 | p = p.link 283 | q = q.link 284 | 285 | print("Number of nodes not included in the cycle are : ", lenRemList) 286 | 287 | length_list = lenCycle + lenRemList 288 | print("Length of the list is : " , length_list) 289 | 290 | p = self.start 291 | for i in range(length_list-1): 292 | p = p.link 293 | p.link = None 294 | 295 | 296 | def insert_cycle(self,x): 297 | if self.start is None: 298 | return 299 | p = self.start 300 | px = None 301 | prev = None 302 | 303 | while p is not None: 304 | if p.info == x: 305 | px = p 306 | prev = p 307 | p = p.link 308 | 309 | if px is not None: 310 | prev.link = px 311 | else: 312 | print(x , " not present in list") 313 | 314 | 315 | def merge2(self,list2): 316 | merge_list = SingleLinkedList() 317 | merge_list.start = self._merge2(self.start, list2.start) 318 | return merge_list 319 | 320 | def _merge2(self, p1, p2): 321 | 322 | if p1.info <= p2.info: 323 | startM = p1 324 | p1 = p1.link 325 | else: 326 | startM = p2 327 | p2 = p2.link 328 | 329 | pM = startM 330 | 331 | while p1 is not None and p2 is not None: 332 | if p1.info <= p2.info : 333 | pM.link = p1 334 | pM = pM.link 335 | p1 = p1.link 336 | else: 337 | pM.link = p2 338 | pM = pM.link 339 | p2 = p2.link 340 | 341 | if p1 is None: 342 | pM.link = p2 343 | else: 344 | pM.link = p1 345 | 346 | return startM 347 | 348 | 349 | 350 | def merge_sort(self): 351 | self.start = self._merge_sort_rec(self.start) 352 | 353 | 354 | def _merge_sort_rec(self,list_start): 355 | 356 | #if list empty or has one element 357 | if list_start is None or list_start.link is None: 358 | return list_start 359 | 360 | #if more than one element 361 | start1 = list_start 362 | start2 = self._divide_list(list_start) 363 | start1 = self._merge_sort_rec(start1) 364 | start2 = self._merge_sort_rec(start2) 365 | startM = self._merge2(start1, start2) 366 | return startM 367 | 368 | 369 | def _divide_list(self, p): 370 | q = p.link.link 371 | while q is not None and q.link is not None: 372 | p = p.link 373 | q = q.link.link 374 | start2 = p.link 375 | p.link = None 376 | return start2 377 | 378 | 379 | 380 | ######################################################################################## 381 | 382 | list = SingleLinkedList() 383 | 384 | list.create_list() 385 | 386 | while True: 387 | print("1.Display list") 388 | print("2.Count the number of nodes") 389 | print("3.Search for an element") 390 | print("4.Insert in empty list/Insert in beginning of the list") 391 | print("5.Insert a node at the end of the list") 392 | print("6.Insert a node after a specified node") 393 | print("7.Insert a node before a specified node") 394 | print("8.Insert a node at a given position") 395 | print("9.Delete first node") 396 | print("10.Delete last node") 397 | print("11.Delete any node") 398 | print("12.Reverse the list") 399 | print("13.Bubble sort by exchanging data") 400 | print("14.Bubble sort by exchanging links") 401 | print("15.MergeSort") 402 | print("16.Insert Cycle") 403 | print("17.Detect Cycle") 404 | print("18.Remove cycle") 405 | print("19.Quit") 406 | 407 | option = int(input("Enter your choice : " )) 408 | 409 | if option == 1: 410 | list.display_list() 411 | elif option == 2: 412 | list.count_nodes() 413 | elif option == 3: 414 | data = int(input("Enter the element to be searched : ")) 415 | list.search(data) 416 | elif option == 4: 417 | data = int(input("Enter the element to be inserted : ")) 418 | list.insert_in_beginning(data) 419 | elif option == 5: 420 | data = int(input("Enter the element to be inserted : ")) 421 | list.insert_at_end(data) 422 | elif option == 6: 423 | data = int(input("Enter the element to be inserted : ")) 424 | x = int(input("Enter the element after which to insert : ")) 425 | list.insert_after(data,x) 426 | elif option == 7: 427 | data = int(input("Enter the element to be inserted : ")) 428 | x = int(input("Enter the element before which to insert : ")) 429 | list.insert_before(data,x) 430 | elif option == 8: 431 | data = int(input("Enter the element to be inserted : ")) 432 | k = int(input("Enter the position at which to insert : ")) 433 | list.insert_at_position(data,k) 434 | elif option == 9: 435 | list.delete_first_node() 436 | elif option == 10: 437 | list.delete_last_node() 438 | elif option == 11: 439 | data = int(input("Enter the element to be deleted : ")) 440 | list.delete_node(data) 441 | elif option == 12: 442 | list.reverse_list() 443 | elif option == 13: 444 | list.bubble_sort_exdata() 445 | elif option == 14: 446 | list.bubble_sort_exlinks() 447 | elif option == 15: 448 | list.merge_sort() 449 | elif option == 16: 450 | data = int(input("Enter the element at which the cycle has to be inserted : ")) 451 | list.insert_cycle(data) 452 | elif option == 17: 453 | if list.has_cycle(): 454 | print("List has a cycle") 455 | else: 456 | print("List does not have a cycle") 457 | elif option == 18: 458 | list.remove_cycle() 459 | elif option == 19: 460 | break 461 | else: 462 | print("Wrong option") 463 | print() 464 | 465 | 466 | -------------------------------------------------------------------------------- /linked-list/single-linked-list.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node: 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.link = None 9 | 10 | class SingleLinkedList: 11 | 12 | def __init__(self): 13 | self.start = None 14 | 15 | def display_list(self): 16 | if self.start is None: 17 | print("List is empty") 18 | return 19 | else: 20 | print("List is : ") 21 | p = self.start 22 | while p is not None: 23 | print(p.info , " ", end='') 24 | p = p.link 25 | print() 26 | 27 | def count_nodes(self): 28 | p = self.start 29 | n = 0 30 | while p is not None: 31 | n+=1 32 | p = p.link 33 | print("Number of nodes in the list = " ,n) 34 | 35 | def search(self,x): 36 | position = 1 37 | p = self.start 38 | while p is not None: 39 | if p.info == x: 40 | print(x , " is at position ", position) 41 | return True 42 | position+=1 43 | p = p.link 44 | else: 45 | print(x," not found in list") 46 | return False 47 | 48 | def insert_in_beginning(self, data): 49 | temp = Node(data) 50 | temp.link = self.start 51 | self.start = temp 52 | 53 | def insert_at_end(self, data): 54 | temp = Node(data) 55 | if self.start is None: 56 | self.start = temp 57 | return 58 | 59 | p = self.start 60 | while p.link is not None: 61 | p = p.link 62 | p.link = temp 63 | 64 | def create_list(self): 65 | n = int(input("Enter the number of nodes : ")) 66 | if n == 0: 67 | return 68 | for i in range(n): 69 | data = int(input("Enter the element to be inserted : ")) 70 | self.insert_at_end(data) 71 | 72 | def insert_after(self,data,x): 73 | pass 74 | 75 | def insert_before(self, data, x): 76 | pass 77 | 78 | def insert_at_position(self, data, k): 79 | pass 80 | 81 | def delete_node(self,x): 82 | pass 83 | 84 | def delete_first_node(self): 85 | pass 86 | 87 | def delete_last_node(self): 88 | pass 89 | 90 | def reverse_list(self): 91 | pass 92 | 93 | def bubble_sort_exdata(self): 94 | pass 95 | 96 | def bubble_sort_exlinks(self): 97 | pass 98 | 99 | def has_cycle(self): 100 | pass 101 | 102 | def find_cycle(self): 103 | pass 104 | 105 | def remove_cycle(self): 106 | pass 107 | 108 | def insert_cycle(self,x): 109 | pass 110 | 111 | def merge2(self,list2): 112 | pass 113 | 114 | def _merge2(self, p1, p2): 115 | pass 116 | 117 | def merge_sort(self): 118 | pass 119 | 120 | def _merge_sort_rec(self,listStart): 121 | pass 122 | 123 | def _divide_list(self, p): 124 | pass 125 | 126 | 127 | ####################################################################################### 128 | 129 | list = SingleLinkedList() 130 | 131 | list.create_list() 132 | 133 | while True: 134 | print("1.Display list") 135 | print("2.Count the number of nodes") 136 | print("3.Search for an element") 137 | print("4.Insert in empty list/Insert in beginning of the list") 138 | print("5.Insert a node at the end of the list") 139 | print("6.Insert a node after a specified node") 140 | print("7.Insert a node before a specified node") 141 | print("8.Insert a node at a given position") 142 | print("9.Delete first node") 143 | print("10.Delete last node") 144 | print("11.Delete any node") 145 | print("12.Reverse the list") 146 | print("13.Bubble sort by exchanging data") 147 | print("14.Bubble sort by exchanging links") 148 | print("15.MergeSort") 149 | print("16.Insert Cycle") 150 | print("17.Detect Cycle") 151 | print("18.Remove cycle") 152 | print("19.Quit") 153 | 154 | option = int(input("Enter your choice : " )) 155 | 156 | if option == 1: 157 | list.display_list() 158 | elif option == 2: 159 | list.count_nodes() 160 | elif option == 3: 161 | data = int(input("Enter the element to be searched : ")) 162 | list.search(data) 163 | elif option == 4: 164 | data = int(input("Enter the element to be inserted : ")) 165 | list.insert_in_beginning(data) 166 | elif option == 5: 167 | data = int(input("Enter the element to be inserted : ")) 168 | list.insert_at_end(data) 169 | elif option == 6: 170 | data = int(input("Enter the element to be inserted : ")) 171 | x = int(input("Enter the element after which to insert : ")) 172 | list.insert_after(data,x) 173 | elif option == 7: 174 | data = int(input("Enter the element to be inserted : ")) 175 | x = int(input("Enter the element before which to insert : ")) 176 | list.insert_before(data,x) 177 | elif option == 8: 178 | data = int(input("Enter the element to be inserted : ")) 179 | k = int(input("Enter the position at which to insert : ")) 180 | list.insert_at_position(data,k) 181 | elif option == 9: 182 | list.delete_first_node() 183 | elif option == 10: 184 | list.delete_last_node() 185 | elif option == 11: 186 | data = int(input("Enter the element to be deleted : ")) 187 | list.delete_node(data) 188 | elif option == 12: 189 | list.reverse_list() 190 | elif option == 13: 191 | list.bubble_sort_exdata() 192 | elif option == 14: 193 | list.bubble_sort_exlinks() 194 | elif option == 15: 195 | list.merge_sort() 196 | elif option == 16: 197 | data = int(input("Enter the element at which the cycle has to be inserted : ")) 198 | list.insert_cycle(data) 199 | elif option == 17: 200 | if list.has_cycle(): 201 | print("List has a cycle") 202 | else: 203 | print("List does not have a cycle") 204 | elif option == 18: 205 | list.remove_cycle() 206 | elif option == 19: 207 | break 208 | else: 209 | print("Wrong option") 210 | print() 211 | 212 | 213 | -------------------------------------------------------------------------------- /linked-list/sorted-linked-list.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node(object): 5 | 6 | def __init__(self,value): 7 | self.info = value 8 | self.link = None 9 | 10 | class SortedLinkedList(object): 11 | 12 | def __init__(self): 13 | self.start = None 14 | 15 | def insert_in_order(self, data): 16 | temp = Node(data) 17 | 18 | # List empty or node to be inserted before first node 19 | if self.start == None or data < self.start.info: 20 | temp.link = self.start 21 | self.start = temp 22 | return 23 | 24 | p = self.start 25 | while p.link is not None and p.link.info <= data: 26 | p = p.link 27 | temp.link = p.link 28 | p.link = temp 29 | 30 | 31 | def create_list(self): 32 | n = int(input("Enter the number of nodes : ")) 33 | if n == 0: 34 | return 35 | 36 | for i in range(n): 37 | data = int(input("Enter the element to be inserted : ")) 38 | self.insert_in_order(data) 39 | 40 | def search(self, x): 41 | if self.start is None: 42 | print("List is empty") 43 | return 44 | p = self.start 45 | position = 1 46 | while p is not None and p.info <= x: 47 | if p.info == x: 48 | break 49 | position+=1 50 | p = p.link 51 | 52 | if p is None or p.info != x: 53 | print(x," not found in list") 54 | else: 55 | print(x , " is at position " , position) 56 | 57 | def display_list(self): 58 | if self.start is None: 59 | print("List is empty") 60 | return 61 | print("List is : ") 62 | p = self.start 63 | while p is not None: 64 | print(p.info , " ", end='') 65 | p = p.link 66 | print() 67 | 68 | ######################################################################### 69 | 70 | list = SortedLinkedList() 71 | list.create_list() 72 | 73 | while True: 74 | print("1.Display list") 75 | print("2.Insert") 76 | print("3.Search for an element") 77 | print("4.Quit") 78 | 79 | option = int(input("Enter your choice : " )) 80 | 81 | if option == 1: 82 | list.display_list() 83 | elif option == 2: 84 | data = int(input("Enter the element to be inserted : ")) 85 | list.insert_in_order(data) 86 | elif option == 3: 87 | data = int(input("Enter the element to be searched : ")) 88 | list.search(data) 89 | elif option == 4: 90 | break 91 | else: 92 | print("Wrong option") 93 | print() 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /searching/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Python 2 | 3 | This [“Data Structures and Algorithms in Python”]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 17 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 18 | [![exception-handling-in-python](https://user-images.githubusercontent.com/98641125/153196201-83c1a210-9c4f-4ba5-a56f-6aa4d4c551b2.png)]( https://www.udemy.com/course/exception-handling-in-python/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /searching/binary-search-recursive.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def binary_search(a, n, searchValue): 5 | return _search(a, 0, n-1, searchValue) 6 | 7 | def _search(a, first, last, searchValue): 8 | if (first > last): 9 | return -1 10 | mid = (first + last) // 2 11 | if searchValue > a[mid]: #Search in right half 12 | return _search(a, mid+1, last, searchValue) 13 | elif searchValue < a[mid]: #Search in left half 14 | return _search(a, first, mid-1, searchValue) 15 | else: 16 | return mid 17 | 18 | ############################################################ 19 | 20 | n = int(input("Enter the number of elements : ")) 21 | a = [None]*n 22 | print("Enter the elements in sorted order - ") 23 | for i in range(n): 24 | a[i] = int(input("Enter element : ")) 25 | 26 | searchValue = int(input("Enter the search value : ")) 27 | index = binary_search(a, n, searchValue) 28 | 29 | if index == -1: 30 | print("Value " , searchValue , " not present in the array") 31 | else: 32 | print("Value " , searchValue , " present at index " , index) 33 | -------------------------------------------------------------------------------- /searching/binary-search.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def binary_search(a, n, searchValue): 5 | first = 0 6 | last = n-1 7 | 8 | while first <= last : 9 | mid = (first + last)//2 10 | if searchValue < a[mid]: 11 | last = mid-1 # Search in left half 12 | elif searchValue > a[mid]: 13 | first = mid+1 # Search in right half 14 | else: 15 | return mid # searchValue present at index mid 16 | return -1 17 | 18 | #################################################### 19 | 20 | n = int(input("Enter the number of elements : ")) 21 | a = [None]*n 22 | 23 | print("Enter the elements in sorted order - ") 24 | for i in range(n): 25 | a[i] = int(input("Enter element : ")) 26 | 27 | searchValue = int(input("Enter the search value : ")) 28 | 29 | index = binary_search(a, n, searchValue) 30 | 31 | if index == -1: 32 | print("Value " , searchValue , " not present in the array") 33 | else: 34 | print("Value " , searchValue , " present at index " , index) 35 | 36 | 37 | -------------------------------------------------------------------------------- /searching/linear-search-sentinel.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def Search(a, n, searchValue): 5 | a[n] = searchValue 6 | i = 0 7 | while searchValue != a[i]: 8 | i+=1 9 | if i < n: 10 | return i 11 | else: 12 | return -1 13 | 14 | 15 | n = int(input("Enter the number of elements : ")) 16 | a = [None]*(n+1) 17 | print("Enter the elements - ") 18 | for i in range(n): 19 | a[i] = int(input("Enter element : ")) 20 | 21 | searchValue = int(input("Enter the search value : ")) 22 | index = Search(a, n, searchValue) 23 | 24 | if index == -1: 25 | print("Value ", searchValue ," not present in the list") 26 | else: 27 | print("Value ", searchValue ," present at index " , index) 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /searching/linear-search-sorted.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def Search(a, n, searchValue): 5 | for i in range(n): 6 | if a[i] >= searchValue: 7 | break 8 | 9 | if a[i] == searchValue: 10 | return i 11 | else: 12 | return -1 13 | 14 | ########################################################### 15 | 16 | n = int(input("Enter the number of elements : ")) 17 | a = [None]*n 18 | print("Enter the elements in sorted order - ") 19 | for i in range(n): 20 | a[i] = int(input("Enter element : ")) 21 | 22 | searchValue = int(input("Enter the search value : ")) 23 | index = Search(a, n, searchValue) 24 | 25 | if index == -1: 26 | print("Value " , searchValue , " not present in the list") 27 | else: 28 | print("Value " , searchValue , " present at index " , index) 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /searching/linear-search.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def Search(a, n, searchValue): 5 | for i in range(n): 6 | if a[i] == searchValue: 7 | return i 8 | return -1 9 | 10 | 11 | ####################################################### 12 | 13 | n = int(input("Enter the number of elements : ")) 14 | a = [None]*n 15 | print("Enter the elements - ") 16 | for i in range(n): 17 | a[i] = int(input("Enter element : ")) 18 | 19 | searchValue = int(input("Enter the search value : ")) 20 | index = Search(a, n, searchValue) 21 | 22 | if index == -1: 23 | print("Value " , searchValue , " not present in the list") 24 | else: 25 | print("Value " , searchValue , " present at index " , index) 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /sorting/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Python 2 | 3 | This [“Data Structures and Algorithms in Python”]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 17 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 18 | [![exception-handling-in-python](https://user-images.githubusercontent.com/98641125/153196201-83c1a210-9c4f-4ba5-a56f-6aa4d4c551b2.png)]( https://www.udemy.com/course/exception-handling-in-python/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /sorting/binary-tree-sort.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node: 5 | def __init__(self,value): 6 | self.info = value 7 | self.lchild = None 8 | self.rchild = None 9 | 10 | class BinarySearchTree: 11 | def __init__(self): 12 | self.root = None 13 | 14 | def is_empty(self): 15 | return self.root == None 16 | 17 | def insert(self,x): 18 | self.root = self._insert(self.root, x) 19 | 20 | def _insert(self,p, x): 21 | if p is None: 22 | p = Node(x) 23 | elif x < p.info : 24 | p.lchild = self._insert(p.lchild, x) 25 | else: 26 | p.rchild = self._insert(p.rchild, x) 27 | return p 28 | 29 | def inorder(self): 30 | self._inorder(self.root) 31 | print() 32 | 33 | def _inorder(self, p): 34 | if p is None : 35 | return 36 | self._inorder(p.lchild) 37 | print(p.info, " ") 38 | self._inorder(p.rchild) 39 | 40 | 41 | #################################### 42 | n = int(input("Enter the number of elements to be sorted : ")) 43 | 44 | tree = BinarySearchTree() 45 | for i in range(n): 46 | x = int(input("Enter element : ")) 47 | tree.insert(x) 48 | 49 | tree.inorder() 50 | 51 | #################################### 52 | 53 | -------------------------------------------------------------------------------- /sorting/bubble-sort-improvement.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def bubble_sort(a): 5 | for x in range(len(a)-1,0,-1): 6 | swaps=0 7 | for j in range(x): 8 | if a[j]>a[j+1]: 9 | a[j],a[j+1] = a[j+1],a[j] 10 | swaps+=1 11 | if swaps == 0: 12 | break 13 | 14 | #################################### 15 | 16 | list1 = [6,3,1,5,9,8] 17 | bubble_sort(list1) 18 | print(list1) 19 | 20 | list2 = [2,3,5,39,11,8,9,166,45,23] 21 | bubble_sort(list2) 22 | print(list2) 23 | 24 | list3 = [1,2,3,4,5,6,7,8,9,10] 25 | bubble_sort(list3) 26 | print(list3) 27 | 28 | list4 = [10,9,8,7,6,5,4,3,2,1] 29 | bubble_sort(list4) 30 | print(list4) 31 | 32 | list5 = [4] 33 | bubble_sort(list5) 34 | print(list5) 35 | 36 | ##################################### 37 | -------------------------------------------------------------------------------- /sorting/bubble-sort.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def bubble_sort(a): 5 | for x in range(len(a)-1,0,-1): 6 | for j in range(x): 7 | if a[j]>a[j+1]: 8 | a[j],a[j+1] = a[j+1],a[j] 9 | 10 | 11 | #################################### 12 | 13 | list1 = [6,3,1,5,9,8] 14 | bubble_sort(list1) 15 | print(list1) 16 | 17 | list2 = [2,3,5,39,11,8,9,166,45,23] 18 | bubble_sort(list2) 19 | print(list2) 20 | 21 | list3 = [1,2,3,4,5,6,7,8,9,10] 22 | bubble_sort(list3) 23 | print(list3) 24 | 25 | list4 = [10,9,8,7,6,5,4,3,2,1] 26 | bubble_sort(list4) 27 | print(list4) 28 | 29 | list5 = [4] 30 | bubble_sort(list5) 31 | print(list5) 32 | 33 | ##################################### 34 | -------------------------------------------------------------------------------- /sorting/heap-sort.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def heap_sort(a, n): 5 | build_heap_bottom_up(a, n) 6 | 7 | while n > 1: 8 | maxValue = a[1] 9 | a[1] = a[n] 10 | a[n] = maxValue 11 | n=n-1 12 | retsore_down(1, a, n) 13 | 14 | def build_heap_bottom_up(a, n): 15 | i=n//2 16 | while i>=1: 17 | retsore_down(i, a, n) 18 | i=i-1 19 | 20 | def retsore_down(i, a, n): 21 | k = a[i] 22 | lchild = 2 * i 23 | rchild = lchild + 1 24 | 25 | while rchild <= n: 26 | if k >= a[lchild] and k >= a[rchild]: 27 | a[i] = k 28 | return 29 | elif a[lchild] > a[rchild]: 30 | a[i] = a[lchild] 31 | i = lchild 32 | else: 33 | a[i] = a[rchild] 34 | i = rchild 35 | 36 | lchild = 2 * i 37 | rchild = lchild + 1 38 | 39 | 40 | # If number of nodes is even 41 | if lchild == n and k < a[lchild]: 42 | a[i] = a[lchild] 43 | i = lchild 44 | a[i] = k 45 | 46 | ################################################### 47 | 48 | n = int(input("Enter the number of elements : ")) 49 | a = [None]*(n+1) 50 | for i in range(1,n+1): 51 | a[i] = int(input("Enter element : ")) 52 | 53 | heap_sort(a,n) 54 | 55 | for i in range(1,n+1): 56 | print(a[i], " ",end='') 57 | print() 58 | 59 | ################################################### 60 | 61 | -------------------------------------------------------------------------------- /sorting/insertion-sort.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def insertion_sort(a): 5 | for i in range(1,len(a)): 6 | temp = a[i] 7 | j = i-1 8 | while j >= 0 and a[j] > temp: 9 | a[j+1] = a[j] 10 | j=j-1 11 | a[j+1] = temp 12 | 13 | #################################### 14 | 15 | list1 = [6,3,1,5,9,8] 16 | insertion_sort(list1) 17 | print(list1) 18 | 19 | list2 = [2,3,5,39,11,8,9,166,45,23] 20 | insertion_sort(list2) 21 | print(list2) 22 | 23 | list3 = [1,2,3,4,5,6,7,8,9,10] 24 | insertion_sort(list3) 25 | print(list3) 26 | 27 | list4 = [10,9,8,7,6,5,4,3,2,1] 28 | insertion_sort(list4) 29 | print(list4) 30 | 31 | list5 = [4] 32 | insertion_sort(list5) 33 | print(list5) 34 | 35 | ##################################### 36 | -------------------------------------------------------------------------------- /sorting/merge-sort-iterative.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def merge_sort(a): 5 | n=len(a) 6 | temp = [None]*n 7 | size = 1 8 | while size <= n-1: 9 | sort_pass(a, temp, size, n) 10 | size = size * 2 11 | 12 | def sort_pass(a,temp,size,n): 13 | low1 = 0 14 | while low1+size <= n-1: 15 | up1 = low1 + size - 1 16 | low2 = low1 + size 17 | up2 = low2 + size - 1 18 | 19 | if up2 >= n: # if length of last sublist is less than size 20 | up2 = n-1 21 | 22 | merge(a, temp, low1, up1, low2, up2) 23 | 24 | low1 = up2 + 1 # Take next two sublists for merging 25 | 26 | for i in range(low1,n): 27 | temp[i] = a[i] # If any sublist is left alone 28 | 29 | copy(a, temp, n) 30 | 31 | # a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] */ 32 | def merge(a, temp, low1, up1, low2, up2): 33 | i = low1 34 | j = low2 35 | k = low1 36 | 37 | while i <= up1 and j <= up2: 38 | if a[i] <= a[j]: 39 | temp[k] = a[i] 40 | i+=1 41 | else: 42 | temp[k] = a[j] 43 | j+=1 44 | k+=1 45 | 46 | while i <= up1: 47 | temp[k] = a[i] 48 | i+=1 49 | k+=1 50 | 51 | while j <= up2: 52 | temp[k] = a[j] 53 | j+=1 54 | k+=1 55 | 56 | # copies temp[low]....temp[up] to a[low]...a[up] 57 | def copy(a, temp, n): 58 | for i in range(n): 59 | a[i] = temp[i] 60 | 61 | 62 | ############################################################# 63 | 64 | list1 = [6,3,1,5,9,8] 65 | merge_sort(list1) 66 | print(list1) 67 | 68 | list2 = [2,3,5,39,11,8,9,166,45,23] 69 | merge_sort(list2) 70 | print(list2) 71 | 72 | list3 = [1,2,3,4,5,6,7,8,9,10] 73 | merge_sort(list3) 74 | print(list3) 75 | 76 | list4 = [10,9,8,7,6,5,4,3,2,1] 77 | merge_sort(list4) 78 | print(list4) 79 | 80 | list5 = [4] 81 | merge_sort(list5) 82 | print(list5) 83 | 84 | ################################################################# 85 | -------------------------------------------------------------------------------- /sorting/merge-sort-recursive.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def merge_sort(a): 5 | n=len(a) 6 | temp = [None]*n 7 | sort(a,temp,0,n-1) 8 | 9 | 10 | def sort(a,temp,low,up): 11 | if low == up: # only one element 12 | return 13 | 14 | mid = (low + up)//2 15 | 16 | sort(a, temp, low, mid) # Sort a[low]....a[mid] 17 | sort(a, temp, mid+1, up) # Sort a[mid+1]....a[up] 18 | 19 | # Merge a[low]...a[mid] and a[mid+1]....a[up] to temp[low]...temp[up] 20 | merge(a, temp, low, mid, mid+1, up) 21 | 22 | # Copy temp[low]...temp[up] to a[low]...a[up] 23 | copy(a, temp, low, up) 24 | 25 | 26 | # a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] 27 | def merge(a, temp, low1, up1, low2, up2): 28 | i = low1 29 | j = low2 30 | k = low1 31 | 32 | while i <= up1 and j <= up2: 33 | if a[i] <= a[j]: 34 | temp[k] = a[i] 35 | i+=1 36 | else: 37 | temp[k] = a[j] 38 | j+=1 39 | k+=1 40 | 41 | while i <= up1: 42 | temp[k] = a[i] 43 | i+=1 44 | k+=1 45 | 46 | while j <= up2: 47 | temp[k] = a[j] 48 | j+=1 49 | k+=1 50 | 51 | # copies temp[low]....temp[up] to a[low]...a[up] 52 | def copy(a, temp, low, up): 53 | for i in range(low,up+1): 54 | a[i] = temp[i] 55 | 56 | 57 | ############################################################# 58 | 59 | list1 = [6,3,1,5,9,8] 60 | merge_sort(list1) 61 | print(list1) 62 | 63 | list2 = [2,3,5,39,11,8,9,166,45,23] 64 | merge_sort(list2) 65 | print(list2) 66 | 67 | list3 = [1,2,3,4,5,6,7,8,9,10] 68 | merge_sort(list3) 69 | print(list3) 70 | 71 | list4 = [10,9,8,7,6,5,4,3,2,1] 72 | merge_sort(list4) 73 | print(list4) 74 | 75 | list5 = [4] 76 | merge_sort(list5) 77 | print(list5) 78 | 79 | ################################################################# 80 | 81 | 82 | -------------------------------------------------------------------------------- /sorting/merging.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def merge(a1,a2,temp): 5 | i = 0 6 | j = 0 7 | k = 0 8 | n1=len(a1) 9 | n2=len(a2) 10 | 11 | while i <= n1-1 and j <= n2-1: 12 | if a1[i] < a2[j]: 13 | temp[k] = a1[i] 14 | i+=1 15 | else: 16 | temp[k] = a2[j] 17 | j+=1 18 | k+=1 19 | 20 | # copy remaining elements of a1, list a2 finished 21 | while i <= n1-1: 22 | temp[k] = a1[i] 23 | i+=1 24 | k+=1 25 | 26 | # copy remaining elements of a2, list a1 finished 27 | while j <= n2-1: 28 | temp[k] = a2[j] 29 | j+=1 30 | k+=1 31 | 32 | 33 | ##################################################################### 34 | n1 = int(input("Enter the number of elements in list a1 : ")) 35 | print("Enter elements in sorted order : " ); 36 | a1 = [None]*n1 37 | for i in range(n1): 38 | a1[i] = int(input("Enter element : ")) 39 | 40 | n2 = int(input("Enter the number of elements in list a2 : ")) 41 | print("Enter elements in sorted order : " ); 42 | a2 = [None]*n2 43 | for i in range(n2): 44 | a2[i] = int(input("Enter element : ")) 45 | 46 | temp=[None]*(n1+n2) 47 | 48 | merge(a1,a2,temp) 49 | 50 | print("Merged list temp is : "); 51 | print(temp) 52 | 53 | ##################################################################### 54 | -------------------------------------------------------------------------------- /sorting/merging1.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | #a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]..temp[up2] 5 | def merge(a,temp,low1,up1,low2,up2): 6 | i = low1 7 | j = low2 8 | k = low1 9 | 10 | while i <= up1 and j <= up2: 11 | if a[i] <= a[j]: 12 | temp[k] = a[i] 13 | i+=1 14 | else: 15 | temp[k] = a[j] 16 | j+=1 17 | k+=1 18 | 19 | while i <= up1: 20 | temp[k] = a[i] 21 | i+=1 22 | k+=1 23 | 24 | while j <= up2: 25 | temp[k] = a[j] 26 | j+=1 27 | k+=1 28 | 29 | ########################################### 30 | 31 | a = [1,2,4,6, 3,5,6,7,13,19] 32 | 33 | temp=[None]*len(a) 34 | 35 | merge(a,temp,0,3,4,9) 36 | 37 | print("Merged list temp is : "); 38 | print(temp) 39 | 40 | 41 | ########################################## 42 | -------------------------------------------------------------------------------- /sorting/quick-sort.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def quick_sort(a): 5 | sort(a,0,len(a)-1) 6 | 7 | def sort(a, low, up): 8 | if low >= up: # zero or one element in sublist 9 | return 10 | p = partition(a,low,up) 11 | sort(a,low,p-1) # Sort left sublist 12 | sort(a,p+1,up) # Sort right sublist 13 | 14 | def partition(a, low, up): 15 | pivot = a[low] 16 | i = low+1 # moves from left to right 17 | j = up # moves from right to left 18 | 19 | while i <= j: 20 | while a[i] < pivot and i < up: 21 | i+=1 22 | while a[j] > pivot: 23 | j-=1 24 | 25 | if i < j: # swap a[i] and a[j] 26 | temp = a[i] 27 | a[i] = a[j] 28 | a[j] = temp 29 | i+=1 30 | j-=1 31 | else: # found proper place for pivot 32 | break 33 | 34 | # Proper place for pivot is j 35 | a[low] = a[j] 36 | a[j] = pivot 37 | 38 | return j 39 | 40 | #################################### 41 | 42 | list1 = [6,3,1,5,9,8] 43 | quick_sort(list1) 44 | print(list1) 45 | 46 | list2 = [2,3,5,39,11,8,9,166,45,23] 47 | quick_sort(list2) 48 | print(list2) 49 | 50 | list3 = [1,2,3,4,5,6,7,8,9,10] 51 | quick_sort(list3) 52 | print(list3) 53 | 54 | list4 = [10,9,8,7,6,5,4,3,2,1] 55 | quick_sort(list4) 56 | print(list4) 57 | 58 | list5 = [4] 59 | quick_sort(list5) 60 | print(list5) 61 | 62 | ##################################### 63 | 64 | -------------------------------------------------------------------------------- /sorting/radix-sort.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class Node: 5 | def __init__(self,value): 6 | self.info = value 7 | self.link = None 8 | 9 | def radix_sort(start): 10 | 11 | rear = [None] * 10 12 | front = [None] * 10 13 | 14 | leastSigPos = 1 15 | mostSigPos = DigitsInLargest(start) 16 | 17 | for k in range(leastSigPos, mostSigPos+1): 18 | #Making all the queues empty at the beginning of each pass 19 | for i in range(10): 20 | rear[i] = None 21 | front[i] = None 22 | 23 | p = start 24 | while p is not None: 25 | #Find kth digit from right in the number 26 | dig = Digit(p.info, k) 27 | 28 | #Insert the node in Queue(dig) 29 | if front[dig] is None: 30 | front[dig] = p 31 | else: 32 | rear[dig].link = p 33 | rear[dig] = p 34 | 35 | p=p.link 36 | 37 | #Join all queues to form new linked list 38 | i = 0 39 | while front[i] is None: #Finding first non empty queue 40 | i=i+1 41 | 42 | start = front[i] 43 | while i <= 8: 44 | if rear[i+1] is not None: #if (i+1)th queue is not empty 45 | rear[i].link = front[i+1] #join end of ith queue to start of (i+1)th queue 46 | else: 47 | rear[i+1] = rear[i] #continue with rear[i] 48 | i=i+1 49 | rear[9].link = None 50 | return start 51 | 52 | #Returns number of digits in the largest element of the list 53 | def DigitsInLargest(start): 54 | #Find largest element 55 | large = 0 56 | p = start 57 | while p is not None: 58 | if p.info > large: 59 | large = p.info 60 | p = p.link 61 | 62 | #Find number of digits in largest element 63 | ndigits = 0 64 | while large != 0: 65 | ndigits = ndigits+1 66 | large//=10 67 | return ndigits 68 | 69 | #Returns kth digit from right in n 70 | def Digit(n, k): 71 | d = 0 72 | for i in range(1,k+1): 73 | d = n%10 74 | n//=10 75 | return d 76 | 77 | 78 | ################################################# 79 | start = None 80 | 81 | n = int(input("Enter the number of elements : ")) 82 | 83 | for i in range(n): #Inserting elements in linked list 84 | data = int(input("Enter element : ")) 85 | 86 | temp = Node(data) 87 | if start is None: 88 | start = temp 89 | else: 90 | p = start 91 | while p.link is not None: 92 | p = p.link 93 | p.link = temp 94 | 95 | start = radix_sort(start) 96 | 97 | print("Sorted list is : ") 98 | p = start 99 | while p is not None : 100 | print(p.info , " ") 101 | p = p.link 102 | print 103 | 104 | ################################################## 105 | 106 | -------------------------------------------------------------------------------- /sorting/selection-sort.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def selection_sort(a): 5 | for i in range(len(a)-1): 6 | minIndex = i 7 | for j in range(i+1,len(a)): 8 | if a[j] < a[minIndex]: 9 | minIndex = j 10 | if i!=minIndex: 11 | a[i],a[minIndex] = a[minIndex],a[i] 12 | 13 | 14 | #################################### 15 | 16 | list1 = [6,3,1,5,9,8] 17 | selection_sort(list1) 18 | print(list1) 19 | 20 | list2 = [2,3,5,39,11,8,9,166,45,23] 21 | selection_sort(list2) 22 | print(list2) 23 | 24 | list3 = [1,2,3,4,5,6,7,8,9,10] 25 | selection_sort(list3) 26 | print(list3) 27 | 28 | list4 = [10,9,8,7,6,5,4,3,2,1] 29 | selection_sort(list4) 30 | print(list4) 31 | 32 | list5 = [4] 33 | selection_sort(list5) 34 | print(list5) 35 | 36 | ##################################### 37 | 38 | -------------------------------------------------------------------------------- /sorting/shell-sort.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def shell_sort(a): 5 | h = int(input("Enter maximum increment(odd value) : ")) 6 | while h>=1: 7 | for i in range(h,len(a)): 8 | temp = a[i] 9 | j=i-h 10 | while j >= 0 and a[j] > temp: 11 | a[j+h] = a[j] 12 | j=j-h 13 | a[j+h] = temp 14 | h=h-2 15 | 16 | ############################################################## 17 | 18 | list1 = [65,73,21,90,6,239,3,35,1,15,5,9,8,23,12,5,7,2,19,34,] 19 | shell_sort(list1) 20 | print(list1) 21 | 22 | -------------------------------------------------------------------------------- /stack-and-queue/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Python 2 | 3 | This [“Data Structures and Algorithms in Python”]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 17 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 18 | [![exception-handling-in-python](https://user-images.githubusercontent.com/98641125/153196201-83c1a210-9c4f-4ba5-a56f-6aa4d4c551b2.png)]( https://www.udemy.com/course/exception-handling-in-python/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /stack-and-queue/circular-queue.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Queue: 8 | 9 | def __init__(self,default_size=10): 10 | self.items = [None] * default_size 11 | self.front = 0 12 | self.count = 0 13 | 14 | def is_empty(self): 15 | return self.count == 0 16 | 17 | def size(self): 18 | return self.count 19 | 20 | def enqueue(self, item): 21 | if self.count == len(self.items): 22 | self.resize( 2*len(self.items) ) 23 | 24 | i = (self.front + self.count) % len(self.items) 25 | self.items[i] = item 26 | self.count+=1 27 | 28 | def dequeue(self): 29 | if self.is_empty(): 30 | raise EmptyQueueError("Queue is empty") 31 | 32 | x = self.items[self.front] 33 | self.items[self.front] = None 34 | self.front = (self.front + 1) % len(self.items) 35 | self.count-=1 36 | return x 37 | 38 | def peek(self): 39 | if self.is_empty(): 40 | raise EmptyQueueError("Queue is empty") 41 | return self.items[self.front] 42 | 43 | def display(self): 44 | print(self.items) 45 | 46 | def resize(self,newsize): 47 | old_list = self.items 48 | self.items = [None]*newsize 49 | i = self.front 50 | for j in range(self.count): 51 | self.items[j] = old_list[i] 52 | i = (1+i)%len(old_list) 53 | self.front = 0 54 | 55 | ########################################################### 56 | 57 | if __name__ == "__main__": 58 | qu = Queue(6) 59 | 60 | while True: 61 | print("1.Enqueue") 62 | print("2.Dequeue") 63 | print("3.Peek") 64 | print("4.Size") 65 | print("5.Display") 66 | print("6.Quit") 67 | 68 | choice = int(input("Enter your choice : ")) 69 | 70 | if choice == 1: 71 | x=int(input("Enter the element : ")) 72 | qu.enqueue(x) 73 | elif choice == 2: 74 | x=qu.dequeue() 75 | print("Element deleted from the queue is : " , x) 76 | elif choice == 3: 77 | print("Element at the front end is " , qu.peek()) 78 | elif choice == 4: 79 | print("Size of queue ", qu.size()) 80 | elif choice == 5: 81 | qu.display() 82 | elif choice == 6: 83 | break 84 | else: 85 | print("Wrong choice") 86 | print() 87 | -------------------------------------------------------------------------------- /stack-and-queue/deque-circular.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Deque: 8 | 9 | def __init__(self,default_size=10): 10 | self.items = [None] * default_size 11 | self.front = 0 12 | self.count = 0 13 | 14 | def is_empty(self): 15 | return self.count == 0 16 | 17 | def size(self): 18 | return self.count 19 | 20 | def insert_front(self, item): 21 | if self.count == len(self.items): 22 | self.resize( 2*len(self.items) ) 23 | 24 | self.front = (self.front - 1) % len(self.items) 25 | self.items[self.front] = item 26 | self.count+=1 27 | 28 | def insert_rear(self, item): 29 | if self.count == len(self.items): 30 | self.resize( 2*len(self.items) ) 31 | 32 | i = (self.front + self.count) % len(self.items) 33 | self.items[i] = item 34 | self.count+=1 35 | 36 | def delete_front(self): 37 | if self.is_empty(): 38 | raise EmptyQueueError("Queue is empty") 39 | 40 | x = self.items[self.front] 41 | self.items[self.front] = None 42 | self.front = (self.front + 1) % len(self.items) 43 | self.count-=1 44 | return x 45 | 46 | def delete_rear(self): 47 | if self.is_empty(): 48 | raise EmptyQueueError("Queue is empty") 49 | rear = (self.front + self.count - 1) % len(self.items) 50 | x = self.items[rear] 51 | self.items[rear] = None 52 | self.count-=1 53 | return x 54 | 55 | def first(self): 56 | if self.is_empty(): 57 | raise EmptyQueueError("Queue is empty") 58 | return self.items[self.front] 59 | 60 | def last(self): 61 | if self.is_empty(): 62 | raise EmptyQueueError("Queue is empty") 63 | rear = (self.front + self.count - 1) % len(self.items) 64 | return self.items[rear] 65 | 66 | def display(self): 67 | print(self.items) 68 | 69 | def resize(self,newsize): 70 | old_list = self.items 71 | self.items = [None]*newsize 72 | i = self.front 73 | for j in range(self.count): 74 | self.items[j] = old_list[i] 75 | i = (1+i)%len(old_list) 76 | self.front = 0 77 | 78 | #################################################################### 79 | 80 | if __name__ == "__main__": 81 | qu = Deque(6) 82 | 83 | while True: 84 | print("1.Insert at the front end") 85 | print("2.Insert at the rear end") 86 | print("3.Delete from front end") 87 | print("4.Delete from rear end") 88 | print("5.Display first element") 89 | print("6.Display last element") 90 | print("7.Display") 91 | print("8.Size") 92 | print("9.Quit") 93 | 94 | choice = int(input("Enter your choice : ")) 95 | 96 | if choice == 1: 97 | x=int(input("Enter the element : ")) 98 | qu.insert_front(x) 99 | elif choice== 2: 100 | x=int(input("Enter the element : ")) 101 | qu.insert_rear(x) 102 | elif choice == 3: 103 | x = qu.delete_front() 104 | print("Element deleted from front end is ", x) 105 | elif choice == 4: 106 | x = qu.delete_rear() 107 | print("Element deleted from rear end is ", x) 108 | elif choice == 5: 109 | print("First element is ", qu.first()) 110 | elif choice == 6: 111 | print("Last element is ", qu.last()) 112 | elif choice == 7: 113 | qu.display() 114 | elif choice == 8: 115 | print("Size of queue " , qu.size()) 116 | elif choice == 9: 117 | break 118 | else: 119 | print("Wrong choice") 120 | print() 121 | 122 | -------------------------------------------------------------------------------- /stack-and-queue/deque-linked.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Node: 8 | def __init__(self,value): 9 | self.info = value 10 | self.prev = None 11 | self.next = None 12 | 13 | class Deque: 14 | 15 | def __init__(self): 16 | self.front = None 17 | self.rear = None 18 | 19 | def is_empty(self): 20 | return self.front == None 21 | 22 | def size(self): 23 | count = 0 24 | p = self.front 25 | while p is not None: 26 | count+=1 27 | p = p.next 28 | return count 29 | 30 | def insert_front(self, item): 31 | temp = Node(item) 32 | if self.is_empty(): 33 | self.front = self.rear = temp 34 | else: 35 | temp.next = self.front 36 | self.front.prev = temp 37 | self.front = temp 38 | 39 | def insert_rear(self, item): 40 | temp = Node(item) 41 | if self.is_empty(): 42 | self.front = self.rear = temp 43 | else: 44 | self.rear.next = temp 45 | temp.prev = self.rear 46 | 47 | self.rear = temp 48 | 49 | def delete_front(self): 50 | if self.is_empty(): 51 | raise EmptyQueueError("Queue is empty") 52 | 53 | x = self.front.info 54 | if self.front.next is None: # list has only one node 55 | self.front = self.rear = None 56 | else: 57 | self.front = self.front.next 58 | self.front.prev = None 59 | return x 60 | 61 | def delete_rear(self): 62 | if self.is_empty(): 63 | raise EmptyQueueError("Queue is empty") 64 | 65 | x = self.rear.info 66 | if self.front.next is None: # list has only one node 67 | self.front = self.rear = None 68 | else: 69 | self.rear = self.rear.prev 70 | self.rear.next = None 71 | return x 72 | 73 | def first(self): 74 | if self.is_empty(): 75 | raise EmptyQueueError("Queue is empty") 76 | 77 | return self.front.info 78 | 79 | def last(self): 80 | if self.is_empty(): 81 | raise EmptyQueueError("Queue is empty") 82 | 83 | return self.rear.info 84 | 85 | def display(self): 86 | if self.front is None: 87 | print("List is empty") 88 | return 89 | 90 | print("List is : ") 91 | p = self.front 92 | while p is not None: 93 | print(p.info, " ", end='') 94 | p = p.next 95 | print() 96 | 97 | 98 | #################################################################### 99 | 100 | if __name__ == "__main__": 101 | qu = Deque() 102 | 103 | while True: 104 | print("1.Insert at the front end") 105 | print("2.Insert at the rear end") 106 | print("3.Delete from front end") 107 | print("4.Delete from rear end") 108 | print("5.Display first element") 109 | print("6.Display last element") 110 | print("7.Display") 111 | print("8.Size") 112 | print("9.Quit") 113 | 114 | choice = int(input("Enter your choice : ")) 115 | 116 | if choice == 1: 117 | x=int(input("Enter the element : ")) 118 | qu.insert_front(x) 119 | elif choice== 2: 120 | x=int(input("Enter the element : ")) 121 | qu.insert_rear(x) 122 | elif choice == 3: 123 | x = qu.delete_front() 124 | print("Element deleted from front end is ", x) 125 | elif choice == 4: 126 | x = qu.delete_rear() 127 | print("Element deleted from rear end is ", x) 128 | elif choice == 5: 129 | print("First element is ", qu.first()) 130 | elif choice == 6: 131 | print("Last element is ", qu.last()) 132 | elif choice == 7: 133 | qu.display() 134 | elif choice == 8: 135 | print("Size of queue " , qu.size()) 136 | elif choice == 9: 137 | break 138 | else: 139 | print("Wrong choice") 140 | print() 141 | -------------------------------------------------------------------------------- /stack-and-queue/deque.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Deque: 8 | def __init__(self): 9 | self.items = [] 10 | 11 | def is_empty(self): 12 | return self.items == [] 13 | 14 | def size(self): 15 | return len(self.items) 16 | 17 | def insert_front(self, item): 18 | self.items.insert(0,item) 19 | 20 | def insert_rear(self, item): 21 | self.items.append(item) 22 | 23 | def delete_front(self): 24 | if self.is_empty(): 25 | raise EmptyQueueError("Queue is Empty") 26 | return self.items.pop(0) 27 | 28 | def delete_rear(self): 29 | if self.is_empty(): 30 | raise EmptyQueueError("Queue is Empty") 31 | return self.items.pop() 32 | 33 | def first(self): 34 | if self.is_empty(): 35 | raise EmptyQueueError("Queue is Empty") 36 | return self.items[0] 37 | 38 | def last(self): 39 | if self.is_empty(): 40 | raise EmptyQueueError("Queue is Empty") 41 | return self.items[-1] 42 | 43 | def display(self): 44 | print(self.items) 45 | 46 | #################################################################### 47 | if __name__ == "__main__": 48 | qu = Deque() 49 | 50 | while True: 51 | print("1.Insert at the front end") 52 | print("2.Insert at the rear end") 53 | print("3.Delete from front end") 54 | print("4.Delete from rear end") 55 | print("5.Display first element") 56 | print("6.Display last element") 57 | print("7.Display") 58 | print("8.Size") 59 | print("9.Quit") 60 | 61 | choice = int(input("Enter your choice : ")) 62 | 63 | if choice == 1: 64 | x=int(input("Enter the element : ")) 65 | qu.insert_front(x) 66 | elif choice== 2: 67 | x=int(input("Enter the element : ")) 68 | qu.insert_rear(x) 69 | elif choice == 3: 70 | x = qu.delete_front() 71 | print("Element deleted from front end is ", x) 72 | elif choice == 4: 73 | x = qu.delete_rear() 74 | print("Element deleted from rear end is ", x) 75 | elif choice == 5: 76 | print("First element is ", qu.first()) 77 | elif choice == 6: 78 | print("Last element is ", qu.last()) 79 | elif choice == 7: 80 | qu.display() 81 | elif choice == 8: 82 | print("Size of queue " , qu.size()) 83 | elif choice == 9: 84 | break 85 | else: 86 | print("Wrong choice") 87 | print() 88 | 89 | -------------------------------------------------------------------------------- /stack-and-queue/infix-to-postfix.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | from StackArray import Stack 5 | 6 | def infix_to_postfix(infix): 7 | postfix = "" 8 | 9 | st = Stack() 10 | 11 | for symbol in infix: 12 | if symbol == ' ' or symbol == '\t': #ignore blanks and tabs 13 | continue 14 | 15 | if symbol == '(': 16 | st.push(symbol) 17 | elif symbol == ')': 18 | next = st.pop() 19 | while next != '(': 20 | postfix = postfix + next 21 | next = st.pop() 22 | elif symbol in "+-*/%^": 23 | while not st.is_empty() and precedence(st.peek()) >= precedence(symbol): 24 | postfix = postfix + st.pop() 25 | st.push(symbol) 26 | else: #operand 27 | postfix = postfix + symbol 28 | 29 | while not st.is_empty(): 30 | postfix = postfix + st.pop() 31 | return postfix 32 | 33 | def precedence(symbol): 34 | if symbol == '(': 35 | return 0 36 | elif symbol in '+-': 37 | return 1 38 | elif symbol in '*/%': 39 | return 2 40 | elif symbol == '^': 41 | return 3 42 | else: 43 | return 0 44 | 45 | def evaluate_postfix(postfix): 46 | st = Stack() 47 | 48 | for symbol in postfix: 49 | if symbol.isdigit(): 50 | st.push( int(symbol) ) 51 | else: 52 | x = st.pop() 53 | y = st.pop() 54 | 55 | if symbol == '+': 56 | st.push(y + x) 57 | elif symbol == '-': 58 | st.push(y - x) 59 | elif symbol == '*': 60 | st.push(y * x) 61 | elif symbol == '/': 62 | st.push (y / x) 63 | elif symbol == '%': 64 | st.push(y % x) 65 | elif symbol == '^': 66 | st.push(y ** x) 67 | 68 | return st.pop() 69 | 70 | #################################################### 71 | 72 | while True: 73 | print("Enter infix expression (q to quit) : ", end = '') 74 | 75 | expression = input() 76 | if expression == 'q': 77 | break 78 | 79 | postfix = infix_to_postfix(expression) 80 | print("Postfix expression is : " ,postfix) 81 | print("Value of expression : " , evaluate_postfix(postfix) ) 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /stack-and-queue/parentheses.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | from StackArray import Stack 5 | 6 | def is_valid(expr): 7 | st = Stack() 8 | 9 | for ch in expr: 10 | if ch in '({[': 11 | st.push(ch) 12 | if ch in ')}]': 13 | if st.is_empty(): 14 | print("Right parentheses are more than left parentheses") 15 | return False 16 | else: 17 | char = st.pop() 18 | if not match_parentheses(char,ch): 19 | print("Mismatched parentheses are ", char , " and " , ch) 20 | return False 21 | 22 | if st.is_empty(): 23 | print("Balanced Parentheses") 24 | return True 25 | else: 26 | print("Left parentheses are more than right parentheses") 27 | return False 28 | 29 | def match_parentheses(left_par, right_par): 30 | if left_par == '[' and right_par == ']': 31 | return True 32 | if left_par == '{' and right_par == '}': 33 | return True 34 | if left_par == '(' and right_par == ')': 35 | return True 36 | return False 37 | 38 | 39 | ############################################################################# 40 | 41 | while True: 42 | print("Enter an expression with parentheses (q to quit) : ", end = ' ') 43 | expression = input() 44 | 45 | if expression == "q": 46 | break 47 | 48 | if is_valid(expression): 49 | print("Valid expression") 50 | else: 51 | print("Invalid expression") 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /stack-and-queue/priority-queue.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Node: 8 | 9 | def __init__(self,value,pr): 10 | self.info = value 11 | self.priority = pr 12 | self.link = None 13 | 14 | class PriorityQueue: 15 | 16 | def __init__(self): 17 | self.front = None 18 | 19 | def enqueue(self, data, data_priority): 20 | 21 | temp = Node(data, data_priority) 22 | 23 | #If queue is empty or element to be added has priority more than first element 24 | if self.is_empty() or data_priority < self.front.priority: 25 | temp.link = self.front 26 | self.front = temp 27 | else: 28 | p = self.front 29 | while p.link != None and p.link.priority <= data_priority: 30 | p = p.link 31 | temp.link = p.link 32 | p.link = temp 33 | 34 | def dequeue(self): 35 | if self.is_empty(): 36 | raise EmptyQueueError("Queue is empty") 37 | x = self.front.info 38 | self.front = self.front.link 39 | return x 40 | 41 | def is_empty(self): 42 | return self.front == None 43 | 44 | def display(self): 45 | if self.is_empty(): 46 | print("Queue is empty") 47 | return 48 | 49 | print("Queue is : ") 50 | p = self.front 51 | while p is not None: 52 | print(p.info , " ", p.priority) 53 | p = p.link 54 | print() 55 | 56 | def size(self): 57 | n = 0 58 | p = self.front 59 | while p is not None: 60 | n+=1 61 | p = p.link 62 | return n 63 | 64 | 65 | 66 | ######################################################################################### 67 | 68 | if __name__ == "__main__": 69 | qu = PriorityQueue() 70 | 71 | while True: 72 | print("1.Enqueue") 73 | print("2.Dequeue") 74 | print("3.Display all queue elements") 75 | print("4.Display size of the queue") 76 | print("5.Quit") 77 | 78 | choice = int(input("Enter your choice : ")) 79 | 80 | if choice == 1: 81 | x = int(input("Enter the element : ")) 82 | pr = int(input("Enter its priority : ")) 83 | qu.enqueue(x,pr) 84 | elif choice == 2: 85 | x=qu.dequeue() 86 | print("Element is : " , x) 87 | elif choice == 3: 88 | qu.display() 89 | elif choice == 4: 90 | print("Size of queue " , qu.size()) 91 | elif choice == 5: 92 | break; 93 | else: 94 | print("Wrong choice") 95 | print() 96 | -------------------------------------------------------------------------------- /stack-and-queue/queue-array-1.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Queue: 8 | 9 | def __init__(self): 10 | self.items = [] 11 | self.front = 0 12 | 13 | def is_empty(self): 14 | return self.front == len(self.items) 15 | 16 | def size(self): 17 | return len(self.items)-self.front 18 | 19 | def enqueue(self, item): 20 | self.items.append(item) 21 | 22 | def dequeue(self): 23 | if self.is_empty(): 24 | raise EmptyQueueError("Queue is Empty") 25 | 26 | x = self.items[self.front] 27 | self.items[self.front] = None 28 | self.front = self.front + 1 29 | return x 30 | 31 | def peek(self): 32 | if self.is_empty(): 33 | raise EmptyQueueError("Queue is Empty") 34 | 35 | return self.items[self.front] 36 | 37 | def display(self): 38 | print(self.items) 39 | 40 | ########################################################### 41 | 42 | if __name__ == "__main__": 43 | qu = Queue() 44 | 45 | while True: 46 | print("1.Enqueue") 47 | print("2.Dequeue") 48 | print("3.Peek") 49 | print("4.Size") 50 | print("5.Display") 51 | print("6.Quit") 52 | 53 | choice = int(input("Enter your choice : ")) 54 | 55 | if choice == 1: 56 | x=int(input("Enter the element : ")) 57 | qu.enqueue(x) 58 | elif choice == 2: 59 | x=qu.dequeue() 60 | print("Element deleted from the queue is : " , x) 61 | elif choice == 3: 62 | print("Element at the front end is " , qu.peek()) 63 | elif choice == 4: 64 | print("Size of queue ", qu.size()) 65 | elif choice == 5: 66 | qu.display() 67 | elif choice == 6: 68 | break; 69 | else: 70 | print("Wrong choice") 71 | print() 72 | 73 | -------------------------------------------------------------------------------- /stack-and-queue/queue-array.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Queue: 8 | 9 | def __init__(self): 10 | self.items = [] 11 | 12 | def is_empty(self): 13 | return self.items == [] 14 | 15 | def size(self): 16 | return len(self.items) 17 | 18 | def enqueue(self, item): 19 | self.items.append(item) 20 | 21 | def dequeue(self): 22 | if self.is_empty(): 23 | raise EmptyQueueError("Queue is Empty") 24 | return self.items.pop(0) 25 | 26 | def peek(self): 27 | if self.is_empty(): 28 | raise EmptyQueueError("Queue is Empty") 29 | return self.items[0] 30 | 31 | def display(self): 32 | print(self.items) 33 | 34 | ########################################################### 35 | 36 | if __name__ == "__main__": 37 | qu = Queue() 38 | 39 | while True: 40 | print("1.Enqueue") 41 | print("2.Dequeue") 42 | print("3.Peek") 43 | print("4.Size") 44 | print("5.Display") 45 | print("6.Quit") 46 | 47 | choice = int(input("Enter your choice : ")) 48 | 49 | if choice == 1: 50 | x=int(input("Enter the element : ")) 51 | qu.enqueue(x) 52 | elif choice == 2: 53 | x=qu.dequeue() 54 | print("Element deleted from the queue is : " , x) 55 | elif choice == 3: 56 | print("Element at the front end is " , qu.peek()) 57 | elif choice == 4: 58 | print("Size of queue ", qu.size()) 59 | elif choice == 5: 60 | qu.display() 61 | elif choice == 6: 62 | break; 63 | else: 64 | print("Wrong choice") 65 | print() 66 | 67 | 68 | -------------------------------------------------------------------------------- /stack-and-queue/queue-circular-linked.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Node: 8 | 9 | def __init__(self,value): 10 | self.info = value 11 | self.link = None 12 | 13 | class Queue: 14 | 15 | def __init__(self): 16 | self.rear = None 17 | 18 | def is_empty(self): 19 | return self.rear == None 20 | 21 | def size(self): 22 | if self.is_empty(): 23 | return 0 24 | n = 0 25 | p = self.rear.link 26 | while True: 27 | n+=1 28 | p = p.link 29 | if p == self.rear.link: 30 | break 31 | return n 32 | 33 | def enqueue(self, data): 34 | temp = Node(data) 35 | 36 | if self.is_empty(): 37 | self.rear = temp 38 | self.rear.link = self.rear 39 | else: 40 | temp.link = self.rear.link 41 | self.rear.link = temp 42 | self.rear = temp 43 | 44 | def dequeue(self): 45 | if self.is_empty(): 46 | raise EmptyQueueError("Queue is Empty") 47 | 48 | if self.rear.link == self.rear: #List has only one node 49 | temp = self.rear 50 | self.rear = None 51 | else: 52 | temp = self.rear.link 53 | self.rear.link = self.rear.link.link 54 | return temp.info 55 | 56 | def peek(self): 57 | if self.is_empty(): 58 | raise EmptyQueueError("Queue is Empty") 59 | return self.rear.link.info 60 | 61 | def display(self): 62 | if self.is_empty(): 63 | print("List is empty") 64 | return 65 | 66 | p = self.rear.link 67 | while True: 68 | print(p.info , " ",end='') 69 | p = p.link 70 | if p == self.rear.link: 71 | break 72 | print() 73 | 74 | ######################################################################## 75 | 76 | if __name__ == "__main__": 77 | qu = Queue() 78 | 79 | while True: 80 | print("1.Enqueue") 81 | print("2.Dequeue") 82 | print("3.Peek") 83 | print("4.Size") 84 | print("5.Display") 85 | print("6.Quit") 86 | 87 | choice = int(input("Enter your choice : ")) 88 | 89 | if choice == 1: 90 | x=int(input("Enter the element : ")) 91 | qu.enqueue(x) 92 | elif choice == 2: 93 | x=qu.dequeue() 94 | print("Element deleted from the queue is : " , x) 95 | elif choice == 3: 96 | print("Element at the front end is " , qu.peek()) 97 | elif choice == 4: 98 | print("Size of queue ", qu.size()) 99 | elif choice == 5: 100 | qu.display() 101 | elif choice == 6: 102 | break; 103 | else: 104 | print("Wrong choice") 105 | print() 106 | 107 | -------------------------------------------------------------------------------- /stack-and-queue/queue-linked.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyQueueError(Exception): 5 | pass 6 | 7 | class Node: 8 | 9 | def __init__(self,value): 10 | self.info = value 11 | self.link = None 12 | 13 | class Queue: 14 | 15 | def __init__(self): 16 | self.front = None 17 | self.rear = None 18 | self.size_queue = 0 19 | 20 | def is_empty(self): 21 | return self.front == None 22 | 23 | def size(self): 24 | return self.size_queue 25 | 26 | def enqueue(self, data): 27 | temp = Node(data) 28 | if self.front == None: 29 | self.front = temp 30 | else: 31 | self.rear.link = temp 32 | self.rear = temp 33 | self.size_queue+=1 34 | 35 | def dequeue(self): 36 | if self.is_empty(): 37 | raise EmptyQueueError("Queue is empty") 38 | x = self.front.info 39 | self.front = self.front.link 40 | self.size_queue-=1 41 | return x 42 | 43 | def peek(self): 44 | if self.is_empty(): 45 | raise EmptyQueueError("Queue is empty") 46 | return self.front.info 47 | 48 | def display(self): 49 | if self.is_empty(): 50 | print("Queue is empty") 51 | return 52 | print("Queue is : ") 53 | p = self.front 54 | while p is not None: 55 | print(p.info, " ", end='') 56 | p = p.link 57 | print() 58 | 59 | 60 | ######################################################################################### 61 | 62 | if __name__ == "__main__": 63 | qu = Queue() 64 | 65 | while True: 66 | print("1.Enqueue") 67 | print("2.Dequeue") 68 | print("3.Peek") 69 | print("4.Size") 70 | print("5.Display") 71 | print("6.Quit") 72 | 73 | choice = int(input("Enter your choice : ")) 74 | 75 | if choice == 1: 76 | x=int(input("Enter the element : ")) 77 | qu.enqueue(x) 78 | elif choice == 2: 79 | x=qu.dequeue() 80 | print("Element deleted from the queue is : " , x) 81 | elif choice == 3: 82 | print("Element at the front end is " , qu.peek()) 83 | elif choice == 4: 84 | print("Size of queue ", qu.size()) 85 | elif choice == 5: 86 | qu.display() 87 | elif choice == 6: 88 | break; 89 | else: 90 | print("Wrong choice") 91 | print() 92 | 93 | -------------------------------------------------------------------------------- /stack-and-queue/stack-array-1.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class StackEmptyError(Exception): 5 | pass 6 | 7 | class StackFullError(Exception): 8 | pass 9 | 10 | class Stack: 11 | 12 | def __init__(self,max_size=10): 13 | self.items = [None] * max_size 14 | self.count = 0 15 | 16 | def size(self): 17 | return self.count 18 | 19 | def is_empty(self): 20 | return self.count == 0 21 | 22 | def is_full(self): 23 | return self.count == len(self.items) 24 | 25 | def push(self,x): 26 | if self.is_full(): 27 | raise StackFullError("Stack is full, can't push") 28 | 29 | self.items[self.count] = x 30 | self.count+=1 31 | 32 | def pop(self): 33 | if self.is_empty(): 34 | raise StackEmptyError("Stack is empty, can't pop") 35 | 36 | x = self.items[self.count-1] 37 | self.items[self.count-1] = None 38 | self.count-=1 39 | return x 40 | 41 | def peek(self): 42 | if self.is_empty(): 43 | raise StackEmptyError("Stack is empty, can't peek") 44 | 45 | return self.items[self.count-1] 46 | 47 | def display(self): 48 | print(self.items) 49 | 50 | ########################################################## 51 | 52 | if __name__ == "__main__": 53 | 54 | st = Stack(8) 55 | 56 | while True: 57 | print("1.Push") 58 | print("2.Pop") 59 | print("3.Peek") 60 | print("4.Size") 61 | print("4.Display") 62 | print("6.Quit") 63 | 64 | choice = int(input("Enter your choice : ")) 65 | 66 | if choice == 1: 67 | x=int(input("Enter the element to be pushed : ")) 68 | st.push(x) 69 | elif choice == 2: 70 | x=st.pop() 71 | print("Popped element is : " , x) 72 | elif choice == 3: 73 | print("Element at the top is : " , st.peek()) 74 | elif choice == 4: 75 | print("Size of stack " , st.size()) 76 | elif choice == 5: 77 | st.display() 78 | elif choice == 6: 79 | break 80 | else: 81 | print("Wrong choice") 82 | print() 83 | -------------------------------------------------------------------------------- /stack-and-queue/stack-array.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyStackError(Exception): 5 | pass 6 | 7 | class Stack: 8 | 9 | def __init__(self): 10 | self.items = [] 11 | 12 | def is_empty(self): 13 | return self.items == [] 14 | 15 | def size(self): 16 | return len(self.items) 17 | 18 | def push(self, item): 19 | self.items.append(item) 20 | 21 | def pop(self): 22 | if self.is_empty(): 23 | raise EmptyStackError("Stack is empty") 24 | return self.items.pop() 25 | 26 | def peek(self): 27 | if self.is_empty(): 28 | raise EmptyStackError("Stack is empty") 29 | return self.items[len(self.items)-1] 30 | 31 | def display(self): 32 | print(self.items) 33 | 34 | ########################################################### 35 | 36 | if __name__ == "__main__": 37 | st = Stack() 38 | 39 | while True: 40 | print("1.Push") 41 | print("2.Pop") 42 | print("3.Peek") 43 | print("4.Size") 44 | print("5.Display") 45 | print("6.Quit") 46 | 47 | choice = int(input("Enter your choice : ")) 48 | 49 | if choice == 1: 50 | x=int(input("Enter the element to be pushed : ")) 51 | st.push(x) 52 | elif choice == 2: 53 | x=st.pop() 54 | print("Popped element is : " , x) 55 | elif choice == 3: 56 | print("Element at the top is : " , st.peek()) 57 | elif choice == 4: 58 | print("Size of stack " , st.size()) 59 | elif choice == 5: 60 | st.display() 61 | elif choice == 6: 62 | break; 63 | else: 64 | print("Wrong choice") 65 | print() 66 | 67 | -------------------------------------------------------------------------------- /stack-and-queue/stack-linked.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class EmptyStackError(Exception): 5 | pass 6 | 7 | class Node: 8 | 9 | def __init__(self,value): 10 | self.info = value 11 | self.link = None 12 | 13 | class Stack: 14 | 15 | def __init__(self): 16 | self.top = None 17 | 18 | def is_empty(self): 19 | return self.top == None 20 | 21 | def size(self): 22 | 23 | if self.is_empty(): 24 | retun 0 25 | 26 | count=0 27 | p = self.top 28 | while p is not None: 29 | count+=1 30 | p = p.link 31 | return count 32 | 33 | def push(self, data): 34 | temp = Node(data) 35 | temp.link = self.top 36 | self.top = temp 37 | 38 | def peek(self): 39 | if self.is_empty(): 40 | raise EmptyStackError("Stack is empty") 41 | return self.top.info 42 | 43 | def pop(self): 44 | if self.is_empty(): 45 | raise EmptyStackError("Stack is empty") 46 | popped = self.top.info 47 | self.top = self.top.link 48 | return popped 49 | 50 | def display(self): 51 | if self.is_empty(): 52 | print("Stack is empty") 53 | return 54 | 55 | print("Stack is : ") 56 | p = self.top 57 | while p is not None: 58 | print(p.info , " ") 59 | p = p.link 60 | 61 | ##################################################################################### 62 | 63 | if __name__ == "__main__": 64 | st = Stack() 65 | 66 | while True: 67 | print("1.Push") 68 | print("2.Pop") 69 | print("3.Peek") 70 | print("5.Size") 71 | print("4.Display") 72 | print("6.Quit") 73 | 74 | choice = int(input("Enter your choice : ")) 75 | 76 | if choice == 1: 77 | x=int(input("Enter the element to be pushed : ")) 78 | st.push(x) 79 | elif choice == 2: 80 | x=st.pop() 81 | print("Popped element is : " , x) 82 | elif choice == 3: 83 | print("Element at the top is : " , st.peek()) 84 | elif choice == 4: 85 | print("Size of stack " , st.size()) 86 | elif choice == 5: 87 | st.display() 88 | elif choice == 6: 89 | break; 90 | else: 91 | print("Wrong choice") 92 | print() 93 | 94 | 95 | -------------------------------------------------------------------------------- /tree/README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms in Python 2 | 3 | This [“Data Structures and Algorithms in Python”]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT ) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes over 100 Quiz questions 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-python-2](https://user-images.githubusercontent.com/98641125/153196106-0eb1a386-c36b-4f14-8675-9d865438f882.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-python-2/?couponCode=GITHUBSTUDENT) 17 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 18 | [![exception-handling-in-python](https://user-images.githubusercontent.com/98641125/153196201-83c1a210-9c4f-4ba5-a56f-6aa4d4c551b2.png)]( https://www.udemy.com/course/exception-handling-in-python/?couponCode=GITHUBSTUDENT) 19 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /tree/binary-search-tree.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class TreeEmptyError(Exception): 5 | pass 6 | 7 | class Node: 8 | def __init__(self,value): 9 | self.info = value 10 | self.lchild = None 11 | self.rchild = None 12 | 13 | class BinarySearchTree: 14 | def __init__(self): 15 | self.root = None 16 | 17 | def is_empty(self): 18 | return self.root == None 19 | 20 | def insert(self,x): 21 | self.root = self._insert(self.root, x) 22 | 23 | def _insert(self,p, x): 24 | if p is None: 25 | p = Node(x) 26 | elif x < p.info : 27 | p.lchild = self._insert(p.lchild, x) 28 | elif x > p.info : 29 | p.rchild = self._insert(p.rchild, x) 30 | else: 31 | print(x, " already present in the tree") 32 | return p 33 | 34 | def insert1(self,x): 35 | p = self.root 36 | par = None 37 | while p is not None: 38 | par = p 39 | if x < p.info : 40 | p = p.lchild 41 | elif x > p.info: 42 | p = p.rchild 43 | else: 44 | print(x, " already present in the tree") 45 | return 46 | 47 | temp = Node(x) 48 | 49 | if par == None: 50 | self.root = temp 51 | elif x < par.info: 52 | par.lchild = temp 53 | else: 54 | par.rchild = temp 55 | 56 | def search(self,x): 57 | return self._search(self.root, x) is not None 58 | 59 | def _search(self, p, x): 60 | if p is None: 61 | return None # key not found 62 | if x < p.info: # search in left subtree 63 | return self._search(p.lchild, x) 64 | if x > p.info: # search in right subtree 65 | return self._search(p.rchild, x) 66 | return p # key found 67 | 68 | def search1(self,x): 69 | p = self.root 70 | while p is not None: 71 | if x < p.info : 72 | p = p.lchild # Move to left child 73 | elif x > p.info: 74 | p = p.rchild # Move to right child 75 | else: # x found 76 | return true 77 | return false 78 | 79 | def delete(self,x): 80 | self.root = self._delete(self.root, x) 81 | 82 | def _delete(self, p, x): 83 | if p is None: 84 | print(x , " not found") 85 | return p 86 | 87 | if x < p.info: # delete from left subtree 88 | p.lchild = self._delete(p.lchild, x) 89 | elif x > p.info: # delete from right subtree 90 | p.rchild = self._delete(p.rchild, x) 91 | else: 92 | # key to be deleted is found 93 | if p.lchild is not None and p.rchild is not None: # 2 children 94 | s = p.rchild 95 | while s.lchild is not None: 96 | s = s.lchild 97 | p.info = s.info 98 | p.rchild = self._delete(p.rchild,s.info) 99 | 100 | else: # 1 child or no child 101 | if p.lchild is not None: # only left child 102 | ch = p.lchild 103 | else: # only right child or no child 104 | ch = p.rchild 105 | p = ch 106 | return p 107 | 108 | def delete1(self,x): 109 | p = self.root 110 | par = None 111 | 112 | while p is not None: 113 | if x == p.info: 114 | break 115 | par = p 116 | if x < p.info: 117 | p = p.lchild 118 | else: 119 | p = p.rchild 120 | 121 | if p == None: 122 | print(x , " not found") 123 | return 124 | 125 | # Case C: 2 children 126 | # Find inorder successor and its parent 127 | 128 | if p.lchild is not None and p.rchild is not None: 129 | ps = p 130 | s = p.rchild 131 | 132 | while s.lchild is not None: 133 | ps = s 134 | s = s.lchild 135 | p.info = s.info 136 | p = s 137 | par = ps 138 | 139 | # Case B and Case A : 1 or no child 140 | if p.lchild is not None: # node to be deleted has left child 141 | ch = p.lchild 142 | else: # node to be deleted has right child or no child 143 | ch = p.rchild 144 | 145 | if par == None: # node to be deleted is root node 146 | self.root = ch 147 | elif p == par.lchild: # node is left child of its parent 148 | par.lchild = ch 149 | else: # node is right child of its parent 150 | par.rchild = ch 151 | 152 | 153 | def min1(self): 154 | if self.is_empty(): 155 | raise TreeEmptyError("Tree is empty") 156 | p = self.root 157 | while p.lchild is not None: 158 | p = p.lchild 159 | return p.info 160 | 161 | def max1(self): 162 | if self.is_empty(): 163 | raise TreeEmptyError("Tree is empty") 164 | p = self.root 165 | while p.rchild is not None: 166 | p = p.rchild 167 | return p.info 168 | 169 | def min2(self): 170 | if self.is_empty(): 171 | raise TreeEmptyError("Tree is empty") 172 | return self._min(self.root).info 173 | 174 | def _min(self,p): 175 | if p.lchild is None: 176 | return p 177 | return self._min(p.lchild) 178 | 179 | def max2(self): 180 | if self.is_empty(): 181 | raise TreeEmptyError("Tree is empty") 182 | return self._max(self.root).info 183 | 184 | def _max(self,p): 185 | if p.rchild is None: 186 | return p 187 | return self._max(p.rchild) 188 | 189 | def display(self): 190 | self._display(self.root,0) 191 | print() 192 | 193 | def _display(self,p,level): 194 | if p is None: 195 | return 196 | self._display(p.rchild, level+1) 197 | print() 198 | 199 | for i in range(level): 200 | print(" ", end='') 201 | print(p.info) 202 | self._display(p.lchild, level+1) 203 | 204 | def preorder(self): 205 | self._preorder(self.root) 206 | print() 207 | 208 | def _preorder(self,p): 209 | if p is None: 210 | return 211 | print(p.info, " ") 212 | self._preorder(p.lchild) 213 | self._preorder(p.rchild) 214 | 215 | def inorder(self): 216 | self._inorder(self.root) 217 | print() 218 | 219 | def _inorder(self, p): 220 | if p is None : 221 | return 222 | self._inorder(p.lchild) 223 | print(p.info, " ") 224 | self._inorder(p.rchild) 225 | 226 | def postorder(self): 227 | self._postorder(self.root) 228 | print() 229 | 230 | def _postorder(self, p): 231 | if p is None: 232 | return 233 | self._postorder(p.lchild) 234 | self._postorder(p.rchild) 235 | print(p.info , " ") 236 | 237 | def height(self): 238 | return self._height(self.root) 239 | 240 | def _height(self,p): 241 | if p is None: 242 | return 0 243 | 244 | hL = self._height(p.lchild) 245 | hR = self._height(p.rchild) 246 | 247 | if hL > hR: 248 | return 1 + hL 249 | else: 250 | return 1 + hR 251 | 252 | ############################################################################################## 253 | 254 | bst = BinarySearchTree() 255 | 256 | while True: 257 | print("1.Display Tree") 258 | print("2.Search(Iterative)") 259 | print("3.Search(Recursive)") 260 | print("4.Insert a new node(Iterative)") 261 | print("5.Insert a new node(Recursive)") 262 | print("6.Delete a node(Iterative)") 263 | print("7.Delete a node(Recursive)") 264 | print("8.Find Minimum key(Iterative)") 265 | print("9.Find Minimum key(Recursive)") 266 | print("10.Find Maximum key(Iterative)") 267 | print("11.Find Maximum key(Recursive)") 268 | print("12.Preorder Traversal") 269 | print("13.Inorder Traversal") 270 | print("14.Postorder Traversal") 271 | print("15.Height of tree") 272 | print("16.Quit") 273 | choice = int(input("Enter your choice : ")) 274 | 275 | if choice == 1: 276 | bst.display() 277 | elif choice == 2: 278 | x = int(input("Enter the key to be searched : ")) 279 | if bst.search1(x): 280 | print("Key found") 281 | else: 282 | print("Key not found") 283 | elif choice == 3: 284 | x = int(input("Enter the key to be searched : ")) 285 | if bst.search(x): 286 | print("Key found") 287 | else: 288 | print("Key not found") 289 | elif choice == 4: 290 | x = int(input("Enter the key to be inserted : ")) 291 | bst.insert1(x) 292 | elif choice == 5: 293 | x = int(input("Enter the key to be inserted : ")) 294 | bst.insert(x) 295 | elif choice == 6: 296 | x = int(input("Enter the element to be deleted : ")) 297 | bst.delete1(x) 298 | elif choice == 7: 299 | x = int(input("Enter the element to be deleted : ")) 300 | bst.delete(x) 301 | elif choice == 8: 302 | print("Minimum key is " , bst.min1()) 303 | elif choice == 9: 304 | print("Minimum key is " , bst.min2()) 305 | elif choice == 10: 306 | print("Maximum key is " , bst.max1()) 307 | elif choice == 11: 308 | print("Maximum key is " , bst.max2()) 309 | elif choice == 12: 310 | bst.preorder() 311 | elif choice == 13: 312 | bst.inorder() 313 | elif choice == 14: 314 | bst.postorder() 315 | elif choice == 15: 316 | print("Height of tree is " , bst.height()) 317 | elif choice == 16: 318 | break 319 | else: 320 | print("Wrong choice") 321 | print() 322 | 323 | -------------------------------------------------------------------------------- /tree/binary-tree.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | from collections import deque 5 | 6 | class Node: 7 | def __init__(self,value): 8 | self.info = value 9 | self.lchild = None 10 | self.rchild = None 11 | 12 | class BinaryTree: 13 | def __init__(self): 14 | self.root = None 15 | 16 | def is_empty(self): 17 | return self.root == None 18 | 19 | def display(self): 20 | self._display(self.root,0) 21 | print() 22 | 23 | def _display(self,p,level): 24 | if p is None: 25 | return 26 | self._display(p.rchild, level+1) 27 | print() 28 | 29 | for i in range(level): 30 | print(" ", end='') 31 | print(p.info) 32 | self._display(p.lchild, level+1) 33 | 34 | def preorder(self): 35 | self._preorder(self.root) 36 | print() 37 | 38 | def _preorder(self,p): 39 | if p is None: 40 | return 41 | print(p.info, " ", end ='') 42 | self._preorder(p.lchild) 43 | self._preorder(p.rchild) 44 | 45 | def inorder(self): 46 | self._inorder(self.root) 47 | print() 48 | 49 | def _inorder(self, p): 50 | if p is None : 51 | return 52 | self._inorder(p.lchild) 53 | print(p.info, " ", end ='') 54 | self._inorder(p.rchild) 55 | 56 | def postorder(self): 57 | self._postorder(self.root) 58 | print() 59 | 60 | def _postorder(self, p): 61 | if p is None: 62 | return 63 | self._postorder(p.lchild) 64 | self._postorder(p.rchild) 65 | print(p.info, " ", end ='') 66 | 67 | def level_order(self): 68 | if self.root is None: 69 | print("Tree is empty") 70 | return 71 | 72 | qu = deque() 73 | qu.append(self.root) 74 | 75 | while len(qu)!=0: 76 | p = qu.popleft() 77 | print(p.info + " ",end='') 78 | if p.lchild is not None: 79 | qu.append(p.lchild) 80 | if p.rchild is not None: 81 | qu.append(p.rchild) 82 | 83 | 84 | def height(self): 85 | return self._height(self.root) 86 | 87 | def _height(self,p): 88 | if p is None: 89 | return 0 90 | 91 | hL = self._height(p.lchild) 92 | hR = self._height(p.rchild) 93 | 94 | if hL > hR: 95 | return 1 + hL 96 | else: 97 | return 1 + hR 98 | 99 | def create_tree(self): 100 | self.root = Node('P') 101 | self.root.lchild = Node('Q') 102 | self.root.rchild = Node('R') 103 | self.root.lchild.lchild = Node('A') 104 | self.root.lchild.rchild = Node('B') 105 | self.root.rchild.lchild = Node('X') 106 | 107 | 108 | ################################################################# 109 | bt = BinaryTree() 110 | 111 | bt.create_tree() 112 | 113 | bt.display() 114 | print() 115 | 116 | print("Preorder : ") 117 | bt.preorder() 118 | print("") 119 | 120 | print("Inorder : ") 121 | bt.inorder() 122 | print() 123 | 124 | print("Postorder : ") 125 | bt.postorder() 126 | print() 127 | 128 | print("Level order : ") 129 | bt.level_order() 130 | print() 131 | 132 | print("Height of tree is " , bt.height()) 133 | 134 | -------------------------------------------------------------------------------- /tree/build-heap.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | def build_heap_top_down(a, n): 5 | for i in range(2, n+1): 6 | restore_up(i, a) 7 | 8 | def build_heap_bottom_up(a, n): 9 | i=n//2 10 | while i>=1: 11 | restore_down(i, a, n) 12 | i=i-1 13 | 14 | def restore_up(i, a): 15 | k = a[i] 16 | iparent = i // 2 17 | 18 | while a[iparent] < k : 19 | a[i] = a[iparent] 20 | i = iparent 21 | iparent = i // 2 22 | a[i] = k 23 | 24 | def restore_down(i, a, n): 25 | k = a[i] 26 | lchild = 2 * i 27 | rchild = lchild + 1 28 | 29 | while rchild <= n: 30 | if k >= a[lchild] and k >= a[rchild]: 31 | a[i] = k 32 | return 33 | elif a[lchild] > a[rchild]: 34 | a[i] = a[lchild] 35 | i = lchild 36 | else: 37 | a[i] = a[rchild] 38 | i = rchild 39 | 40 | lchild = 2 * i 41 | rchild = lchild + 1 42 | 43 | 44 | # If number of nodes is even 45 | if lchild == n and k < a[lchild]: 46 | a[i] = a[lchild] 47 | i = lchild 48 | a[i] = k 49 | 50 | #################################################### 51 | 52 | a1=[99999,20,33,15,6,40,60,45,16,75,10,80,12] 53 | n1 = len(a1)-1 54 | 55 | build_heap_bottom_up(a1,n1) 56 | 57 | for i in range(1,n1+1): 58 | print(a1[i], " ",end='') 59 | print() 60 | 61 | a2=[99999,20,33,15,6,40,60,45,16,75,10,80,12] 62 | n2 = len(a2)-1 63 | 64 | build_heap_top_down(a2,n2) 65 | 66 | for i in range(1,n2+1): 67 | print(a2[i], " ",end='') 68 | print() 69 | 70 | #################################################### 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /tree/heap.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) Deepali Srivastava - All Rights Reserved 2 | # This code is part of DSA course available on CourseGalaxy.com 3 | 4 | class HeapEmptyError(Exception): 5 | pass 6 | 7 | class Heap: 8 | 9 | def __init__(self,maxSize=10): 10 | self.a = [None]*maxSize 11 | self.n = 0 12 | self.a[0] = 99999 13 | 14 | def insert(self, value): 15 | self.n+=1 16 | self.a[self.n] = value 17 | self.restore_up(self.n) 18 | 19 | def restore_up(self,i): 20 | k = self.a[i] 21 | iparent = i // 2 22 | 23 | while self.a[iparent] < k: # No sentinel - while(iparent>=1 && self.a[iparent]= self.a[lchild] and k >= self.a[rchild]: 47 | self.a[i] = k 48 | return 49 | else: 50 | if self.a[lchild] > self.a[rchild]: 51 | self.a[i] = self.a[lchild] 52 | i = lchild 53 | else: 54 | self.a[i] = self.a[rchild] 55 | i = rchild 56 | 57 | lchild = 2 * i 58 | rchild = lchild + 1 59 | 60 | # If number of nodes is even 61 | if lchild == self.n and k < self.a[lchild]: 62 | self.a[i] = self.a[lchild] 63 | i = lchild 64 | self.a[i] = k 65 | 66 | def display(self): 67 | if self.n == 0: 68 | print("Heap is empty") 69 | return 70 | print("Heap size = " , self.n) 71 | for i in range(1,self.n+1): 72 | print(self.a[i] , " ", end =' ') 73 | print() 74 | 75 | ###################################################### 76 | 77 | h = Heap(20) 78 | while True: 79 | print("1.Insert") 80 | print("2.Delete root") 81 | print("3.Display") 82 | print("4.Exit") 83 | print("Enter your choice : ") 84 | choice = int(input("Enter your choice : ")) 85 | 86 | if choice == 1: 87 | value = int(input("Enter the value to be inserted : ")) 88 | h.insert(value) 89 | elif choice == 2: 90 | print("Maximum value is " , h.delete_root()) 91 | elif choice == 3: 92 | h.display() 93 | elif choice == 4: 94 | break 95 | else: 96 | print("Wrong choice") 97 | 98 | 99 | --------------------------------------------------------------------------------