Finished assignment no. 5

main
Isabell Pflug 3 years ago
parent 69537f1367
commit 03773a6e56

@ -0,0 +1,24 @@
#include <string>
#include <iostream>
#include "BinarySearchTree.h"
using namespace std;
int main() {
BinarySearchTree b;
string wants_more_input = "y";
string t_string;
while (wants_more_input == "y") {
cout << "Willst du eine (weitere) Zeichenkette hinzufügen? (y/n)";
cin >> wants_more_input;
if (wants_more_input == "y") {
cout << "Eingabe: ";
cin >> t_string;
b.insert(t_string);
cout << endl;
} else
cout << endl;
}
b.list();
return 0;
}

@ -0,0 +1,71 @@
#include "BinarySearchTree.h"
using namespace std;
Node * walk_tree(string key, Node *n) {
if (n->get_key() == key)
return n;
if (n->get_key() > key) {
if (n->get_left_child() != NULL)
return walk_tree(key, n->get_left_child());
return n;
}
if (n->get_right_child() != NULL)
return walk_tree(key, n->get_right_child());
return n;
}
Node * BinarySearchTree::search(string key) {
if (this->root == NULL)
return NULL;
Node * t_node = walk_tree(key, this->root);
if (t_node->get_key() != key)
return NULL;
else
return t_node;
}
int BinarySearchTree::insert(string key) {
if (this->root == NULL) {
Node * new_node = new Node(key);
this->root = new_node;
return 0;
}
if (BinarySearchTree::search(key) != NULL)
return 0;
Node * t_node = walk_tree(key, this->root);
t_node->insert(key);
return 0;
}
void inOrderTreePrint(Node *n) {
if (n != NULL) {
inOrderTreePrint(n->get_left_child());
cout << n->get_key() << ", ";
inOrderTreePrint(n->get_right_child());
}
}
void BinarySearchTree::list() {
cout << "All keys in ascending order:" << endl;
inOrderTreePrint(this->root);
cout << "(end)" << endl;
// mit der inOrderTreePrint Methode fällt mir gerade kein
// eleganter Weg ein, um das letzte Komma weglassen zu können.
// also füge ich das "(end)" am Ende hinzu, damit es zumindest nicht
// so unvollständig aussieht.
}
void delete_rec(Node * n) {
if (n->get_left_child() != NULL)
delete_rec(n->get_left_child());
if (n->get_right_child() != NULL)
delete_rec(n->get_right_child());
delete n;
}
BinarySearchTree::~BinarySearchTree() {
if (this->root == NULL)
return;
delete_rec(this->root);
}

@ -0,0 +1,15 @@
#include <string>
#include <iostream>
#include <limits>
#include "Node.h"
class BinarySearchTree {
public:
~BinarySearchTree();
void list();
int insert(std::string key);
Node * search(std::string key);
private:
Node * root = NULL;
};

@ -0,0 +1,26 @@
#include "Node.h"
using namespace std;
void Node::insert(string key) {
Node * new_node = new Node(key);
if (this->key > key)
this->left_child = new_node;
else
this->right_child = new_node;
}
std::string Node::get_key() {
return this->key;
}
Node * Node::get_left_child() {
return this->left_child;
}
Node * Node::get_right_child() {
return this->right_child;
}
Node::Node(string key) {
this->key = key;
}

@ -0,0 +1,14 @@
#include <string>
class Node {
private:
std::string key = "";
Node * left_child = NULL;
Node * right_child = NULL;
public:
Node(std::string key);
std::string get_key();
Node * get_left_child();
Node * get_right_child();
void insert(std::string key);
};

@ -0,0 +1,41 @@
#include <vector>
#include <string>
#include <iostream>
#include "VectorSort.h"
using namespace std;
int main() {
vector<string> v;
string wants_more_input = "y";
string t_string;
while (wants_more_input == "y") {
cout << "Willst du eine (weitere) Zeichenkette hinzufügen? (y/n)";
cin >> wants_more_input;
if (wants_more_input == "y") {
cout << "Eingabe: ";
cin >> t_string;
v.push_back(t_string);
cout << endl;
} else
cout << endl;
}
if (v.size() == 0) {
cout << "Dann halt nicht..." << endl;
return 0;
}
VectorSort<string> v_sort(&v);
cout << "Vor der Sortierung: ";
v_sort.print();
v_sort.sort();
cout << endl << "Nach der Sortierung: ";
v_sort.print();
return 0;
}

@ -0,0 +1,48 @@
#include <vector>
#include <iostream>
template <typename T>
class VectorSort {
private:
std::vector<T *> v;
public:
VectorSort(std::vector<T> *v) {
for (long unsigned int i = 0; i < (*v).size(); i++) {
this->v.push_back(&(*v)[i]);
}
};
int sort() { // ist bubble sort
if (this->v.size() == 0)
return 0;
long unsigned int n = this->v.size();
bool is_sorted = false;
for (long unsigned int i = 0; i < n - 1; i++) {
is_sorted = true;
for (long unsigned int j = 0; j < n - i - 1; j++) {
if (*(this->v[j]) > *(this->v[j + 1])) {
std::swap(this->v[j], this->v[j + 1]);
is_sorted = false;
}
}
if (is_sorted) {
break;
}
}
return 0;
}
void print() {
if (this->v.size() == 0)
return;
std::cout << "{ ";
long unsigned int i = 0;
for (; i < (this->v.size())-1; i++) {
std::cout << *(this->v[i]) << ", ";
}
std::cout << *(this->v[i]) << " }" << std::endl;
}
};
Loading…
Cancel
Save