Welche der folgenden code-Zeile mit malloc?

Habe ich die folgende Implementierung Spiegel der binäre Baum.

#include<stdio.h>
#include<stdlib.h>

/* A binary tree node has data, pointer to left child
   and a pointer to right child */
struct node
{
    int data;
    struct node* left;
    struct node* right;
};

/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct node* newNode(int data)

{
  struct node* node = (struct node*)
                       malloc(sizeof(struct node));
  node->data = data;
  node->left = NULL;
  node->right = NULL;

  return(node);
}


/* Change a tree so that the roles of the  left and
    right pointers are swapped at every node.

 So the tree...
       4
      /\
     2   5
    /\
   1   3

 is changed to...
       4
      /\
     5   2
        /\
       3   1
*/
void mirror(struct node* node)
{
  if (node==NULL)
    return; 
  else
  {
    struct node* temp;

    /* do the subtrees */
    mirror(node->left);
    mirror(node->right);

    /* swap the pointers in this node */
    temp        = node->left;
    node->left  = node->right;
    node->right = temp;
  }
}


/* Helper function to test mirror(). Given a binary
   search tree, print out its data elements in
   increasing sorted order.*/
void inOrder(struct node* node)
{
  if (node == NULL)
    return;

  inOrder(node->left);
  printf("%d ", node->data);

  inOrder(node->right);
} 


/* Driver program to test mirror() */
int main()
{
  struct node *root = newNode(1);
  root->left        = newNode(2);
  root->right       = newNode(3);
  root->left->left  = newNode(4);
  root->left->right = newNode(5);

  /* Print inorder traversal of the input tree */
  printf("\n Inorder traversal of the constructed tree is \n");
  inOrder(root);

  /* Convert tree to its mirror */
  mirror(root);

  /* Print inorder traversal of the mirror tree */
  printf("\n Inorder traversal of the mirror tree is \n"); 
  inOrder(root);

  getchar();
  return 0; 
}

Mir geht es um die folgende Zeile:

  struct node* node = (struct node*)
                       malloc(sizeof(struct node));

Habe ich fortgeschrittene Kenntnisse in c/c++, aber ich bin mir ziemlich Angst vor der Zeiger. Auch nach mehreren versuchen habe ich nie in der Lage, Zeiger. Ich vermeide Sie so weit wie möglich, aber wenn Sie kommen, um die Implementierung von Datenstrukturen wie Bäume, es gibt keine anderen Optionen. Warum sind wir mit malloc und sizeof hier? Auch, warum sind wir casting (struct Knoten*)?

prüfen Sie die Antwort. Wenn Sie finden, dass irgendetwas fehlt, fühlen Sie sich frei zu Fragen.

InformationsquelleAutor rishiag | 2013-09-21

Schreibe einen Kommentar