main.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 122 行

CPP
122
字号
#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 "../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 "ANODE.H"

 

template <class Entry>
void pr_node(Binary_node<Entry> *&x)
{
   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;
}
 
#include "ATREE.H"
#include "ANODE.CPP"
#include "ATREE.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\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, AVL_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> t = s;
   Search_tree<int> s1; Search_tree<int> t1 = s1;

   AVL_tree<int> test_tree;
   while (do_command(get_command(), test_tree));
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?