Visualizzazione dei risultati da 1 a 3 su 3

Discussione: [C++]Albero

  1. #1

    [C++]Albero

    codice:
    struct STree
    {
    	int item;
    	STree *l, *r;
    	STree(int Item, STree *L, STree *R)
    	{
    	  item = Item;
    		l = L;
    		r = R;
    	}
    };
    
    typedef STree *node;
    
    class CTree
    {
    	private:
    		node tree;
    		int rad;
    	public:
     		CTree();
     		CTree(int n);
        ~CTree();	
        void add(int n);
        int serch(int n);
    };
    
    CTree::CTree()
    {
    	rad = 0;
    	tree = new STree(0, NULL, NULL);
    }
    // init tree
    CTree::CTree(int n)
    {
    	rad = n;
    	tree = new STree(n, NULL, NULL);
    }
    
    CTree::~CTree()
    {
    }
    
    void CTree::add(int n)
    {
    	node p = tree;
    	while( (p->l != NULL) && (p->r != NULL) )
    	{
    		if(n > rad)
    		{
    			p = p->l;
    		}
        else
        {
        	p = p->r;
        }
    	}
    	if(p->l == NULL)
    	{
    		p->r->item = n;
    	}
    	else if(p->r == NULL)
    	{
    		p->l->item = n;
    	}
    }
    
    int CTree::serch(int n)
    {
    	node p = tree;
    	while( (p->l != NULL) && (p->r != NULL) && (p->item != n) )
    	{
    	  if(n > rad)
    		{
    			p = p->l;
    		}
        else
        {
        	p = p->r;
        }
      }
      return p->item;
    }
    Dove sbaglio??



    Tnk 100000000000
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  2. #2
    codice:
     
    #include "STree.hpp"
    
    class CTree
    {
    	private:
    		node tree;
    		int rad;
    	public:
     		CTree();
     		CTree(int n);
        ~CTree();	
        void add(int n);
        int serch(int n);
    };
    
    CTree::CTree()
    {
    	rad = 0;
    	tree = new STree(0, NULL, NULL);
    }
    // init tree
    CTree::CTree(int n)
    {
    	rad = n;
    	tree = new STree(n, NULL, NULL);
    }
    
    CTree::~CTree()
    {
    }
    
    void CTree::add(int n)
    {
    	node p = tree;
    	while( (p->l != NULL) && (p->r != NULL) )
    	{
    		if(n > rad)
    		{
    			p = p->l;
    		}
        else
        {
        	p = p->r;
        }
    	}
    	if(p->l == NULL)
    	{
    	  // ERRORE QUI
    		p->r = new STree(n, NULL, NULL);
    	}
    	else if(p->r == NULL)
    	{
    	  // QUI
    		p->l = new STree(n, NULL, NULL);
    	}
    }
    
    int CTree::serch(int n)
    {
    	node p = tree;
    	while( (p->l != NULL) && (p->r != NULL) && (p->item != n) )
    	{
    	  if(n > rad)
    		{
    			p = p->l;
    		}
        else
        {
        	p = p->r;
        }
      }
      return p->item;
    }
    Risolto
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  3. #3
    codice:
    #include "STree.hpp"
    #define DEBUG
    
    class CTree
    {
     private:
      node tree;
      int rad;
     public:
       CTree();
       CTree(int n);
        ~CTree(); 
        void add(int n);
        int serch(int n);
    };
    
    CTree::CTree()
    {
     rad = 0;
     tree = new STree(0, NULL, NULL);
    }
    // init tree
    CTree::CTree(int n)
    {
     rad = n;
     tree = new STree(n, NULL, NULL);
    }
    
    CTree::~CTree()
    {
    }
    
    void CTree::add(int n)
    {
     node p = tree;
     while( (p->l != NULL) && (p->r != NULL) )
     {
       // SE è MINORE VA A SINISTRA
      if(n > rad)
      {
       p = p->l;
      }
      // SE è MAGGIORE VA A DESTRA
        else if(n < rad)
        {
         p = p->r;
        }
     }
      p->r = new STree(n, NULL, NULL);
        #ifdef DEBUG
        #include <iostream>
         std::cout << "Stato elemento => " << p->item << "\n";
        std::cout << "Stato l =>" << ((p->l == NULL) ? " NODO ESTERNO" : " NODO INTERNO") << "\n";
           std::cout << "Stato r =>" << ((p->r == NULL) ? " NODO ESTERNO" : " NODO INTERNO") << "\n";
        #endif
    }
    // NON VA
    int CTree::serch(int n)
    {
     node p = tree;
     while( (p->l != NULL) && (p->r != NULL) && (p->item != n) )
     {
       if(n > rad)
      {
       p = p->l;
      }
        else if(n < rad)
        {
         p = p->r;
        }
        #ifdef DEBUG
        #include <iostream>
       std::cout << "Siamo all ' elemento " << p->item << "\n";
        #endif
      }
      return p->item;
    }
    Cosi me li mette sempre su r..............come mai???
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.