Advertisement
Kaidul

Binary Search Tree

Mar 19th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. /**
  6.    Binary Search Tree
  7.  
  8.    Operation        Best Case Running Time      Worst Case Running Time
  9. __________________________________________________________________________
  10.  
  11.      Search                  log2 n                     n
  12.      Insert                  log2 n                     n
  13.      Delete                  log2 n                     n
  14.      Preorder Traversal         n                       n
  15.      Inorder Traversal              n                       n
  16.      Postorder Traversal            n                       n
  17.  
  18.      function : insert(key), remove(key), search(key), max(), min(), preorder(), postorder(), inorder(), size(), isEmpty()
  19.  
  20. **/
  21.  
  22. struct treeNode {
  23.     treeNode* left;
  24.     treeNode* right;
  25.     int key;
  26. }*root;
  27.  
  28. template <class T> class BinarySearchTree {
  29. public:
  30.     BinarySearchTree() {
  31.         root = NULL;
  32.     }
  33.  
  34.     bool isEmpty() const {
  35.         return root == NULL;
  36.     }
  37.     void inorder(treeNode*);
  38.     void traverseInorder();
  39.  
  40.     void preorder(treeNode*);
  41.     void traversePreorder();
  42.  
  43.     void postorder(treeNode*);
  44.     void traversePostorder();
  45.  
  46.     void insert(T);
  47.  
  48.     void remove(T);
  49.  
  50.     treeNode* search(const T &);
  51.  
  52.     treeNode* findHelper(const T &, treeNode* &);
  53.     treeNode* find(const T &);
  54.  
  55.     treeNode* minHelper(treeNode* &);
  56.     treeNode* min();
  57.  
  58.     treeNode* maxHelper(treeNode* &);
  59.     treeNode* max();
  60.  
  61.     int sizeHelper(treeNode*);
  62.     int size();
  63. };
  64.  
  65. // Smaller elements go left
  66. // larger elements go right
  67. template <class T> void BinarySearchTree<T>::insert(T d) {
  68.     treeNode* t = new treeNode;
  69.     treeNode* parent;
  70.     t->key = d;
  71.     t->left = NULL;
  72.     t->right = NULL;
  73.     parent = NULL;
  74.  
  75.     // is this a new tree?
  76.     if(isEmpty()) root = t;
  77.     else {
  78.         //Note: ALL insertions are as leaf nodes
  79.         treeNode* curr;
  80.         curr = root;
  81.         // Find the Node's parent
  82.         while(curr) {
  83.             parent = curr;
  84.             if(t->key > curr->key) curr = curr->right;
  85.             else curr = curr->left;
  86.         }
  87.  
  88.         if(t->key < parent->key)
  89.             parent->left = t;
  90.         else
  91.             parent->right = t;
  92.     }
  93. }
  94.  
  95. template <class T>void BinarySearchTree<T>::remove(T d) {
  96.     //Locate the element
  97.     bool found = false;
  98.     if(isEmpty()) {
  99.         cout<<" This Tree is empty! "<<endl;
  100.         return;
  101.     }
  102.  
  103.     treeNode* curr;
  104.     treeNode* parent;
  105.     curr = root;
  106.  
  107.     // root to leaf search (top-down)
  108.     while(curr) {
  109.         if(curr->key == d) {
  110.             found = true;
  111.             break;
  112.         } else {
  113.             parent = curr;
  114.             if(d > curr->key) curr = curr->right;
  115.             else curr = curr->left;
  116.         }
  117.     }
  118.  
  119.     if(!found) {
  120.         cout<<" key not found! "<<endl;
  121.         return;
  122.     }
  123.  
  124.  
  125.     // 3 cases :
  126.     // 1. We're removing a leaf node
  127.     // 2. We're removing a node with a single child
  128.     // 3. we're removing a node with 2 children
  129.  
  130.  
  131.     //We're looking at a leaf node
  132.     if( curr->left == NULL && curr->right == NULL) {
  133.         if(parent->left == curr) parent->left = NULL;
  134.         else parent->right = NULL;
  135.         delete curr;
  136.         return;
  137.     }
  138.  
  139.     // Node with single child
  140.     if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL
  141.             && curr->right == NULL)) {
  142.         if(curr->left == NULL && curr->right != NULL) {
  143.             if(parent->left == curr) {
  144.                 parent->left = curr->right;
  145.                 delete curr;
  146.             } else {
  147.                 parent->right = curr->right;
  148.                 delete curr;
  149.             }
  150.         } else { // left child present, no right child
  151.             if(parent->left == curr) {
  152.                 parent->left = curr->left;
  153.                 delete curr;
  154.             } else {
  155.                 parent->right = curr->left;
  156.                 delete curr;
  157.             }
  158.         }
  159.         return;
  160.     }
  161.  
  162.     //Node with 2 children
  163.     // replace node with smallest value in right subtree
  164.     if (curr->left != NULL && curr->right != NULL) {
  165.         treeNode* chkr;
  166.         chkr = curr->right;
  167.         if((chkr->left == NULL) && (chkr->right == NULL)) {
  168.             curr = chkr;
  169.             delete chkr;
  170.             curr->right = NULL;
  171.         } else { // right child has children
  172.             //if the node's right child has a left child
  173.             // Move all the way down left to locate smallest element
  174.  
  175.             if((curr->right)->left != NULL) {
  176.                 treeNode* lcurr;
  177.                 treeNode* lcurrp;
  178.                 lcurrp = curr->right;
  179.                 lcurr = (curr->right)->left;
  180.                 while(lcurr->left != NULL) {
  181.                     lcurrp = lcurr;
  182.                     lcurr = lcurr->left;
  183.                 }
  184.                 curr->key = lcurr->key;
  185.                 delete lcurr;
  186.                 lcurrp->left = NULL;
  187.             } else {
  188.                 treeNode* tmp;
  189.                 tmp = curr->right;
  190.                 curr->key = tmp->key;
  191.                 curr->right = tmp->right;
  192.                 delete tmp;
  193.             }
  194.  
  195.         }
  196.         return;
  197.     }
  198.  
  199. }
  200.  
  201. template <class T>treeNode* BinarySearchTree<T>::search(const T &d) {
  202.     treeNode* current = root;
  203.     while (current) {
  204.         if(current -> key == d) {
  205.             return current;
  206.         } else if(d < current -> key) {
  207.             current = current -> left;
  208.         } else current = current -> right;
  209.     }
  210.     return NULL;
  211. }
  212.  
  213. template <class T> treeNode* BinarySearchTree <T> :: findHelper(const T &d, treeNode* &node) {
  214.     if (!node || node -> key == d) return node;
  215.     else if(d < node -> key) findHelper(d, node -> left);
  216.     else findHelper(d, node -> right);
  217. }
  218.  
  219. template <class T> treeNode* BinarySearchTree <T> :: find(const T &d) {
  220.     return findHelper(d, root);
  221. }
  222.  
  223. template <class T> treeNode* BinarySearchTree <T> :: minHelper(treeNode* &node) {
  224.     if(node -> left == NULL) return node;
  225.     else minHelper(node -> left);
  226. }
  227.  
  228. template <class T> treeNode* BinarySearchTree <T> :: min() {
  229.     return minHelper(root);
  230. }
  231.  
  232. template <class T> treeNode* BinarySearchTree <T> :: maxHelper(treeNode* &node) {
  233.     if(node -> right == NULL) return node;
  234.     else maxHelper(node -> right);
  235. }
  236.  
  237. template <class T> treeNode* BinarySearchTree <T> :: max() {
  238.     return maxHelper(root);
  239. }
  240.  
  241. int count;
  242. template<class T>int BinarySearchTree<T>::sizeHelper(treeNode* p) {
  243.     if(p != NULL) {
  244.         if(p->left) sizeHelper(p->left);
  245.         if(p->right) sizeHelper(p->right);
  246.         count++;
  247.     } else return count;
  248. }
  249.  
  250. template<class T>int BinarySearchTree<T>::size() {
  251.     count = 0;
  252.     return sizeHelper(root);
  253. }
  254.  
  255.  
  256. /**
  257.   in-order - leftChild, root, rightChild
  258.   pre-order - root, leftChild, rightChild
  259.   post-order - leftChild, rightChild, root
  260. **/
  261. template<class T>void BinarySearchTree<T>::inorder(treeNode* p) {
  262.     if(p != NULL) {
  263.         if(p->left) inorder(p->left);
  264.         cout<<" "<<p->key<<" ";
  265.         if(p->right) inorder(p->right);
  266.     } else return;
  267. }
  268.  
  269. template<class T>void BinarySearchTree<T>::traverseInorder() {
  270.     inorder(root);
  271. }
  272.  
  273. template<class T>void BinarySearchTree<T>::preorder(treeNode* p) {
  274.     if(p != NULL) {
  275.         cout<<" "<<p->key<<" ";
  276.         if(p->left) preorder(p->left);
  277.         if(p->right) preorder(p->right);
  278.     } else return;
  279. }
  280.  
  281. template<class T>void BinarySearchTree<T>::traversePreorder() {
  282.     preorder(root);
  283. }
  284.  
  285. template<class T>void BinarySearchTree<T>::postorder(treeNode* p) {
  286.     if(p != NULL) {
  287.         if(p->left) postorder(p->left);
  288.         if(p->right) postorder(p->right);
  289.         cout<<" "<<p->key<<" ";
  290.     } else return;
  291. }
  292.  
  293. template<class T>void BinarySearchTree<T>::traversePostorder() {
  294.     postorder(root);
  295. }
  296.  
  297. int main() {
  298.     BinarySearchTree <int> btree;
  299.     int choice, tmp, tmp1;
  300.     treeNode* node;
  301.     cout<<" Binary Search Tree Operations "<<endl;
  302.     cout<<" ----------------------------- "<<endl;
  303.     cout<<" 1. Insertion/Creation "<<endl;
  304.     cout<<" 2. In-Order Traversal "<<endl;
  305.     cout<<" 3. Pre-Order Traversal "<<endl;
  306.     cout<<" 4. Post-Order Traversal "<<endl;
  307.     cout<<" 5. Removal "<<endl;
  308.     cout<<" 6. Search "<<endl;
  309.     cout<<" 7. Find smallest Element "<<endl;
  310.     cout<<" 8. Find largest Element "<<endl;
  311.     cout<<" 9. Total Element "<<endl;
  312.     cout<<" 10. Exit "<<endl;
  313.     while(1) {
  314.         cout<<"\n Enter your choice : ";
  315.         cin>>choice;
  316.         switch(choice) {
  317.         case 1 :
  318.             cout<<" Enter Number to be inserted : ";
  319.             cin>>tmp;
  320.             btree.insert(tmp);
  321.             break;
  322.         case 2 :
  323.             cout<<endl;
  324.             cout<<" In-Order Traversal "<<endl;
  325.             cout<<" -------------------"<<endl;
  326.             btree.traverseInorder();
  327.             break;
  328.         case 3 :
  329.             cout<<endl;
  330.             cout<<" Pre-Order Traversal "<<endl;
  331.             cout<<" -------------------"<<endl;
  332.             btree.traversePreorder();
  333.             break;
  334.         case 4 :
  335.             cout<<endl;
  336.             cout<<" Post-Order Traversal "<<endl;
  337.             cout<<" --------------------"<<endl;
  338.             btree.traversePostorder();
  339.             break;
  340.         case 5 :
  341.             cout<<" Enter key to be deleted : ";
  342.             cin>>tmp1;
  343.             btree.remove(tmp1);
  344.             break;
  345.         case 6 :
  346.             cout << " Enter key to be search : ";
  347.             cin >> tmp1;
  348.             //node = btree.search(tmp1);
  349.             node = btree.find(tmp1);
  350.             if(node) {
  351.                 cout << " Node Found!";
  352.                 cout << "\n key : " << node -> key;
  353.                 if(node -> left) cout << " left : " << (node -> left) -> key;
  354.                 if(node -> right) cout << " right : " << (node -> right) -> key << "\n";
  355.             } else cout << " Node not Found!\n";
  356.             break;
  357.         case 7 :
  358.             cout << " Smallest Element : " << btree.min() -> key << "\n";
  359.             break;
  360.         case 8 :
  361.             cout << " Largest Element : " << btree.max() -> key << "\n";
  362.             break;
  363.         case 9 :
  364.             cout << " Total Element : " << btree.size() << "\n";
  365.             break;
  366.         case 10 :
  367.             return 0;
  368.  
  369.         }
  370.     }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement