Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269

    [C++] Inserimento in un albero binario

    Salve ragazzi ho la seguente function per inserire un elemento in un bst

    codice:
    #include <iostream>
    
    
    using namespace std;
    
    
    // Creo il tipo di dato tree
    typedef struct node {
        node *left;
        node *right;
        int info;
    } tree;
    
    
    // Prototipi delle functions
    tree bst_insert(tree root, int elem);
    
    
    
    int main()
    {
    
        tree *root;
        int elemento;
    
        root=null;
    
        do {
            cout<<"Inserisci l'elemento da inserire:"<<endl;
            cin>>elemento;
            bst_insert(root, elemento);
            cout<<"Vuoi inserire un altro elemento?";
            cin>>scelta;
        } while (scelta == 's' or scelta == 'S');
    }
    
    
    /* Inserimento di un elemento nel bst */
    tree bst_insert(tree root, int elem)
    {
          if ( root == null ) {
    
               root = new tree;
               root.info = elem;
               root.left = null;
               root.right = null;
    
          }
          else {
               if ( elem < root.info ) {
                    root = bst_insert(root.left, elem);
               }
               else if ( elem > root.info ) {
                    root = bst_insert(root.right, elem);
               }
               else {
                    cout<<"Dato duplicato\n";
               }
          }
          return (root);
    }
    Mi dà questi 6 errori:

    codice:
    Compiling: C:\Users\Gaten\Desktop\bst.cpp
    C:\Users\Gaten\Desktop\bst.cpp: In function 'int main()':
    C:\Users\Gaten\Desktop\bst.cpp:30: error: 'null' was not declared in this scope
    C:\Users\Gaten\Desktop\bst.cpp:35: error: conversion from 'tree*' to non-scalar type 'tree' requested
    C:\Users\Gaten\Desktop\bst.cpp: In function 'tree bst_insert(tree, int)':
    C:\Users\Gaten\Desktop\bst.cpp:85: error: 'null' was not declared in this scope
    C:\Users\Gaten\Desktop\bst.cpp:87: error: no match for 'operator=' in 'root = (tree*)operator new(12u)'
    C:\Users\Gaten\Desktop\bst.cpp:8: note: candidates are: node& node::operator=(const node&)
    C:\Users\Gaten\Desktop\bst.cpp:95: error: conversion from 'node*' to non-scalar type 'tree' requested
    C:\Users\Gaten\Desktop\bst.cpp:98: error: conversion from 'node*' to non-scalar type 'tree' requested
    Process terminated with status 1 (0 minutes, 0 seconds)
    6 errors, 0 warnings
    Qualcuno può aiutarmi?
    Con i sogni possiamo conoscere il futuro...

  2. #2
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    Chiedo scusa, mi sono imbrogliato, invece di postare nella sezione "programmazione" ho postato nella sezione "PHP", potete spostarmi il thread? Grazie.
    Con i sogni possiamo conoscere il futuro...

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.