Skip to content

Commit ec98fc2

Browse files
author
tiberius
committed
further development and bug fixing on the tree
1 parent d7279c6 commit ec98fc2

File tree

4 files changed

+46
-73
lines changed

4 files changed

+46
-73
lines changed

cpp4j/cpp4j.pro

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1+
######################################################################
2+
# Automatically generated by qmake (2.01a) Tue Jun 12 15:45:01 2012
3+
######################################################################
4+
15
TEMPLATE = app
2-
CONFIG += console
3-
CONFIG -= qt
6+
TARGET =
7+
DEPENDPATH += .
8+
INCLUDEPATH += .
49

10+
# Input
11+
HEADERS += greater.h \
12+
less.h \
13+
maptofirst.h \
14+
pair.h \
15+
rationalnumber.h \
16+
rationalnumberarray.h \
17+
tree.h \
18+
treeiterator.h \
19+
treenode.h
520
SOURCES += rationalnumber.cpp \
6-
rationalnumberarray.cpp \
21+
rationalnumberarray.cpp \
22+
testMain.cpp \
23+
testTemplates.cpp \
724
testRNA.cpp \
8-
testCPP_RN.cpp \
925
testRN.cpp \
10-
testMain.cpp \
1126
testCPP_RNA.cpp \
12-
testTemplates.cpp
13-
14-
HEADERS += \
15-
rationalnumber.h \
16-
rationalnumberarray.h \
17-
pair.h \
18-
less.h \
19-
greater.h \
20-
maptofirst.h \
21-
tree.h \
22-
treenode.h \
23-
treeiterator.h
24-
27+
testCPP_RN.cpp

cpp4j/testTemplates.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ int testTemplates(void)
113113
cout << "tree 4-3-2-1-5: ";
114114
assert(printAndCount(t) == 5);
115115

116-
117-
#if 0 // move this line down while your implementation proceeds...
118-
119116
// test clear()
120117
t.clear();
121118
cout << "after clear(): ";
@@ -141,6 +138,8 @@ int testTemplates(void)
141138
cout << "6-element tree: ";
142139
assert(printAndCount(t) == 6);
143140

141+
#if 0 // move this line down while your implementation proceeds...
142+
144143
// now we contruct a tree with a "reverse" order
145144
typedef Tree< float, Greater<float> > RevFloatTree;
146145
RevFloatTree ft;

cpp4j/tree.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,16 @@ template <class T, template<typename> class Order = Less > class Tree {
2626
}
2727
}
2828

29-
//void clear();
30-
iterator begin() {
31-
if (m_root == NULL)
32-
return iterator(NULL, this);
29+
void clear() {
30+
m_root = NULL;
31+
}
3332

34-
return iterator (m_root->findFirst(), this);
33+
iterator begin() {
34+
return iterator (m_root, this);
3535
}
3636

3737
iterator end() {
38-
if (m_root == NULL)
39-
return iterator(NULL, this);
40-
41-
return iterator (m_root->findLast(), this);
38+
return iterator (NULL, this);
4239
}
4340

4441
/*iterator first();
@@ -69,7 +66,7 @@ template <class T, template<typename> class Order = Less > class Tree {
6966
if (next.m_node == NULL) {
7067
next.m_node = new node(value);
7168
next.m_node->m_up = i.m_node;
72-
i.m_node->m_left = next.m_node;
69+
i.m_node->m_right = next.m_node;
7370
return next;
7471

7572
} else

cpp4j/treeiterator.h

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,61 +19,35 @@ template <class T, template<typename> class Order > class TreeIterator {
1919
return m_node->value();
2020
}
2121

22-
// zum naechsten Element
22+
// zum vorherigen Element
2323
TreeIterator<T, Order>& operator++() {
24-
// erst rechts prüfen, ob vorhanden
25-
// ansonsten solange nach oben, bis größerer gefunden
26-
if (m_node->m_right != NULL) {
27-
m_node = m_node->m_right;
24+
if (m_node == NULL)
2825
return *this;
29-
}
30-
31-
TreeNode<T, Order> * parent = m_node;
32-
33-
Order<T> functor;
34-
do {
35-
36-
parent = parent->m_up;
37-
38-
// keep position
39-
if (parent == NULL)
40-
return *this;
4126

27+
if (m_node->m_left == NULL) {
28+
TreeNode<T, Order> * parent = m_node;
29+
while (parent != NULL && parent->m_right == NULL)
30+
parent = parent->m_up;
4231

43-
} while (functor(m_node->m_value, parent->m_value));
32+
if (m_node == parent->m_right || m_node->m_up == parent->m_right)
33+
m_node = NULL;
34+
else
35+
m_node = parent->m_right;
36+
return *this;
37+
}
4438

45-
m_node = parent;
39+
m_node = m_node->m_left;
4640
return *this;
4741
}
4842

49-
// zum vorherigen Element
43+
// zum naechsten Element
5044
TreeIterator<T, Order>& operator--() {
51-
if (m_node->m_left != NULL) {
52-
TreeNode<T, Order> node = m_node.m_left;
53-
while (node->m_right != NULL)
54-
node = node->m_right;
55-
56-
m_node = node;
57-
} else if (m_node->m_up != NULL) {
58-
TreeNode<T, Order> * parent = m_node;
59-
60-
Order<T> functor;
61-
do {
62-
parent = parent->m_up;
63-
// keep position
64-
if (parent == NULL)
65-
return *this;
66-
67-
} while (functor(m_node->m_value, parent->m_value));
45+
}
6846

69-
m_node=parent;
70-
}
7147

72-
return *this;
73-
}
7448

7549
bool operator==(const TreeIterator<T, Order> &rhs) const {
76-
return this->m_node == rhs.m_node && this->m_tree == rhs.m_tree;
50+
return this->m_node == rhs.m_node && this->m_tree == rhs.m_tree;
7751
}
7852

7953
bool operator!=(const TreeIterator<T, Order> &rhs) const {

0 commit comments

Comments
 (0)