我用于打印树的显示功能似乎只打印第一个元素而不是其余元素。我不知道为什么我怀疑我在没有递归的情况下使用的插入功能可能是原因,但似乎无法理解它出错的地方。关于如何纠正它或代码失败的任何解释都会有所帮助。感谢。
#include <stdio.h>
#include<stdlib.h>
void insert(int data_add,struct tree *temp);
void display(struct tree *temp);
struct tree
{
int data;
struct tree *left;
struct tree *right;
} *root = NULL;
int main()
{
int data_add,n;
while(1)
{
printf("\n\n1.Add\n2.Display\n4.Exit\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("\nEnter the element to add ");
scanf("%d",&data_add);
insert(data_add,root);
break;
case 2: printf("The nos are: ");
display(root);
break;
/*case 3: printf("The nos are: ");
reversedisplay(root);*/
case 4: exit(1);
break;
default: printf("\nChoose a appropriate option");
}
}
}
void insert(int data,struct tree *temp)
{
struct tree *current;
current = (struct tree*) malloc(sizeof(struct tree));
current->data = data;
if(root == NULL)
{
root = current;
current->left = NULL;
current->right = NULL;
}
else
{
while(temp!=NULL)
{
if(data<temp->data)
{
temp = temp->left;
}
else
{
temp = temp->right;
}
}
temp = current;
current->left = NULL;
current->right = NULL;
}
}
void display(struct tree *temp)
{
if(temp == NULL)
return;
display(temp->right);
display(temp->left);
printf("%d",temp->data);
}
答案 0 :(得分:0)
The problem in your code is that while inserting a node you are not making the new inserted node as the left or right child of any node, because of which the new node is not actually inserted in your tree.
Your code where things go wrong -
temp = current;
current->left = NULL;
current->right = NULL;
After coming out of the loop, temp
is NULL , now current
is assigned to temp
, but there is no way to reach the new node from any other node in the tree, because it is not left/right child of any node in the tree.
Here I present the correct code for the insert function -
void insert(int data,struct tree *temp)
{
struct tree *current;
current = (struct tree*) malloc(sizeof(struct tree));
current->data = data;
current->left = NULL;
current->right = NULL;
if(root == NULL)
{
root = current;
current->left = NULL;
current->right = NULL;
}
else
{
struct tree *par=temp; //par is used to keep track of node whose left
//or right child will be the new node.
while(temp!=NULL)
{
par=temp;
if(data<temp->data)
temp=temp->left;
else temp=temp->right;
}
// The below part is used to make the new node as left or right child
// of the appropriate node.
if(data<par->data)
par->left=current;
else
par->right=current;
}
}
Moreover , slight change in your display
function, change your printf
statement to -
printf("%d ",temp->data);
.
In previous version all the node values would be printed without no spaces giving an impression that only one number is printed.
答案 1 :(得分:0)
The problem is in the insert
function. You never link the new node to the old one. You would need to use pointers keep the address of the right or left node. Instead of that, you only copy the address of current node in the local temp variable. You code should be:
...
else
{
t = &temp;
while(*t!=NULL)
{
if(data<(*t)->data)
{
t = &(*t)->left;
}
else
{
t = &(*t)->right;
}
}
*t = current; // actually copy current in right of left of last node
current->left = NULL;
current->right = NULL;
}
But that's not all, in order to display the elements in order, you should change display to:
void display(struct tree *temp)
{
if(temp == NULL)
return;
display(temp->left); // left side first
printf("%d",temp->data); // then current
display(temp->right); // finally right side
}