2015-09-26 42 views
0

我嘗試編譯該程序代碼塊,但是它給出了一個錯誤信息錯誤在二叉搜索樹插入功能

不能轉換空隙樹節點

(插入函數),雖然我已經在參數部分本身中指定了聲明t。我嘗試刪除該行,但該程序沒有給出任何輸出。有什麼建議麼?

#include<iostream> 
#include<malloc.h> 
using namespace std; 
struct treenode; 
typedef struct treenode *position; 
typedef struct treenode *searchtree; 
typedef int elementtype; 
position find(elementtype x,searchtree t); 
position findmin(searchtree t); 
position findmax(searchtree t); 
searchtree insert(elementtype x,searchtree t); 
searchtree delete1(elementtype x,searchtree t); 
struct treenode 
{ 
    elementtype element; 
    searchtree left; 
    searchtree right; 
}; 
position find(elementtype x,searchtree t) 
{ 
    if(t==NULL) 
     return NULL; 
    else if(x<t->element) 
     return find(x,t->left); 
    else if(x>t->element) 
     return find(x,t->right); 
    else 
     return t; 
} 
position findmin(searchtree t) 
{ 
    if(t==NULL) 
     return NULL; 
    else if(t->left==NULL) 
     return t; 
    else 
     return findmin(t->left); 
} 
position findmax(searchtree t) 
{ 
    if(t!=NULL) 
     while(t->right!=NULL) 
      t=t->right; 
    return t; 
} 
searchtree insert(elementtype x,searchtree t) 
{ 
    if(t==NULL) 
    { 
     t=malloc(sizeof(struct treenode)); //gives me error on compiling 
     if(t==NULL) 
     { 
      cout<<"\n Out of space"; 
      exit(0); 
     } 
     t->element=x; 
     t->right=t->left=NULL; 
    } 
    else if(x<t->element) 
     t->left=insert(x,t->left); 
    else if(x>t->element) 
     t->right=insert(x,t->right); 
    return t; 
} 
void display(searchtree t) 
{ 
    if(t==NULL) 
     return; 
    display(t->left); 
    cout<<t->element; 
    display(t->right); 
} 
searchtree deletion(elementtype x,searchtree t) 
{ 
    position tmpcell; 
    if(t==NULL) 
    { 
     cout<<"\nElement not found"; 
     return NULL; 
    } 
    else if(x<t->element) 
     t->left=deletion(x,t->left); 
    else if(x>t->element) 
     t->right=deletion(x,t->right); 
    else if(t->left && t->right) 
    { 
     tmpcell=findmin(t->right); 
     t->element=tmpcell->element; 
     t->right=deletion(t->element,t->right); 
    } 
    else 
    { 
     tmpcell=t; 
     if(t->left==NULL) 
      t=t->right; 
     else if(t->right==NULL) 
      t=t->left; 
     free(tmpcell); 
    } 
    return t; 
} 
int main() 
{ 
    int ch,x; 
    position p; 
    searchtree t=NULL,min,max; 
    while(1) 
    { 
     cout<<"\n1. Insert\n2. Delete\n3.Find min\n4. Find max\n5. Display\n6. Exit"; 
     cout<<"\nEnter your choice:"; 
     cin>>ch; 
     switch(ch) 
     { 
     case 1: 
      cout<<"\nEnter the element:"; 
      cin>>x; 
      t=insert(x,t); 
      break; 
     case 2: 
      cout<<"\nEnter the element to be deleted:"; 
      cin>>x; 
      t=deletion(x,t); 
      break; 
     case 3: 
      min=findmin(t); 
      if(min) 
       cout<<"\nMinimum element is "<<min->element; 
      else 
       cout<<"\nEmpty tree"; 
      break; 
     case 4: 
      max=findmax(t); 
      if(max) 
       cout<<"\nMaximum element is "<<max->element; 
      else 
       cout<<"\nEmpty tree"; 
      break; 
     case 5: 
      display(t); 
      break; 
     case 6: 
      exit(0); 
     default : 
      cout<<"\nWrong choice"; 
     } 
    } 
    return 0; 
} 
+0

請注意,在main()中存在未使用的變量'position p;'。 –

+0

如果你真的想要C++,那麼所有具有'searchtree t'參數('find()','findmax()',...)的函數應該是'treenode'的類成員。沒有't->'的代碼會更好。 –

回答

0

在C指針void *被自動安全地提升爲任何其他指針類型。因此,它不投malloc輸出中您的指針,例如Do I cast the result of malloc?

然而需要,在C++中就需要投malloc()輸出,例如 Why does C++ require a cast for malloc() but C doesn't?

由於您使用C++,你必須顯式轉換void*指向所需的指針類型:

// valid in C, but not valid in C++ 
struct treenode *t = malloc(sizeof(struct treenode)); 

// C++ 
t = (searchtree)(malloc(sizeof(struct treenode))); 
// or 
t = (struct treenode*)(malloc(sizeof(struct treenode))); 
// or 
t = static_cast<searchtree>(malloc(sizeof(struct treenode))); 

當然適用於C++是有意義的考慮本地C++功能的使用。您正在爲一個結構實例分配內存,因此可以使用new

t = new treenode; // instead of malloc 
... 
delete tmpcell; // instead of free(tmpcell); 
+1

在C++中,你應該只**使用'new' /'delete',否則構造函數/析構函數代碼將不會運行.... – Soren

+0

@Soren我同意。我開始解釋'malloc'錯誤,後來我意識到它完全是關於一個結構實例。 –

+0

謝謝大家。該程序現在可以與新的treenode命令一起使用,但需要對輸出進行很少的打印。除了新函數,我還可以使用malloc()和顯式指針強制轉換。我的理解是正確的嗎? – falcon97