File tree Expand file tree Collapse file tree 2 files changed +38
-23
lines changed Expand file tree Collapse file tree 2 files changed +38
-23
lines changed Original file line number Diff line number Diff line change @@ -51,8 +51,9 @@ class AVL {
51
51
52
52
void erase (const T &x) {
53
53
if (!isEmpty ()) {
54
- tree = tree->erase (x);
55
- numNodes--;
54
+ bool found = false ;
55
+ tree = tree->erase (x, found);
56
+ if (found) numNodes--;
56
57
}
57
58
}
58
59
@@ -64,7 +65,7 @@ class AVL {
64
65
}
65
66
}
66
67
67
- private :
68
+ public :
68
69
69
70
struct Node {
70
71
Node *left, *right;
@@ -93,51 +94,58 @@ class AVL {
93
94
return update ();
94
95
}
95
96
96
- Node *erase (const T &x) {
97
+ Node *erase (const T &x, bool &found ) {
97
98
if (value == x) {
99
+ found = true ;
98
100
if (left == 0 && right == 0 ) {
99
101
delete this ;
100
102
return 0 ;
101
103
} else if (left == 0 ) {
104
+ Node *aux = right;
102
105
*this = *right;
103
- delete right ;
106
+ delete aux ;
104
107
} else if (right == 0 ) {
108
+ Node *aux = left;
105
109
*this = *left;
106
- delete left ;
110
+ delete aux ;
107
111
} else {
108
112
// Tracing path to rightmost leaf of the left subtree
109
113
std::stack<Node*> trace;
110
114
111
115
Node *current = left;
112
- while (current-> right != 0 ) {
116
+ while (current != 0 ) {
113
117
trace.push (current);
114
118
current = current->right ;
115
119
}
116
120
121
+ current = trace.top ();
117
122
value = current->value ;
118
123
Node *lsubtree = current->left ;
119
124
delete current;
125
+ trace.pop ();
120
126
121
- if (trace.empty ()) trace.push (left);
122
-
123
- trace.top ()->right = lsubtree;
124
-
125
- do {
126
- trace.top ()->update ();
127
+ if (trace.empty ()) { left = lsubtree; }
128
+ else {
129
+ trace.top ()->right = lsubtree;
127
130
trace.pop ();
128
- } while (!trace.empty ());
131
+ while (!trace.empty ()) {
132
+ current = trace.top ();
133
+ current->right = current->right ->update ();
134
+ trace.pop ();
135
+ }
136
+ }
129
137
}
130
138
return update ();
131
139
}
132
140
else if (x < value) {
133
141
if (left != 0 ) {
134
- left = left->erase (x);
142
+ left = left->erase (x, found );
135
143
return update ();
136
144
} else return this ;
137
145
}
138
146
else {
139
147
if (right != 0 ) {
140
- right = right->erase (x);
148
+ right = right->erase (x, found );
141
149
return update ();
142
150
} else return this ;
143
151
}
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ using namespace std;
5
5
using namespace alg ;
6
6
7
7
const unsigned N = 4096 *32 ;
8
- const unsigned N_ELEMS_TO_REMOVE = 4096 * 8 ;
8
+ const unsigned N_ELEMS_TO_REMOVE = N- 128 ; // Must be between 0 and N-1
9
9
10
10
template <typename T>
11
11
void printTreeStatus (const AVL<T> &t) {
@@ -40,20 +40,27 @@ int main()
40
40
cout << " ERROR: Value " << values[idx] << " was inserted and not found!" << endl;
41
41
}
42
42
43
- cout << " Now removing " << N_ELEMS_TO_REMOVE << " random elements for the tree... " ;
43
+ cout << " Now removing a random element from the tree... " ;
44
+ unsigned idx = rand () % N;
45
+ avl.erase (values[idx]);
46
+ cout << " Done" << endl;
47
+
48
+ printTreeStatus (avl);
49
+
50
+ cout << " Now removing the root of the tree " << N_ELEMS_TO_REMOVE << " times... " ;
44
51
for (unsigned i = 0 ; i < N_ELEMS_TO_REMOVE; ++i) {
45
- unsigned idx = rand () % N;
46
- avl.erase (values[idx]);
52
+ avl.erase (avl.root ());
47
53
}
48
54
cout << " Done" << endl;
49
55
50
56
printTreeStatus (avl);
51
57
52
- cout << " Do you want to see the GraphViz representation of the Tree (Y/n)? " ;
58
+ // Outputting to cerr so the output can be redirected with ./avl_demo 2> <name>.gvz
59
+ cout << " Do you want to output the GraphViz representation of the tree to the cerr stream (Y/n)? " ;
53
60
char usrInput;
54
61
cin >> usrInput;
55
- if (usrInput == ' Y' || usrInput == ' y' ) avl.toGraphViz (cout , " AVL" );
56
-
62
+ if (usrInput == ' Y' || usrInput == ' y' ) avl.toGraphViz (cerr , " AVL" );
63
+
57
64
return 0 ;
58
65
}
59
66
You can’t perform that action at this time.
0 commit comments