binäre Bäume (programmieren in C)



  • BAU DEINEN BAUM SAUBER UND TURN DANN....



  • noobLolo schrieb:

    BAU DEINEN BAUM SAUBER UND TURN DANN....

    sry das war ein bischen wild...

    in dem link den ich dir geschickt hab in den du evtl. reingeschaut hast steht das so...

    /*
     Given a binary tree, return true if a node
     with the target data is found in the tree. Recurs
     down the tree, chooses the left or right
     branch by comparing the target to each node.
    */
    static int lookup(struct node* node, int target) {
      // 1. Base case == empty tree
      // in that case, the target is not found so return false
      if (node == NULL) {
        return (false);
      }
      else {
        // 2. see if found here
        if (target == node->data) return(true);
        else {
          // 3. otherwise recur down the correct subtree
          if (target < node->data) return(lookup(node->left, target));
          else return(lookup(node->right, target));
        }
      }
    }
    

    und für dich schaut das dann so ungefähr aus

    /*
     Given a binary tree, return true if a node
     with the target data is found in the tree. Recurs
     down the tree, chooses the left or right
     branch by comparing the target to each node.
    */
    struct node *lookup(struct node* bn, int i){
      if(bn == NULL)
        return NULL;
      else if(i == bn->ZAHL)
        return bn;
      else{
        if(i < bn->ZAHL)
          return lookup(bn->links, i);
        else
          return lookup(bn->rechts, i);
      }
    }
    

    und verwendet wirds

    int main(void){
      struct binaer_baum root;
      struct binaer_knoten a;
      struct binaer_knoten b;
      struct binaer_knoten c;
      root.wurzel = &a;
      a.ZAHL = 5;
      b.ZAHL = 2;
      c.ZAHL = 7;
      a.links = &b;
      a.rechts = &c;
      b.links = NULL;
      c.links = NULL;
      b.rechts = NULL;
      c.rechts = NULL;
    
      if(lookup(root.wurzel,5)==NULL)
        printf("oO nix gefunden");
      else
        printf("jipee hab was");
    }
    

    axo und

    void suchen(struct binaer_knoten *bn, int i){
    statt
    void suchen(struct binaer_knoten *bn, unsigned int i){
    

    und

    struct binaer_knoten *knoten, **neu;
    statt
    struct binaer_knoten *knoten, *neu;
    
    und dann
    neu = baum->wurzel;
    und
    knoten= baum->wurzel;
    
    und
    neu = knoten->rechts; 
    statt
    neu = &knoten->rechts; 
    
    und
    neu = knoten->links; 
    statt
    neu = &knoten->links;
    

    sonst sollte suchen schon mehr oder weniger passen... also bau mal das bäumchen sauber



  • arr

    struct binaer_knoten *lookup(struct binaer_knoten* bn, int i){ 
    statt
    struct node *lookup(struct node* bn, int i){
    


  • so schauts bischen besser aus

    if(bn == NULL)
      return NULL;
    else{
      if(i < bn->ZAHL)
        return lookup(bn->links, i);
      else
        if(i == bn->ZAHL)
          return bn;
        else
          return lookup(bn->rechts, i);
    }
    


  • if(bn == NULL)
      return NULL;
    else{
      if(i < bn->ZAHL)
        return lookup(bn->links, i);
      else
        if(i > bn->ZAHL)
          return lookup(bn->rechts, i);
        else
          return bn;
    }
    

    so jetzt reichts aber 😃



  • machs doch gleich so 😉

    if(bn == NULL)
      return NULL;
    else if(i < bn->ZAHL)
      return lookup(bn->links, i);
    else if(i > bn->ZAHL)
      return lookup(bn->rechts, i);
    else
      return bn;
    


  • Danke für die Hilfe.
    Übrigens lolo der Baum is ja wohl recht ordentlich! Manman!

    BAU DEINEN BAUM SAUBER UND TURN DANN....



  • hallo, ich habe die vorbeiträge gelesen. Auch ich arbeite an einer ähnlichen Aufgabe.
    Deshalb die frage: Ich habe preorder eingebaut, aber ich bekomme nichts ausgegeben. hier der coder baumausgabefunktion

    struct binaer_knoten{
       int ZAHL;
       struct binaer_knoten *links;
       struct binaer_knoten *rechts;
    };
    
    void baum_ausgeben (struct binaer_knoten * wurzel)
    {
     int i;
    
     if (wurzel != NULL)             /* Ist der Baum leer? Nein, dann gib ihn aus */
     { 
    
        printf("%d\n",wurzel->ZAHL);    /* Preorder-Notation: W */
    
        baum_ausgeben(wurzel->links);   /* Preorder-Notation: L */
        baum_ausgeben(wurzel->rechts);  /* Preorder-Notation: R */
    
     }
     return;
    };
    ......
    ......
    
    int main(void) {
    .....
     baum_ausgeben(re->wurzel);
    


  • Das problem ist das mit der ausgabe in main() funktion...



  • Hier wird anschneind nicht geholfen....



  • Zeig doch den gesamten Code. So viel sollte es nicht sein.



  • Ganz klar: re->wurzel ist NULL! (Baum eben leer)


Anmelden zum Antworten