Facebook
From Ana, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 199
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6.         int val;
  7.         node *L;
  8.         node *R;
  9.         node *O;
  10.         node() { node *O = NULL; };
  11. };
  12.  
  13. void insertbst(node *&root, int x) {
  14.         if (root == NULL) {
  15.                 root = new node;
  16.                 root->val = x;
  17.                 root->L = root->R = NULL;
  18.                 cout << "nic tu nie ma" << endl;
  19.         }
  20.         else {
  21.                 if (x < root->val) {
  22.                         insertbst(root->L, x);
  23.                         root->L->O = root;
  24.                         cout << "idziemy głębiej w lewo..." << endl;
  25.                 }
  26.                 else {
  27.                         insertbst(root->P, x);
  28.                         root->P->O = root;
  29.                         cout << "idziemy głębiej w prawo..." << endl;
  30.                 }
  31.         }
  32. }
  33.  
  34. void inorder(node *&root) {
  35.         if (root != NULL) {
  36.                 inorder(root->L);
  37.                 cout << root->val << " ";
  38.                 inorder(root->P);
  39.  
  40.         }
  41. }
  42.  
  43. int main() {
  44.         node *root = NULL;
  45.         insertbst(root, 4);
  46.         insertbst(root, 2);
  47.         insertbst(root, 11);
  48.         insertbst(root, 1);
  49.         insertbst(root, 7);
  50.         insertbst(root, 25);
  51.         inorder(root);
  52.  
  53.         system("PAUSE");
  54.         return 0;
  55. }