Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.cpp
- * Author: bkmz
- *
- * Created on 11 Март 2010 г., 10:25
- */
- #include <stdlib.h>
- #include <algorithm>
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <string>
- #include <vector>
- using namespace std;
- struct node{
- char txt[50];
- int info;
- int c;
- int dph;
- node *llink;
- node *rlink;
- node *prev;
- };
- ifstream input ("input");
- vector <node*> min_list;
- node *tree(node *p, int w, int depth, node *prev){
- if (p==NULL){
- p = new node;
- p->info = w; //Value
- p->llink = NULL;
- p->rlink = NULL;
- p->dph = depth; //Depth of binary tree
- p->c = 1; //repetitions
- p->prev = prev; //link for the previous node of bin tree
- }else if (w == p->info){
- p->c++;
- }else if (w < p->info){
- p->llink = tree (p->llink, w, depth+1, p);
- }else {
- p->rlink = tree (p->rlink, w, depth+1 , p);
- }
- return p;
- }
- void rev_tree_print(node *ptr){ //print tree from the end
- if(ptr!=NULL){
- rev_tree_print(ptr->prev);
- cout << ptr->info << endl;
- }
- }
- void treeprint_debug(node *p){ //Print tree with debug information
- if (p!=NULL){
- treeprint_debug(p->llink);
- cout << p->info << " __ " << p->dph << flush;
- if (p->llink == NULL && p->rlink == NULL){
- cout << " __ " << "List" << flush;
- min_list.push_back(p);
- }
- cout << endl;
- treeprint_debug(p->rlink);
- }
- }
- void treeprint(node *p){
- if (p!=NULL){
- treeprint(p->llink);
- if (p->llink == NULL && p->rlink == NULL){
- min_list.push_back(p);
- }
- cout << p->info << endl;
- treeprint(p->rlink);
- }
- }
- node *find_min_left (vector<node*> Input){ // if need min right branch, use <=
- int vmin=Input[0]->dph;
- node *pointer=Input[0];
- for(int i=1;i<Input.size();i++){
- if(vmin >= Input[i]->dph){
- vmin = Input[i]->dph;
- pointer = Input[i];
- }
- }
- return pointer;
- }
- /*
- *
- */
- struct sum {
- int left;
- int right;
- int info;
- };
- vector<sum> treees;
- extern sum nonexists;
- void count_depth_simmetrical(node *p , node *root, vector<sum> &input);
- void printtree_struct(vector <sum> treees){
- for (int i=2;i<treees.size() ; i++){
- cout << "dph: " << i << " "<< "left: " << treees[i].left << "\t" << "right: " << treees[i].right << "\tinfo: " << treees[i].info << endl;
- }
- }
- int check_symmetrical_bin_tree(vector<sum> left, vector<sum> right){
- if(left.size() != right.size()){
- return 2;
- }
- for(int i=0;i<left.size();i++){
- if (left[i].left != right[i].right ){
- return 1;
- }
- }
- return 0;
- }
- int main() {
- node *root = NULL;
- int tmp;
- while(!input.eof()){
- input >> tmp;
- root = tree(root, tmp, 1, NULL);
- }
- // treeprint(root); //not for print. Just find list in bin tree
- //rev_tree_print(find_min_left(min_list)); //Reverse print min left branch
- // vector<sum> left,right;
- // count_depth_simmetrical(root->llink , root->llink, left);
- // printtree_struct(left);
- // cout << endl;
- // count_depth_simmetrical(root->rlink , root->rlink, right);
- // printtree_struct(right);
- // if(!check_symmetrical_bin_tree(left, right)){
- // cout << "Bin Tree Symmetrical!" << endl;
- // }
- //cout << endl;
- // treeprint(root);
- return (EXIT_SUCCESS);
- }
- void count_depth_simmetrical(node *p, node *root, vector<sum> &input){
- sum tmp;
- sum nonexists;
- if(p!=NULL){
- count_depth_simmetrical(p->llink , root, input);
- // cout << "__ "<< p->info << endl;
- if(p->prev != NULL){
- if (p->prev->llink == p){
- if (input.size() <= p->dph){
- while(input.size() <= p->dph){
- nonexists.left = 0;
- nonexists.right = 0;
- nonexists.info = 0;
- input.push_back(nonexists);
- }
- }
- tmp.left = 0;
- tmp.right = 0;
- tmp.info = 0;
- tmp = input[p->dph];
- tmp.info = p->info;
- tmp.left++;
- input[p->dph] = tmp;
- }else if (p->prev->rlink == p){
- if (input.size() <= p->dph){
- while(input.size() <= p->dph){
- nonexists.left = 0;
- nonexists.right = 0;
- nonexists.info = 0;
- input.push_back(nonexists);
- }
- }
- tmp.left = 0;
- tmp.right = 0;
- tmp.info = 0;
- tmp = input[p->dph];
- tmp.info = p->info;
- tmp.right++;
- input[p->dph] = tmp;
- }
- }
- count_depth_simmetrical(p->rlink , root, input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement