2015-05-29 83 views
0

我试图使用二叉搜索树来打印给定数组的所有独特元素。打印数组中的所有独特元素

我在做什么:

  1. 输入所有的阵列中的数字。
  2. 搜索用于由一个在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; 
}  
+7

什么是你的问题? – SBI

回答

0

您的代码有一些错误,你的函数插入,应该返回一个结构节点*,否则你不会建立一个二叉搜索树。 在您的初始代码中,当您调用insert时,您始终使用空节点进行调用。

这是你应该做的,只需很少的改动。

#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 

struct node 
{ 
    int data; 
    struct node* left; 
    struct node* right; 
} *root = NULL; 

struct node* 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("inserted %d\n", value); 
     return n; 
    }else{ 
     if(value == n->data){ 
      printf("Duplicated Value %d\n", value); 
      return n; 
     } 
     if ((n->data) > value){ 
      n->left = insert(n->left, value); 
     }else{ 
      n->right = insert(n->right, value); 
     } 
     return n; 
    } 
} 

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++) 
     root = insert(root, a[i]); 
    printf("\n"); 
    return 0; 
} 

顺便说一句,this应该是有益的,这是一个测试example。 ;)

你应该创建数组AFTER,你知道的大小,否则你可能会分配太多的内存。当然,你应该给用户创造超过100个元素

二叉树的可能性
int i, n; 
printf("Enter the number of elements : "); 
scanf("%d",&n); 
int a[n]; 
0

第一: 不要在返回void ..但如果你喜欢你需要返回的地址树,因为它每次调用函数都会改变! 在你过去的代码,你有充分的节点没有他的左,右连接时间发送不同的树..

#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 

struct node 
{ 
    int data; 
    struct node* left; 
    struct node* right; 
} *root = NULL; 

struct node* 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 n; 
    } 

    else if(value == n->data) 
       return n; 

    else if ((n->data) > value) 
       n->left = insert(n->left, value); 

     else 
      insert(n->right, value); 

     return n; 
    } 


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++) 
     root = insert(root, a[i]); 
    printf("\n"); 
    return 0; 
} 

PS:这家伙有告诉你:

int i, n; 
printf("Enter the number of elements : "); 
scanf("%d",&n); 
int a[n]; 

你不能做那在c! 你必须知道表格的大小,你无法制作一张长度为一个表格的变量! (用于分配的大小,你只需要在C使用链表)

0
#include<stdlib.h> 
    #include<stdio.h> 
    #include<string.h> 

    struct node 
{ 
    int data; 
    struct node* left; 
    struct node* right; 
} *root = NULL; 

struct node* 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 n; 
} 

else if(value == n->data) 
      return n; 

else if ((n->data) > value) 
      n->left = insert(n->left, value); 

    else 
     insert(n->right, value); 

    return n; 
} 


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++) 
    root = insert(root, a[i]); 
    printf("\n"); 
    return 0; 
} 

您可以修改动态分配的代码。

int *a,n; 
scanf("%d",n); 
a = (int)malloc(n*sizeof(int));