Hello I am trying to create a Red-Black Tree in c++, The problem is on the left_rotate method. I print the tree in order using recursive method and included some extra std::cout to see what is going on. The problem is that when i insert values to the tree from 1-8, the root should be the number 4. Instead the root stucks at number 2 for some reason, when i try it the other way around from 10-1 the right_rotate method works like a charm. I have also added some cout to see where is the iterator going after fixing 1 violation. It goes to the right Path. when 8 number is inserted it recolors, then goes up to number 6 where it should perform a left_rotate but it doesnt for some reason.... here is the code
p.s : please forgive my english :)
struct Node
{
int value;
bool isBlack;
bool isLeft;
Node *parent, *left, *right;
};
class RBtree
{
private:
Node *root;
protected:
void inorder(Node *t)
{
if (t == nullptr)
return;
inorder(t->left);
if (t == root)
cout << "ROOT: \n";
cout << "value: " << t->value << " color: ";
if (t->isBlack)
cout << " B \n";
else
cout << " R \n";
if (!t->isLeft)
{
cout << "ITS A RIGHT CHILD \n";
}
else
{
cout << "ITS A LEFT CHILD \n";
}
if (t->parent)
cout << "PARENT: " << t->parent->value << "\n";
else
cout << "NONE\n";
if (t->left)
cout << "LEFT CHILD IS: " << t->left->value << "\n";
else
cout << "LEFT CHILD IS NULL \n";
if (t->right)
cout << "RIGHT CHILD IS: " << t->right->value << "\n\n";
else
{
cout << "RIGHT CHILD IS NULL \n\n";
}
cout << "--------------------- \n\n";
inorder(t->right);
}
void fix_violations(Node *);
void recolor(Node *t);
void left_rotate(Node *);
void right_rotate(Node *);
public:
RBtree()
{
root = nullptr;
}
void insert(int);
void remove(int);
void print_inorder()
{
inorder(root);
}
};
void RBtree::insert(int v)
{
Node * t = new Node(); // temp node
t->value = v;
t->isBlack = false;
t->isLeft = false;
t->parent = nullptr;
t->left = nullptr;
t->right = nullptr;
if (root == nullptr)
{
root = t;
root->isBlack = true;
return;
}
Node *it = root; // create iterator to root
while (it)
{
if (v < it->value)
{
if (!it->left)
{
it->left = t;
t->parent = it;
t->isLeft = true;
//cout << "inserted ";
break;
}
it = it->left;
}
else if (v > it->value)
{
if (!it->right)
{
it->right = t;
t->parent = it;
t->isLeft = false;
//cout << "inserted ";
break;
}
it = it->right;
}
else
{
break;
}
}
fix_violations(t);
}
// check violations method
void RBtree::fix_violations(Node *t)
{
cout << "Iterated on: ";
while (t != root)
{
cout << t->value << " ";
if (!t->parent || !t->parent->parent)
{
break;
}
if (!t->isBlack && !t->parent->isBlack)
{
bool u_isBlack = true;
if (!t->isLeft)
{
if (t->parent->parent->left)
u_isBlack = t->parent->parent->left->isBlack;
}
else
{
if (t->parent->parent->right)
u_isBlack = t->parent->parent->right->isBlack;
}
if (!t->isBlack && !u_isBlack)
{
cout << " recolor ";
recolor(t);
}
else if (!t->isBlack && u_isBlack)
{
if (t->isLeft && t->parent->isLeft)
{
cout << " right-rotation ";
right_rotate(t);
}
else if (!t->left && !t->parent->isLeft)
{
cout << " left-rotation ";
left_rotate(t);
}
else if (t->isLeft && !t->parent->isLeft)
{
//right_left_rotate(t);
}
else if (!t->isLeft && t->parent->isLeft)
{
// left_right_rotate(t);
}
else
;
}
}
t = t->parent;
}
cout << "\n\n";
root->isBlack = true;
}
void RBtree::recolor(Node *t)
{
Node *u; // uncle;
if (!t->isLeft)
u = t->parent->parent->left;
else
u = t->parent->parent->right;
t->parent->isBlack = true;
t->parent->parent->isBlack = false;
u->isBlack = true;
}
void RBtree::left_rotate(Node *t)
{
Node *p = t->parent;
Node *g = p->parent;
if (!g->parent) // if grand parent has no parent then it is root
{
// disconnect nodes
g->right = nullptr;
p->parent = nullptr;
// parents left child
Node *p_left = p->left;
if (p_left)
{ // if left child of parent exists disconnect it
p->left = nullptr;
p_left->parent = nullptr;
}
root = p;
root->left = g;
g->parent = p;
g->isLeft = true;
if (p_left)
{
g->right = p_left;
p_left->parent = g;
p_left->isLeft = false;
}
}
else
{
Node *pg = g->parent; // grand parent's parent
pg->right = nullptr;
g->right = nullptr;
g->parent = nullptr;
p->parent = nullptr;
Node *p_left = p->left;
if (p_left)
{
p->left = nullptr;
p_left->parent = nullptr;
}
pg->right = p;
p->parent = pg;
p->left = g;
g->parent = p;
g->isLeft = true;
if (p_left)
{
g->right = p_left;
p_left->parent = g;
p_left->isLeft = false;
}
}
// recolor
p->isBlack = true;
t->isBlack = false;
g->isBlack = false;
}
void RBtree::right_rotate(Node *t)
{
Node * p = t->parent; // parent
Node * g = p->parent; // grand-parent
Node * u = g->right; // uncle
if (!g->parent) // if grand-parent's parent is null then g is root
{
g->left = nullptr;
p->parent = nullptr;
Node *p_right = p->right;
if (p_right)
p_right->parent = nullptr;
root = p;
root->right = g;
g->parent = p;
g->isLeft = false;
if (p_right)
{
g->left = p_right;
p_right->parent = g;
p_right->isLeft = true;
}
}
else
{
Node *pg = g->parent;
pg->left = nullptr;
g->parent = nullptr;
g->left = nullptr;
Node *p_right = p->right;
if (p_right)
p_right->parent = nullptr;
pg->left = p;
p->parent = pg;
p->right = g;
g->parent = p;
g->isLeft = false;
if (p_right)
{
g->left = p_right;
p_right->parent = g;
p_right->isLeft = true;
}
}
// recolor
p->isBlack = true;
t->isBlack = false;
g->isBlack = false;
}
int main()
{
RBtree a;
a.insert(1);
a.insert(2);
a.insert(3);
a.insert(4);
a.insert(5);
a.insert(6);
a.insert(7);
a.insert(8);
a.print_inorder();
}
Related
I have implemented deque through linked list.
Below You can see my code:
#include <iostream>
using namespace std;
struct Node
{
int mData;
Node* pPrev, *pNext;
Node()
{
this->mData = mData;
pPrev = pNext = NULL;
}
Node(int value)
{
mData = value;
pPrev = pNext = NULL;
}
};
class Deque
{
private:
Node *pFront, *pRear;
int mSize;
public:
Deque()
{
pFront = pRear = NULL;
mSize = 0;
}
void pushFront(int data)
{
Node* newNode = new Node(data);
if (newNode == NULL)
{
cout << "Error";
}
else
{
if (pFront == NULL)
{
pRear = pFront = newNode;
}
else
{
newNode->pNext = pFront;
pFront->pPrev = newNode;
pFront = newNode;
}
mSize++;
}
}
void pushLast(int data)
{
Node* newNode = new Node(data);
if (newNode == NULL)
{
cout << "Error";
}
else
{
if (pRear == NULL)
{
pFront = pRear = newNode;
}
else
{
newNode->pPrev = pRear;
pRear->pNext = newNode;
pRear = newNode;
}
mSize++;
}
}
void deleteFront()
{
if (isEmpty())
{
cout << "Deque is empty";
}
else
{
Node* temp = pFront;
pFront = pFront->pNext;
if (pFront == NULL)
{
pRear = NULL;
}
else
{
pFront->pPrev = NULL;
}
free(temp);
mSize--;
}
}
void deleteLast()
{
if (isEmpty())
{
cout << "Deque is empty";
}
else
{
Node* temp = pRear;
pRear = pRear->pPrev;
if (pRear == NULL)
{
pFront = NULL;
}
else
{
pRear->pNext = NULL;
}
free(temp);
mSize--;
}
}
int getFront()
{
if (isEmpty())
{
cout << "Deque is empty";
}
else
{
return pFront->mData;
}
}
int getLast()
{
if (isEmpty())
{
cout << "Deque is empty";
}
else
{
return pRear->mData;
}
}
void swap()
{
if (isEmpty())
{
cout << "Deque is empty";
}
else
{
Node* temp = pFront;
while (temp->pNext != NULL) {
temp = temp->pNext;
}
Node* tmp2 = new Node();
tmp2->mData = temp->mData;
temp->mData = pFront->mData;
temp->pNext = NULL;
pFront->mData = tmp2->mData;
}
}
bool isEmpty()
{
return (mSize == 0);
}
int getSize()
{
return mSize;
}
void reverse()
{
auto curr = pFront; // current pointer
Node* prev = NULL; // previous pointer
while (curr) {
auto temp = curr->pNext;
curr->pNext = prev;
prev = curr;
pFront = prev;
curr = temp;
}
}
bool isBelong(int data)
{
Node* temp = pFront;
while (temp != NULL)
{
if (data == temp->mData)
{
return true;
}
temp = temp->pNext;
}
return false;
}
void clear()
{
pRear = NULL;
while (pFront != NULL)
{
Node* temp = pFront;
pFront = pFront->pNext;
delete temp;
}
pFront = NULL;
mSize = 0;
}
void show()
{
Node* node = pFront;
while (node != NULL)
{
cout << node->mData << " ";
node = node->pNext;
}
}
};
int main()
{
Deque deque; int num;
while (true)
{
int choice;
cout << "\n0.Exit.\n1.Insertion(head).\n2.Insertion(rear).\n3.Deletion(head).\n4.Deletion(rear).\n5.Get head.\n6.Get rear.\n7.Check emptyness.\n8.Check size.\n9.Clear deque.\n10.Swap front and rear.\n11.Check belonginess.\n12.Reverse deque.\n";
cin >> choice;
switch (choice)
{
case 0: return 0;
case 1:
cout << "Insertion to head - input value : "; cin >> num;
deque.pushFront(num);
deque.show();
system("pause");
system("cls");
break;
case 2:
cout << "Insertion to rear - input value : "; cin >> num;
deque.pushLast(num);
deque.show();
system("pause");
system("cls");
break;
case 3:
deque.deleteFront();
deque.show();
system("pause");
system("cls");
break;
case 4:
deque.deleteLast();
deque.show();
system("pause");
system("cls");
break;
case 5:
if (deque.isEmpty())
{
cout << "Deque is empty";
}
else
{
cout << "First element of deque : " << deque.getFront();
}
system("pause");
system("cls");
break;
case 6:
if (deque.isEmpty())
{
cout << "Deque is empty";
}
else
{
cout << "Last element of deque : " << deque.getLast();
}
system("pause");
system("cls");
break;
case 7:
if (deque.isEmpty())
{
cout << "Deque is empty";
}
else
{
cout << "Deque is not empty: "; deque.show();
}
system("pause");
system("cls");
break;
case 8:
cout << "Size of deque : " << deque.getSize();
system("pause");
system("cls");
break;
case 9:
deque.clear();
system("pause");
system("cls");
break;
case 10:
cout << "Deque before swap: "; deque.show(); cout << endl;
deque.swap();
cout << "Deque after swap: "; deque.show(); cout << endl;
system("pause");
system("cls");
break;
case 11:
cout << "Input number : "; int number; cin >> number;
if (deque.isBelong(number))
{
cout << number << " belongs.\n";
}
else
{
cout << number << " does not belong.\n";
}
system("pause");
system("cls");
break;
case 12:
cout << "Deque before reverse: "; deque.show(); cout << endl;
cout << "Deque after reverse: "; deque.reverse(); deque.show(); cout << endl;
system("pause");
system("cls");
break;
default:
cout << "There is no " << choice << " choice!" << endl;
system("pause");
system("cls");
break;
}
}
}
However, reverse() method works inappropriate way.
Testing code I noticed that several methods working inappropriate way after reversing:
getRear() - returns old value;
deleteLast() - crashes.
Reverse() method based totally on same method in linked list implementation,as swap(), although it works with some problems.
I have not looked through all your code though I am sure it has some bugs as for example in the default constructor of the class Node
Node()
{
this->mData = mData;
pPrev = pNext = NULL;
}
where uninitialized data member mData is assigned to itself.
Or if you are using the operator new then you should use the operator delete instead of the standard C function free as you are doing
free(temp);
Or another example. Your method reverse does not change the pointer pRear. Or the data member pPrev also is not changed for the pointer curr.
Or the method swap has a memory leak.
And so on.
I will show how the method Reverse can be implemented.
In fact what you need is to swap data members pPrev and pNext for each node of the list.
Here is a demonstration program. I have made some changes for your classes to simplify the implementation of the demonstration program. For example I have made the class Node an internal class of the class Deque (you should also do that.). In any case you can use the idea that is realized in the method Reverse in your own implementation of the method.
#include <iostream>
#include <utility>
class Deque
{
private:
struct Node
{
int mData;
Node *pPrev, *pNext;
} *pFront = nullptr, *pRear = nullptr;
int mSize = 0;
public:
void Push( int data )
{
Node *newNode = new Node{ data, pRear, nullptr };
if (pFront == nullptr)
{
pFront = newNode;
}
else
{
pRear->pNext = newNode;
}
pRear = newNode;
++mSize;
}
Deque & Reverse()
{
if (pFront && pFront->pNext)
{
std::swap( pFront, pRear );
for (Node *current = pFront; current; current = current->pNext)
{
std::swap( current->pPrev, current->pNext );
}
}
return *this;
}
std::ostream &Show( std::ostream &os = std::cout ) const
{
os << mSize << ": ";
for (Node *current = pFront; current; current = current->pNext)
{
os << current->mData << " -> ";
}
return os << "null";
}
std::ostream &ShowReversed( std::ostream &os = std::cout ) const
{
os << mSize << ": ";
for (Node *current = pRear; current; current = current->pPrev)
{
os << current->mData << " -> ";
}
return os << "null";
}
};
int main( void )
{
Deque deque;
const int N = 10;
for (int i = 0; i < N; i++)
{
deque.Push( i );
}
deque.Show() << '\n';
deque.ShowReversed() << '\n';
std::cout << '\n';
deque.Reverse();
deque.Show() << '\n';
deque.ShowReversed() << '\n';
std::cout << '\n';
}
The program output is
10: 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
10: 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> null
10: 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> null
10: 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
What's wrong with this code?
I really don't know. I'm beginner. Sorry for my english.
#include <iostream>
using namespace std;
struct elem
{
int wartosc;
elem* nast;
elem* poprz;
};
class ListaDwukierunkowa
{
protected:
elem **lista;
public:
ListaDwukierunkowa(elem** lista)
{
this->lista = lista;
}
void dodaj_elem(int do_dodania)
{
if (*lista == nullptr)
{
*lista = new elem;
(*lista)->wartosc = do_dodania;
(*lista)->nast = nullptr;
(*lista)->poprz = nullptr;
}
else
{
elem *temp = *lista;
while (temp->nast != nullptr)
{
temp = temp->nast;
}
temp->nast = new elem;
temp->nast->wartosc = do_dodania;
temp->nast->nast = nullptr;
temp->nast->poprz = temp;
}
}
void wyswietl_elem()
{
cout << endl;
if (lista == nullptr)
{
cout << "Lista jest pusta" << endl;
}
while (lista != nullptr)
{
cout << "poprz: " << lista->poprz << " | ten: " << lista << " | war:" << lista->wartosc << " | nast:" << lista->nast << endl;
lista = lista->nast;
}
}
void usun_elem(int do_usun)
{
elem *temp = *lista;
if (*lista == nullptr)
return;
while (temp->wartosc != do_usun)
{
temp = temp->nast;
if (temp == nullptr)
return;
}
if (temp->poprz)
temp->poprz->nast = temp->nast;
else
*lista = temp->nast;
if (temp->nast)
temp->nast->poprz = temp->poprz;
delete temp;
}
int liczba_elem()
{
int liczba = 0;
while (lista != nullptr)
{
liczba += 1;
lista = lista->nast;
}
return liczba;
}
int liczba_elem_o_war(int x)
{
int liczba = 0;
while (lista != nullptr)
{
if (lista->wartosc == x)
{
liczba += 1;
}
lista = lista->nast;
}
return liczba;
}
bool czy_zawiera(int x)
{
bool czy = false;
while (lista != nullptr)
{
if (lista->wartosc == x)
{
czy = true;
}
lista = lista->nast;
}
return czy;
}
void zwolnij_liste()
{
while (*lista != nullptr)
{
elem* nast = (*lista)->nast;
delete *lista;
*lista = nast;
}
*lista = nullptr;
}
};
int main()
{
ListaDwukierunkowa lista;
lista.dodaj_elem(1);
lista.wyswietl_elem();
// dodaj_elem(&wsk, 12);
// dodaj_elem(&wsk, 9);
// dodaj_elem(&wsk, 8);
// dodaj_elem(&wsk, 9);
// dodaj_elem(&wsk, 9);
// wyswietl_elem(wsk);
// cout << liczba_elem(wsk) << endl;
// cout << liczba_elem_o_war(wsk, 9) << endl;
// cout << boolalpha << czy_zawiera(wsk, 12) << endl;
// usun_elem(&wsk, 1);
// wyswietl_elem(wsk);
// zwolnij_liste(&wsk);
}
error: request for member 'poprz' in '*((ListaDwukierunkowa*)this)->ListaDwukierunkowa::lista', which is of pointer type 'elem*' (maybe you meant to use '->' ?)
cout << "poprz: " << lista->poprz << " | ten: " << lista << " | war:" << lista->wartosc << " | nast:" << lista->nast << endl;
enter image description here
image of errors,
lista is of type elem**: a pointer to a pointer.
The -> operator defererences one level of pointers. So lista->poprz is the same as (*lista).poprz.
In this case you need to dereference twice: (*lista)->poprz.
Note: I have not tried to understand why lista is a pointer to a pointer. It could be that you simply want elem* instead.
I am working on my RPN calculator project.
At this moment I would like to put all the numbers from an entered calculation on stack, but unfortunately the process finishes with exit code 11 and I am stuck. It extacly happens when I type for example 3, and then 3+3.
I debugged it and I can see that the problem is in the line:
if(header!= nullptr) {
in here:
cell* List::Last() {
if(header!= nullptr) {
cell *i;
for(i = header; i->next != nullptr; i = i->next);
return i;
}
else return nullptr;
}
Exception EXC_BAD_ACCESS. I suppose it has something to do with my header in the list, but I have no idea why. I have read about this exception but it still doesn't help me. As far as I can see, most people get it with arrays. I would be very grateful for any hints... My full code is here (it is not finished yet, so it might seem that there is a lot of useless methods):
#include <iostream>
using namespace std;
typedef int element;
struct cell
{
element element;
cell * next;
};
class List {
protected:
cell * header;
public:
List();
~List();
void Insert(element x, cell * p);
void Delete(cell * p);
element Retrieve(cell * p);
cell * Locate(element x);
cell * First();
cell * Next(cell * p);
cell * Previous(cell * p);
cell * Last();
void print();
bool empty() {
return header == nullptr;
}
};
List::List() {
header = nullptr;
}
List::~List() {
cell *tmp;
while(header != nullptr) {
tmp = header;
header = header->next;
delete tmp;
}
}
void List::Insert(element x, cell * p) {
cell *tmp;
if (p != nullptr) {
tmp = p->next;
p->next = new cell;
p->next->element = x;
p->next->next = tmp;
} else {
header = new cell;
header->element = x;
header->next = nullptr;
}
}
element List::Retrieve(cell * p) {
return p->element;
}
void List::Delete(cell *p) {
cell* tmp;
tmp=p->next;
p->next = p->next->next;
delete tmp;
}
void List::print() {
for (cell *i = header; i != nullptr; i = i->next) {
cout << i->element << endl;
}
}
cell* List::Locate(element x) {
cell* tmp;
tmp = header;
while(tmp->next != nullptr) {
if(tmp->next->element == x) return tmp;
tmp = tmp->next;
}
return tmp;
}
cell* List::First() {
return header;
}
cell* List::Last() {
if(header!= nullptr) {
cell *i;
for(i = header; i->next != nullptr; i = i->next);
return i;
}
else return nullptr;
}
cell* List::Next(cell *p) {
return p->next;
}
cell* List::Previous(cell *p) {
cell* tmp;
tmp = header;
while(tmp->next != p) tmp = tmp->next;
return tmp;
}
class Stack {
List * list;
public:
Stack();
element top();
element pop();
void push(element x);
bool empty();
void makenull();
};
Stack::Stack() {
list = new List();
}
element Stack::top() {
return this->list->Retrieve(this->list->Last());
}
element Stack::pop() {
int tmp = this->list->Retrieve(this->list->Last());
cell* c = this->list->Previous(list->Last());
this->list->Delete(c);
return tmp;
}
void Stack::push(element x) {
this->list->Insert(x, this->list->Last());
}
bool Stack::empty() {
return this->list->empty();
}
void Stack::makenull() {
delete list;
}
class RPN {
Stack *stack;
public:
RPN();
int type;
int result;
char calculation[];
int menu();
int calculate();
bool isNum(char c);
bool isOperand(char c);
void putOnStack();
void enterCalculation();
void convertToRPN();
void convertFromRPN();
};
RPN::RPN() {
stack = new Stack();
type = 0;
result = 0;
}
int RPN::menu() {
cout << endl << "Choose option:" << endl;
cout << "1. Convert to RPN" << endl;
cout << "2. Convert from RPN" << endl;
cout << "3. RPN calculator" << endl;
cout << "4. Regular calculator" << endl;
cout << "5. Finish" << endl;
cin >> type;
switch (type) {
case 1:
enterCalculation();
convertFromRPN();
break;
case 2:
enterCalculation();
convertToRPN();
break;
case 3:
enterCalculation();
convertFromRPN();
calculate();
break;
case 4:
enterCalculation();
calculate();
break;
case 5:
break;
default:
cout << "Nie ma takiej opcji!" << endl;
break;
}
return type;
}
bool RPN::isNum(char c) {
return isdigit(c);
}
bool RPN::isOperand(char c) {
return c == '+' || c == '-' || c == '/' || c == '*';
}
void RPN::putOnStack() {
for(int i = 0; i < strlen(calculation); i++)
if(isNum(calculation[i])) stack->push(calculation[i]);
}
void RPN::enterCalculation() {
cout << "Type your calculation:" << endl;
cin >> calculation;
putOnStack();
}
void RPN::convertToRPN() {
}
void RPN::convertFromRPN() {
}
int RPN::calculate() {
return result;
}
int main() {
auto *rpn = new RPN();
while(rpn->type != 5)
rpn->menu();
return 0;
}
try this:
cell* List::Last() {
cell *i = header;
while(i->next != nullptr && i != nullptr)i=i->next;
return i;
}
Thank you for all your help. I managed to cope with that problem.
Maybe this will help somebody, I post my code below.
#include <iostream>
using namespace std;
typedef string element;
struct cell {
string element;
cell * next;
};
class List {
public:
cell * header;
List();
~List();
element Retrieve(cell * p);
cell* Previous(cell * p);
cell * Last();
void Insert(const element &x, cell * p);
void Delete(cell * p);
bool empty() {
return header == nullptr;
}
};
List::List() {
header = nullptr;
}
List::~List() {
cell *tmp;
while(header != nullptr) {
tmp = header;
header = header->next;
delete tmp;
}
}
element List::Retrieve(cell * p) {
return p->element;
}
void List::Delete(cell *p) {
cell* tmp;
if(p->next == nullptr) {
cout << "Error! No next element to delete!" << endl;
} else if(p->next->next == nullptr) {
delete p->next;
p->next = nullptr;
} else {
tmp = p->next;
p->next = p->next->next;
delete tmp;
}
}
cell* List::Previous(cell *p) {
if(p == header) {
cout << "There is no previous element for the header!" << endl;
return nullptr;
} else {
if(header -> next == p) {
return header;
} else {
cell *tmp = header;
while(tmp->next != p) tmp = tmp->next;
return tmp;
}
}
}
void List::Insert(const element &x, cell * p) {
cell *tmp;
if (p != nullptr) {
if(p -> next == nullptr) {
p->next = new cell;
p->next->element = x;
p->next->next = nullptr;
} else {
tmp = p->next;
p->next = new cell;
p->next->element = x;
p->next->next = tmp;
}
} else {
header = new cell;
header->element = x;
header->next = nullptr;
}
}
cell* List::Last() {
if(header != nullptr) {
if(header-> next == nullptr) return header;
else {
cell *i;
for(i = header; i->next != nullptr; i = i->next);
return i;
}
}
else return nullptr;
}
class Stack {
public:
List * list;
Stack();
void push(const element &x);
void makenull();
bool empty();
element pop();
element top();
};
Stack::Stack() {
list = new List();
}
void Stack::makenull() {
this->list->header = nullptr;
}
bool Stack::empty() {
return this->list->empty();
}
element Stack::top() {
return this->list->Retrieve(this->list->Last());
}
element Stack::pop() {
string tmp;
if(list->Last() == list->header) {
tmp = this->list->header->element;
this->list->header = nullptr;
} else {
tmp = this->list->Retrieve(this->list->Last());
cell* c = this->list->Previous(list->Last());
this->list->Delete(c);
}
return tmp;
}
void Stack::push(const element &x) {
if(this->list->header != nullptr) {
this->list->Insert(x, this->list->Last());
} else {
this->list->header = new cell;
this->list->header->element = x;
this->list->header->next = nullptr;
}
}
class RPN {
public:
RPN();
~RPN();
Stack *stack;
int type;
string calculation;
int menu();
string convertToRPN();
string convertFromRPN();
double calculateRPN();
void enterCalculation();
bool isNum(string s);
bool isOperator(char c);
int compareOperators(string op1, string op2);
};
RPN::RPN() {
stack = new Stack();
type = 0;
}
RPN::~RPN() {
delete stack;
}
int RPN::menu() {
cout << endl << "Choose option:" << endl;
cout << "1. Convert from RPN" << endl;
cout << "2. Convert to RPN" << endl;
cout << "3. RPN calculator" << endl;
cout << "4. Calculator" << endl;
cout << "5. Exit" << endl;
cin >> type;
cin.ignore();
switch (type) {
case 1:
enterCalculation();
convertFromRPN();
break;
case 2:
enterCalculation();
convertToRPN();
break;
case 3:
enterCalculation();
calculateRPN();
break;
case 4:
enterCalculation();
calculation = convertToRPN();
calculateRPN();
break;
case 5:
break;
default:
cout << "No such option! Choose a correct one." << endl;
break;
}
return type;
}
bool RPN::isNum(string s) {
return !s.empty() && std::find_if(s.begin(), s.end(), [](char c) { return !isdigit(c); }) == s.end();
}
bool RPN::isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int RPN::compareOperators(string op1, string op2) {
if ((op1[0] == '*' || op1[0] == '/') && (op2[0] == '+' || op2[0] == '-')) return -1;
else if ((op1[0] == '+' || op1[0] == '-') && (op2[0] == '*' || op2[0] == '/')) return 1;
return 0;
}
void RPN::enterCalculation() {
calculation[0] = 0;
cout << "Input expression:" << endl;
getline(cin, calculation);
}
string RPN::convertToRPN() {
auto len = static_cast<int>(calculation.length());
stack->makenull();
string RPNcalc;
RPNcalc.clear();
int j = 0;
int par1 = 0;
int par2 = 0;
while(isspace(calculation[j])) j++;
if(!isNum(string(1, calculation[j])) && (calculation[j] != '(')) {
cout << "Expression incorrect!" << endl;
string errStr = "err";
return errStr;
}
j = 0;
while(isspace(calculation[len-1-j])) j++;
if(!isNum(string(1, calculation[len-1-j])) && (calculation[len-1-j] != ')')) {
cout << "Expression incorrect!" << endl;
string errStr = "err";
return errStr;
}
for (int i = 0; i < len; i++) {
if (isNum(string(1, calculation[i])) || calculation[i] == '.') {
if(isspace(calculation[i+1])) {
RPNcalc.push_back(calculation[i]);
RPNcalc.push_back(' ');
} else {
if(isNum(string(1, calculation[i])) && calculation[i+1] != '.' && !isNum(string(1, calculation[i]))) {
RPNcalc.push_back(calculation[i]);
RPNcalc.push_back(' ');
} else if(isOperator(calculation[i+1])) {
RPNcalc.push_back(calculation[i]);
RPNcalc.push_back(' ');
} else if(calculation[i+1] == ')') {
RPNcalc.push_back(calculation[i]);
RPNcalc.push_back(' ');
} else {
RPNcalc.push_back(calculation[i]);
}
}
} else if (isOperator(calculation[i])) {
while(!stack->empty() && stack->top()[0] != '(' && compareOperators(stack->top(), string(1, calculation[i])) <= 0) {
RPNcalc.push_back(stack->pop()[0]);
RPNcalc.push_back(' ');
}
stack->push(string(1, calculation[i]));
} else if(calculation[i] == '(') {
par1++;
stack->push(string(1, calculation[i]));
} else if(calculation[i] == ')') {
par2++;
while(!stack->empty()) {
if(stack->top()[0] == '(') {
stack->pop();
break;
}
RPNcalc.push_back(stack->pop()[0]);
RPNcalc.push_back(' ');
}
} else if(isspace(calculation[i])) {
} else {
cout << "Expression incorrect!" << endl;
string errStr = "err";
return errStr;
}
}
if(par1 != par2) {
cout << "Expression incorrect!" << endl;
string errStr = "err";
return errStr;
}
while(!stack->empty()) {
if(RPNcalc.back() != ' ') RPNcalc.push_back(' ');
RPNcalc.push_back(stack->top()[0]);
stack->pop();
}
if(type != 4) cout << "RPN expression: " << RPNcalc << endl;
return RPNcalc;
}
string RPN::convertFromRPN() {
auto len = static_cast<int>(calculation.length());
stack->makenull();
string num;
string right;
string left;
string newExpr;
int j = 0;
while(isspace(calculation[j])) j++;
if(!isNum(string(1, calculation[j]))) {
cout << "Expression incorrect!" << endl;
string errStr = "err";
return errStr;
}
j = 0;
while(isspace(calculation[len-1-j])) j++;
if(!isOperator(calculation[len-1-j])) {
cout << "Expression incorrect!" << endl;
string errStr = "err";
return errStr;
}
for (int i = 0; i < len; i++) {
if (isOperator(calculation[i])) {
right = stack->pop();
left = stack->pop();
newExpr.clear();
newExpr += '(';
newExpr += left;
newExpr += ' ';
newExpr += calculation[i];
newExpr += ' ';
newExpr += right;
newExpr += ')';
stack->push(newExpr);
} else if (isNum(string(1, calculation[i])) || calculation[i] == '.') {
if(isspace(calculation[i+1])) {
num.push_back(calculation[i]);
stack->push(num);
num.clear();
} else num.push_back(calculation[i]);
} else if(isspace(calculation[i])) {
} else {
cout << "Expression incorrect!" << endl;
string errStr = "err";
return errStr;
}
}
newExpr = stack->top();
newExpr.erase(newExpr.begin());
newExpr.erase(newExpr.end()-1);
cout << "Infix notation: " << newExpr;
return newExpr;
}
double RPN::calculateRPN() {
auto len = static_cast<int>(calculation.length());
stack->makenull();
double a = 0;
double b = 0;
double result = 0;
string num;
num.clear();
if(isNum(string(1, calculation[len-1]))) {
cout << "Expression incorrect!" << endl;
return 0;
}
for (int i = 0; i < len; i++) {
if (isNum(string(1, calculation[i])) || calculation[i] == '.') {
if(isspace(calculation[i+1])) {
num.push_back(calculation[i]);
stack->push(num);
num.clear();
} else num.push_back(calculation[i]);
} else if (isOperator(calculation[i])) {
a = stod(stack->pop());
b = stod(stack->pop());
switch (calculation[i]) {
case '*':
stack->push(to_string(a * b));
break;
case '/':
stack->push(to_string(b / a));
break;
case '-':
stack->push(to_string(b - a));
break;
case '+':
stack->push(to_string(a + b));
break;
default:
break;
}
} else if(isspace(calculation[i])) {
} else {
cout << "Expression incorrect!" << endl;
return 0;
}
}
result = stod(stack->pop());
if(!stack->empty()) {
cout << "Expression incorrect!" << endl;
return 0;
}
cout << "Result: " << result << endl;
return result;
}
int main() {
cout << endl << "POSTFIX-INFIX CALCULATOR AND CONVERTER "<< endl;
auto *rpn = new RPN();
while(rpn->type != 5)
rpn->menu();
return 0;
}
This is my code so far:
Its a 2-3 tree implementation
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
// TwoThreeNode class
class TwoThreeNode {
private:
// Gets the value of the smallest data item in the subtree
// rooted by this node
int getSmallest() {
TwoThreeNode *node = this;
while (!node->isLeaf()) node = node->child[0];
return node->key[0];
}
// Insert into a node with 1 child
void insert1Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
newChild->parent = this;
if (newKey < child[0]->key[0]) {
// newNode is inserted as first child of root
child[1] = child[0];
child[0] = newChild;
key[0] = child[1]->getSmallest();
}
else {
// newNode is iserted as second child of root
child[1] = newChild;
key[0] = newSmallest;
}
}
// Insert into a node with 2 children
void insert2Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
newChild->parent = this;
if (newKey < child[0]->key[0]) {
child[2] = child[1];
child[1] = child[0];
child[0] = newChild;
key[1] = key[0];
key[0] = child[1]->getSmallest();
updateParentSmallest(newSmallest);
}
else if (newKey < child[1]->key[0]) {
child[2] = child[1];
child[1] = newChild;
key[1] = key[0];
key[0] = newSmallest;
}
else {
child[2] = newChild;
key[1] = newSmallest;
}
}
// Insert into a node with 3 children
void insert3Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
int splitSmallest = -1;
TwoThreeNode *splitNode = new TwoThreeNode();
splitNode->parent = parent;
if (newKey < child[0]->key[0] || newKey < child[1]->key[0]) {
// newChild is inserted in current node
splitSmallest = key[0];
splitNode->child[0] = child[1];
splitNode->child[1] = child[2];
splitNode->key[0] = key[1];
child[1]->parent = splitNode;
child[2]->parent = splitNode;
newChild->parent = this;
if (newKey < child[0]->key[0]) {
// newChild is inserted as first child
child[1] = child[0];
child[0] = newChild;
key[0] = child[1]->getSmallest();
updateParentSmallest(newSmallest);
}
else {
// newChild is inserted as second child
child[1] = newChild;
key[0] = newSmallest;
}
}
else {
// newChild is inserted in split node
child[2]->parent = splitNode;
newChild->parent = splitNode;
if (newKey < child[2]->key[0]) {
// newChild is inserted as first child
splitSmallest = newSmallest;
splitNode->child[0] = newChild;
splitNode->child[1] = child[2];
splitNode->key[0] = key[1];
}
else {
// newChild is inserted as second child
splitSmallest = key[1];
splitNode->child[0] = child[2];
splitNode->child[1] = newChild;
splitNode->key[0] = newSmallest;
}
}
child[2] = NULL;
key[1] = -1;
if (parent->parent == NULL) {
// At root, so new root needs to be created
TwoThreeNode *newNode = new TwoThreeNode();
parent->child[0] = newNode;
newNode->parent = parent;
newNode->child[0] = this;
parent = newNode;
}
parent->insert(splitNode, splitSmallest);
}
// Update the parent nods efor the smallest child value
void updateParentSmallest(int data) {
switch (sibNumber()) {
case 0: if (parent->parent != NULL) parent->updateParentSmallest(data); break;
case 1: parent->key[0] = data; break;
case 2: parent->key[1] = data; break;
}
}
public:
int key[2];
TwoThreeNode *parent, *child[3];
// Constructor
TwoThreeNode(int data = -1) {
key[0] = data;
key[1] = -1;
parent = child[0] = child[1] = child[2] = NULL;
}
// Check if node is a leaf
bool isLeaf() {
return (child[0] == NULL);
}
// Get which sibling the node is
int sibNumber() {
for (int i = 0; i < 3; ++i) {
if (this == parent->child[i]) return i;
}
return -1;
}
// Insertion
void insert(TwoThreeNode *newChild, int newSmallest) {
if (child[1] == NULL) insert1Siblings(newChild, newSmallest);
else if (child[2] == NULL) insert2Siblings(newChild, newSmallest);
else insert3Siblings(newChild, newSmallest);
}
};
// TwoThreeTree class
class TwoThreeTree {
private:
TwoThreeNode *root;
// Find the appropriate operation point
TwoThreeNode* findSpot(TwoThreeNode *node, int data) {
if (node == NULL) return NULL;
while (!node->isLeaf()) {
if (node->key[0] == data || node->key[1] == data)
return NULL;
if (node->key[0] == -1 || data < node->key[0])
node = node->child[0];
else if (node->key[1] == -1 || data < node->key[1])
node = node->child[1];
else
node = node->child[2];
}
if (node->key[0] == data) return NULL;
return node->parent;
}
// Recursively print the subtree starting from the given node
void print(TwoThreeNode *node, int tabs = 0) {
for (int i = 0; i < tabs; ++i) {
cout << "\t";
}
if (node == NULL) {
cout << "`--> NULL" << endl;
return;
}
cout << "`--> " << node->sibNumber()
<< ": ( " << node->key[0] << ", " << node->key[1] << ")" << endl;
if (!node->isLeaf()) {
++tabs;
print(node->child[0], tabs);
print(node->child[1], tabs);
print(node->child[2], tabs);
}
}
public:
// Constructor
TwoThreeTree() {
root = new TwoThreeNode();
root->child[0] = new TwoThreeNode();
root->child[0]->parent = root;
}
// Insert
bool insert(int data) {
TwoThreeNode *newNode = new TwoThreeNode(data);
TwoThreeNode *spot = root->child[0];
if (spot->child[0] == NULL) {
// First insertion
newNode->parent = spot;
spot->child[0] = newNode;
}
else {
spot = findSpot(spot, data);
if (spot == NULL) return false;
spot->insert(new TwoThreeNode(data), data);
}
return true;
}
// Print
void print() {
print(root->child[0]);
cout << endl;
}
};
// Main function
int main(int argc, char **argv) {
if (argc <= 1) {
cout << argv[0] << ": too few arguments" << endl;
cout << "Usage: " << argv[0] << " filename" << endl;
exit(1);
}
// Open file
ifstream infile;
infile.open(argv[1]);
// Create tree and insert data
TwoThreeTree ttTree;
int x;
while (infile.good()) {
infile >> x;
if (!infile.eof()) {
cout << x << endl;
ttTree.insert(x);
ttTree.print();
}
}
infile.close();
return 0;
}
So I am able to insert numbers into my tree using a text file but now I need a function to help me search for a node Any pointers on how I can get started?
Really would appreciate the help, thanks!
Looks like you're pretty close. If you know how a linked list is searched, it is a similar process.
List:
while (this_node.value != search_term)
this_node = this_node.next;
A 2-3 tree is a bit more complex:
while (this_node.value != search_term)
pick_which_node_is_next();
It looks from your code like you can handle this, no problem.
I have written a code to create and display a binary tree. This program worked and satisfied the requirements of the course I was on, however afterwards I attempted to add to the working program to find the height of the tree and so far with no luck. I have looked at many examples and none of the additions or changes I have made to my program made it work.
#include <cstdlib>
#include <iostream>
#include <cstdio>
using namespace std;
class TreeNode
{ public:
TreeNode *pLeft;
int data;
TreeNode *pRight;
TreeNode *leftHeight;
TreeNode *rightHeight;
TreeNode(int num)
{
data = num;
pLeft = NULL;
pRight = NULL;
}
}; ////////////////////////////////////////////
class Binary_Tree
{ private:
//int getHeight;
public:
TreeNode *rootPtr;
Binary_Tree()
{ rootPtr = NULL;
}
/* ---------------------------------- */
void Insert(int value)
{
TreeNode *pNewNode = new TreeNode(value);
TreeNode *pCurrent;
TreeNode *pPrevious;
pPrevious = NULL;
pCurrent = rootPtr;
while(pCurrent != NULL)
{ pPrevious = pCurrent;
if (value < (pPrevious -> data))
pCurrent = pCurrent -> pLeft;
else
pCurrent = pCurrent -> pRight;
}
if(pPrevious == NULL)
{ rootPtr = pNewNode;
}
else
{ if (value < (pPrevious -> data))
{ pPrevious -> pLeft = pNewNode;
pNewNode -> pLeft = pCurrent;
}
else
{ pPrevious -> pRight = pNewNode;
pNewNode -> pRight = pCurrent;
}
}
}
/* ------------------------------------------- */
int getHeight(TreeNode *TreePtr)
{
if( TreePtr == NULL)
{
return(0);
}
else
{
leftHeight = getHeight(r->pLeft);
rightHeight = getHeight(r->pRight);
if(leftHeight > rightHeight)
{
return(leftHeight + 1);
}
else
{
return(rightHeight + 1);
}
}
}
/* ------------------------------------------- */
void Display(TreeNode *TreePtr, int count)
{
if(TreePtr !=NULL)
{
count++;
Display(TreePtr -> pRight, count);
for(int i = i; i < count; i++)
{
printf(" ");
}
printf("%d \n", TreePtr -> data);
Display(TreePtr -> pLeft, count);
}
}//////////////////////end display //////////////////////
void DisplayInOrder(TreeNode *TreePtr)
{
if(TreePtr != NULL)
{
DisplayInOrder(TreePtr -> pLeft);
cout << TreePtr -> data << endl;
DisplayInOrder(TreePtr -> pRight);
}
}
};
/* -------------------------------------- */
/////////////////////////////////////////////////////////////
int main()
{ int number[8] = {7, 9, 3, 2, 12, 5, 8};
Binary_Tree Tree;
for (int i = 0; i <8; i++)
{
cout << "data inserted = "<< number[i] << endl;
Tree.Insert(number[i]);
}
cout << endl << "Display Tree" << endl << endl;
Tree.Display(Tree.rootPtr, 0);
cout << endl;
cout << endl << "tree height" << endl << endl;
getHeight(root);
cout << endl;
Tree.DisplayInOrder(Tree.rootPtr);
system("PAUSE");
return EXIT_SUCCESS;
}
There were compilation problems. After fixing them it seems to print the height. Note the "final height" line with the value 4
#include <cstdlib>
#include <iostream>
#include <cstdio>
using namespace std;
class TreeNode
{ public:
TreeNode *pLeft;
int data;
TreeNode *pRight;
TreeNode *leftHeight;
TreeNode *rightHeight;
TreeNode(int num)
{
data = num;
pLeft = NULL;
pRight = NULL;
}
}; ////////////////////////////////////////////
class Binary_Tree
{ private:
//int getHeight;
public:
TreeNode *rootPtr;
Binary_Tree()
{ rootPtr = NULL;
}
/* ---------------------------------- */
void Insert(int value)
{
TreeNode *pNewNode = new TreeNode(value);
TreeNode *pCurrent;
TreeNode *pPrevious;
pPrevious = NULL;
pCurrent = rootPtr;
while(pCurrent != NULL)
{ pPrevious = pCurrent;
if (value < (pPrevious -> data))
pCurrent = pCurrent -> pLeft;
else
pCurrent = pCurrent -> pRight;
}
if(pPrevious == NULL)
{ rootPtr = pNewNode;
}
else
{ if (value < (pPrevious -> data))
{ pPrevious -> pLeft = pNewNode;
pNewNode -> pLeft = pCurrent;
}
else
{ pPrevious -> pRight = pNewNode;
pNewNode -> pRight = pCurrent;
}
}
}
/* ------------------------------------------- */
int getHeight(TreeNode *TreePtr)
{
if( TreePtr == NULL)
{
return(0);
}
else
{
int leftHeight = getHeight(TreePtr->pLeft);
int rightHeight = getHeight(TreePtr->pRight);
if(leftHeight > rightHeight)
{
return(leftHeight + 1);
}
else
{
return(rightHeight + 1);
}
}
}
/* ------------------------------------------- */
void Display(TreeNode *TreePtr, int count)
{
if(TreePtr !=NULL)
{
count++;
Display(TreePtr -> pRight, count);
for(int i = i; i < count; i++)
{
printf(" ");
}
printf("%d \n", TreePtr -> data);
Display(TreePtr -> pLeft, count);
}
}//////////////////////end display //////////////////////
void DisplayInOrder(TreeNode *TreePtr)
{
if(TreePtr != NULL)
{
DisplayInOrder(TreePtr -> pLeft);
cout << TreePtr -> data << endl;
DisplayInOrder(TreePtr -> pRight);
}
}
};
/* -------------------------------------- */
/////////////////////////////////////////////////////////////
int main()
{ int number[8] = {7, 9, 3, 2, 12, 5, 8};
Binary_Tree Tree;
for (int i = 0; i <8; i++)
{
cout << "data inserted = "<< number[i] << endl;
Tree.Insert(number[i]);
}
cout << endl << "Display Tree" << endl << endl;
Tree.Display(Tree.rootPtr, 0);
cout << endl;
cout << endl << "tree height" << endl << endl;
int height = Tree.getHeight(Tree.rootPtr);
cout << endl;
cout << "final height: " << height << endl;
Tree.DisplayInOrder(Tree.rootPtr);
system("PAUSE");
return EXIT_SUCCESS;
}
you have some errors in your code:
you are using r where it should be TreePtr
you did not declared root, i guess u wanted to use Tree
leftHeight and rightHeight needed to be declared
the declaration of getHeight is inside the class so you need to call Tree.getHeight. btw, this method should not be in the class or should not get a tree node.
here is the fixed code:
#include <cstdlib>
#include <iostream>
#include <cstdio>
using namespace std;
class TreeNode
{ public:
TreeNode *pLeft;
int data;
TreeNode *pRight;
TreeNode *leftHeight;
TreeNode *rightHeight;
TreeNode(int num)
{
data = num;
pLeft = NULL;
pRight = NULL;
}
}; ////////////////////////////////////////////
class Binary_Tree
{ private:
//int getHeight;
public:
TreeNode *rootPtr;
Binary_Tree()
{ rootPtr = NULL;
}
/* ---------------------------------- */
void Insert(int value)
{
TreeNode *pNewNode = new TreeNode(value);
TreeNode *pCurrent;
TreeNode *pPrevious;
pPrevious = NULL;
pCurrent = rootPtr;
while(pCurrent != NULL)
{ pPrevious = pCurrent;
if (value < (pPrevious -> data))
pCurrent = pCurrent -> pLeft;
else
pCurrent = pCurrent -> pRight;
}
if(pPrevious == NULL)
{ rootPtr = pNewNode;
}
else
{ if (value < (pPrevious -> data))
{ pPrevious -> pLeft = pNewNode;
pNewNode -> pLeft = pCurrent;
}
else
{ pPrevious -> pRight = pNewNode;
pNewNode -> pRight = pCurrent;
}
}
}
/* ------------------------------------------- */
int getHeight(TreeNode *TreePtr)
{
if( TreePtr == NULL)
{
return(0);
}
else
{
int leftHeight = getHeight(TreePtr->pLeft);
int rightHeight = getHeight(TreePtr->pRight);
if(leftHeight > rightHeight)
{
return(leftHeight + 1);
}
else
{
return(rightHeight + 1);
}
}
}
/* ------------------------------------------- */
void Display(TreeNode *TreePtr, int count)
{
if(TreePtr !=NULL)
{
count++;
Display(TreePtr -> pRight, count);
for(int i = i; i < count; i++)
{
printf(" ");
}
printf("%d \n", TreePtr -> data);
Display(TreePtr -> pLeft, count);
}
}//////////////////////end display //////////////////////
void DisplayInOrder(TreeNode *TreePtr)
{
if(TreePtr != NULL)
{
DisplayInOrder(TreePtr -> pLeft);
cout << TreePtr -> data << endl;
DisplayInOrder(TreePtr -> pRight);
}
}
};
/* -------------------------------------- */
/////////////////////////////////////////////////////////////
int main()
{ int number[8] = {7, 9, 3, 2, 12, 5, 8};
Binary_Tree Tree;
for (int i = 0; i <8; i++)
{
cout << "data inserted = "<< number[i] << endl;
Tree.Insert(number[i]);
}
cout << endl << "Display Tree" << endl << endl;
Tree.Display(Tree.rootPtr, 0);
cout << endl;
cout << endl << "tree height" << endl << endl;
cout << Tree.getHeight(Tree.rootPtr) << endl;
cout << endl;
Tree.DisplayInOrder(Tree.rootPtr);
system("PAUSE");
return EXIT_SUCCESS;
}