C++ --priority_queue实现

文章目录

  • 1. 普通版本实现优先级队列
    • 1.1 push()
    • 1.2 pop()
    • 1.3 top()
    • 1.4 size()
    • 1.5 empty()
    • 1.6 完整代码
  • 2. 仿函数实现优先级队列

1. 普通版本实现优先级队列

1.1 push()

void adjust_up(int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (_container[parent] < _container[child])
		{
			swap(_container[parent], _container[child]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
void push(const T& val)
{
	_container.push_back(val);
	adjust_up(_container.size() - 1);
}

1.2 pop()

void adjust_down(int parent)
{
	int child = parent * 2 + 1;
	while (child < _container.size())
	{
		if (child + 1 < _container.size() && _container[child] < _container[child + 1])
		{
			child = child + 1;
		}
		if (_container[parent] < _container[child])
		{
			swap(_container[parent], _container[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
void pop()
{
	swap(_container[0], _container[_container.size() - 1]);
	_container.pop_back();
	adjust_down(0);
}

1.3 top()

const T& top()
{
	return _container[0];
}

1.4 size()

size_t size()
{
	return _container.size();
}

1.5 empty()

bool empty()
{
	return _container.empty();
}

1.6 完整代码

namespace my_priority_queue
{
	template >
	class priority_queue
	{
	public:
		void adjust_up(int child)
		{
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (_container[parent] < _container[child])
				{
					swap(_container[parent], _container[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(int parent)
		{
			int child = parent * 2 + 1;
			while (child < _container.size())
			{
				if (child + 1 < _container.size() && _container[child] < _container[child + 1])
				{
					child = child + 1;
				}
				if (_container[parent] < _container[child])
				{
					swap(_container[parent], _container[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}

		}

		void push(const T& val) 
		{
			_container.push_back(val);
			adjust_up(_container.size() - 1);
		}

		void pop()
		{
			swap(_container[0], _container[_container.size() - 1]);
			_container.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _container[0];
		}

		size_t size()
		{
			return _container.size();
		}

		bool empty()
		{
			return _container.empty();
		}

	private:
		Container _container;
	};
}

2. 仿函数实现优先级队列

namespace my_priority_queue
{
	template 
	struct less
	{
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template 
	struct greator
	{
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template , class Compare = less>
	class priority_queue
	{
	public:
		void adjust_up(int child)
		{
			Compare compare;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				//if (_container[parent] < _container[child])
				if (compare(_container[parent],_container[child]))
				{
					swap(_container[parent], _container[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(int parent)
		{
			Compare compare;
			int child = parent * 2 + 1;
			while (child < _container.size())
			{
				if (child + 1 < _container.size() && 		compare(_container[child],_container[child + 1]))
				{
					child = child + 1;
				}
				if (compare(_container[parent], _container[child]))
				{
					swap(_container[parent], _container[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}

		}

		void push(const T& val) 
		{
			_container.push_back(val);
			adjust_up(_container.size() - 1);
		}

		void pop()
		{
			swap(_container[0], _container[_container.size() - 1]);
			_container.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _container[0];
		}

		size_t size()
		{
			return _container.size();
		}

		bool empty()
		{
			return _container.empty();
		}

	private:
		Container _container;
	};
}

仿函数在这里就是类中实现一个()运算符重载,通过对象直接调用,比较大小返回一个真假值。

你可能感兴趣的:(c++,c++,算法,开发语言)