我试图使用二叉搜索树来打印给定数组的所有独特元素。打印数组中的所有独特元素
我在做什么:
- 输入所有的阵列中的数字。
- 搜索用于由一个在BST的阵列的一个的每个元素,
- 如果(元件未在BST找到)
- 把它存在和打印
- 别的
- 去下一个元素
- 如果(元件未在BST找到)
#include<stdlib.h>
#include<stdio.h>
struct node
{
int data;
struct node* left;
struct node* right;
} *root = NULL;
void insert(struct node *n, int value)
{
if (n == NULL)
{
n = (struct node*)malloc(sizeof(struct node));
n->data = value;
n->left = NULL;
n->right = NULL;
printf("%d ", value);
return;
}
if ((n->data) == value)
return;
if ((n->data) > value)
insert(n->left, value);
else
insert(n->right, value);
}
int main()
{
int a[100], i, n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
printf("After removal of duplicates, the new list is : \n");
for (i = 0; i < n; i++)
insert(root, a[i]);
printf("\n");
return 0;
}
什么是你的问题? – SBI