Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.73 KB | None | 0 0
  1. //
  2. //  BST.h
  3. //  BSThw
  4. //
  5. //  Created by Joshua Garza on 4/12/18.
  6. //  Copyright © 2018 Joshua Garza. All rights reserved.
  7. //
  8.  
  9. #ifndef BST_h
  10. #define BST_h
  11. #include <iostream>
  12. #include <algorithm>
  13. using namespace std;
  14.  
  15. class binarySearchTree
  16. {
  17. private:
  18.     class node
  19.     {
  20.     public:
  21.         node * left;
  22.         node * right;
  23.         double data;
  24.        
  25.         node(double x)
  26.         {
  27.             left = NULL;
  28.             right = NULL;
  29.             data = x;
  30.         }
  31.        
  32.     };
  33.    
  34.     node * root;
  35.     node * temp;
  36.     node * current;
  37.     node * doomedNode;
  38.     node * trailCurrent;
  39.  
  40.     void recInsert(node * &r, double x)
  41.     {
  42.         if (r == NULL)
  43.         {
  44.             r = new node(x);
  45.         }
  46.         else
  47.         {
  48.             //check to see if x is less than root->data
  49.             if (x < r->data)
  50.             {
  51.                 recInsert(r->left, x);
  52.             }
  53.             else //check to see if x is greater than root->data
  54.             {
  55.                 recInsert(r->right, x);
  56.             }
  57.         }
  58.     }
  59.  
  60.     node * recRemove(node * root, double data)
  61.     {
  62.         //3 cases: no children, one child, 2 children
  63.         if (root == NULL)
  64.         {
  65.             return NULL;
  66.         }
  67.         else if (data < root->data)
  68.         {
  69.             root->left = recRemove(root->left, data);
  70.         }
  71.         else if(data > root->data)
  72.         {
  73.             root->right = recRemove(root->right, data);
  74.         }
  75.         else
  76.         {
  77.             if (root->right == NULL && root->left == NULL) //no children
  78.             {
  79.                 delete root;
  80.                 root = NULL;
  81.                 return root;
  82.             }
  83.             else if(root->left == NULL) //only right child
  84.             {
  85.                 temp = root;
  86.                 root = root->right;
  87.                 delete temp;
  88.                 return root;
  89.             }
  90.             else if(root->right == NULL) //only left child
  91.             {
  92.                 temp = root;
  93.                 root = root->left;
  94.                 delete temp;
  95.                 return root;
  96.             }
  97.             else //2 children
  98.             {
  99.                 temp->data = findMin(root->right);
  100.                 root->data = temp->data;
  101.                 root->right = recRemove(root->right, temp->data);
  102.             }
  103.         }
  104.        
  105.         return root;
  106.     }
  107.  
  108.     void displayInOrder(node * r) //display tree in oder
  109.     {
  110.         if(r == NULL)
  111.         {
  112.             //do nothing
  113.         }
  114.         else
  115.         {
  116.             displayInOrder(r->left);
  117.             cout << r->data << endl;
  118.             displayInOrder(r->right);
  119.         }
  120.     }
  121.  
  122.     void displayPreOrder(node * r)
  123.     {
  124.         if(r == NULL)
  125.         {
  126.             //do nothing
  127.         }
  128.         else
  129.         {  
  130.             cout << r->data << endl;
  131.             displayPreOrder(r->left);
  132.             displayPreOrder(r->right);
  133.         }
  134.     }
  135.  
  136.     void displayPostOrder(node * r)
  137.     {
  138.         if(r == NULL)
  139.         {
  140.             //do nothing
  141.         }
  142.         else
  143.         {
  144.             displayPostOrder(r->left);
  145.             displayPostOrder(r->right);
  146.             cout << r->data << endl;
  147.         }
  148.     }
  149.     //recursive function that returns the number of leaves(a node who has no children)
  150.     int recNumLeaves(node * p)
  151.     {
  152.         if (p == NULL)
  153.         {
  154.             return 0;
  155.         }
  156.         if (p->left == NULL && p->right == NULL)
  157.         {
  158.             return 1;
  159.         }
  160.         else //recursive call
  161.         {
  162.             return recNumLeaves(p->left) + recNumLeaves(p->right);
  163.         }
  164.     }
  165.     //recursive function that returns the number of nodes in the tree
  166.     int recNumItems(node * p)
  167.     {
  168.         if (p == NULL)
  169.         {
  170.             return 0;
  171.         }
  172.         else
  173.         {
  174.             return 1 + recNumItems(p->left) + recNumItems(p->right);
  175.         }
  176.     }
  177.     //recursive function that gets the height of the tree
  178.     int recGetHeight(node * p)
  179.     {
  180.         if(p == NULL)
  181.         {
  182.             return -1;
  183.         }
  184.         else
  185.         {
  186.             int L = recGetHeight(p->left);
  187.             int R = recGetHeight(p->right);
  188.             return max(L,R) + 1;
  189.         }
  190.     }
  191.     //found this function on geeksforgeeks.org
  192.     int recFullNodes(node * root)
  193.     {
  194.         if (root == NULL)
  195.         {
  196.             return 0;
  197.         }
  198.         int count = 0;
  199.         if(root->left && root->right)
  200.         {
  201.             count++;
  202.         }
  203.         count += (recFullNodes(root->left) + recFullNodes(root->right));
  204.         return count;
  205.     }
  206.    
  207.         double findMin(node * p)
  208.         {
  209.             if(p == NULL)
  210.             {
  211.                 return -1;
  212.             }
  213.             else
  214.             {
  215.                 //in a balanced BST the minimum node is the leftmost node so,
  216.                 //we traverse the left subtree until we get to the leftmost node and return and remove it.
  217.                 temp = p;
  218.                 while(temp->left != NULL)
  219.                 {
  220.                     temp = temp->left;
  221.                 }
  222.                 return temp->data;
  223.             }
  224.         }
  225.    
  226.     double findMax(node * p)
  227.     {
  228.         if(p == NULL)
  229.         {
  230.             return -1;
  231.         }
  232.         else
  233.         {
  234.             //in a balanced BST the maximum node is the rightmost node so,
  235.             //we traverse the right subtree until we get to the rightmost node and return and remove it.
  236.             temp = p;
  237.             while(temp->right != NULL)
  238.             {
  239.                 temp = temp->right;
  240.             }
  241.             return temp->data;
  242.         }
  243.     }
  244.     //I found this and the next function on geeksforgeeks.org
  245.     void printGivenLevel(node * root, int level)
  246.     {
  247.         if (root == NULL)
  248.         {
  249.             return;
  250.         }
  251.         if (level == 1)
  252.         {
  253.             cout << root->data << " ";
  254.         }
  255.         else if (level > 1)
  256.         {
  257.             printGivenLevel(root->left, level-1);
  258.             printGivenLevel(root->right, level-1);
  259.         }
  260.     }
  261.  
  262.     void printLevelOrder(node * root)
  263.     {
  264.         int h = recGetHeight(root);
  265.         for(int i = 1; i <= h; i++)
  266.         {
  267.             printGivenLevel(root, i);
  268.             cout << endl;
  269.         }
  270.     }
  271.     node * sortedArrayBST(double * arr, int start, int end)
  272.     {
  273.         int mid = (start + end)/2;
  274.         if (start > end)
  275.         {
  276.             return NULL;
  277.         }
  278.         //because the array will be sorted
  279.         //the root of the tree will contain the item in the middle of the array so everything less than the middle will go in the left subtree
  280.         //and everything greater than the middle will go in the right subtree
  281.         root = new node(arr[mid]);
  282.         //recursively make left subtree
  283.         root->left = sortedArrayBST(arr, start, end-1);
  284.         //recursively make left subtree
  285.         root->right = sortedArrayBST(arr, mid + 1, end);
  286.        
  287.         return root;
  288.     }
  289.  
  290.    
  291. public:
  292.     binarySearchTree()
  293.     {
  294.         root = NULL;
  295.     }
  296.     //constructor that builds a perfectly balanced BST when passed and array arr and number of items n
  297.     binarySearchTree (double * arr, int start, int end)
  298.     {
  299.         root = sortedArrayBST(arr, start, end);
  300.     }
  301.     void insert(double x)
  302.     {
  303.         recInsert(root, x);
  304.     }
  305.  
  306.     node * remove(double x) //not gonna work
  307.     {
  308.         return recRemove(root, x);
  309.     }
  310.         //method to remove smallest node
  311.         node * extractMin()
  312.         {
  313.             return recRemove(root, findMin(root));
  314.         }
  315.     // method to remove largest node
  316.     node * extractMax() // need to remove
  317.     {
  318.         // return findMax(root);
  319.         return recRemove(root, findMax(root));
  320.     }
  321.     void displayInOrder()
  322.     {
  323.         displayInOrder(root);
  324.     }
  325.     void displayPreOrder()
  326.     {
  327.         displayPreOrder(root);
  328.     }
  329.     void displayPostOrder()
  330.     {
  331.         displayPostOrder(root);
  332.     }
  333.     //part 4: Implement a level order traversal.  State the worst-case big-oh run time of your method.
  334.     //A "level order" visits the items in order of level, ie, root first, then roots children, then root's grandchildren, etc.
  335.     void levelOrderDisplay()
  336.     {
  337.         printLevelOrder(root); //worst case run time: O(n^2)
  338.     }
  339.     //a leaf is a parent node whose left and right child are NULL;
  340.    int numLeaves()
  341.    {
  342.        return recNumLeaves(root);
  343.    }
  344.     //returns total number of nodes
  345.    int numItems()
  346.    {
  347.        return recNumItems(root);
  348.    }
  349.     //returns height of tree
  350.    int getHeight()
  351.    {
  352.        return recGetHeight(root);
  353.    }
  354.  
  355.     int fullNodes()
  356.     {
  357.         return recFullNodes(root);
  358.        
  359.     }
  360.  
  361.  
  362. };
  363.  
  364. #endif /* BST_h */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement