Skip to content
Snippets Groups Projects
Commit 28bee4f8 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

weitere Beispielprogramme 3.5.2018

parent ebc148f2
Branches 2021ss
No related tags found
No related merge requests found
#include <iostream>
using namespace std;
template <typename TElement>
class TTree
{
struct TEntry
{
TElement Content;
bool empty;
TEntry ()
{
empty = true;
}
};
struct TLeaf
{
TEntry Entry[3];
TLeaf *Leaf[4];
TLeaf ()
{
for (int i = 0; i < 4; i++)
Leaf[i] = NULL;
}
void SetEntry (int i, TElement e)
{
Entry[i].Content = e;
Entry[i].empty = false;
}
};
TLeaf *Root;
public:
TTree ()
{
Root = NULL;
}
void Insert (TElement e)
{
if (!Root)
Root = new TLeaf ();
int i = 0;
while (i < 3 && !Root->Entry[i].empty && e > Root->Entry[i].Content)
i++;
if (i < 3)
{
if (!Root->Entry[i].empty)
for (int j = 2; j > i; j--)
Root->Entry[j] = Root->Entry[j - 1];
Root->SetEntry (i, e);
}
}
void Print ()
{
for (int i = 0; i < 3; i++)
cout << Root->Entry[i].Content << " ";
cout << endl;
}
};
int main ()
{
TTree <int> t;
t.Insert (42);
t.Insert (13);
t.Insert (137);
t.Print ();
return 0;
}
#include <iostream>
using namespace std;
const int MaxLeaves = 4;
const int MaxEntries = MaxLeaves - 1;
template <typename TElement>
class TTree
{
struct TEntry
{
TElement Content;
bool empty;
TEntry ()
{
empty = true;
}
};
struct TLeaf
{
TEntry Entry[MaxEntries];
TLeaf *Leaf[MaxLeaves];
TLeaf ()
{
for (int i = 0; i < MaxLeaves; i++)
Leaf[i] = NULL;
}
void SetEntry (int i, TElement e)
{
Entry[i].Content = e;
Entry[i].empty = false;
}
};
TLeaf *Root;
public:
TTree ()
{
Root = NULL;
}
void Insert (TElement e)
{
if (!Root)
Root = new TLeaf ();
int i = 0;
while (i < MaxEntries && !Root->Entry[i].empty && e > Root->Entry[i].Content)
i++;
if (i < MaxEntries)
{
if (!Root->Entry[i].empty)
for (int j = MaxEntries - 1; j > i; j--)
Root->Entry[j] = Root->Entry[j - 1];
Root->SetEntry (i, e);
}
}
void Print ()
{
for (int i = 0; i < MaxEntries; i++)
cout << Root->Entry[i].Content << " ";
cout << endl;
}
};
int main ()
{
TTree <int> t;
t.Insert (42);
t.Insert (13);
t.Insert (137);
t.Print ();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment