*&在C?

时间:2015-12-29 06:39:13

标签: c++ c serialization tree binary-tree

我对树木的概念不熟悉。我正在研究SerializationdeSerialization。我从链接获得了一个示例程序,复制并执行它。它跑了,但是当我试图理解它时,我无法理解一行 - void deSerialize(Node *&root, FILE *fp)

为什么*&是什么意思?

整个代码是:

#include <stdio.h>
#define MARKER -1

/* A binary tree Node has key, pointer to left and right children */
struct Node
{
int key;
struct Node* left, *right;
};

/* Helper function that allocates a new Node with the
given key and NULL left and right pointers. */
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}

// This function stores a tree in a file pointed by fp
void serialize(Node *root, FILE *fp)
{
// If current node is NULL, store marker
if (root == NULL)
{
    fprintf(fp, "%d ", MARKER);
    return;
}

// Else, store current node and recur for its children
fprintf(fp, "%d ", root->key);
serialize(root->left, fp);
serialize(root->right, fp);
}

// This function constructs a tree from a file pointed by 'fp'
void deSerialize(Node *&root, FILE *fp)
{
// Read next item from file. If theere are no more items or next
// item is marker, then return
int val;
if ( !fscanf(fp, "%d ", &val) || val == MARKER)
   return;

// Else create node with this item and recur for children
root = newNode(val);
deSerialize(root->left, fp);
deSerialize(root->right, fp);
}

// A simple inorder traversal used for testing the constructed tree
void inorder(Node *root)
{
if (root)
{
    inorder(root->left);
    printf("%d ", root->key);
    inorder(root->right);
}
}

/* Driver program to test above functions*/
int main()
{
// Let us construct a tree shown in the above figure
struct Node *root        = newNode(20);
root->left               = newNode(8);
root->right              = newNode(22);
root->left->left         = newNode(4);
root->left->right        = newNode(12);
root->left->right->left  = newNode(10);
root->left->right->right = newNode(14);

// Let us open a file and serialize the tree into the file
FILE *fp = fopen("tree.txt", "w");
if (fp == NULL)
{
    puts("Could not open file");
    return 0;
}
serialize(root, fp);
fclose(fp);

// Let us deserialize the storeed tree into root1
Node *root1 = NULL;
fp = fopen("tree.txt", "r");
deSerialize(root1, fp);

printf("Inorder Traversal of the tree constructed from file:\n");
inorder(root1);

return 0;
}

任何帮助表示感谢。

3 个答案:

答案 0 :(得分:6)

*&不是单个符号。但结合了两个:

*指针。 &以供参考。

所以你有这个功能:

void deSerialize(Node *&root, FILE *fp)

此函数的第一个参数是对节点指针的引用。

含义 - 使用它时,你发送一个Node *对象。函数本身可以更改此指针值 - 因为您通过引用传递它。

这允许您在函数内部分配内存。

编写此函数的另一种方法是:

Node *deSerialize(Node *root, FILE *fp)

并以不同的方式使用它:

root->left = deSerialize(root->left, fp)

请在此处查看完整解决方案:http://ideone.com/5GzAyd。相关部分:

Node *deSerialize(Node *root, FILE *fp)
{
    // Read next item from file. If theere are no more items or next
    // item is marker, then return
    int val;
    if ( !fscanf(fp, "%d ", &val) || val == MARKER)
       return NULL;

    // Else create node with this item and recur for children
    root = newNode(val);
    root->left = deSerialize(root->left, fp);
    root->right = deSerialize(root->right, fp);
    return root;
}

答案 1 :(得分:4)

  

在C?

它没有任何意义,会给你语法错误。

  

在C ++中?

它是指针的引用。

节点*&amp; root。所以它的

 Node*  which is the Node pointer
 & root where  root is reference variable  

请注意,这是在C ++而不是C。

您也可以查看相关主题:What does *& in a function declaration mean?

答案 2 :(得分:1)

您可以将其视为

Node* &root

它只是对指针变量的引用。您需要它,因为可能存在您的根是空的并且您需要更改其值的情况。
你可以通过使用指针指针来实现它,即。

 Node **root

但参考文献有其优点 -

初始化引用以引用对象后,无法将其更改为引用另一个对象(可以始终使用指针)。因此,它确保您不会意外地将根更改为指向其他地方,如果您使用指向指针的话,这是可能的。