Skip to main content

Posts

Showing posts from October, 2021

Binary Search Tree and Binary Heap in PYTHON

Here is the Python code for Binary Search Tree :- Content included:- 1.) Insertion 2.) Search Operation 3.) Deletion 4.) Minimum Node 5.) Maximum Node 6.) Preorder Traversal 7.) InOrder Traversal 8.) PostOrder Traversal class BST: def __init__(self, data): self.root = data self.left = None self.right =None def insertnode(self, data): if self.root is None: self.root = data return if self.root is data: return elif self.root > data: if self.left is not None: self.left.insertnode(data) else: self.left = BST(data) elif self.root data: if self.left is not None: self.left.searchoprn(data) else: print("Data is not present.") elif self.root self.root: if self.right: selfright = self.right.deletenode(data) ...

Queues in PYTHON

  The Python Code for Queue is here :- class Node: def __init__(self, data): self.data = data self.next = None class Queue: def __init__(self): self.front = self.rear = None def enqueue(self, data): newnode = Node(data) newnode.next = self.rear self.rear = newnode if self.front is None: self.front = newnode def dequeue(self): temp = self.rear while temp.next is not self.front: temp = temp.next self.front = temp temp.next = None def traverse(self): temp = self.rear while temp is not None: print(temp.data, end = " --> ") temp = temp.next print("None") new = Queue() new.enqueue(4) new.enqueue(1) new.enqueue(0) new.enqueue(2) new.traverse() new.dequeue() new.traverse() new.dequeue() new.traverse() 2 --> 0 --> 1 --> 4 --> None 2 --> 0 --> 1 --> None 2 ...

Stacks in PYTHON

  The Python code for Stacks is :- class Stack: def __init__(self): self.top = None def push(self, data): newnode = Node(data) newnode.next = self.top self.top = newnode def pop(self): self.top = self.top.next def traverse(self): temp = self.top while temp is not None: print(temp.data, "--> ", end ="") temp = temp.next print("None") new = Stack() new.push(4) new.push(2) new.push(0) new.traverse() new.pop() new.traverse() 0 --> 2 --> 4 --> None 2 --> 4 --> None So this was the python code and its result of Stacks. Hope you understood the code well. The post will contain the Python code for Queues. Please share it with your friends and help them learning python. John Veer Contact mail id - john.veer.utube@gmail.com Contact us for any query  Thanks for reading ! print("Bye !") www.basicpythonprogramme.blogsp...

Circular Linked List in PYTHON

  So here is the Python code for Circular Linked List :- class Node: def __init__(self,val): self.data = val self.next = None class Clinklist: def __init__(self): self.head = None def insert_begin(self, val): temp = Node(val) temp.next = self.head if self.head is not None: curr = self.head while curr.next is not self.head: curr = curr.next curr.next = temp else: temp.next = temp self.head = temp def printlist(self): curr = self.head if self.head is not None: while curr.next is not self.head: print(curr.data,"->",end=" ") curr = curr.next print(curr.data,"->",end=" ") print("Head") else: print("None") # Insert at end. def insert_end(self, val): if self.head is not None: temp = Node(val) curr = self.head while curr.next != self.head: curr = curr.next ...

Doubly Linked List in PYTHON

  Here is the Python code for the Doubly Linked List :- #first of all creat a class node class Node :     def __init__ ( self , data ):         self . data = data         self . next = None         self . prev = None #now create a class of doubly linked list class DLL :     def __init__ ( self ):         self . head = None #function to insert the node at begining     def insertatbeg ( self , data ):         newnode = Node ( data )         newnode . prev = None         newnode . next = self . head         self . head = newnode #function to insert the node at the end     def insertatend ( self , data ):         newnode = Node ( data )         if self . head is None :             self . head ...

Singly Linked List is PYTHON

  Here is the python code for singly linked list :- #first of all make a node class as given below class Node: def __init__(self, data): self.data = data self.next = None #now create a linkedlist class to insert and delete the nodes class LL: def __init__(self): self.head = None #function to insert the node at begining def insertatbeg(self, data): newnode = Node(data) if self.head is None: self.head = newnode else: newnode.next = self.head self.head = newnode #function to insert the node at last def insertatend(self, data): newnode = Node(data) if self.head is None: self.head = newnode return else: temp = self.head while temp.next is not None: temp = temp.next temp.next = newnode newnode.next = None #function to insert the node at a given position ...

Linked List (For Beginners) Explanation Without Code

Linked List Linked List is a chain of nodes. In which each node contains data and reference of the next node. And that next node contain its own data and reference to the next node. Here reference is considered as address of the next node.  Here we can see the representation of the basic linked list. Here we have the first node contains the data "1" and the the "next " as the reference of the next node. Similarly the second node has its own data "2" and "next" as the reference of the next node. And so on. Here we can the representation of a particular node containing its "data" and "address". And the whole thing is said as a single "NODE" . The reference of the node 2 is stored in the node 1. And the reference of the node 3 is stored in node 2. And so on, but the reference of the first node is called as head. And the reference of the last node is called as tail and considered as "None". Advantages of Linked...

Classes and Objects in PYTHON (Object Oriented Programming)

Solving a problem by creating objects is one of the most popular approach in programming. This is called Object Oriented Programming This concept is based on using reusable code but differently from functions. It is based on DRY (Don't Repeat Yourself) Principle. CLASS :- A class is a blueprint for creating objects. It can described as a group of objects. Objects are the instances of a class. We will further define this in our upcoming code in this post. Lets see a basic program for the class and objects to understand it properly. class Students: pass #we have three students alok, vinay and aashish alok = Students() vinay = Students() aashish = Students() #there data is given as per alok.name = "Alok Kumar" alok.age = 17 alok.standard = "11 th" vinay.name = "Vinay Sharma" vinay.age = 18 vinay.standard = "12 th" aashish.name = "Aashish Mishra" aashish.age = 16 aashish.standard = "10 th" print(vinay) print(alok.a...

Quicksort Algorithm and Data structure in PYTHON

  So, now what is Quicksort ? Quicksort, or partition-exchange sort, is a sorting algorithm developed by Tony Hoare that, on average, makes O comparisons to sort n items. In the worst case, it makes O comparisons, though this behavior is rare. Quicksort is often faster in practice than other O algorithms. Additionally, quicksort's sequential and localized memory references work well with a cache. Quicksort is a comparison sort and, in efficient implementations, is not a stable sort. Quicksort can be implemented with an in-place partitioning algorithm, so the entire sort can be done with only O additional space used by the stack during the recursion. Properties of Quicksort :- The quick sort is an  in-place, divide-and-conquer, massively recursive sort algorithm . The efficiency of the algorithm is majorly impacted by which element is chosen as the pivot point. The worst-case efficiency of the quick sort is o (n²) when the list is sorted and left most element is chosen as the p...