From 4f38679129cc243a3db811e041009e9afd8f70b9 Mon Sep 17 00:00:00 2001 From: jonas Date: Fri, 10 Feb 2017 10:13:29 +0100 Subject: Added Self-Sorting mechanism for optimizing tree +cTree::sort() --- tree/src/cTree.cpp | 13 +++++++++++-- tree/src/cTree.h | 1 + tree/src/main.cpp | 36 ++++++------------------------------ 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/tree/src/cTree.cpp b/tree/src/cTree.cpp index c342f34..32c7179 100644 --- a/tree/src/cTree.cpp +++ b/tree/src/cTree.cpp @@ -33,12 +33,13 @@ void cTree::remove(cData* _data) list dataList; //List of Data that has to be re-inserted into the tree root->remove(_data, &dataList, &root); + insertList(&dataList); ///TODO: Optimize!!! - while(!dataList.empty()) + /*while(!dataList.empty()) { insert(dataList.front().clone()); dataList.pop_front(); - } + }*/ } void cTree::getList(list* _list) @@ -94,6 +95,14 @@ void cTree::insertList(list* _list) insertList(&tmpList); } +void cTree::sort() +{ + list sortetList; + root->getSortet(&sortetList); + clear(); + insertList(&sortetList); +} + diff --git a/tree/src/cTree.h b/tree/src/cTree.h index 40fda77..cff7dcd 100644 --- a/tree/src/cTree.h +++ b/tree/src/cTree.h @@ -24,6 +24,7 @@ public: void balance(); void getList(list*); void remove(cData*); + void sort(); void clear(); cData* search(string); diff --git a/tree/src/main.cpp b/tree/src/main.cpp index 1fe9fc3..0b9299a 100644 --- a/tree/src/main.cpp +++ b/tree/src/main.cpp @@ -11,43 +11,19 @@ int main (void) { cTree* a = new cTree(); - a->insert("z"); - a->insert("f"); - a->insert("a"); - a->insert("n"); - a->insert("p"); - a->insert("k"); - a->draw(); - cout << "-------------------" << endl; - list list, list2; - a->getList(&list); - - list2 = list; - - unsigned int asdf = list2.size(); - for(unsigned int i = 0; i < asdf;i++) + for (char i = '1'; i <= 'z'; i ++) { - cout << list2.front().getData() << ", "; - list2.pop_front(); + string s(&i); + a->insert(&s[0]); } - cout << endl; - a->clear(); - a->insertList(&list); + a->draw(); + cout << "-------------------" << endl; + a->sort(); a->draw(); delete a; - /*while(1) - { - a->insert("h"); - a->insert("y"); - a->insert("k"); - a->insert("z"); - a->remove(a->search("h")); - a->remove(a->search("k")); - a->clear(); - }*/ return 0; } -- cgit v1.2.3