언젠가는 되어있겠지
데이터 공부 노트
언젠가는 되어있겠지
전체 방문자
오늘
어제
  • 분류 전체보기 (410)
    • Programming (14)
      • Python (6)
      • Algorithm (8)
    • OS (9)
      • Linux (9)
    • DBMS (2)
      • MySQL (2)
    • IoT Apps (6)
      • AppInventor (5)
      • Arduino (1)
    • LeetCode (151)
      • Easy (136)
      • Medium (13)
      • Hard (2)
    • Course (207)
      • [IBM] Data Science (9)
      • [IBM] Data Engineering (5)
      • [Inflearn] Pandas (12)
      • [Inflearn] Public Data Anal.. (8)
      • [Kaggle] Data Science (49)
      • [Youtube] Informations (2)
      • [Coursera] Machine Learning (40)
      • [Progrmiz] Data Structure A.. (15)
      • [DataQuest] Data Engineerin.. (67)
    • Data Engineering (20)
      • [Data Quest] Handling Datas.. (16)
      • [Data Quest] Data Pipeline (4)
    • Certificate (0)
      • 빅데이터 분석기사 (0)
    • SQLD (0)
    • 하고 싶은 이야기 (0)
    • 소설 (0)

블로그 메뉴

  • 홈
  • 태그

공지사항

인기 글

태그

  • Kaggle Course
  • Data Science

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
언젠가는 되어있겠지

데이터 공부 노트

Programming/Python

[DS] Queue

2022. 9. 3. 13:58

1. What is Priority Queue ?

A priority queue is a special type of queue in which each element is associated with a priority value. And, elements are served on the basis of their priority.

 

Generally, the value of the element itself is considered for assigning the priority. For example, the element with the highest value is considered the highest priority element.

2. Inplementation of Priority Queue

class PriorityQueue: 
    def __init__(self): 
        self.queue = [] 
        
    def _heapify(self, n, i): 
        largest = i
        l = 2 * i + 1
        r = 2 * i + 2 
        
        if l < n and self.queue[i] < self.queue[l]:
            largest = l
        if r < n and self.queue[largest] < self.queue[r]: 
            largest = r 
            
        if largest != i: 
            self.queue[i], self.queue[largest] = self.queue[largest], self.queue[i]
            self._heapify(n, largest)
            
    def insert(self, new):
        size = len(self.queue)
        if size == 0 : 
            self.queue.append(new) 
        else : 
            self.queue.append(new)
            for i in range((size // 2) - 1, -1, -1): 
                self._heapify(size, i) 
                
    def deleteNode(self, num):
        size = len(self.queue)
        i = 0
        for i in range(0, size):
            if num == self.queue[i]:
                break
        self.queue[i], self.queue[size - 1] = self.queue[size - 1], self.queue[i]
        self.queue.remove(size - 1)
        for i in range((len(self.queue) // 2) - 1, -1

 

3. Library of Priority Queue

Python offer module of queue. The queue module implements multi-producer, multi-consumer queues. There are some methods can use commonly.

from queue import Queue, LifoQueue, PrioriyQueue

 

  • Queue.qsize() : Returns the size of queue.
  • Queue.empty() : Return True if queue is empty, False if not.
  • Queue.put(item) : Push item to queue.
  • Queue.put_nowait(item) : Work same as put(item, False)
  • Queue.get() : Pop element from queue and return it.
  • Queue.get_nowait() : Work same as queue(item, False)

 

 

https://docs.python.org/ko/3.7/library/queue.html

 

queue — 동기화된 큐 클래스 — Python 3.7.13 문서

queue — 동기화된 큐 클래스 소스 코드: Lib/queue.py The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queu

docs.python.org

 

 

'Programming > Python' 카테고리의 다른 글

[DS] Heap  (0) 2022.09.03
[Error] Error and Exception  (0) 2022.08.16
[BeautifulTable] Print list object in table format  (0) 2022.07.04
[Pandas] Return multiple columns  (0) 2022.07.03
[BeautifulSoup] Web Scraping  (0) 2022.07.03
    'Programming/Python' 카테고리의 다른 글
    • [DS] Heap
    • [Error] Error and Exception
    • [BeautifulTable] Print list object in table format
    • [Pandas] Return multiple columns
    언젠가는 되어있겠지
    언젠가는 되어있겠지

    티스토리툴바