回答編集履歴
4
追記
answer
CHANGED
@@ -15,4 +15,91 @@
|
|
15
15
|
treeが空のとき、false(=0)を返す。
|
16
16
|
空のときは最大値は存在しないのだからなにも返せないはず。
|
17
17
|
たとえば range_error例外をthrowせんならんのでは?
|
18
|
-
※ int* を返すことにすれば、NULL(nullptr)で"存在しない"ことを表現できるけど。
|
18
|
+
※ int* を返すことにすれば、NULL(nullptr)で"存在しない"ことを表現できるけど。
|
19
|
+
|
20
|
+
...で、僕ならこう書く:
|
21
|
+
```C++
|
22
|
+
#include <iostream>
|
23
|
+
|
24
|
+
// 二分探索木のクラス
|
25
|
+
template<typename T>
|
26
|
+
class BinarySearchTree {
|
27
|
+
struct BinNode {
|
28
|
+
T key; // 各頂点のキー
|
29
|
+
BinNode* left;
|
30
|
+
BinNode* right;
|
31
|
+
BinNode(T d, BinNode *l=nullptr, BinNode *r=nullptr) : key(d), left(l), right(r) {}
|
32
|
+
};
|
33
|
+
BinNode* root; // 根頂点
|
34
|
+
public:
|
35
|
+
BinarySearchTree( ) { root = nullptr; }; // 初期状態
|
36
|
+
~BinarySearchTree( ) { makeEmpty( root ); }; // デストラクタ
|
37
|
+
bool insert( const T& data ) { return insert( data, root ); };
|
38
|
+
void show() const { show( root ); std::cout << std::endl; };
|
39
|
+
bool findLargest(T& result) const { return findLargest(root, result); }
|
40
|
+
private:
|
41
|
+
bool insert( const T& data, BinNode* & tree);
|
42
|
+
void makeEmpty( BinNode* tree );
|
43
|
+
void show( BinNode* tree ) const;
|
44
|
+
bool findLargest( BinNode* tree, T& result) const;
|
45
|
+
};
|
46
|
+
|
47
|
+
template<typename T>
|
48
|
+
void BinarySearchTree<T>::makeEmpty( BinarySearchTree<T>::BinNode* tree ) {
|
49
|
+
if( tree !=nullptr ) {
|
50
|
+
makeEmpty( tree->left );
|
51
|
+
makeEmpty( tree->right );
|
52
|
+
delete tree;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
template<typename T>
|
57
|
+
bool BinarySearchTree<T>::insert( const T& data, BinarySearchTree<T>::BinNode* & tree ) {
|
58
|
+
if (tree == nullptr) {
|
59
|
+
tree = new BinNode(data);
|
60
|
+
return true;
|
61
|
+
}
|
62
|
+
else if (tree->key == data)
|
63
|
+
return false;
|
64
|
+
else if (tree->key > data)
|
65
|
+
return insert(data, tree->left);
|
66
|
+
else
|
67
|
+
return insert(data, tree->right);
|
68
|
+
}
|
69
|
+
|
70
|
+
template<typename T>
|
71
|
+
void BinarySearchTree<T>::show( BinarySearchTree<T>::BinNode* tree ) const {
|
72
|
+
if (tree != nullptr) {
|
73
|
+
show( tree->left );
|
74
|
+
std::cout << tree->key << ". ";
|
75
|
+
show( tree->right );
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
// 二分木全体の最大値を求める関数
|
80
|
+
template<typename T>
|
81
|
+
bool BinarySearchTree<T>::findLargest( BinarySearchTree<T>::BinNode* tree, T& result) const {
|
82
|
+
if(tree==nullptr) return false;
|
83
|
+
else{
|
84
|
+
result = tree->key;
|
85
|
+
T cmax;
|
86
|
+
if ( findLargest(tree->left, cmax) && cmax > result ) result = cmax;
|
87
|
+
if ( findLargest(tree->right, cmax) && cmax > result ) result = cmax;
|
88
|
+
return true;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
int main() {
|
93
|
+
BinarySearchTree<int> bst; // 二分探索木
|
94
|
+
|
95
|
+
for ( int data : { 1, 3, 5, 7, 9, 0, 2, 4, 6, 8} ) {
|
96
|
+
std::cout << "insert: " << data << std::endl;
|
97
|
+
bst.insert(data);
|
98
|
+
bst.show();
|
99
|
+
int max;
|
100
|
+
if ( bst.findLargest(max) ) {
|
101
|
+
std::cout << "最大値は " << max << std::endl << std::endl;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
```
|
3
微修正
answer
CHANGED
@@ -14,4 +14,5 @@
|
|
14
14
|
```
|
15
15
|
treeが空のとき、false(=0)を返す。
|
16
16
|
空のときは最大値は存在しないのだからなにも返せないはず。
|
17
|
-
たとえば range_error例外をthrowせんならんのでは?
|
17
|
+
たとえば range_error例外をthrowせんならんのでは?
|
18
|
+
※ int* を返すことにすれば、NULL(nullptr)で"存在しない"ことを表現できるけど。
|
2
追記
answer
CHANGED
@@ -1,8 +1,17 @@
|
|
1
1
|
× int findLargest() const;
|
2
2
|
○ int findLargest() const { return findLargest(root); }
|
3
3
|
|
4
|
-
ついでに
|
4
|
+
[ついでに]
|
5
5
|
|
6
6
|
× else max=rmax;
|
7
7
|
○ if (rmax>max) max=rmax;
|
8
|
-
※ max, rmax, lmax の最大値を求めるんだから。
|
8
|
+
※ max, rmax, lmax の最大値を求めるんだから。
|
9
|
+
|
10
|
+
[おまけ]
|
11
|
+
```C++
|
12
|
+
int BinarySearchTree::findLargest( BinNode<int>* tree ) const {
|
13
|
+
if(tree==NULL) return false;
|
14
|
+
```
|
15
|
+
treeが空のとき、false(=0)を返す。
|
16
|
+
空のときは最大値は存在しないのだからなにも返せないはず。
|
17
|
+
たとえば range_error例外をthrowせんならんのでは?
|
1
加筆
answer
CHANGED
@@ -1,2 +1,8 @@
|
|
1
1
|
× int findLargest() const;
|
2
|
-
○ int findLargest() const { return findLargest(root); }
|
2
|
+
○ int findLargest() const { return findLargest(root); }
|
3
|
+
|
4
|
+
ついでに:
|
5
|
+
|
6
|
+
× else max=rmax;
|
7
|
+
○ if (rmax>max) max=rmax;
|
8
|
+
※ max, rmax, lmax の最大値を求めるんだから。
|