您的位置:首页 > 其它

优先队列的初始化,删除,插入操作

2015-11-27 23:21 495 查看
//头文件
//BinaryTreeNode.h
#ifndef BINARYTREENODE_H
#define BINARYTREENODE_H
template<typename T>
class BinaryTreeNode{
public:
T data;
int position;
BinaryTreeNode<T>* leftChild;
BinaryTreeNode<T>* rightChild;
BinaryTreeNode<T>* parent;
BinaryTreeNode(){

}
BinaryTreeNode(const T& val, int pos, BinaryTreeNode<T>* left, BinaryTreeNode<T>* right, BinaryTreeNode<T>* par) :data(val), leftChild(left), rightChild(right), parent(par){
this->position = pos;
}
};
#endif
//priorityQueue.h
#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H
#include"stdafx.h"
template<typename T>
class PriorityQueue{
private:
BinaryTreeNode<T>* root;
int count;//该变量代表数组大小
T* data;
int pos;
public:
PriorityQueue();
~PriorityQueue();
bool isEmpty()const;
void Creat();
void Changedata(BinaryTreeNode<T>* &str,BinaryTreeNode<T>* &ptr);
void PrintTree()const;
void Insertdata(const T& val);
void DeleteNode();
private:
void Clear();
void CreatLeaf();
void getdata();
};
template<typename T>
PriorityQueue<T>::PriorityQueue()
{
this->root = NULL;
this->data = NULL;
this->pos = 0;
}
template<typename T>
PriorityQueue<T>::~PriorityQueue()
{
this->Clear();
}
template<typename T>
void PriorityQueue<T>::Clear()
{
BinaryTreeNode<T>* p = NULL;
if (this->isEmpty())
{
cout << "the priorityqueue is empty" << endl;
exit(true);
}
queue<BinaryTreeNode<T>*>node;
node.push(this->root);
while (!node.empty())
{
p = node.front();
node.pop();
if (p->leftChild)
node.push(p->leftChild);
if (p->rightChild)
node.push(p->rightChild);
delete p;
}
this->root = NULL;
}
template<typename T>
bool PriorityQueue<T>::isEmpty()const
{
if (this->root == NULL)
return true;
return false;
}
template<typename T>
void PriorityQueue<T>::Creat()
{
this->getdata();
this->CreatLeaf();
}
template<typename T>
void PriorityQueue<T>::Changedata(BinaryTreeNode<T>* &str, BinaryTreeNode<T>* &ptr)
{
//str为当前节点,ptr为父节点
T val = str->data;
str->data = ptr->data;
ptr->data = val;
val = data[str->position];
data[str->position] = data[ptr->position];
data[ptr->position] = val;
}
template<typename T>
void PriorityQueue<T>::CreatLeaf()
{
//该程序是建立最大堆,该程序不使用递归,使用普通的算法
queue<BinaryTreeNode<T>*>node;
BinaryTreeNode<T>* prev = NULL;
int i = 0;
this->root = new BinaryTreeNode<T>(data[i++],pos++, NULL, NULL, prev);
node.push(this->root);
BinaryTreeNode<T>* str = NULL;
while (true)
{
str = node.front();
node.pop();
prev = str;
if (i < count)
{
str->leftChild = new BinaryTreeNode<T>(data[i++],pos++, NULL, NULL, prev);
}
else
{
break;
}
node.push(str->leftChild);
BinaryTreeNode<T>* ptr = prev;
BinaryTreeNode<T>* temp = str->leftChild;
while (ptr!=NULL)
{
if (temp->data > ptr->data)
{
this->Changedata(temp, ptr);
temp = temp->parent;
ptr = ptr->parent;
}
else
break;
}
if (i < count)
{
str->rightChild = new BinaryTreeNode<T>(data[i++],pos++, NULL, NULL, prev);
}
else
{
break;
}
node.push(str->rightChild);
ptr = prev;
temp = str->rightChild;
while (ptr!=NULL)
{
if (temp->data > ptr->data)
{
this->Changedata(temp, ptr);
temp = temp->parent;
ptr = ptr->parent;
}
else
break;
}
}
count++;
}
template<typename T>
void PriorityQueue<T>::getdata()
{
cout << "输入节点个数:";
cin >> count;
int value;
data = new T[count];
cout << "输入每个节点的数据:";
for (int i = 0; i < count; i++)
{
cin >> value;
data[i] = value;
}
}
template<typename T>
void PriorityQueue<T>::PrintTree()const
{
if (this->root == NULL)
{
cout << "the tree is empty" << endl;
exit(true);
}
BinaryTreeNode<T>* str = this->root;
queue<BinaryTreeNode<T>*>node;
node.push(str);
while (!node.empty())
{
str = node.front();
node.pop();
if (str->leftChild)
node.push(str->leftChild);
if (str->rightChild)
node.push(str->rightChild);
cout << str->data << " ";
}
}
template<typename T>
void PriorityQueue<T>::Insertdata(const T& val)
{
//在程序中我给出的prev变量此时的指向是
//在创建是prev变量已经指向最后一个拥有孩子的节点的变量
//所以在这里可以直接讨论orev孩子的个数
BinaryTreeNode<T>* temp = NULL;
BinaryTreeNode<T>* ptr = NULL;
queue<BinaryTreeNode<T>*>node;
if (this->root == NULL)
{
cout << "the queue is empty" << endl;
exit(true);
}
node.push(this->root);
BinaryTreeNode<T>* str = NULL;
while (!node.empty())
{
str = node.front();
node.pop();
if (str->leftChild != NULL&&str->rightChild == NULL)
{
data[pos] = val;
str->rightChild = new BinaryTreeNode<T>(val, pos++, NULL, NULL, str);
temp = str->rightChild;
ptr = str;
while (ptr)
{
if (temp->data > ptr->data)
{
this->Changedata(temp, ptr);
temp = temp->parent;
ptr = ptr->parent;
}
else
{
break;
}
}
return;
}
else if (str->leftChild == NULL)
{
data[pos] = val;
str->leftChild = new BinaryTreeNode<T>(val, pos++, NULL, NULL, str);
temp = str->leftChild;
ptr = str;
while (ptr)
{
if (temp->data > ptr->data)
{
this->Changedata(temp, ptr);
temp = temp->parent;
ptr = ptr->parent;
}
else
{
break;
}
}
return;
}
if (str->leftChild)
node.push(str->leftChild);
if (str->rightChild)
node.push(str->rightChild);
}
}
template<typename T>
void PriorityQueue<T>::DeleteNode()
{
//删除栈顶元素
//方法是将栈顶元素给最后一个节点
queue<BinaryTreeNode<T>*>node;
if (this->root == NULL)
{
cout << "the queue is empty" << endl;
exit(true);
}
BinaryTreeNode<T>* str = this->root;
node.push(str);
while (!node.empty())
{
str = node.front();
node.pop();
if (str->leftChild)
node.push(str->leftChild);
if (str->rightChild)
node.push(str->rightChild);
}
this->Changedata(this->root, str);
BinaryTreeNode<T>* temp = str->parent;
if (temp->leftChild == str)
{
temp->leftChild = NULL;
}
if (temp->rightChild == str)
{
temp->rightChild = NULL;
}
delete str;
str = NULL;
count--;
BinaryTreeNode<T>* ptr = this->root;
while (ptr)
{
if (ptr->leftChild&&ptr->rightChild && (ptr->data < ptr->leftChild->data || ptr->data<ptr->rightChild->data))
{
if (ptr->leftChild->data > ptr->rightChild->data)
{
this->Changedata(ptr, ptr->leftChild);
ptr = ptr->leftChild;
}
else
{
this->Changedata(ptr, ptr->rightChild);
ptr = ptr->rightChild;
}
}
else if(ptr->leftChild != NULL&&ptr->rightChild == NULL&&ptr->data<ptr->leftChild->data)
{
this->Changedata(ptr, ptr->leftChild);
ptr = ptr->leftChild;
}
else
{
break;
}
}
}
#endif
//simulate.h
#ifndef SIMULATE_H
#define SIMULATE_H
#include"stdafx.h"
template<typename T>
class Simulate{
private:
PriorityQueue<T>simulate;
public:
void start();
};
template<typename T>
void Simulate<T>::start()
{
simulate.Creat();
simulate.PrintTree();
simulate.Insertdata(11);
cout << endl;
simulate.PrintTree();
cout << endl;
simulate.DeleteNode();
simulate.PrintTree();
}
#endif
//主函数
// PriorityQueue.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
Simulate<int>user;
user.start();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: