Buscar

2014.2arvore_exercicios_alunos

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

arvore_exercicios_alunos/funcoes.cpp
#include "funcoes.h"
//metodos classe no
no::no()
{
}
no::no(int info)
{
 dir = NULL;
 esq = NULL;
 this->info = info;
 
}
no::~no()
{
 cout<<"Valor excluído: "<<info;
}
void no::lista()
{
 cout<<info<<"\n";
} 
no *no::getesq()
{
 return esq;
}
void no::setesq(no *esq)
{
 this->esq = esq;
}
no *no::getdir()
{
 return dir;
}
void no::setdir(no *dir)
{
 this->dir=dir;
}
int no::getinfo()
{
 return info;
}
void no::setinfo(int info)
{
 this->info = info;
}
//metodos classe tree
tree::tree()
{
 arv=NULL;
}
tree::~tree()
{
 
}
no *tree::get()
{
 return arv;
}
void tree::set(no *aux)
{
 this->arv=aux;
}
no* tree::consulta(int x)
{
 //1. Implementar este método antes do INSERE
 cout << "O metodo CONSULTA ainda nao foi implementado" << endl;
 return NULL;
}
void tree::destroe(no *aux)
{
}
void tree::insere(int x)
{
 //2. implementar este método
 cout << "O metodo INSERE ainda nao foi implementado" << endl;
}
 
 
void tree::preordem(no *aux)
 {
 if(aux!=NULL)
 {aux->lista();
 preordem(aux->getesq());
 preordem(aux->getdir());
 }
	 	 
}
void tree::posordem(no *aux)
{
 	 if(aux!=NULL)
 	 {
	 	 posordem(aux->getesq());
	 	 posordem(aux->getdir());
	 	 aux->lista();
		 }
}
void tree::inordem(no *aux)
{
 	 if(aux!=NULL)
 	 {
	 	 inordem(aux->getesq());
	 	 aux->lista();
	 	 inordem(aux->getdir());
		 }
} 
void tree::busca_remocao(int x)
{
 no *aux, *pai;
 pai=NULL;
 aux = arv;
 while(aux!=NULL && x!=aux->getinfo())
 {
 pai = aux;
 if (x<aux->getinfo())
 	 aux=aux->getesq(); 
 else
 aux=aux->getdir();
 }
 if(aux!=NULL)
 remove(aux, pai);
 else
 cout<<"Valor não encontrado na arvore";
 	 
}
void tree::remove(no *aux,no *pai)
{
 no *mm;
 if(aux->getdir()==NULL && aux->getdir()==NULL)
 {
 if (pai==NULL) // Pode ser Pai== ARV;.. pois pai é o primeiro no da arvore
 arv==NULL;
 else
 if(aux->getinfo() < pai->getinfo())
 pai->setesq(NULL);
 else
 pai->setdir(NULL);
 delete(aux); 
 }
 else
 {
 if(aux->getdir()==NULL)
 {
 if(pai==NULL)
 arv=arv->getesq();
 else
 if(aux->getinfo() < pai-> getinfo())
 pai->setesq(aux->getesq());
 else
 pai->setdir(aux->getesq());
 delete(aux);
 }
 else
 if(aux->getesq()==NULL)
 {
 if(pai == NULL)
	 arv = arv-> getdir();
 else
 if(aux->getinfo() < pai-> getinfo())
 pai->setesq(aux->getdir());
 else
 pai->setdir(aux->getdir());
 delete(aux);
	}
 else
 {
 pai = aux;
	mm = aux->getesq();
	while(mm->getdir()!=NULL)
	{
	 pai = mm;
	 mm = mm->getdir();
 }
 aux->setinfo(mm->getinfo());
 if(mm->getinfo() > pai->getinfo())
 pai->setdir(mm->getesq());
 else
 pai->setesq(mm->getesq());
	
 }
 }
}
//-------------------------------------------------------------
void tree::imprimearvore()
{
 stack<no*> globalStack;
 globalStack.push(arv);
 int nBlanks = 32;
 bool isRowEmpty = false;
 cout << 
 "......................................................";
 cout << endl;
 while(isRowEmpty==false)
 {
 stack<no*> localStack;
 isRowEmpty = true;
 for(int j=0; j<nBlanks; j++)
 cout << ' ';
 while(globalStack.empty()==false)
 {
 no* temp = globalStack.top();
 globalStack.pop();
 if(temp != NULL)
 {
 cout << temp->getinfo();
 localStack.push(temp->getesq());
 localStack.push(temp->getdir());
 if(temp->getesq() != NULL || temp->getdir() != NULL)
 isRowEmpty = false;
 }
 else
 {
 cout << "--";
 localStack.push(NULL);
 localStack.push(NULL);
 }
 for(int j=0; j<nBlanks*2-2; j++)
 cout << ' ';
 } //end while globalStack not empty
 cout << endl;
 nBlanks /= 2;
 while(localStack.empty()==false)
 {
 globalStack.push( localStack.top() );
 localStack.pop();
 }
 } //end while isRowEmpty is false
 cout << 
 "......................................................";
 cout << endl;
 } //end displayTree()
 
 void tree::decrescente( no* aux){
	 	 if(aux!=NULL)
	 	 {
		 	 decrescente(aux->getdir());
		 	 aux->lista();
		 	 decrescente(aux->getesq());
			 }
}
 int tree::soma(no *aux, int x){
 
 	 	 if(aux!=NULL){
		 x = soma(aux->getesq(), x);
		 x=x+aux->getinfo();
	 	 x= soma(aux->getdir(), x);
	 	 
	 	 
	 	 
 }
 return x;
 
} 
 int tree::qtdInfo(no *aux, int x){
 	 
 	 if(aux!=NULL){
		 x = qtdInfo(aux->getesq(), x);
		 x++;
	 	 x = qtdInfo(aux->getdir(), x);
		 }
return x;
}
 	 
