main.cpp
来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 120 行
CPP
120 行
#include "../../C/UTILITY.H"
#include "../../6/LINKLIST/LIST.H"
#include "../../6/LINKLIST/LIST.CPP"
#include "../../8/LINKLIST/SORTABLE.H"
#include "../../8/LINKLIST/MERGE.CPP"
void write_ent(int &x)
{
cout << x << " , ";
}
#include "NODE.H"
#include "../../10/BT/TREE.H"
#include "NODE.CPP"
#include "../../10/BT/TREE.CPP"
#include "../../10/BST/STREE.H"
#include "../../10/BST/SNODE.CPP"
#include "../../10/BST/STREE.CPP"
#include "RBCODE.H"
#include "RBNODE.H"
template <class Entry>
void pr_node(Binary_node<Entry> *&x)
{
cout << "( " << x->data << " : -> " << " ";
switch (x->get_color()) {
case red: cout << " R"; break;
case black: cout << " B"; break;
}
cout << " ) ===> ";
if (x->left != NULL) cout << (x->left)->data << " ";
if (x->right != NULL) cout << (x->right)->data << " ";
cout << " \n" << flush;
}
#include "RBTREE.H"
#include "RBNODE.CPP"
#include "RBTREE.CPP"
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\tRemove 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, RB_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 << "RB-tree demonstration finished.\n";
return 0;
}
return 1;
}
main()
{
Binary_tree<int> t; Binary_tree<int> s = t;
Search_tree<int> t1; Search_tree<int> s1 = t1;
RB_tree<int> test_tree;
while (do_command(get_command(), test_tree));
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?