Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "tree.h"
- tree::~tree()
- {
- del(root);
- root=0;
- curr=0;
- }
- void tree:: del(tree_node *root)
- {
- if(!root) return;
- if(root->child)
- del(root->child);
- if(root->brother)
- del(root->brother);
- delete root;
- }
- int tree :: operator < (const tree & b)
- {
- if (*root<*b.root) return 1;
- return 0;
- }
- int tree :: operator > (const tree & b)
- {
- if (*root>*b.root) return 1;
- return 0;
- }
- int tree :: operator == (const tree & b)
- {
- if (*root==*b.root) return 1;
- return 0;
- }
- void tree::insert(tree_node *root, tree_node *add)
- {
- if (!root->child)
- root->child=add;
- else
- {
- if (*root<*add)
- {
- if (*root<*root->child) insert(root->child,add);
- else
- {
- if (!root->child->brother) root->child->brother=add;
- else
- {
- if (!(*root->child->brother>*root))
- {
- if (!root->child->brother->brother) root->child->brother->brother=add;
- else insert(root->child->brother->brother,add);
- }
- else insert(root->child->brother,add);
- }
- }
- }
- if (*add==*root)
- {
- if (*root->child==*root) insert(root->child,add);
- else
- {
- if (*root->child<*root)
- {
- if (!root->child->brother) root->child->brother=add;
- else
- {
- if (*root->child->brother==*root) insert(root->child->brother,add);
- else
- {
- add->brother=root->child->brother;
- root->child->brother=add;
- }
- }
- }
- else
- {
- add->brother=root->child;
- root->child=add;
- }
- }
- }
- if (*root>*add)
- {
- if (*root->child<*root) insert(root->child,add);
- else
- {
- add->brother=root->child;
- root->child=add;
- }
- }
- }
- }
- int tree::read(FILE *fp,int n1,int n2,int n3)
- {
- int i;
- root= new tree_node();
- if((root->read(fp,n2,n3))<0) {delete root;root=0;return -1;}
- curr=root;
- for(i=1;i<n1;i++)
- { curr=new tree_node();
- if(!curr){ return -1;}
- if((curr)->read(fp,n2,n3)<0)
- {
- delete curr;
- curr=0;
- return -1;
- }
- insert(root,curr);
- }
- curr=root;
- return 0;
- }
- void tree:: print(FILE *fp,tree_node *root,int level,int kol_level,int n2,int n3)
- {
- int i;
- if(!root) return;
- if(level>kol_level) return;
- for(i=0;i<2*level;i++)
- printf(" ");
- root->print(fp,level,n2,n3);
- if (root->child) print(fp,root->child,level+1,kol_level,n2,n3);
- if(root->brother) print(fp,root->brother,level,kol_level,n2,n3);
- return;
- }
- int tree::go_to_child()
- {
- if(!curr) return 1;
- if(!(curr->child)) return 2;
- curr=curr->child;
- return 0;
- }
- int tree::go_to_brother()
- {
- if(!curr) return 1;
- if(!(curr->brother)) return 2;
- curr=curr->brother;
- return 0;
- }
- int tree::go_to_root()
- {
- curr=root;
- return 0;
- }
- int tree::del_child()
- {
- tree_node *tmp;
- if(!curr) return 1;
- if(!(curr->child)) return 2;
- if(curr->child->child) return 3;
- tmp=curr->child;
- curr->child=curr->child->brother;
- delete tmp;
- return 0;
- }
- int tree::del_brother()
- {
- tree_node *tmp;
- if(!curr) return 1;
- if(!(curr->brother)) return 2;
- if(curr->brother->child) return 3;
- tmp=curr->brother;
- curr->brother=curr->brother->brother;
- delete tmp;
- return 0;
- }
- int tree::del_root()
- {
- if(!root) return 1;
- if(root->child) return 2;
- delete root;
- root=0;
- curr=0;
- return 0;
- }
- int tree::del_child_subtree()
- {
- tree_node *tmp;
- if(!curr) return 1;
- if(!curr->child) return 2;
- tmp=curr->child;
- curr->child=curr->child->brother;
- tmp->brother=0;
- del(tmp);
- return 0;
- }
- int tree::del_brother_subtree()
- {
- tree_node *tmp;
- if(!curr) return 1;
- if(!curr->brother) return 2;
- tmp=curr->brother;
- curr->brother=curr->brother->brother;
- tmp->brother=0;
- del(tmp);
- return 0;
- }
- int tree::add_child(tree_node *add)
- {
- if(!curr) return 1;
- if(!curr->child) curr->child=add;
- else
- {
- add->brother=curr->child;
- curr->child=add;
- }
- return 0;
- }
- int tree::add_brother(tree_node *add)
- {
- if(!curr) return 1;
- if(curr==root) return 2;
- if(!curr->brother) curr->brother=add;
- else
- {
- add->brother=curr->brother;
- curr->brother=add;
- }
- return 0;
- }
- int tree::add_root(tree_node *add)
- {
- if(!root)
- {
- root=add;
- curr=add;
- }
- else return 1;
- return 0;
- }
- void tree::print_current(FILE *fp,int n2,int n3)
- {
- if(!curr) return;
- curr->print(fp,0,n2,n3);
- }
- void tree:: menu()
- {
- char *p;
- char s[LEN];
- int k,n1,n2,n3;
- print_menu_tree();
- while(fgets(s,LEN,stdin))
- {
- k=strtol(s,&p,10);
- if(p==s) continue;
- switch(k)
- {
- case -1: return ;
- case 0:
- {
- if(curr) curr->menu();
- else printf("Нет текущего элемента\n");
- break;
- }
- case 1:
- { printf("Введите максимальный уровнь дерева и кол-во элементов списка и стека для печати:\n");
- if(scanf("%d %d %d",&n1,&n2,&n3)!=3)
- {
- printf("Error\n");
- break;
- }
- print(stdout,root,0,n1,n2,n3);
- break;
- }
- case 2:
- { int r=go_to_child();
- if(r==1) printf("Нет текущего элемента\nНевозможно перейти к потомку\n");
- if(r==2) printf("Нет потомка\nНевозможно перейти к потомку\n");
- break;
- }
- case 3:
- { int r=go_to_brother();
- if(r==1) printf("Нет текущего элемента\nНевозможно перейти к брату\n");
- if(r==2) printf("Нет брата\nНевозможно перейти к брату\n");
- break;
- }
- case 4:
- {
- go_to_root();
- break;
- }
- case 5:
- { int r=del_child();
- if(r==1) printf("Нет текущего элемента\nНевозможно удалить потомка\n");
- if(r==2) printf("Нет потомка\nНевозможно удалить потомка\n");
- if(r==3) printf("Потомок не является концевой вершиной\nНевозможно удалить потомка\n");
- break;
- }
- case 6:
- { int r=del_brother();
- if(r==1) printf("Нет текущего элемента\nНевозможно удалить брата\n");
- if(r==2) printf("Нет брата\nНевозможно удалить потомка\n");
- if(r==3) printf("Брат не является концевой вершиной\nНевозможно удалить потомка\n");
- break;
- }
- case 7:
- { int r=del_root();
- if(r==1) printf("Нет корня\nНевозможно удалить корень\n");
- if(r==2) printf("У корня есть потомки\nНевозможно удалить корень\n");
- break;
- }
- case 8:
- { int r=del_child_subtree();
- if(r==1) printf("Нет корня\nНевозможно удалить поддерево потомка\n");
- if(r==2) printf("Нет потомка\nНевозможно удалить поддерево потомка\n");
- break;
- }
- case 9:
- { int r=del_brother_subtree();
- if(r==1) printf("Нет корня\nНевозможно удалить поддерево брата\n");
- if(r==2) printf("Нет брата\nНевозможно удалить поддерево брата\n");
- break;
- }
- case 10:
- {
- tree_node * p=new tree_node();
- if(!p)
- {
- printf("Error\n");
- break;
- }
- printf("Введите добавляемый элемент:\n");
- if(p->read(stdin,1,1)<0)
- {
- printf("Error read\n");
- delete p;
- break ;
- }
- if((add_child(p))!=0)
- {
- printf("Нет текущего элемента\nНевозможно добавить потомка\n");
- delete p;
- }
- break;
- }
- case 11:
- {
- tree_node * p=new tree_node();
- if(!p)
- {
- printf("Error\n");
- break;
- }
- printf("Введите добавляемый элемент:\n");
- if(p->read(stdin,1,1)<0)
- {
- printf("Error read\n");
- delete p;
- break ;
- }
- int r=add_brother(p);
- if(r==1)
- {
- printf("Нет текущего элемента\nНевозможно добавить брата\n");
- delete p;
- }
- if(r==2)
- {
- printf("Текущий элемент-корень дерева\nНевозможно добавить брата\n");
- delete p;
- }
- break;
- }
- case 12:
- {
- tree_node * p=new tree_node();
- if(!p)
- {
- printf("Error\n");
- break;
- }
- printf("Введите добавляемый элемент:\n");
- if(p->read(stdin,1,1)<0)
- {
- printf("Error read\n");
- delete p;
- break ;
- }
- if((add_root(p))!=0)
- {
- printf("Корень уже существует\nНевозможно добавить корень\n");
- delete p;
- }
- break;
- }
- case 13:
- {
- printf("Введите кол-во элементов (список,стек) для печати:\n");
- if(scanf("%d %d",&n2,&n3)!=2)
- {
- printf("Error\n");
- break;
- }
- print_current(stdout,n2,n3);
- break;
- }
- default: { printf("Error number\n");}
- }
- print_menu_tree();
- }
- return ;
- }
- void print_menu_tree()
- {
- printf("В классе TREE доступны следующие функции:\n");
- printf("-1-выйти\n");
- printf("0-вызвать меню от текущего элемента\n");
- printf("1-печать\n");
- printf("2-перейти к потомку\n");
- printf("3-перейти к брату\n");
- printf("4-перейти к корню дерева\n");
- printf("5-удалить потомка\n");
- printf("6-удалить брата\n");
- printf("7-удалить корень\n");
- printf("8-удалить поддерево потомка\n");
- printf("9-удалить поддерево брата\n");
- printf("10-добавить потомка\n");
- printf("11-добавить брата\n");
- printf("12-добавить корень\n");
- printf("13-печать текущего элемента\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement