回答編集履歴

4

追記

2021/02/18 06:26

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -33,3 +33,177 @@
33
33
  たとえば range_error例外をthrowせんならんのでは?
34
34
 
35
35
  ※ int* を返すことにすれば、NULL(nullptr)で"存在しない"ことを表現できるけど。
36
+
37
+
38
+
39
+ ...で、僕ならこう書く:
40
+
41
+ ```C++
42
+
43
+ #include <iostream>
44
+
45
+
46
+
47
+ // 二分探索木のクラス
48
+
49
+ template<typename T>
50
+
51
+ class BinarySearchTree {
52
+
53
+ struct BinNode {
54
+
55
+ T key; // 各頂点のキー
56
+
57
+ BinNode* left;
58
+
59
+ BinNode* right;
60
+
61
+ BinNode(T d, BinNode *l=nullptr, BinNode *r=nullptr) : key(d), left(l), right(r) {}
62
+
63
+ };
64
+
65
+ BinNode* root; // 根頂点
66
+
67
+ public:
68
+
69
+ BinarySearchTree( ) { root = nullptr; }; // 初期状態
70
+
71
+ ~BinarySearchTree( ) { makeEmpty( root ); }; // デストラクタ
72
+
73
+ bool insert( const T& data ) { return insert( data, root ); };
74
+
75
+ void show() const { show( root ); std::cout << std::endl; };
76
+
77
+ bool findLargest(T& result) const { return findLargest(root, result); }
78
+
79
+ private:
80
+
81
+ bool insert( const T& data, BinNode* & tree);
82
+
83
+ void makeEmpty( BinNode* tree );
84
+
85
+ void show( BinNode* tree ) const;
86
+
87
+ bool findLargest( BinNode* tree, T& result) const;
88
+
89
+ };
90
+
91
+
92
+
93
+ template<typename T>
94
+
95
+ void BinarySearchTree<T>::makeEmpty( BinarySearchTree<T>::BinNode* tree ) {
96
+
97
+ if( tree !=nullptr ) {
98
+
99
+ makeEmpty( tree->left );
100
+
101
+ makeEmpty( tree->right );
102
+
103
+ delete tree;
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ template<typename T>
112
+
113
+ bool BinarySearchTree<T>::insert( const T& data, BinarySearchTree<T>::BinNode* & tree ) {
114
+
115
+ if (tree == nullptr) {
116
+
117
+ tree = new BinNode(data);
118
+
119
+ return true;
120
+
121
+ }
122
+
123
+ else if (tree->key == data)
124
+
125
+ return false;
126
+
127
+ else if (tree->key > data)
128
+
129
+ return insert(data, tree->left);
130
+
131
+ else
132
+
133
+ return insert(data, tree->right);
134
+
135
+ }
136
+
137
+
138
+
139
+ template<typename T>
140
+
141
+ void BinarySearchTree<T>::show( BinarySearchTree<T>::BinNode* tree ) const {
142
+
143
+ if (tree != nullptr) {
144
+
145
+ show( tree->left );
146
+
147
+ std::cout << tree->key << ". ";
148
+
149
+ show( tree->right );
150
+
151
+ }
152
+
153
+ }
154
+
155
+
156
+
157
+ // 二分木全体の最大値を求める関数
158
+
159
+ template<typename T>
160
+
161
+ bool BinarySearchTree<T>::findLargest( BinarySearchTree<T>::BinNode* tree, T& result) const {
162
+
163
+ if(tree==nullptr) return false;
164
+
165
+ else{
166
+
167
+ result = tree->key;
168
+
169
+ T cmax;
170
+
171
+ if ( findLargest(tree->left, cmax) && cmax > result ) result = cmax;
172
+
173
+ if ( findLargest(tree->right, cmax) && cmax > result ) result = cmax;
174
+
175
+ return true;
176
+
177
+ }
178
+
179
+ }
180
+
181
+
182
+
183
+ int main() {
184
+
185
+ BinarySearchTree<int> bst; // 二分探索木
186
+
187
+
188
+
189
+ for ( int data : { 1, 3, 5, 7, 9, 0, 2, 4, 6, 8} ) {
190
+
191
+ std::cout << "insert: " << data << std::endl;
192
+
193
+ bst.insert(data);
194
+
195
+ bst.show();
196
+
197
+ int max;
198
+
199
+ if ( bst.findLargest(max) ) {
200
+
201
+ std::cout << "最大値は " << max << std::endl << std::endl;
202
+
203
+ }
204
+
205
+ }
206
+
207
+ }
208
+
209
+ ```

3

微修正

2021/02/18 06:25

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -31,3 +31,5 @@
31
31
  空のときは最大値は存在しないのだからなにも返せないはず。
32
32
 
33
33
  たとえば range_error例外をthrowせんならんのでは?
34
+
35
+ ※ int* を返すことにすれば、NULL(nullptr)で"存在しない"ことを表現できるけど。

2

追記

2021/02/18 03:38

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- ついでに:
7
+ [ついでに]
8
8
 
9
9
 
10
10
 
@@ -13,3 +13,21 @@
13
13
  ○ if (rmax>max) max=rmax;
14
14
 
15
15
  ※ max, rmax, lmax の最大値を求めるんだから。
16
+
17
+
18
+
19
+ [おまけ]
20
+
21
+ ```C++
22
+
23
+ int BinarySearchTree::findLargest( BinNode<int>* tree ) const {
24
+
25
+  if(tree==NULL) return false;
26
+
27
+ ```
28
+
29
+ treeが空のとき、false(=0)を返す。
30
+
31
+ 空のときは最大値は存在しないのだからなにも返せないはず。
32
+
33
+ たとえば range_error例外をthrowせんならんのでは?

1

加筆

2021/02/18 03:35

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -1,3 +1,15 @@
1
1
  × int findLargest() const;
2
2
 
3
3
  ○ int findLargest() const { return findLargest(root); }
4
+
5
+
6
+
7
+ ついでに:
8
+
9
+
10
+
11
+ × else max=rmax;
12
+
13
+ ○ if (rmax>max) max=rmax;
14
+
15
+ ※ max, rmax, lmax の最大値を求めるんだから。