Advertisement
Draegon

binary_search_tree.cpp

Dec 6th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.03 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Node {
  6.  
  7.     int value;
  8.     Node* parent;
  9.     Node* left;
  10.     Node* right;
  11.  
  12.   public:
  13.  
  14.     Node(int v) {value = v; left = NULL; right = NULL; parent = NULL;}
  15.     int getValue(void) {return value;}
  16.     Node* getParent(void) {return parent;}
  17.     //int getParentValue(void) {return}
  18.     Node* getLeft(void) {return left;}
  19.     Node* getRight(void) {return right;}
  20.     void setParent(Node* p) {parent = p;}
  21.     void setLeft(Node* l) {left = l; if(l != NULL) l->parent = this;}
  22.     void setRight(Node* r) {right = r; r->parent = this;}
  23. };
  24.  
  25. class SortedArray {
  26.  
  27.     int sorted_array[40];
  28.     int size_ar;
  29.   public:
  30.     SortedArray() {size_ar = 0;}
  31.     void Insert(int num) {sorted_array[size_ar] = num; size_ar++;}
  32.     int getSize() {return size_ar;}
  33.     void print() {
  34.  
  35.         for(unsigned int i=0; i<size_ar; i++)
  36.             cout << sorted_array[i] << endl;
  37.     }
  38. };
  39.  
  40. class Queue {
  41.  
  42.     Node* elements[40];
  43.     int first;
  44.     int last;
  45.  
  46.   public:
  47.     Queue(){first = 0; last = -1;}
  48.     void Enqueue(Node* node) {elements[++last] = node;}
  49.     Node* Dequeue() {return elements[first++];}
  50.     bool QueueNotEmpty() {return first <= last;}
  51.  
  52. };
  53. Node* Find(Node* n, int key){
  54.  
  55.     //cout << n->getValue() << endl;
  56.  
  57.     if (n == NULL){
  58.         cout << "Node of value " << key <<" not found!" << endl;
  59.         return NULL;
  60.     }
  61.  
  62.     if((key == n->getValue()))
  63.         //cout << "Key found: " << n->getValue() << endl;
  64.         return n;
  65.  
  66.     if (key < n->getValue())
  67.         Find(n->getLeft(),key);
  68.  
  69.     else
  70.         Find(n->getRight(), key);
  71. }
  72.  
  73. Node* MinorKey(Node* n){
  74.  
  75.     if (n->getLeft() == NULL){
  76.         cout << "The minor key is: " << n->getValue() << endl;
  77.         return n;
  78.     } else {
  79.         return MinorKey(n->getLeft());
  80.     }
  81. }
  82.  
  83. Node* BiggerKey(Node* n){
  84.  
  85.     if (n->getRight() == NULL) {
  86.         cout << "The bigger key is: " << n->getValue() << endl;
  87.         return n;
  88.     } else {
  89.        return BiggerKey(n->getRight());
  90.     }
  91. }
  92.  
  93. void Insert(Node* n, int key){
  94.  
  95.     cout << n->getValue() << endl;
  96.     if (key < n->getValue()){
  97.  
  98.         if (n->getLeft() == NULL){
  99.  
  100.             Node* aux = new Node(key);
  101.             n->setLeft(aux);
  102.             cout << "Node inserted: " << aux->getValue() << endl;
  103.         } else {
  104.             Insert(n->getLeft(), key);
  105.         }
  106.      } else {
  107.  
  108.         if (n->getRight() == NULL){
  109.  
  110.             Node* aux = new Node(key);
  111.             n->setRight(aux);
  112.             cout << "Node inserted: " << aux->getValue() << endl;
  113.         } else{
  114.             Insert(n->getRight(), key);
  115.         }
  116.      }
  117. }
  118.  
  119.  
  120. bool Delete(Node* n, int key) {
  121.  
  122.     Node* node = Find(n, key);
  123.  
  124.     if (node->getLeft() == NULL && node->getRight() == NULL) {
  125.         cout << "Node deleted: " << node->getValue() << endl;
  126.  
  127.         if (node->getValue() <= node->getParent()->getValue()){
  128.             node->getParent()->setLeft(NULL);
  129.             node = NULL;
  130.             return true;
  131.         } else {
  132.             node->getParent()->setRight(NULL);
  133.             node = NULL;
  134.             return true;
  135.         }
  136.     }
  137.  
  138.     if (node->getLeft() != NULL && node->getRight() == NULL) {
  139.         cout<< "Node deleted: " << node->getValue() << endl;
  140.  
  141.         if (node->getValue() <= node->getParent()->getValue()){
  142.  
  143.             cout << "1" << endl;
  144.             node->getParent()->setLeft(node->getLeft());
  145.             node->getLeft()->setParent(node->getParent());
  146.             node = NULL;
  147.             return true;
  148.         } else {
  149.  
  150.             node->getParent()->setLeft(node->getRight());
  151.             node->getRight()->setParent(node->getParent());
  152.             node = NULL;
  153.             return true;
  154.         }
  155.     }
  156.  
  157.     if(node->getLeft() == NULL && node->getRight() != NULL) {
  158.         cout<< "Node deleted: " << node->getValue() << endl;
  159.  
  160.         if(node->getValue() <= node->getParent()->getValue()) {
  161.  
  162.             node->getParent()->setLeft(node->getRight());
  163.             node->getRight()->setParent(node->getParent());
  164.             node = NULL;
  165.             return true;
  166.         } else {
  167.  
  168.             node->getParent()->setRight(node->getRight());
  169.             node->getRight()->setParent(node->getParent());
  170.             node = NULL;
  171.             return true;
  172.         }
  173.     }
  174.  
  175.     /*if (n->getLeft() != NULL && n->getRight() != NULL) {
  176.  
  177.         Node* node = MinorKey(n->getRight());
  178.         cout << node->getValue() << endl;
  179.  
  180.         node->setParent(n->getParent());
  181.         n->getRight()->setParent(node);
  182.         node
  183.         if(n->getValue() >= n->getParent()->getValue())
  184.             n->getParent()->setRight(node);
  185.         else
  186.             n->getParent()->setLeft(node);
  187.     }*/
  188.  
  189. }
  190.  
  191. SortedArray my_array;
  192.  
  193. Node* DepthFirstSearch(Node* n){
  194.  
  195.     if (n != NULL){
  196.  
  197.         DepthFirstSearch(n->getLeft());
  198.         my_array.Insert(n->getValue());
  199.         DepthFirstSearch(n->getRight());
  200.     }
  201. }
  202.  
  203. Queue q;
  204.  
  205. void BreadthFirstSearch(Node* n){
  206.  
  207.     if (n != NULL){
  208.  
  209.         q.Enqueue(n);
  210.         cout << n->getValue() << endl;
  211.         while(q.QueueNotEmpty()) {
  212.  
  213.             Node* aux = q.Dequeue();
  214.  
  215.  
  216.             if (aux->getLeft() != NULL)
  217.                  BreadthFirstSearch(aux->getLeft());
  218.  
  219.             if (aux->getRight() != NULL)
  220.                  BreadthFirstSearch(aux->getRight());
  221.  
  222.         }
  223.     }
  224.  
  225. }
  226.  
  227. int PathLength(Node* n, int key) {
  228.  
  229.     int len = 0;
  230.  
  231.     if (n->getValue() == key)
  232.         return len;
  233.  
  234.     else {
  235.  
  236.         while(n->getValue() != key) {
  237.  
  238.             if (key < n->getValue())
  239.                 n = n->getLeft();
  240.             else
  241.                 n = n->getRight();
  242.             len++;
  243.         }
  244.     }
  245.     return len;
  246. }
  247.  
  248. int main (){
  249.  
  250.     Node n1(21);
  251.     Insert(&n1,13);
  252.     Insert(&n1,30);
  253.     Insert(&n1,16);
  254.     Insert(&n1,28);
  255.     Insert(&n1,50);
  256.     Insert(&n1,15);
  257.     Insert(&n1,18);
  258.     Insert(&n1,42);
  259.     /*Node n2(13);
  260.     Node n3(30);
  261.     Node n4(16);
  262.     Node n5(28);
  263.     Node n6(50);
  264.     Node n7(15);
  265.     Node n8(18);
  266.     Node n9(42);
  267.  
  268.     n1.setLeft(&n2);
  269.     n1.setRight(&n3);
  270.     n2.setRight(&n4);
  271.     n4.setLeft(&n7);
  272.     n4.setRight(&n8);
  273.     n3.setLeft(&n5);
  274.     n3.setRight(&n6);
  275.     n6.setLeft(&n9);*/
  276.     cout << endl;
  277.     cout << "Node 18 path length: " << PathLength(&n1, 18) << endl;
  278.  
  279.     cout << endl;
  280.     cout << "Search:" << endl;
  281.     Node* aux = Find(&n1, 15);
  282.     cout << "Node found: " << aux->getValue() << endl;
  283.     cout << endl;
  284.  
  285.     cout << "Search:" << endl;
  286.     aux = Find(&n1, 42);
  287.     cout << "Node found: " << aux->getValue() << endl;
  288.     cout << endl;
  289.  
  290.     MinorKey(&n1);
  291.     BiggerKey(&n1);
  292.     cout << endl;
  293.  
  294.     Insert(&n1,40);
  295.     cout << endl;
  296.     Insert(&n1,14);
  297.     cout << endl;
  298.  
  299.     cout << "Search:" << endl;
  300.     aux = Find(&n1, 14);
  301.     cout << "Node found: " << aux->getValue() << endl;
  302.     cout << endl;
  303.  
  304.     Find(&n1, 200);
  305.     cout << endl;
  306.  
  307.     Insert(&n1,2);
  308.     cout << endl;
  309.  
  310.     aux = MinorKey(&n1);
  311.     cout << "Parent of the minor key " << aux->getValue() << " is " << (aux->getParent()->getValue()) << endl;
  312.     cout << endl;
  313.  
  314.     //Delete(&n1, 2);
  315.  
  316.     //cout << endl;
  317.     //Find(&n1,2);
  318.  
  319.     //cout<< endl;
  320.     Insert(&n1, 1);
  321.     cout << endl;
  322.  
  323.     Delete(&n1, 2);
  324.     cout << endl;
  325.  
  326.     Find(&n1, 2);
  327.     cout << endl;
  328.  
  329.     aux = MinorKey(&n1);
  330.     cout << "Parent of the minor key " << aux->getValue() << " is " << (aux->getParent()->getValue()) << endl;
  331.  
  332.     cout << endl;
  333.     Insert(&n1, 29);
  334.     cout << endl;
  335.  
  336.     Delete(&n1, 28);
  337.     cout << endl;
  338.  
  339.     Find(&n1, 28);
  340.     cout << endl;
  341.  
  342.     aux = Find(&n1, 29);
  343.     cout << endl;
  344.  
  345.     cout << "Parent of the node 29 is " << (aux->getParent()->getValue()) << endl;
  346.  
  347.     cout << endl;
  348.     DepthFirstSearch(&n1);
  349.  
  350.     cout << endl;
  351.     cout << "Sorted Array size: " << my_array.getSize() << endl;
  352.  
  353.     cout << endl;
  354.     cout << "Sorted elements: " << endl;
  355.     my_array.print();
  356.  
  357.     cout << endl;
  358.     cout << "BFS: " << endl;
  359.     BreadthFirstSearch(&n1);
  360.  
  361.  
  362.     return 0;
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement