Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1994 - 1999
  6. //
  7. // File: tstsplay.cxx
  8. //
  9. //--------------------------------------------------------------------------
  10. ////////////////////////////////////////////////////////////
  11. //
  12. // File name: testsplay.cxx
  13. //
  14. // Title: Splay Tree class
  15. //
  16. // Desciption: Used for testing the splay tree class
  17. //
  18. // Author: Scott Holden (t-scotth)
  19. //
  20. // Date: August 16, 1994
  21. //
  22. ////////////////////////////////////////////////////////////
  23. #include <iostream.h>
  24. #include <fstream.h>
  25. #include "String.hxx"
  26. #include "splaytre.hxx"
  27. void __cdecl
  28. main( int argc, char *argv[] )
  29. {
  30. if ( argc != 2 ) {
  31. cerr << "Error: wrong number of arguments" << endl;
  32. return;
  33. }
  34. ifstream dictionary( argv[1] );
  35. if ( !dictionary ) {
  36. cerr << "Error: cannot open dictionary file" << endl;
  37. return;
  38. }
  39. String words;
  40. SplayTree<String> *DictTree = new SplayTree<String>;
  41. for ( ; !dictionary.eof(); ) {
  42. dictionary >> words;
  43. DictTree->Insert( new String( words ) );
  44. }
  45. String entry;
  46. String saveWord( "A" );
  47. String *FindDelWord;
  48. for ( ; ; ) {
  49. cout << "Options: quit, add <word>, find <word>, delete <word>, size (of dict), +, -, max, min\n" << endl;
  50. cin >> entry;
  51. if ( ( entry == "quit" ) || ( entry == "q" ) ) {
  52. break;
  53. }
  54. else if ( ( entry == "size" ) || ( entry == "s" ) ) {
  55. cout << DictTree->Size() << " words in the dictionary\n" << endl;
  56. }
  57. #ifdef DEBUGRPC
  58. else if ( ( entry == "height" ) || ( entry == "h" ) ) {
  59. cout << DictTree->Depth() << " is the current heigth of the tree\n" << endl;
  60. }
  61. #endif // DEBUGRPC
  62. else if ( ( entry == "add" ) || ( entry == "a" ) ) {
  63. cin >> words;
  64. DictTree->Insert( new String( words ) );
  65. cout << endl;
  66. saveWord = words;
  67. }
  68. else if ( ( entry == "find" ) || ( entry == "f" ) ) {
  69. cin >> words;
  70. FindDelWord = DictTree->Find( &words );
  71. if ( FindDelWord ) {
  72. cout << *FindDelWord << " is in the dictionary\n" << endl;
  73. saveWord = *FindDelWord;
  74. }
  75. else cout << "The word is not in the dictionary\n" << endl;
  76. }
  77. else if ( ( entry == "delete" ) || ( entry == "d" ) ) {
  78. cin >> words;
  79. DictTree->Delete( &words );
  80. cout << endl;
  81. }
  82. else if ( entry == "+" ) {
  83. FindDelWord = DictTree->Successor( &saveWord );
  84. if ( FindDelWord ) {
  85. cout << "Successor of previous find/insert: " << *FindDelWord << endl << endl;
  86. saveWord = *FindDelWord;
  87. }
  88. }
  89. else if ( entry == "-" ) {
  90. FindDelWord = DictTree->Predecessor( &saveWord );
  91. if ( FindDelWord ) {
  92. cout << "Predecessor of previous find/insert: " << *FindDelWord << endl << endl;
  93. saveWord = *FindDelWord;
  94. }
  95. }
  96. else if ( entry == "max" ) {
  97. FindDelWord = DictTree->Maximum();
  98. if ( FindDelWord ) {
  99. cout << "The greatest word in the dictionary is: " << *FindDelWord << endl << endl;
  100. }
  101. }
  102. else if ( entry == "min" ) {
  103. FindDelWord = DictTree->Minimum();
  104. if ( FindDelWord ) {
  105. cout << "The smallest word in the dictionary is: " << *FindDelWord << endl << endl;
  106. }
  107. }
  108. else cerr << "Error: Wrong options" << endl;
  109. }
  110. //DictTree->Print();
  111. }