2
#include<stdio.h>
#include<stdlib.h>
typedef struct BTreeNode BTNode;
struct BTreeNode
{
int value;
struct BTreeNode *left_child,*right_child;
};
BTNode* insert(int input_value, BTNode **head_node)
{
BTNode *temp,*head;
temp = malloc(sizeof(BTNode));
temp->value = input_value;
temp->left_child = NULL;
temp->right_child = NULL;
head = *head_node;
while(1)
{
if(head == NULL)
{
head = temp;
printf("Inserted successfully\n");
return *head_node;
}
if(temp->value > head->value)
{
head = head->right_child;
}
else if(temp->value < head->value)
{
head = head->left_child;
}
else
{
break;
}
}
return head;
}
void printtree(BTNode **head_node)
{
BTNode *head;
head = *head_node;
if(head == NULL)
{
printf("Print exit\n");
return;
}
else
{
printf("%d\n",head->value);
printtree(&(head->left_child));
printtree(&(head->right_child));
}
}
int main()
{
BTNode *root=NULL;
root=insert(23,&root);
root=insert(32,&root);
root=insert(230,&root);
root=insert(3,&root);
root=insert(2,&root);
root=insert(50,&root);
printtree(&root);
}
Я пытаюсь вставить узлы в двоичное дерево поиска и распечатать дерево. Когда я вызываю функцию printtree()
, она будет NULL
условие i.e, корень NULL
. Я не понимаю, почему он не работает может кто-нибудь помочь?Не удалось распечатать Дерево двоичного поиска
даже сейчас он не печатает значения узлов. Он печатает заявление «Выход печати». Что может быть проблемой? Спасибо – sr116
Он работал с небольшим fix.Have, чтобы вернуть голову в случае, если голова равна null вместо * headnode – sr116