Python/자료구조4 [python] LinkedList-bubble sort : 주어진 연결리스트를 버블 정렬을 이용하여 정렬하는 코드 # Linked List. class Node: def __init__ (self, elem, next=None): self.data = elem self.link = next # 코드 6.5: 연결리스트 클래스 class LinkedList: # 리스트의 데이터: 생성자에서 정의 및 초기화 def __init__( self ): self.head = None # 리스트의 연산: 클래스의 메소드 def isEmpty( self ): return self.head == None def isFull( self ): return False def getNode(self, pos) : if pos < 0 : return None node = self.h.. 2024. 3. 27. [python] 이진트리 / 이진 탐색트리(+백준 5639) 1. 이진트리 모든 노드의 차수가 self.data): if(self.right): self.right.insert(item) else: self.right=Node(item) else: raise IndexError def search(self,item,parent=None): if(itemself.data): if(self.right): return self.right.search(item,self) else: print('Not Exist') return None,None else: print('exist') return self,parent def delete(self, item): delNode, parent = self.search(item) if(delNode): if(parent==None.. 2023. 11. 21. [python] 우선순위 큐 1. Heap(힙) : 주로 완전이진트리 기반으로 구현하며 정렬하는 용도의 이진트리이다 -> 마지막 레벨 노드 제외하고는 포화상태인 트리 cf> 힙 구현 : 완전이진트리 기반이기에 연결리스트보다 배열로 구현하는 게 더 적합함 1-1. max heap / min heap Max heap Min heap 부모 노드의 키가 자식 노드들의 키보다 크거나 같은 트리 부모 노드의 키가 자식 노드들의 키보다 작거나 같은 트리 꺼내면 내림차순 정렬 꺼내면 오름차순 정렬 1-2. 우선순위 큐 : 큐와 유사하지만 우선순위가 높은 아이템이 먼저 처리됨 : max heap과 min heap을 이용하여 우선순위 큐 구현 -> 힙의 key를 우선순위로 사용한다면 힙은 우선순위 큐의 구현체가 된다 1-2-1. 주요 동작 inser.. 2023. 11. 20. [python] dummy node 활용한 Linked List #맨 앞에 dummy node를 추가한 형태 class Node: def __init__(self, item): self.data= item self.next = None class LinkedList: def __init__(self): self.nodeCount =0 self.head = Node(None) self.tail = None self.head.next = self.tail def traverse(self): result =[] cur= self.head.next while(cur is not None): result.append(cur.data) cur=cur.next return result def getAt(self, pos): if pos self.nodeCount: return N.. 2023. 11. 14. 이전 1 다음