15 #ifndef INCLUDE_BINARYTREE_HH_
16 #define INCLUDE_BINARYTREE_HH_
40 template <
typename T,
typename Comparator = std::less<T>>
61 template <
class T,
typename Comparator>
77 template <
class T,
typename Comparator>
80 template <
typename T,
typename Comparator>
177 std::swap(*
this, copy);
273 void preOrder(std::ostream &out = std::cout)
const;
289 void inOrder(std::ostream &out = std::cout)
const;
297 void inOrder_loop( std::ostream &out = std::cout)
const;
305 void postOrder(std::ostream &out = std::cout)
const;
321 void levelOrder(std::ostream &out = std::cout)
const;
592 void swaplr(BinNode *_root);
605 template <
class T,
typename Comparator>
608 template <
class T,
typename Comparator>
614 template <
class T,
typename Comparator>
623 template <
class T,
typename Comparator>
635 template <
class T,
typename Comparator>
641 template <
class T,
typename Comparator>
647 template <
class T,
typename Comparator>
655 template <
class T,
class U>
659 if (!_root1 && !_root2)
662 if (!_root1 || !_root2)
665 if (!value_type_equal(_root1->
data, _root2->
data))
668 return is_symmetrical_subtree(_root1->
left, _root2->
right) && is_symmetrical_subtree(_root2->
left, _root1->
right);
671 template <
class T,
class U>
675 if (!_root1 && !_root2)
678 if (!_root1 || !_root2)
681 if (!value_type_equal(_root1->data, _root2->data))
683 return is_equal_subtree(_root1->left, _root2->left) && is_equal_subtree(_root1->right, _root2->right);
686 template <
typename T,
typename U>
689 size_type size_of_tree = size_loop();
690 size_type height_of_tree = height_loop();
693 if (((size_type(1) << (height_of_tree + 1)) - 1) == size_of_tree)
698 template <
class T,
typename Comparator>
705 BinNode *current_node =
nullptr;
707 node_queue.
push(root_);
708 while (!node_queue.empty())
710 current_node = node_queue.front();
713 if (!current_node->
left && current_node->
right)
716 if (current_node->
left)
717 node_queue.push(current_node->
left);
718 if (current_node->
right)
719 node_queue.push(current_node->
right);
727 if (!current_node->
left || !current_node->
right)
730 while (!node_queue.empty())
732 current_node = node_queue.front();
735 if (current_node->
left || current_node->
right)
742 throw(TreeException(
"Bug in member function \"isCompleteTree\", since control should not reach here",
"isCompleteTree()"));
745 template <
class T,
typename Comparator>
751 out << _root->data <<
' ';
752 preOrder(_root->left, out);
753 preOrder(_root->right, out);
756 template <
class T,
typename Comparator>
767 typedef BinNode *SnapShotStruct;
779 SnapShotStruct currentSnapshot{root_};
780 snapshotStack.
push(currentSnapshot);
783 while (!snapshotStack.
empty())
785 currentSnapshot = snapshotStack.
top();
792 if (!currentSnapshot)
799 out << currentSnapshot->data <<
' ';
808 if (currentSnapshot->right)
809 snapshotStack.
push(currentSnapshot->right);
810 if (currentSnapshot->left)
811 snapshotStack.
push(currentSnapshot->left);
818 template <
class T,
typename Comparator>
821 preOrder(root_, out);
824 template <
class T,
typename Comparator>
830 inOrder(root->left, out);
831 out << root->data <<
' ';
832 inOrder(root->right, out);
835 template <
class T,
typename Comparator>
839 struct SnapShotStruct
849 SnapShotStruct currentSnapshot{root_, 0};
850 snapshotStack.
push(currentSnapshot);
853 while (!snapshotStack.
empty())
855 currentSnapshot = snapshotStack.
top();
859 switch (currentSnapshot.stage)
864 if (!currentSnapshot.root)
870 currentSnapshot.stage = 1;
871 snapshotStack.
push(currentSnapshot);
872 snapshotStack.
push(SnapShotStruct{currentSnapshot.root->left, 0});
878 out << currentSnapshot.root->data <<
' ';
879 snapshotStack.
push(SnapShotStruct{currentSnapshot.root->right, 0});
893 template <
class T,
typename Comparator>
899 template <
class T,
typename Comparator>
905 postOrder(root->left, out);
906 postOrder(root->right, out);
907 out << root->data <<
' ';
910 template <
class T,
typename Comparator>
914 struct SnapShotStruct
924 SnapShotStruct currentSnapshot{root_, 0};
925 snapshotStack.
push(currentSnapshot);
928 while (!snapshotStack.
empty())
930 currentSnapshot = snapshotStack.
top();
934 switch (currentSnapshot.stage)
939 if (!currentSnapshot.root)
945 currentSnapshot.stage = 1;
946 snapshotStack.
push(currentSnapshot);
950 snapshotStack.
push(SnapShotStruct{currentSnapshot.root->right, 0});
951 snapshotStack.
push(SnapShotStruct{currentSnapshot.root->left, 0});
957 out << currentSnapshot.root->data <<
' ';
971 template <
class T,
typename Comparator>
974 postOrder(root_, out);
977 template <
class T,
typename Comparator>
983 BinNode *current_node =
nullptr;
985 node_queue.
push(root_);
987 while (!node_queue.empty())
989 current_node = node_queue.front();
992 if (current_node->left)
993 node_queue.push(current_node->left);
995 if (current_node->right)
996 node_queue.push(current_node->right);
998 out << current_node->data <<
' ';
1002 template <
class T,
typename Comparator>
1008 if (value_type_equal(root->data, x))
1011 BinNode *target =
nullptr;
1012 if ((target = find(x, root->left)))
1015 return find(x, root->right);
1018 template <
class T,
typename Comparator>
1021 BinNode *target = find(x, root_);
1025 clear(target->left);
1028 template <
class T,
typename Comparator>
1035 clear(target->
right);
1038 template <
class T,
typename Comparator>
1042 if (!target || !target->
left)
1048 template <
class T,
typename Comparator>
1052 if (!target || !target->
right)
1058 template <
class T,
typename Comparator>
1064 swaplr(_root->left);
1065 swaplr(_root->right);
1066 std::swap(_root->left, _root->right);
1069 template <
class T,
typename Comparator>
1073 if (!_root || (!_root->left && !_root->right))
1076 return static_cast<size_type>(_root->left && _root->right) + CountDegreeTwo(_root->left) + CountDegreeTwo(_root->right);
1079 template <
class T,
typename Comparator>
1083 if (!_root || (!_root->left && !_root->right))
1086 size_type left_height = height(_root->left);
1087 size_type right_height = height(_root->right);
1088 return 1 + ((left_height > right_height) ? left_height : right_height);
1091 template <
class T,
typename Comparator>
1095 struct SnapShotStruct
1111 SnapShotStruct currentSnapshot{__root, 0, 0, 0};
1112 snapshotStack.
push(currentSnapshot);
1115 while (!snapshotStack.
empty())
1117 currentSnapshot = snapshotStack.
top();
1118 snapshotStack.
pop();
1121 switch (currentSnapshot.stage)
1126 if (!currentSnapshot._root || (!currentSnapshot._root->left && !currentSnapshot._root->right))
1134 currentSnapshot.stage = 1;
1135 snapshotStack.
push(currentSnapshot);
1138 SnapShotStruct newSnapshot{currentSnapshot._root->left, 0, 0, 0};
1139 snapshotStack.
push(newSnapshot);
1148 currentSnapshot.left_height = returnVal;
1149 currentSnapshot.stage = 2;
1150 snapshotStack.
push(currentSnapshot);
1153 SnapShotStruct newSnapshot{currentSnapshot._root->right, 0, 0, 0};
1154 snapshotStack.
push(newSnapshot);
1162 currentSnapshot.right_height = returnVal;
1165 returnVal = 1 + ((currentSnapshot.left_height > currentSnapshot.right_height) ? currentSnapshot.left_height : currentSnapshot.right_height);
1182 template <
class T,
typename Comparator>
1185 return height(root_);
1188 template <
class T,
typename Comparator>
1191 return height_loop(root_);
1194 template <
class T,
typename Comparator>
1204 return 1 + size(_root->
left) + size(_root->
right);
1207 template <
class T,
typename Comparator>
1211 struct SnapShotStruct
1226 SnapShotStruct currentSnapshot{__root, 0, 0};
1227 snapshotStack.
push(currentSnapshot);
1230 while (!snapshotStack.
empty())
1232 currentSnapshot = snapshotStack.
top();
1233 snapshotStack.
pop();
1236 switch (currentSnapshot.stage)
1241 if (!currentSnapshot._root)
1251 currentSnapshot.stage = 1;
1252 snapshotStack.
push(currentSnapshot);
1255 SnapShotStruct newSnapshot{currentSnapshot._root->left, 0, 0};
1256 snapshotStack.
push(newSnapshot);
1264 currentSnapshot.addVal = returnVal;
1267 currentSnapshot.stage = 2;
1268 snapshotStack.
push(currentSnapshot);
1271 SnapShotStruct newSnapshot{currentSnapshot._root->right, 0, 0};
1272 snapshotStack.
push(newSnapshot);
1281 returnVal = 1 + currentSnapshot.addVal + returnVal;
1299 template <
class T,
typename Comparator>
1305 template <
class T,
typename Comparator>
1308 return size_loop(root_);
1311 template <
class T,
typename Comparator>
1323 std::cout <<
"\n请输入根结点: ";
1327 if (std::cin.fail() || value_type_equal(this_data, flag))
1331 std::cerr <<
"根结点必须含合法数据!\n";
1337 root_ =
new BinNode(this_data, 0, 0);
1338 node_queue.push(root_);
1340 while (!node_queue.empty())
1343 this_node = node_queue.front();
1350 std::cout <<
"\n请输入结点:" << this_node->data <<
" 的left child(输入 " << flag <<
" 表示无):\n";
1358 std::cerr <<
"非法输入!";
1363 if (value_type_equal(this_data, flag))
1366 std::cout <<
"已指定该结点无left child\n";
1371 std::cout <<
"已指定左child为:" << this_data <<
'\n';
1372 node_queue.push(this_node->left =
new BinNode(this_data, 0, 0));
1381 std::cout <<
"\n请输入结点:" << this_node->data <<
" 的right child(输入 " << flag <<
" 表示无):\n";
1389 std::cerr <<
"非法输入!";
1394 if (value_type_equal(this_data, flag))
1397 std::cout <<
"已指定该结点无right child\n";
1402 std::cout <<
"已指定右child为:" << this_data <<
'\n';
1403 node_queue.push(this_node->right =
new BinNode(this_data, 0, 0));
1413 template <
class T,
typename Comparator>
1416 if (bin_tree.empty())
1422 out <<
"(层次打印) 格式:\"parent lchild rchild\" ( " << flag <<
" 表示空结点 )\n";
1429 node_data_queue.push(bin_tree.root(flag));
1431 while (!node_data_queue.empty())
1434 this_data = node_data_queue.front();
1435 node_data_queue.pop();
1438 out << this_data <<
'\t' << (left_data = bin_tree.lchild(this_data, flag)) <<
'\t' << (right_data = bin_tree.
rchild(this_data, flag)) <<
'\n';
1441 if (!bin_tree.value_type_equal(left_data, flag))
1442 node_data_queue.push(left_data);
1444 if (!bin_tree.value_type_equal(right_data, flag))
1445 node_data_queue.push(right_data);
1449 template <
class T,
typename Comparator>
1450 bool operator==(
const binaryTree<T, Comparator> &lhs,
const binaryTree<T, Comparator> &rhs)