2015-05-29 40 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));