我有以下代码
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
/* newNode() allocates a new node with the given data and NULL left and
right pointers. */
struct node* newNode(int data)
{
// Allocate memory for new node
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
// Initialize left and right children as NULL
node->left = NULL;
node->right = NULL;
return(node);
}
int main(int argc, const char * argv[])
{
printf("You Typed the following %s", argv[1]);
/*create root*/
struct node *root = newNode(1);
/* following is the tree after above statement
1
/ \
NULL NULL
*/
root->left = newNode(2);
root->right = newNode(3);
/* 2 and 3 become left and right children of 1
1
/ \
2 3
/ \ / \
NULL NULL NULL NULL
*/
root->left->left = newNode(4);
/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 NULL NULL NULL
/ \
NULL NULL
*/
getchar();
return 0;
}
在内存中生成二叉树。我的计划是将文件指针加载到&#34;节点&#34;这棵树这些文件来自命令行参数
然后根据用户输入我想在树中向左(或向右),当一个节点被击中时,它应该打印出文件内容等等......
我该怎么做? 非常感谢任何帮助