home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / treeprt.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-14  |  5.3 KB  |  161 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: treeprt.cpp
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 01/23/1997 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. Tree printing functions used to debug the code that works with
  32. (R)ed (B)lack binary search trees.   
  33. */
  34. // ----------------------------------------------------------- //   
  35. #include <iostream.h>
  36. #include "treeprt.h"
  37. #include "twalk.h"
  38.  
  39. BNodeXY *GetNodePosn(BNodeBase *Tree, int &x, int y, NodeWidthFunc NodeWidth)
  40. // Using an in-order traversal, figure out x and y of each node
  41. // in the tree Tree, assuming that the top Left hand corner of the
  42. // box bounding Tree is at (x, y). Builds a parallel shadow tree
  43. // of nodes having x & y in them.
  44. {
  45.   if (Tree == 0) return 0;
  46.   BNodeXY *l = GetNodePosn(Tree->Left, x, y+1, NodeWidth);
  47.   BNodeXY *xyt = new BNodeXY;
  48.   xyt->Left = l;
  49.   xyt->x = x;
  50.   x += NodeWidth(Tree);
  51.   xyt->y = y;
  52.   y++;
  53.   xyt->Right = GetNodePosn(Tree->Right, x, y, NodeWidth);
  54.   return xyt;
  55. }
  56.  
  57. void PrintUp(BNodeBase *Tree, PrtFunc PrtNode, NodeWidthFunc NodeWidth)
  58. // Print tree Tree upright, using positions in xyt as a guide
  59. {
  60.   int spacing, last_sp, x, oldy;
  61.   BNodeBase *nxt;
  62.   BNodeXY *xynxt;
  63.  
  64.   if (Tree == 0) { cout << "Empty tree\n"; return; }
  65.  
  66.   x = 1;
  67.   BNodeXY *xyt = GetNodePosn(Tree, x, 0, NodeWidth);
  68.  
  69.   TreeWalkerb tw(Tree, LVLORDER);
  70.   TreeWalker<BNodeXY> xytw(xyt, LVLORDER);
  71.  
  72.   oldy = 0;
  73.   last_sp = 0;
  74.   while(1) {
  75.     xynxt = xytw.Next();
  76.     if (xynxt == 0) break;
  77.     if (xynxt->y != oldy) {
  78.        oldy = xynxt->y;
  79.        cout << '\n';
  80.        last_sp = 0;
  81.     }
  82.     spacing = xynxt->x - last_sp - 1;
  83.     if (spacing < 0) spacing = 0;
  84.     while(spacing--) cout << ' ';
  85.     nxt = tw.Next();
  86.     PrtNode(nxt);
  87.     last_sp = xynxt->x + NodeWidth(nxt) - 1;
  88.   }
  89.   cout << "\n\n";
  90.  
  91.   // Need to delete shadow tree. Easy to do with postorder traversal
  92.   xytw.Reset(xyt, POSTORDER);
  93.   while(1) {
  94.     xynxt = xytw.Next();
  95.     if (xynxt == 0) break;
  96.     delete xynxt;
  97.   }
  98. }
  99.  
  100. void PrintSideWays(BNodeBase *Tree, PrtFunc PrtNode, int inc, int space)
  101. // Using a right-to-Left in-order traversal
  102. {
  103.   while(Tree) {
  104.     PrintSideWays(Tree->Right, PrtNode, inc, space+inc);
  105.     for(int i = 0; i<space; i++) cout << ' ';
  106.     PrtNode(Tree); cout << '\n';
  107.     Tree = Tree->Left;
  108.     space += inc;
  109.   }
  110. }
  111.  
  112. void TestTraversals(BNodeBase *root, VisitFunc Visit)
  113. // Test every traversal method
  114. {
  115.   BNodeBase *nxt;
  116.  
  117.   if (root == 0) { cout << "Tree is empty\n"; return; }
  118.  
  119.   cout << "RecPre:   "; PreOrder(root, Visit); cout << '\n';
  120.   cout << "FlatPre:  "; PreOrderFlat(root, Visit); cout << '\n';
  121.   cout << "IterPre:  ";
  122.   TreeWalkerb tw(root, PREORDER);
  123.   while((nxt = tw.Next()) != 0) Visit(nxt);
  124.   cout << '\n';
  125.  
  126.   cout << "RecIn:    "; InOrder(root, Visit); cout << '\n';
  127.   cout << "FlatIn:   "; InOrderFlat(root, Visit); cout << '\n';
  128.   cout << "IterIn:   ";
  129.   tw.Reset(root, INORDER);
  130.   while((nxt = tw.Next()) != 0) Visit(nxt);
  131.   cout << '\n';
  132.  
  133.   cout << "RecPost:  "; PostOrder(root, Visit); cout << '\n';
  134.   cout << "FlatPost: "; PostOrderFlat(root, Visit); cout << '\n';
  135.   cout << "IterPost: ";
  136.   tw.Reset(root, POSTORDER);
  137.   while((nxt = tw.Next()) != 0) Visit(nxt);
  138.   cout << '\n';
  139.  
  140.   cout << "FlatLvl:  "; LvlOrder(root, Visit);  cout << '\n';
  141.   cout << "IterLvl:  ";
  142.   tw.Reset(root, LVLORDER);
  143.   while((nxt = tw.Next()) != 0) Visit(nxt);
  144.   cout << '\n';
  145. }
  146.  
  147. void TestTraversalsII(BNodeBase *root, VisitFunc Visit)
  148. // Shorthand version
  149. {
  150.   if (root == 0) { cout << "Tree is empty\n"; return; }
  151.  
  152.   cout << "FlatPre:  "; PreOrderFlat(root, Visit); cout << '\n';
  153.   cout << "FlatIn:   "; InOrderFlat(root, Visit); cout << '\n';
  154.   cout << "FlatPost: "; PostOrderFlat(root, Visit); cout << '\n';
  155.   cout << "FlatLvl:  "; LvlOrder(root, Visit);  cout << '\n';
  156. }
  157. // ----------------------------------------------------------- // 
  158. // ------------------------------- //
  159. // --------- End of File --------- //
  160. // ------------------------------- //
  161.