Advertisement
Bkmz

Untitled

Aug 20th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. /*
  2.  * File:   main.cpp
  3.  * Author: bkmz
  4.  *
  5.  * Created on 11 Март 2010 г., 10:25
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <algorithm>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string.h>
  13. #include <string>
  14. #include <vector>
  15.  
  16.  
  17. using namespace std;
  18.  
  19. struct node{
  20.      char txt[50];
  21.      int info;
  22.      int c;
  23.      int dph;
  24.     node *llink;
  25.     node *rlink;
  26.     node *prev;
  27. };
  28.  
  29. ifstream input ("input");
  30. vector <node*> min_list;
  31.  
  32. node *tree(node *p, int w, int depth, node *prev){
  33.     if (p==NULL){
  34.         p = new node;
  35.         p->info  = w;       //Value
  36.         p->llink = NULL;
  37.         p->rlink = NULL;
  38.         p->dph   = depth;   //Depth of binary tree
  39.         p->c     = 1;       //repetitions
  40.         p->prev  = prev;    //link for the previous node of bin tree
  41.     }else if (w == p->info){
  42.         p->c++;
  43.     }else if (w < p->info){
  44.         p->llink = tree (p->llink, w, depth+1, p);
  45.     }else {
  46.         p->rlink = tree (p->rlink, w, depth+1 , p);
  47.     }
  48.     return p;
  49. }
  50.  
  51. void rev_tree_print(node *ptr){ //print tree from the end
  52.     if(ptr!=NULL){
  53.        rev_tree_print(ptr->prev);
  54.        cout << ptr->info << endl;
  55.     }
  56. }
  57.  
  58. void treeprint_debug(node *p){ //Print tree with debug information
  59.     if (p!=NULL){
  60.         treeprint_debug(p->llink);
  61.         cout << p->info  << " __ " << p->dph << flush;
  62.         if (p->llink == NULL && p->rlink == NULL){
  63.             cout << " __ " << "List" << flush;
  64.             min_list.push_back(p);
  65.         }
  66.         cout << endl;
  67.         treeprint_debug(p->rlink);
  68.     }
  69. }
  70. void treeprint(node *p){
  71.     if (p!=NULL){
  72.         treeprint(p->llink);
  73.         if (p->llink == NULL && p->rlink == NULL){
  74.             min_list.push_back(p);
  75.         }
  76.         cout << p->info << endl;
  77.         treeprint(p->rlink);
  78.     }
  79. }
  80.  
  81. node *find_min_left (vector<node*> Input){ // if need min right branch, use <=
  82.  
  83.     int vmin=Input[0]->dph;
  84.     node *pointer=Input[0];
  85.     for(int i=1;i<Input.size();i++){
  86.         if(vmin >= Input[i]->dph){
  87.             vmin = Input[i]->dph;
  88.             pointer = Input[i];
  89.         }
  90.     }
  91.     return pointer;
  92. }
  93. /*
  94.  *
  95.  */
  96.  
  97. struct sum {
  98.     int left;
  99.     int right;
  100.     int info;
  101. };
  102. vector<sum> treees;
  103. extern sum nonexists;
  104.  
  105. void count_depth_simmetrical(node *p , node *root, vector<sum> &input);
  106.  
  107. void printtree_struct(vector <sum> treees){
  108.  
  109.     for (int i=2;i<treees.size() ; i++){
  110.         cout  << "dph: " << i << " "<< "left: " << treees[i].left << "\t" << "right: " << treees[i].right << "\tinfo: " << treees[i].info <<   endl;
  111.  
  112.     }
  113.  
  114. }
  115.  
  116. int check_symmetrical_bin_tree(vector<sum> left, vector<sum> right){
  117.  
  118.     if(left.size() != right.size()){
  119.         return 2;
  120.     }
  121.  
  122.     for(int i=0;i<left.size();i++){
  123.         if (left[i].left != right[i].right ){
  124.             return 1;
  125.  
  126.         }
  127.     }
  128.  
  129.     return 0;
  130. }
  131. int main() {
  132.     node *root = NULL;
  133.     int tmp;
  134.  
  135.  
  136.     while(!input.eof()){
  137.         input >> tmp;
  138.         root = tree(root, tmp, 1, NULL);
  139.     }
  140.  
  141. //    treeprint(root); //not for print. Just find list in bin tree
  142.  
  143.     //rev_tree_print(find_min_left(min_list)); //Reverse print min left branch
  144.  
  145. //    vector<sum> left,right;
  146. //    count_depth_simmetrical(root->llink , root->llink, left);
  147.  
  148. //    printtree_struct(left);
  149.  
  150. //    cout << endl;
  151.  
  152. //    count_depth_simmetrical(root->rlink , root->rlink, right);
  153.  
  154. //    printtree_struct(right);
  155.  
  156. //    if(!check_symmetrical_bin_tree(left, right)){
  157. //        cout << "Bin Tree Symmetrical!" << endl;
  158.  
  159. //    }
  160.  
  161.  
  162.  
  163.     //cout << endl;
  164.  
  165.    // treeprint(root);
  166.  
  167.  
  168.  
  169.     return (EXIT_SUCCESS);
  170. }
  171.  
  172.  
  173.  void count_depth_simmetrical(node *p, node *root, vector<sum> &input){
  174.     sum tmp;
  175.     sum nonexists;
  176.     if(p!=NULL){
  177.         count_depth_simmetrical(p->llink , root, input);
  178.        // cout << "__ "<< p->info << endl;
  179.  
  180.         if(p->prev != NULL){
  181.         if (p->prev->llink == p){
  182.             if (input.size() <= p->dph){
  183.                 while(input.size() <= p->dph){
  184.                     nonexists.left  = 0;
  185.                     nonexists.right = 0;
  186.                     nonexists.info  = 0;
  187.                     input.push_back(nonexists);
  188.                 }
  189.             }
  190.             tmp.left  = 0;
  191.             tmp.right = 0;
  192.             tmp.info  = 0;
  193.             tmp = input[p->dph];
  194.             tmp.info = p->info;
  195.             tmp.left++;
  196.             input[p->dph] = tmp;
  197.  
  198.         }else if (p->prev->rlink == p){
  199.             if (input.size() <= p->dph){
  200.                 while(input.size() <= p->dph){
  201.                     nonexists.left  = 0;
  202.                     nonexists.right = 0;
  203.                     nonexists.info  = 0;
  204.                     input.push_back(nonexists);
  205.                 }
  206.             }
  207.             tmp.left  = 0;
  208.             tmp.right = 0;
  209.             tmp.info  = 0;
  210.             tmp = input[p->dph];
  211.             tmp.info = p->info;
  212.             tmp.right++;
  213.             input[p->dph] = tmp;
  214.  
  215.         }
  216.         }
  217.  
  218.         count_depth_simmetrical(p->rlink , root, input);
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement