Basic Tree Template
Aug 5, 2013 at 11:49am UTC
What i'm whilling to do is to create a basic Tree that accepts all kind of data.
But i get an error when trying to dynamically initializate an arm.
The Tree should work like this:
Father->Son(Where son is a list of Sons)
And when i do Son->Add(new ArmS<T>) it requires me to put an int insthead of *int
here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
//tree.h
#include "list.h"
template <class T>
struct ArmS
{
T Item;
ArmS *Father;
List<ArmS> *Sons;
int NumberOfSons;
};
template <class T>
class Tree
{
public :
Tree();
ArmS<T> *Up();
ArmS<T> *Down();
ArmS<T> *Down(int );
int RNumberOfSons();
void NewArm(T);
T AddItem(T);
private :
ArmS<T> Arm;
ArmS<T> *CurrentArm;
};
template <class T>
Tree<T>::Tree()
{
this ->CurrentArm = &this ->Arm;
}
template <class T>
void Tree<T>::NewArm(T Item)
{
if (this ->CurrentArm->NumberOfSons = 0)
{
this ->CurrentArm->Item = Item;
}
//errors are here
else
{
this ->CurrentArm->Sons->Add(new ArmS<T>); //actually this line.
this ->CurrentArm->NumberOfSons++;
this ->CurrentArm->Sons->Return(this ->CurrentArm->NumberOfSons)->Father = this ->CurrentArm;
this ->CurrentArm->Sons->Return(this ->CurrentArm->NumberOfSons)->Item = Item;
}
}
template <class T>
T Tree<T>::AddItem(T Item)
{
this ->CurrentArm->Item = Item;
}
template <class T>
int Tree<T>::RNumberOfSons()
{
return this ->CurrentArm->NumberOfSons;
}
template <class T>
ArmS<T> *Tree<T>::Up()
{
if (this ->CurrentArm->Father == NULL)
{
return this ->CurrentArm;
}
else
{
this ->CurrentArm = this ->CurrentArm->Father;
return this ->CurrentArm;
}
}
template <class T>
ArmS<T> *Tree<T>::Down()
{
if (this ->CurrentArm->NumberOfSons == 0)
{
return this ->CurrentArm;
}
else
{
this ->CurrentArm = this ->CurrentArm->Sons->Return(1);
return this ->CurrentArm;
}
}
template <class T>
ArmS<T> *Tree<T>::Down(int Pos)
{
this ->CurrentArm->Sons->Return(Pos);
return this ->CurrentArm;
}
[code]
//list.h
#pragma once
template <class T>
struct Object
{
T Item;
Object *Previous;
Object *Next;
int N;
};
template <class T>
class List
{
public :
List();
T Add(T);
/*T Delete(int); To Implement */
T Next();
T Previous();
T Return(int ); //To "Betterize"
//
private :
Object<T> *Start;
Object<T> *End;
Object<T> Item;
static int ItemNumber;
int ItemNumberP();
};
template <class T>
int List<T>::ItemNumber = 0;
template <class T>
int List<T>::ItemNumberP()
{
ItemNumber++;
return ItemNumber;
}
//Now
template <class T>
List<T>::List()
{
this ->Item.Item = 0;
this ->Item.Next = 0;
this ->Item.Previous = 0;
this ->Start = 0;
this ->End = 0;
}
template <class T>
T List<T>::Add(T Itm)
{
if (this ->Start == 0)
{
this ->Start = &this ->Item;
this ->End = this ->Start;
this ->Item.Item = Itm;
this ->Item.N = ++ItemNumber;
}
else
{
this ->End->Next = new Object<T>;
this ->End->Next->Previous = this ->End;
this ->End = this ->End->Next;
this ->End->Item = Itm;
this ->End->N = ++ItemNumber;
}
}
/*To Implement
template <class T>
T List<T>::Delete(int N)
{
}
template <class T>
T List<T>::Previous()
{
return CurrentItem->Previous;
}
template <class T>
T List<T>::Next()
{
return CurrentItem->Next;
}
*/
template <class T>
T List<T>::Return(int Selection)
{
if (Selection > this ->End->N)
{
return this ->End->Item;
}
else
{
bool found = true ;
Object<T> *ToFound;
ToFound = this ->Start;
do
{
if (Selection == ToFound->N)
{
found = true ;
}
else
{
ToFound = ToFound->Next;
found = false ;
}
}
while (found != true );
return ToFound->Item;
}
}
[code]
and ofc main.
[code]
#include <iostream>
#include <fstream>
#include "list.h"
#include "list2.h"
#include "tree.h"
using namespace std;
int main()
{
Tree<int > tre;
cout << "Hello world!" << endl;
cout << "Testing Tree:" << endl;
cout << "U: UP \t D: DOWN" << endl;
tre.NewArm(123);
tre.Down();
return 0;
}
The errors:
1 2 3 4 5 6 7 8 9 10 11 12 13
\Projects\Tree\tree.h||In instantiation of 'void Tree<T>::NewArm(T) [with T = int]' :|
\Tree\main.cpp|14|required from here|
\tree.h|36|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Tree\tree.h|42|error: no matching function for call to 'List<ArmS<int> >::Add(ArmS<int>*)' |
\Tree\tree.h|42|note: candidate is:|
\Tree\list.h|52|note: T List<T>::Add(T) [with T = ArmS<int >]|
\Tree\list.h|52|note: no known conversion for argument 1 from 'ArmS<int>*' to 'ArmS<int>' |
\Tree\tree.h|44|error: base operand of '->' has non-pointer type 'ArmS<int>' |
\Tree\tree.h|45|error: base operand of '->' has non-pointer type 'ArmS<int>' |
\Tree\tree.h||In instantiation of 'ArmS<T>* Tree<T>::Down() [with T = int]' :|
\main.cpp|15|required from here|
\Tree\tree.h|84|error: cannot convert 'ArmS<int>' to 'ArmS<int>*' in assignment|
||=== Build finished: 4 errors, 5 warnings (0 minutes, 0 seconds) ===|
Last edited on Aug 5, 2013 at 11:49am UTC
Aug 5, 2013 at 12:04pm UTC
on line 8 you probably want this:
List<ArmS *> Sons; // Now it's a list of pointers to ArmS. Note the position of the *
you can have a pointer to the list, but you need the list as an object anyway
Aug 5, 2013 at 12:27pm UTC
But why this line has changed:
this ->CurrentArm->Sons.Add(new ArmS<T>); //. insthead of ->
also
List<ArmS *> Sons; // Now it's a list of pointers to ArmS
before what was(sorry for my english) i use a grammatical Approach.
Last edited on Aug 5, 2013 at 12:31pm UTC
Aug 5, 2013 at 12:34pm UTC
But why this line has changed:
because
Sons
is now the object itself
not a pointer.
So you don't need to
Sons = new List<ArmS *>
(what you didn't do anyway -> crash)
Aug 5, 2013 at 1:52pm UTC
OK, i got another problem, here is the changed tree.h
What i'm willing to do is this:
First i add something, then i add 2 sons,
then i go down to the first son and try to add a son.
But my program crashes..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
//tree.h
#include "list.h"
template <class T>
struct ArmS
{
T Item;
ArmS *Father;
List<ArmS*> Sons;
int NumberOfSons;
};
template <class T>
class Tree
{
public :
Tree();
ArmS<T> *Up();
ArmS<T> *Down();
ArmS<T> *Down(int );
ArmS<T> *GoTop();
void NewArm(T);
T Return();
ArmS<T> Arm;
ArmS<T> *CurrentArm;
};
template <class T>
Tree<T>::Tree()
{
this ->CurrentArm = &this ->Arm;
this ->CurrentArm->NumberOfSons = 0;
this ->CurrentArm->Item = NULL;
}
template <class T>
void Tree<T>::NewArm(T Item)
{
if (this ->CurrentArm->Item == NULL)
{
this ->CurrentArm->Item = Item;
this ->CurrentArm->NumberOfSons++;
}
else
{
this ->CurrentArm->Sons.Add(new ArmS<T>);
this ->CurrentArm->NumberOfSons++;
this ->CurrentArm->Sons.Return(this ->CurrentArm->NumberOfSons)->Father = this ->CurrentArm;
this ->CurrentArm->Sons.Return(this ->CurrentArm->NumberOfSons)->Item = Item;
}
}
template <class T>
T Tree<T>::Return()
{
return this ->CurrentArm->Item;
}
template <class T>
ArmS<T> *Tree<T>::Up()
{
if (this ->CurrentArm->Father == NULL)
{
return this ->CurrentArm;
}
else
{
this ->CurrentArm = this ->CurrentArm->Father;
return this ->CurrentArm;
}
}
template <class T>
ArmS<T> *Tree<T>::Down()
{
if (this ->CurrentArm->NumberOfSons == 0)
{
return this ->CurrentArm;
}
else
{
this ->CurrentArm = this ->CurrentArm->Sons.Return(1);
return this ->CurrentArm;
}
}
template <class T>
ArmS<T> *Tree<T>::Down(int Pos)
{
if (this ->CurrentArm->NumberOfSons == 0)
{
return this ->CurrentArm;
}
else
{
this ->CurrentArm = this ->CurrentArm->Sons.Return(Pos);
return this ->CurrentArm;
}
}
template <class T>
ArmS<T> *Tree<T>::GoTop()
{
this ->CurrentArm = this ->Arm;
}
and The main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <fstream>
#include "list.h"
#include "list2.h"
#include "tree.h"
using namespace std;
int main()
{
Tree<int > tre;
cout << "Hello world!" << endl;
cout << "Adding Items to the tre" << endl;
tre.NewArm(123);
tre.NewArm(432);
tre.NewArm(444);
tre.Down();
tre.NewArm(44444);
return 0;
}
Aug 5, 2013 at 4:09pm UTC
It is permitted to BUMP?
Aug 6, 2013 at 8:01am UTC
somehow i think that you have a rather faint idea about what you're doing?
your current problem is the (oddly named) function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
template <class T>
T List<T>::Return(int Selection)
{
if (Selection > this ->End->N) // what happens when this->End is null?
{
return this ->End->Item;
}
else
{
bool found = true ;
Object<T> *ToFound;
ToFound = this ->Start; // what happen when this->Start is null?
do
{
if (Selection == ToFound->N)
{
found = true ;
}
else
{
ToFound = ToFound->Next;
found = false ;
}
}
while (found != true );
return ToFound->Item; // what will happen if ToFound is null?
}
}
Your're trying to find the object (usually named as node) by the index, but the way you use the index in the tree does not correlate with the index in the list. For instance
this ->CurrentArm->Sons.Return(this ->CurrentArm->NumberOfSons)->Father = this ->CurrentArm
this ->CurrentArm->NumberOfSons
in a subtree might be 1, but there might be a lot of others in other branches/'arm's. The indexes in the list are continuous hence 1 is a complete different 'arm'
Topic archived. No new replies allowed.