'An error occurs during the implementation of the linked list tree
enter code here
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int data;
struct _node* rightChild;
struct _node* leftChild;
}Node;
Node* create(int data) { // create node function
Node* node = (Node*)malloc(sizeof(node));
node->rightChild = NULL;
node->leftChild = NULL;
node->data = data;
return node;
}
void Inorder(Node* ptr) { // travel
if (ptr)
{
printf("%c ", ptr->data);
Inorder(ptr->leftChild);
Inorder(ptr->rightChild);
}
}
int main(void)
{
Node* node[300];
for (int i = 1; i < 300; i++) {
if (i == 1) {
node[i] = create(i);
}
else {
if (i % 2 == 0) {
node[i / 2]->leftChild = create(i);
}
else {
node[i / 2]->rightChild = create(i);
}
}
}
Inorder(node[10]);
}
I would like to implement a binary tree using a Node* array, rather than taking variables input one by one. But I keep getting errors in that area. Thanks for the advice.Which part do I need to modify to make that part implemented through a for statement? As far as I understand, both the left and right parts of the node array have passed values, so why am I getting an error?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
