main.cpp
来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 144 行
CPP
144 行
#include "../../C/UTILITY.H"
void write_ent(int &x)
{
cout << x << " , ";
}
#include "../../6/LINKLIST/LIST.H"
#include "../../6/LINKLIST/LIST.CPP"
#include "../../8/LINKLIST/SORTABLE.H"
#include "../../8/LINKLIST/MERGE.CPP"
#include "../BT/NODE.H"
#include "../BT/TREE.H"
#include "../BT/NODE.CPP"
#include "../BT/TREE.CPP"
#include "../BST/STREE.H"
#include "../BST/SNODE.CPP"
#include "../BST/STREE.CPP"
#include "BUILDTRE.H"
#include "BUILD.CPP"
template <class Entry>
void pr_node(Binary_node<Entry> *&x)
{ // tree testing function
cout << "( " << x->data << " : -> ";
switch (x->get_balance()) {
case left_higher: cout << "L"; break;
case right_higher: cout << "R"; break;
case equal_height: cout << "E"; break;
}
cout << " ) ===> ";
if (x->left != NULL) cout << (x->left)->data << " ";
if (x->right != NULL) cout << (x->right)->data << " ";
cout << " \n" << flush;
}
template <class Entry>
void prenode(Binary_node<Entry> *root, void (*f)(Binary_node<Entry> *&))
{ // tree testing function
if (root != NULL) {
(*f)(root);
prenode(root->left, f);
prenode(root->right, f);
}
}
template <class Entry>
void Binary_tree<Entry>::prenode(void (*f)(Binary_node<Entry> *&))
{ // tree testing function
if (root != NULL) {
(*f)(root);
::prenode(root->left, f);
::prenode(root->right, f);
}
}
int get_int()
{
char c;
int ans = 0, sign = 1;
do {
cin.get(c);
if (c == '-') sign = -1;
} while (c < '0' || c > '9');
while (c >= '0' && c <= '9') {
ans = 10 * ans + c - '0';
cin.get(c);
}
return ans * sign;
}
char get_command()
{
char c, d;
cout << "Select command (H for help) and press <Enter>:";
while (1) {
do {
cin.get(c);
} while (c == '\n');
do {
cin.get(d);
} while (d != '\n');
c = tolower(c);
if (c == 'r' || c == '#' || c == 'i' || c == 'c' ||
c == 'q' || c == 'p') {
return c;
}
cout << "Please enter a valid command or H for help:";
cout << "\n\t[R]emove entry\t[P]rint tree\t[#] size of tree\n"
<< "\t[C]lear tree\t[I]nsert entry\n"
<< "\t[Q]uit.\n";
}
}
int do_command(char c, Search_tree<int> &test_tree)
{
int x;
int sz;
switch (c) {
case 'r':
cout << "Enter integer to remove:" << flush;
x = get_int();
if (test_tree.remove(x) != success) cout << "Not found!\n";
break;
case 'i':
cout << "Enter new integer to insert:";
x = get_int();
test_tree.insert(x);
break;
case 'c':
test_tree.clear();
cout << "Tree is cleared.\n";
break;
case 'p':
sz = test_tree.size();
if (sz == 0) cout << "Tree is empty.\n";
else test_tree.prenode(pr_node);
break;
case '#':
cout << "The size of the tree is " << test_tree.size() << "\n";
break;
case 'q':
cout << "Tree demonstration finished.\n";
return 0;
}
return 1;
}
int main()
{
Binary_tree<int> s; Binary_tree<int> t2 = s;
Search_tree<int> s1; Search_tree<int> t1 = s1;
List<int> supply;
cout << "Building a tree from squares" << endl;
for (int i = 0; i < 20; i++) supply.insert(i, i * i);
Buildable_tree<int> t;
t.build_tree(supply);
t.inorder(write_ent);
while (do_command(get_command(), t));
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?