Skip to content

Commit 0204bb0

Browse files
author
tiberius
committed
tree now fully working
1 parent 93cac3b commit 0204bb0

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

cpp4j/testTemplates.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ int printAndCountBackwards(Container & c) {
5050
int n = 0;
5151
typename Container::iterator i = c.end();
5252
while(i != c.begin()) {
53-
--i;
53+
// had to swap the following two lines?
5454
cout << *i << " ";
55+
--i;
56+
5557
n++;
5658
}
5759
cout << endl;
@@ -152,8 +154,6 @@ int testTemplates(void)
152154
cout << "10-element tree: ";
153155
assert(printAndCount(t) == 10);
154156

155-
#if 0 // move this line down while your implementation proceeds...
156-
157157
// now we contruct a tree with a "reverse" order
158158
//typedef Tree< float, Greater<float> > RevFloatTree; // had to replace this line by the following
159159
typedef Tree< float, Greater > RevFloatTree;
@@ -165,11 +165,18 @@ int testTemplates(void)
165165
cout << "reverse-sorted 4-float tree: ";
166166
assert(printAndCount(ft) == 4);
167167

168+
RevFloatTree::iterator beginIt = ft.begin();
169+
RevFloatTree::iterator endIt = ft.end();
170+
cout << *beginIt;
171+
cout << *endIt;
172+
168173
// if we list elements backwards, they should be
169174
// in the same order as with the function Less<>
170175
cout << "listing backwards: ";
171176
assert(printAndCountBackwards(ft) == 4);
172177

178+
#if 0 // move this line down while your implementation proceeds...
179+
173180
/////////////////////////////////////////
174181
// TEST MAP
175182

cpp4j/tree.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,17 @@ template <class T, template<class> class Order = Less > class Tree {
3232
// lowest key
3333
const iterator begin() const {
3434
TreeNode<T, Order> * node = m_root;
35-
while (node != NULL) {
36-
if (node->m_left != NULL)
37-
node = node->m_left;
38-
else
39-
break;
40-
}
35+
while (node != NULL && node->m_left != NULL)
36+
node = node->m_left;
4137

4238
return iterator (node, this, node != NULL ? iterator::begin : iterator::end);
4339
}
4440

4541
// highest key
4642
const iterator end() const {
4743
TreeNode<T, Order> * node = m_root;
48-
while (node != NULL) {
49-
if (node->m_right != NULL)
50-
node = node->m_right;
51-
else
52-
break;
53-
}
44+
while (node != NULL && node->m_right != NULL)
45+
node = node->m_right;
5446

5547
return iterator (node, this, iterator::end);
5648
}

cpp4j/treeiterator.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,47 @@ template <class T, template<typename> class Order > class TreeIterator {
6868

6969
// zum vorherigen Element
7070
TreeIterator<T, Order>& operator--() {
71-
//if (m_node == NULL || !m_end)
71+
if (m_node == NULL || m_mode == begin)
72+
return *this;
73+
74+
m_mode == middle;
75+
76+
if (m_node->m_left != NULL) {
77+
m_node = m_node->m_left;
78+
while (m_node->m_right != NULL)
79+
m_node = m_node->m_right;
80+
81+
return *this;
82+
}
83+
84+
if (m_node->m_up != NULL) {
85+
if (m_node->m_up->m_right == m_node) {
86+
// wir kamen von rechts
87+
m_node = m_node->m_up;
88+
return *this;
89+
}
90+
91+
TreeNode<T, Order> * node = m_node;
92+
93+
// wir kamen von links, also so lange nach oben, bis wir einen neuen linken Pfad haben
94+
while (node->m_up->m_left == node) {
95+
96+
node = node->m_up;
97+
98+
if (node->m_up == NULL) { // reached root, nothing else to do
99+
m_mode = begin;
100+
return *this;
101+
}
102+
103+
}
104+
105+
m_node = node->m_up;
106+
107+
return *this;
108+
109+
}
110+
111+
m_mode = end;
72112
return *this;
73113
}
74114

0 commit comments

Comments
 (0)