int tree::qtdFolha(no *aux, int x)
{
 //implementar este método para retornar a quantidade de folhas existente na árvore 	 
return 0;
}
	 
 
void tree::menu()
{
 cout<<"\n\n########################### MENU ####################################";
 cout<<"\n# #";
 cout<<"\n# [ 1 ] Inserir [ 1 ] #";
 cout<<"\n# [ 2 ] Remover [ 2 ] #";
 cout<<"\n# [ 3 ] Consultar em Pre-Ordem [ 3 ] #";
 cout<<"\n# [ 4 ] Consultar em Pos-Ordem [ 4 ] #";
 cout<<"\n# [ 5 ] Consultar em In-Ordem [ 5 ] #";
 cout<<"\n# [ 6 ] Imprime [ 6 ] #";
 cout<<"\n# [ 7 ] Consulta decrescente [ 7 ] #";
 cout<<"\n# [ 8 ] Somar [ 8 ] #";
 cout<<"\n# [ 9 ] Quantidade de elementos [ 9 ] #";
 cout<<"\n# [ 10] Quantidade de folhas [ 10] #";
 cout<<"\n# [ 0 ] Sair [ 0 ] #";
 cout<<"\n# #";
 cout<<"\n#####################################################################";
}
arvore_exercicios_alunos/funcoes.h
#include <iostream>
#include <cstdlib>
#include <stack>
using namespace std;
class no
{
 private:
 no *esq;
 no *dir;
 int info;
 public:
 no();
 no(int info);
~no();
 void lista();
 no *getesq();
 void setesq(no *esq);
 no *getdir();
 void setdir(no *dir);
 int getinfo();
 void setinfo(int info);
};
class tree
{
 private:
 no *arv;
 public:
 tree();
 ~tree();
 no *get();
 void set(no *aux);
 no* consulta(int x);
 void insere(int x);
 void destroe(no *aux);
 void preordem(no *aux);
 void posordem(no *aux);
 void inordem(no *aux);
 void busca_remocao(int x);
 void remove(no *aux,no *pai);
 void imprimearvore(); 
 void decrescente( no* aux);
 int soma(no *aux, int x);
 int qtdFolha(no *aux, int x);
 int qtdInfo(no *aux, int x); 
 //*****************************************
 void menu();
 //*****************************************
};
arvore_exercicios_alunos/funcoes.o
arvore_exercicios_alunos/implementar_INSERE_e_CONSULTA_QTDFOLHA.txt
arvore_exercicios_alunos/main.cpp
#include "funcoes.h"
int main()
{
 int x,opcao;
 tree *marv = new tree;
 
 
 do
 {
 
 system("cls");
 marv->menu(); 
 cout<<"\n\n\tInforme a opcao desejada: ";
 cin>>opcao; 
 switch(opcao)
 {
 case 1://insere elementos
 cout<<"Digite o valor a ser inserido: ";
 cin>>x;
 no *aux;
 aux = marv->consulta(x);
 if (aux ==NULL)
 marv->insere(x);
 else
 cout<<"Elemento já existe";
 break;
 
 case 2: //Remover
 if(marv!=NULL)
 {
 cout<<"Digite valor a ser removido: ";
 cin>>x;
 	marv->busca_remocao(x);
 }
 else
 cout<<"Arvore Vazia";
	 
 break;
 
 case 3: //Consulta em pre-Ordem
 marv->preordem(marv->get() );
 break;
 
 case 4: //Consulta em pos-Ordem
 marv->posordem(marv->get());
 break;
 
 case 5: //consulta em in-Ordem
 marv->inordem(marv->get());
 break;
 
 case 6: //Imprimir]
 marv->imprimearvore();
 break;
 
 case 7: //Imprime decrescente
 	 marv->decrescente(marv->get());
 break;
 
 case 8: // SOma elementos
 x = marv->soma(marv->get(), 0);
 cout<<"Soma dos elementos: "<<x<<"\n";
 break;
 
 case 9:
 	 x = marv->qtdInfo(marv->get(), 0);
 	 cout<<"Quantidade de elementos: "<<x<<"\n";
 break;
 
 case 10:
 	 x= marv->qtdFolha(marv->get(), 0);
 	 cout<<"Quantidade de Folhas: "<<x<<"\n";
 	 break;
 	 
 
 
 
 }
 
 
 system("pause");
 } while(opcao!=0);
 
 return 1;
} 
 
