summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar jonas <himself@jonasgunz.de> 2017-01-30 18:56:29 +0100
committerGravatar jonas <himself@jonasgunz.de> 2017-01-30 18:56:29 +0100
commit033866129a330b386f5d4b972a55d59f240b5d26 (patch)
tree48c23f5446b196d677047c0af711a2fcd6c7bd8f
parentc55ef6a36cf8c8d862ce9a8aa4277b637c86c278 (diff)
downloadtree-033866129a330b386f5d4b972a55d59f240b5d26.tar.gz
misc
-rw-r--r--tree/src/cNode.cpp49
-rw-r--r--tree/src/cNode.h26
-rw-r--r--tree/src/cTree.cpp48
-rw-r--r--tree/src/cTree.h7
4 files changed, 105 insertions, 25 deletions
diff --git a/tree/src/cNode.cpp b/tree/src/cNode.cpp
index 0da4a34..b6542e6 100644
--- a/tree/src/cNode.cpp
+++ b/tree/src/cNode.cpp
@@ -31,19 +31,27 @@ cData cDatanode::getDataObject()
return *data;
}//getDataObject
-void cDatanode::insert(cData* _data, cNode* _me = NULL)
+void cDatanode::insert(cData* _data, cNode** _me)
{
if (*_data > *data)
//nextBigger->isEnd() ? (void)(nextBigger = new cDatanode(_data)) : nextBigger->insert(_data);
- nextBigger->insert(_data, nextBigger);
+ nextBigger->insert(_data, &nextBigger);
else
- nextSmaller->insert(_data, nextSmaller);
+ nextSmaller->insert(_data, &nextSmaller);
//nextSmaller->isEnd() ? (void)(nextSmaller = new cDatanode(_data)) : nextSmaller->insert(_data);
}//insert
-void cDatanode::remove(cData* _data)
+void cDatanode::remove(cData* _data, list<cData>* _list)
{
-
+ if(*_data == *data)
+ {
+ nextSmaller->getSortet(_list);
+ nextBigger->getSortet(_list);
+ }
+ else if (*_data > *data)
+ nextBigger->remove(_data, _list);
+ else if (*_data < *data)
+ nextSmaller->remove(_data, _list);
}//remove
cData* cDatanode::search(string _search)
@@ -62,12 +70,20 @@ bool cDatanode::isEnd()
return false;
}//isEnd
-list<cData>* cDatanode::getSortet(list<cData>* _list)
+void cDatanode::getSortet(list<cData>* _list)
{
nextSmaller->getSortet(_list);
_list->push_back(*data);
- return nextBigger->getSortet(_list);
-}//getSortet
+ nextBigger->getSortet(_list);
+}//getSorte
+
+void cDatanode::clear()
+{
+ nextSmaller->clear();
+ delete nextSmaller;
+ nextBigger->clear();
+ delete nextSmaller;
+}
//
//==============================================================================================================================
@@ -87,15 +103,15 @@ cData cEndnode::getDataObject()
return (cData)NULL;
}
-void cEndnode::insert(cData* _data, cNode* _me)
+void cEndnode::insert(cData* _data, cNode** _me)
{
- _me = _data;
+ *_me = new cDatanode(_data);
delete this;
}
-void cEndnode::remove(cData*)
+void cEndnode::remove(cData*, list<cData>*)
{
-
+ return;
}
cData* cEndnode::search(string)
@@ -103,12 +119,15 @@ cData* cEndnode::search(string)
return NULL;
}
-list<cData>* cEndnode::getSortet(list<cData>* _list)
+void cEndnode::getSortet(list<cData>* _list)
{
- return _list;
+ return;
}
-
+void cEndnode::clear()
+{
+ return;
+}
diff --git a/tree/src/cNode.h b/tree/src/cNode.h
index 26267aa..04a621d 100644
--- a/tree/src/cNode.h
+++ b/tree/src/cNode.h
@@ -22,11 +22,12 @@ public:
/*
* returns dataObject
*/
- virtual void insert(cData*, cNode* = NULL) = 0;
+ virtual void insert(cData*, cNode** = NULL) = 0;
/*
* Inserts *cData into tree
+ * cNode** required to change pointer in the caller's pointer to next element
*/
- virtual void remove(cData*) = 0;
+ virtual void remove(cData*, list<cData>*) = 0;
/*
* Removes *cData from tree
*/
@@ -38,12 +39,15 @@ public:
/*
* returns false
*/
- virtual list<cData>* getSortet(list<cData>* _list) = 0;
+ virtual void getSortet(list<cData>* _list) = 0;
+
+ virtual void clear() = 0;
};
class cDatanode:public cNode
{
+public:
cDatanode(cData* _data);
~cDatanode();
@@ -51,11 +55,11 @@ class cDatanode:public cNode
/*
* returns dataObject
*/
- void insert(cData*, cNode* = NULL);
+ void insert(cData*, cNode** = NULL);
/*
* Inserts *cData into tree
*/
- void remove(cData*);
+ void remove(cData*, list<cData>*);
/*
* Removes *cData from tree
*/
@@ -67,7 +71,9 @@ class cDatanode:public cNode
/*
* returns false
*/
- list<cData>* getSortet(list<cData>* _list);
+ void getSortet(list<cData>* _list);
+
+ void clear();
private:
cNode *nextSmaller, *nextBigger;
cData *data;
@@ -84,11 +90,11 @@ public:
/*
* returns dataObject
*/
- void insert(cData*, cNode* );
+ void insert(cData*, cNode** );
/*
* Inserts *cData into tree
*/
- void remove(cData*);
+ void remove(cData*, list<cData>*);
/*
* Removes *cData from tree
*/
@@ -100,7 +106,9 @@ public:
/*
* returns false
*/
- list<cData>* getSortet(list<cData>* _list);
+ void getSortet(list<cData>* _list);
+
+ void clear();
private:
};
diff --git a/tree/src/cTree.cpp b/tree/src/cTree.cpp
new file mode 100644
index 0000000..d09119b
--- /dev/null
+++ b/tree/src/cTree.cpp
@@ -0,0 +1,48 @@
+/*
+ * cTree.cpp
+ *
+ * Created on: Jan 30, 2017
+ * Author: jonas
+ */
+
+#include "cTree.h"
+
+cTree::cTree()
+{
+ root = new cEndnode();
+}
+
+cTree::~cTree()
+{
+ root->clear();
+ delete root;
+}
+
+void cTree::insert(cData* _data)
+{
+ root->insert(_data, &root);
+}
+
+void cTree::insert(string _data)
+{
+ cData* tmp = new cData(_data);
+ insert(tmp);
+}
+
+void cTree::remove(cData* _data)
+{
+ list<cData> dataList; //List of Data that ahs to be re-inserted into the tree
+ root->remove(_data, &dataList);
+
+ ///TODO: Optimize!!!
+ while(!dataList.empty())
+ {
+ insert(&dataList.front());
+ dataList.pop_front();
+ }
+}
+
+void cTree::getList(list<cData>* _list)
+{
+ root->getSortet(_list);
+}
diff --git a/tree/src/cTree.h b/tree/src/cTree.h
index a6f1b83..9806f94 100644
--- a/tree/src/cTree.h
+++ b/tree/src/cTree.h
@@ -10,19 +10,24 @@
#include "cNode.h"
#include <string>
+#include <list>
using namespace std;
class cTree{
public:
+ cTree();
+ ~cTree();
void insert(cData*);
void insert(string);
-
+ void balance();
+ void getList(list<cData>*);
void remove(cData*);
cData search(string);
private:
+ cNode* root;
};