Guest User

Untitled

a guest
Nov 3rd, 2011
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <queue>
  4. #include <cstdlib>
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. struct BSTNode {
  11.                 int value;
  12.                 BSTNode* left;
  13.                 BSTNode* right;
  14. };
  15.  
  16. void unbalancedInsert(BSTNode* &head, int v)
  17. {
  18.         //empty tree or foundlocation
  19.         if(head == NULL)
  20.         {
  21.                 head = new BSTNode;
  22.                 head->value = v;
  23.                 head->left = NULL;
  24.                 head->right = NULL;
  25.  
  26.         }
  27.  
  28.         else if(v < head->value)
  29.         {
  30.                 unbalancedInsert(head->left, v);
  31.         }
  32.  
  33.         else if(v > head->value)
  34.         {
  35.                 unbalancedInsert(head->right, v);
  36.         }
  37.  
  38.         else
  39.         {
  40.                 cout << "Duplicate ignored" << endl;
  41.         }
  42.  
  43. }
  44.  
  45. void inOrder(BSTNode* root)
  46. {
  47.  
  48.  
  49. }
  50.  
  51. void postOrder(BSTNode* root)
  52. {
  53.  
  54.  
  55. }
  56.  
  57.  
  58. void preOrder(BSTNode* root)
  59. {
  60.  
  61.  
  62. }
  63.  
  64.  
  65. void levelOrder(BSTNode* root)
  66. {
  67.  
  68.  
  69. }
  70.  
  71.  
  72. int main() {
  73.  
  74.         //create the tree
  75.         BSTNode* head = NULL;
  76.         unbalancedInsert(head, 54);
  77.         unbalancedInsert(head, 22);
  78.         unbalancedInsert(head, 17);
  79.         unbalancedInsert(head, 41);
  80.         unbalancedInsert(head, 36);
  81.         unbalancedInsert(head, 30);
  82.         unbalancedInsert(head, 74);
  83.         unbalancedInsert(head, 76);
  84.         unbalancedInsert(head, 26);
  85.         unbalancedInsert(head, 27);
  86.         unbalancedInsert(head, 45);
  87.         unbalancedInsert(head, 82);
  88.  
  89.         cout << endl;
  90.         cout << "Ricky to the rescue:" << endl;
  91.         cout << "printing inorder: ";
  92.         inOrder(head);
  93.         cout << endl;
  94.         cout<< "Answer:           17 22 26 27 30 36 41 45 54 74 76 82";
  95.         cout << endl;
  96.         cout << endl;
  97.         cout << "printing postorder: ";
  98.         postOrder(head);
  99.         cout << endl;
  100.         cout <<"Answer:             17 27 26 30 36 45 41 22 82 76 74 54  ";
  101.         cout << endl;
  102.  
  103.         cout << endl;
  104.         cout << endl;
  105.         cout << "Stacey saves the day" << endl;
  106.         cout << "printing preorder: ";
  107.         preOrder(head);
  108.         cout << endl;
  109.         cout <<"Answer:            54 22 17 41 36 30 26 27 45 74 76 82";
  110.         cout << endl;
  111.         cout << endl;
  112.         cout << "Quiet down, Quinn's in Town" << endl;
  113.         cout << "printing levelorder: ";
  114.         levelOrder(head);
  115.         cout << endl;
  116.  
  117.         cout <<"Answer:              54 22 74 17 41 76 36 45 82 30 26 27";
  118.         cout << endl;
  119.  
  120.  
  121.         return 0;
  122. }
  123.  
  124.  
Advertisement
Add Comment
Please, Sign In to add comment