arvore_exercicios_alunos/main.o
arvore_exercicios_alunos/Makefile.win
# Project: PArv
# Makefile created by Dev-C++ 4.9.9.2
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
RES = 
OBJ = funcoes.o main.o $(RES)
LINKOBJ = funcoes.o main.o $(RES)
LIBS = -L"C:/Dev-Cpp/lib" 
INCS = -I"C:/Dev-Cpp/include" 
CXXINCS = -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" 
BIN = Parv.exe
CXXFLAGS = $(CXXINCS) 
CFLAGS = $(INCS) 
RM = rm -f
.PHONY: all all-before all-after clean clean-custom
all: all-before Parv.exe all-after
clean: clean-custom
	${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
	$(CPP) $(LINKOBJ) -o "Parv.exe" $(LIBS)
funcoes.o: funcoes.cpp
	$(CPP) -c funcoes.cpp -o funcoes.o $(CXXFLAGS)
main.o: main.cpp
	$(CPP) -c main.cpp -o main.o $(CXXFLAGS)
arvore_exercicios_alunos/Parv.dev
[Project]
FileName=PArv.dev
Name=PArv
UnitCount=3
Type=1
Ver=1
ObjFiles=
Includes=
Libs=
PrivateResource=
ResourceIncludes=
MakeIncludes=
Resources=
Compiler=
Linker=
IsCpp=1
Icon=
ExeOutput=
ObjectOutput=
OverrideOutput=0
OverrideOutputName=
HostApplication=
Folders=
CommandLine=
IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=0
CompilerSettings=
[Unit1]
FileName=funcoes.cpp
Folder=PArv
Compile=1
CompileCpp=1
OverrideBuildCmd=0
BuildCmd=
[Unit2]
FileName=funcoes.h
Folder=PArv
Compile=1
CompileCpp=1
OverrideBuildCmd=0
BuildCmd=
[Unit3]
FileName=main.cpp
Folder=PArv
Compile=1
CompileCpp=1
OverrideBuildCmd=0
BuildCmd=
[VersionInfo]
Major=0
Minor=1
Release=1
Build=1
LanguageID=1033
CharsetID=1252
CompanyName=
FileVersion=
FileDescription=Developed using the Dev-C++ IDE
InternalName=
LegalCopyright=
LegalTrademarks=
OriginalFilename=
ProductName=
ProductVersion=
arvore_exercicios_alunos/Parv.exe

Teste o Premium para desbloquear

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

Continue navegando