Buscar

ARVORE 2.0 20170327T183642Z 001

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

ARVORE 2.0/main.cpp
#include <iostream>
#include "Arvore.h"
using namespace std;
int main()
{
 Tree* t=new Tree();
 Tree* node;
 int op=-1;
 int num;
 do
 {
 cout<<"\nMENU\n\n";
 cout<<"Digite:\n1 - Para inserir\n2 - Para mostrar a arvore\n0 - Para sair\n\nOpção: ";
 cin>>op;
 if(op==1)
 {
 node =new Tree();
 cout<<"Digite um numero: ";
 cin>>num;
 node->setKey(num);
 Tree::Insert(t,node);
 Tree::Inorder(t);
 }
 if(op==2)
 {
 Tree::Inorder(t);
 }
 }while(op!=0);
 cout<<"\n\nBye\n\n";
}
ARVORE 2.0/ARVORE.pro
TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.cpp
HEADERS += \
 Arvore.h
ARVORE 2.0/Arvore.h
#ifndef ARVORE_H
#define ARVORE_H
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
class Tree
{
protected:
 Tree* right;
 Tree* left;
 Tree* root;
 Tree* p;
 int key;
public:
 Tree()
 {
 right=0,
 left=0,
 key=0;
 }
 virtual ~Tree(){}
 void setRight(Tree* right);
 void setLeft(Tree* left);
 void setParent(Tree* p);
 void setRoot(Tree *root){this->root=root;}
 void setKey(int key){this->key=key;}
 int getKey() const{return this->key;}
 Tree* getRight() const{return this->right;}
 Tree* getLeft() const{return this->left;}
 Tree* getRoot() const {return this->root;}
 Tree* getParent() const{return this->p;}
 static void Inorder(Tree* x)
 {
 if(x!=0)
 {
 Tree::Inorder(x->left);
 cout<<x->key<<";";
 Tree::Inorder(x->right);
 }
 }
 static Tree* Search(Tree* x , int k)
 {
 if(x==0||k==x->key)
 return x;
 if(k<x->key)
 return Tree::Search(x->left,k);
 else
 return Tree::Search(x->right,k);
 }
 static Tree* InterativeTreeSearch(Tree* x, int k)
 {
 while(x!=0&&k!=x->key)
 if(k<x->key)
 x=x->left;
 else
 x=x->right;
 return x;
 }
 static Tree* Minimum(Tree* x)
 {
 while(x->left!=0)
 x=x->left;
 return x;
 }
 static Tree* Maximum(Tree* x)
 {
 while(x->right!=0)
 x=x->right;
 return x;
 }
 static Tree* Sucessor(Tree* x)
 {
 Tree* y;
 if(x->right!=0)
 return Tree::Minimum(x->right);
 y=x->p;
 while(y!=0&&x==y->right)
 {
 x=y;
 y=y->p;
 }
 return y;
 }
 static void Insert(Tree* T,Tree* z)
 {
 Tree* y=0;
 Tree* x=T->root;
 while(x!=0)
 {
 y=x;
 if(z->key<x->key)
 x=x->left;
 else
 x=x->right;
 }
 z->p=y;
 if(y==0)
 T->root=z;//tree is null
 else if(z->key<y->key)
 y->left=z;
 else y->right=z;
 if(T->p==NULL)
 Tree::clone(T);
 }
 static void clone(Tree* T)
 {
 T->right=T->root->right;
 T->key=T->root->key;
 T->left= T->root->left;
 T->p=T->root->p;
 }
 static void Transplant(Tree* T,Tree* u,Tree*v)
 {
 if(u->p==0)
 T->root=v;
 else if(u==u->p->left)
 u->p->left=v;
 else
 u->p->right=v;
 if(v!=0)
 v->p=u->p;
 }
 static void Delete(Tree* t,Tree* z)
 {
 Tree* y;
 if(z->left==0)
 Tree::Transplant(t,z,z->right);
 else if(z->right==0)
 Tree::Transplant(t,z,z->left);
 else
 {
 y=Tree::Minimum(z->right);
 if(y->p!=z)
 {
 Tree::Transplant(t,y,y->right);
 y->right=z->right;
 y->right->p=y;
 }
 Tree::Transplant(t,z,y);
 y->left=z->left;
 y->left->p=y;
 }
 }
};
#endif //Arvore

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais