Skip to content

Commit 72564ae

Browse files
committed
Finished Tree class, defined Forest, BinForest type.
1 parent 7ace622 commit 72564ae

31 files changed

+900
-130
lines changed

Graph/main.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
int main()
44
{
55
DiGraph<char, int> graph;
6-
graph.show();
76
auto vertex_A = graph.insert('A');
87
auto vertex_B = graph.insert('B');
98
auto vertex_C = graph.insert('C');
@@ -15,11 +14,7 @@ int main()
1514
graph.insert(vertex_A, vertex_C, 0, 3);
1615
graph.insert(vertex_C, vertex_A, 0, 4);
1716

18-
// graph.show("Graph", false, "neato");
19-
cout << "n_vertice = " << graph.n_vertice() << endl;
20-
cout << "n_edges = " << graph.n_edges() << endl;
21-
graph.erase(vertex_A);
22-
cout << graph.exist(5) << endl;
17+
graph.show("Graph", false, "neato");
2318
// graph.erase(vertex_A);
2419
// graph.show("Graph2", false, "neato");
2520
// graph.erase(1);

Graph/main.exe

-8.12 KB
Binary file not shown.

List/list.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ typename List<DataType>::iterator List<DataType>::find(const DataType& element)c
313313
List<DataType>::iterator it;
314314
for(it = begin(); it != end(); it++)
315315
{
316-
if(it->data == element)
316+
if(*it == element)
317317
{
318318
break;
319319
}
@@ -405,6 +405,7 @@ typename List<DataType>::iterator List<DataType>::erase(iterator it)
405405
Node* q = p->link;
406406
p->link = q->link;
407407
delete q;
408+
length--;
408409
return iterator(p->link);
409410
}
410411
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

BinTree/bintree.h renamed to Tree/BinTree/bintree.cpp

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,4 @@
1-
#ifndef BINTREE_H
2-
#define BINTREE_H
3-
4-
#include <iostream>
5-
#include <fstream>
6-
7-
#include <stack.h>
8-
#include <queue.h>
9-
10-
using namespace std;
11-
12-
template<class DataType>
13-
class BinTree
14-
{
15-
public:
16-
enum TravType {NONE, PRE, POST, IN, LEVEL};
17-
class Node
18-
{
19-
public:
20-
DataType data;
21-
22-
Node *parent = NULL;
23-
Node *lchild = NULL;
24-
Node *rchild = NULL;
25-
26-
Node *next = NULL;
27-
Node *prev = NULL;
28-
29-
public:
30-
Node(){} // tested
31-
Node(const DataType& _data, Node *_parent = NULL, Node *_lchild = NULL, Node *_rchild = NULL) :
32-
data(_data), parent(_parent), lchild(_lchild), rchild(_rchild){} // tested
33-
~Node(){parent = NULL; lchild = NULL; rchild = NULL;} // tested
34-
int size()const; // tested
35-
int height()const; // tested
36-
int level()const; // finished
37-
bool isleaf()const; // finished
38-
bool isroot()const; // finished
39-
bool islchild()const; // finished
40-
bool isrchild()const; // finished
41-
Node* brother()const; // finished
42-
bool belong_to(const BinTree<DataType>& tree)const; // finished
43-
Node* insert_lchild(const DataType& value); // finished
44-
Node* insert_rchild(const DataType& value); // finished
45-
};
46-
47-
class iterator
48-
{
49-
private:
50-
Node* _ptr = NULL;
51-
52-
public:
53-
iterator(){}
54-
iterator(Node* ptr) : _ptr(ptr){}
55-
iterator& operator =(Node* ptr);
56-
iterator& operator ++();
57-
iterator operator ++(int);
58-
iterator& operator --();
59-
iterator operator --(int);
60-
bool operator ==(Node* ptr){return _ptr == ptr;}
61-
bool operator ==(const iterator& it){return it._ptr == _ptr;}
62-
bool operator !=(Node* ptr){return _ptr != ptr;}
63-
bool operator !=(const iterator& it){return it._ptr != _ptr;}
64-
Node* ptr()const{return _ptr;}
65-
DataType& operator *(){return _ptr->data;}
66-
};
67-
68-
private:
69-
Node *_root = NULL;
70-
int _size = 0;
71-
72-
iterator _begin = iterator(NULL);
73-
iterator _rear = iterator(NULL);
74-
iterator _end = iterator(NULL);
75-
76-
private:
77-
static Node* new_Node(const DataType& value,
78-
Node* parent = NULL,
79-
Node* lchild = NULL,
80-
Node* rchild = NULL); // finished
81-
void copy(BinTree<DataType>& dest_tree, const BinTree<DataType>& src_tree); // finished
82-
83-
static Node* trav_left_branch(Node* node, Stack<Node*>& stack);
84-
void trav_pre();
85-
86-
static void gotoHLVFL(Stack<Node*>& stack);
87-
void trav_post();
88-
89-
static Node* goto_most_left(Node* node, Stack<Node*>& stack);
90-
void trav_in();
91-
92-
void trav_level();
93-
94-
public:
95-
BinTree(){} // finished
96-
BinTree(const BinTree<DataType>& tree); // finished
97-
~BinTree(); // finished
98-
99-
void clear(); // finished
100-
int size()const; // finished
101-
int height()const; // finished
102-
bool empty()const; // finished
103-
bool has_node(const Node& node)const; // finished
104-
bool has_node(Node* node)const; // finished
105-
Node* root()const; // finished, tested
106-
107-
Node* insert_root(const DataType& value); // finished, tested
108-
Node* insert_lchild(Node* node, const DataType& value); // finished, tested
109-
Node* insert_rchild(Node* node, const DataType& value); // finished, tested
110-
Node* attach_lchild(Node* node, const BinTree<DataType>& tree); // finished, tested
111-
Node* attach_rchild(Node* node, const BinTree<DataType>& tree); // finished, tested
112-
int remove(Node* node); // finished
113-
BinTree<DataType>& secede(Node* node); // finished
114-
static BinTree<DataType>& subtree(Node* node); // finished
115-
BinTree<DataType>& operator =(const BinTree<DataType>& tree); // finished
116-
void show(const string& filename = "temp.pdf")const; // finished
117-
void write(const string& filename)const; // finished
118-
119-
void trav_method(TravType method);
120-
iterator begin()const; // finished
121-
iterator rear()const;
122-
iterator end()const; // finished
123-
};
1+
#ifdef BINTREE_H
1242

1253
template<class DataType>
1264
typename BinTree<DataType>::Node* BinTree<DataType>::new_Node(const DataType& value,

Tree/BinTree/bintree.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#ifndef BINTREE_H
2+
#define BINTREE_H
3+
4+
#include <iostream>
5+
#include <fstream>
6+
7+
#include <stack.h>
8+
#include <queue.h>
9+
10+
using namespace std;
11+
12+
template<class DataType>
13+
class BinTree
14+
{
15+
public:
16+
enum TravType {NONE, PRE, POST, IN, LEVEL};
17+
class Node
18+
{
19+
public:
20+
DataType data;
21+
22+
Node *parent = NULL;
23+
Node *lchild = NULL;
24+
Node *rchild = NULL;
25+
26+
Node *next = NULL;
27+
Node *prev = NULL;
28+
29+
public:
30+
Node(){} // tested
31+
Node(const DataType& _data, Node *_parent = NULL, Node *_lchild = NULL, Node *_rchild = NULL) :
32+
data(_data), parent(_parent), lchild(_lchild), rchild(_rchild){} // tested
33+
~Node(){parent = NULL; lchild = NULL; rchild = NULL;} // tested
34+
int size()const; // tested
35+
int height()const; // tested
36+
int level()const; // finished
37+
bool isleaf()const; // finished
38+
bool isroot()const; // finished
39+
bool islchild()const; // finished
40+
bool isrchild()const; // finished
41+
Node* brother()const; // finished
42+
bool belong_to(const BinTree<DataType>& tree)const; // finished
43+
Node* insert_lchild(const DataType& value); // finished
44+
Node* insert_rchild(const DataType& value); // finished
45+
};
46+
47+
class iterator
48+
{
49+
private:
50+
Node* _ptr = NULL;
51+
52+
public:
53+
iterator(){}
54+
iterator(Node* ptr) : _ptr(ptr){}
55+
iterator& operator =(Node* ptr);
56+
iterator& operator ++();
57+
iterator operator ++(int);
58+
iterator& operator --();
59+
iterator operator --(int);
60+
bool operator ==(Node* ptr){return _ptr == ptr;}
61+
bool operator ==(const iterator& it){return it._ptr == _ptr;}
62+
bool operator !=(Node* ptr){return _ptr != ptr;}
63+
bool operator !=(const iterator& it){return it._ptr != _ptr;}
64+
Node* ptr()const{return _ptr;}
65+
DataType& operator *(){return _ptr->data;}
66+
};
67+
68+
private:
69+
Node *_root = NULL;
70+
int _size = 0;
71+
72+
iterator _begin = iterator(NULL);
73+
iterator _rear = iterator(NULL);
74+
iterator _end = iterator(NULL);
75+
76+
private:
77+
static Node* new_Node(const DataType& value,
78+
Node* parent = NULL,
79+
Node* lchild = NULL,
80+
Node* rchild = NULL); // finished
81+
void copy(BinTree<DataType>& dest_tree, const BinTree<DataType>& src_tree); // finished
82+
83+
static Node* trav_left_branch(Node* node, Stack<Node*>& stack);
84+
void trav_pre();
85+
86+
static void gotoHLVFL(Stack<Node*>& stack);
87+
void trav_post();
88+
89+
static Node* goto_most_left(Node* node, Stack<Node*>& stack);
90+
void trav_in();
91+
92+
void trav_level();
93+
94+
public:
95+
BinTree(){} // finished
96+
BinTree(const BinTree<DataType>& tree); // finished
97+
~BinTree(); // finished
98+
99+
void clear(); // finished
100+
int size()const; // finished
101+
int height()const; // finished
102+
bool empty()const; // finished
103+
bool has_node(const Node& node)const; // finished
104+
bool has_node(Node* node)const; // finished
105+
Node* root()const; // finished, tested
106+
107+
Node* insert_root(const DataType& value); // finished, tested
108+
Node* insert_lchild(Node* node, const DataType& value); // finished, tested
109+
Node* insert_rchild(Node* node, const DataType& value); // finished, tested
110+
Node* attach_lchild(Node* node, const BinTree<DataType>& tree); // finished, tested
111+
Node* attach_rchild(Node* node, const BinTree<DataType>& tree); // finished, tested
112+
int remove(Node* node); // finished
113+
BinTree<DataType>& secede(Node* node); // finished
114+
static BinTree<DataType>& subtree(Node* node); // finished
115+
BinTree<DataType>& operator =(const BinTree<DataType>& tree); // finished
116+
void show(const string& filename = "temp.pdf")const; // finished
117+
void write(const string& filename)const; // finished
118+
119+
void trav_method(TravType method);
120+
iterator begin()const; // finished
121+
iterator rear()const;
122+
iterator end()const; // finished
123+
};
124+
125+
class BinForest : public List< BinTree<DataType> >{};
126+
127+
#include "bintree.cpp"
128+
#endif
File renamed without changes.

Tree/Tree/Figures/0.dot

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
graph
2+
{
3+
0--1;
4+
0--2;
5+
0--3;
6+
7+
1[shape="circle",label="C7"];
8+
2[shape="circle",label="C8"];
9+
3[shape="circle",label="C9"];
10+
0[shape="circle",label="B3"];
11+
}

Tree/Tree/Figures/Current

14 KB
Binary file not shown.

Tree/Tree/Figures/Current.dot

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
graph
2+
{
3+
0--1;
4+
0--2;
5+
0--3;
6+
3--4;
7+
3--5;
8+
3--6;
9+
2--7;
10+
2--8;
11+
2--9;
12+
1--10;
13+
1--11;
14+
1--12;
15+
16+
10[shape="circle",label="C1"];
17+
11[shape="circle",label="C2"];
18+
12[shape="circle",label="C3"];
19+
1[shape="circle",label="B1"];
20+
7[shape="circle",label="C7"];
21+
8[shape="circle",label="C8"];
22+
9[shape="circle",label="C9"];
23+
2[shape="circle",label="B3"];
24+
4[shape="circle",label="C10"];
25+
5[shape="circle",label="C11"];
26+
6[shape="circle",label="C12"];
27+
3[shape="circle",label="B4"];
28+
0[shape="circle",label="A1"];
29+
}

Tree/Tree/Figures/Current1

14 KB
Binary file not shown.

Tree/Tree/Figures/Current1.dot

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
graph
2+
{
3+
0--1;
4+
0--2;
5+
0--3;
6+
3--4;
7+
3--5;
8+
3--6;
9+
2--7;
10+
2--8;
11+
2--9;
12+
1--10;
13+
1--11;
14+
1--12;
15+
16+
10[shape="circle",label="C1"];
17+
11[shape="circle",label="C2"];
18+
12[shape="circle",label="C3"];
19+
1[shape="circle",label="B1"];
20+
7[shape="circle",label="C7"];
21+
8[shape="circle",label="C8"];
22+
9[shape="circle",label="C9"];
23+
2[shape="circle",label="B3"];
24+
4[shape="circle",label="C10"];
25+
5[shape="circle",label="C11"];
26+
6[shape="circle",label="C12"];
27+
3[shape="circle",label="B4"];
28+
0[shape="circle",label="A1"];
29+
}

Tree/Tree/Figures/Current2

15.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)