以下代码是 Red Black Tree 程序的一部分,它必须采用item
char 或 int,所以我决定使用模板类,但是我不知道如何通过完整的程序扩展它,编译器向我发送了一千个错误:
代码有德语名称,所以如果它更容易理解,我将翻译其中一些:
baum = tree
knote = node
links = left
rechts = right
rot = red
doppel = double
mittlere = middle
eltern = parent
einfuegen = insert
rs = rb = red black
记事本.hpp
#pragma once
template <class T>
class Knote {
public:
Knote(T data = 0);
bool rot;
T item;
Knote *links;
Knote *rechts;
Knote *eltern;
};
记事本.cpp
#include "Knote.hpp"
Knote<int>::Knote(int data)
{
this->item = data;
eltern = nullptr;
links = nullptr;
rechts = nullptr;
rot = true;
}
现在我应该怎么做?
鲍姆.hpp
#pragma once
#include "Knote.hpp"
#include <vector>
class Baum
{
public:
Baum();
void einfuegen(int x);
void ausgabe_levelorder();
void ausgabe_inorder();
private:
Knote<int>* head;
void rs_einfuegen(Knote<int>* &knote, Knote<int>* &eltern, int x, bool sw);
int rot(Knote<int>* &knote);
void links_rotation(Knote<int> * &links_knote);
void rechts_rotation(Knote<int> * &links_knote);
void levelorder(Knote<int>* knote, std::vector<Knote<int>*> &knoteQueue, int niveau, std::vector<int> &niveauQueue);
void sort_levelorder(std::vector<Knote<int>*> &knoteQueue, std::vector<int> &niveauQueue);
void inorder(Knote<int>* knote);
};
鲍姆.cpp
#include "Baum.hpp"
#include <iostream>
using namespace std;
Baum::Baum()
{
...
}
// XXX
void Baum::einfuegen(int x)
{
...
}
// XXX
int Baum::rot(Knote<int>* &knote)
{
...
}
// XXX
void Baum::rs_einfuegen(Knote<int> *& knote, Knote<int> *&eltern, int x, bool sw)
{
...
}
// XXX
void Baum::links_rotation(Knote<int>* &links_knote)
{
...
}
// XXX
void Baum::rechts_rotation(Knote<int>* &rechts_knote)
{
...
}
// XXX
void Baum::ausgabe_levelorder()
{
...
}
// XXX
void Baum::levelorder(Knote<int>* knote, vector<Knote<int>*> &knoteQueue, int niveau, vector<int> &niveauQueue)
{
...
}
// XXX
void Baum::sort_levelorder(vector<Knote<int>*> &knoteQueue, vector<int> &niveauQueue)
{
...
}
// XXX
void Baum::ausgabe_inorder()
{
inorder(head->rechts);
cout << endl;
}
// XXX
void Baum::inorder(Knote<int>* knote)
{
if (knote != nullptr)
{
inorder(knote->links);
cout << knote->item << " ";
inorder(knote->rechts);
}
}