From 55e62ed8caf3b2a1f1f83333e6daaff627673e53 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 26 Apr 2022 10:18:06 +0800 Subject: [PATCH] feat: add PriorityQueue --- datastructure/queue/priorityqueue.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 datastructure/queue/priorityqueue.go diff --git a/datastructure/queue/priorityqueue.go b/datastructure/queue/priorityqueue.go new file mode 100644 index 0000000..f182a53 --- /dev/null +++ b/datastructure/queue/priorityqueue.go @@ -0,0 +1,21 @@ +package datastructure + +import "github.com/duke-git/lancet/v2/lancetconstraints" + +// PriorityQueue is a implemented by binary heap tree +// which are referred to as the left child and the right child. +// type T should implements Compare function in lancetconstraints.Comparator interface. +type PriorityQueue[T any] struct { + items []T + size int + comparator lancetconstraints.Comparator +} + +// NewPriorityQueue return a pointer of PriorityQueue +func NewPriorityQueue[T any](capacity int, comparator lancetconstraints.Comparator) *NewPriorityQueue[T] { + return &NewPriorityQueue[T]{ + items: make([]T, capacity+1), + size: 0, + comparator: comparator, + } +}