2016-02-15 53 views
0

我一個嵌套的結構是這樣BST鏈接和案件沒有工作

typedef struct Node_link { 
struct Node_base *parent, *left, *right; 
}Node_link; 


typedef struct Node_base { 
struct Node_link link[2]; 
}Node_base; 

typedef struct Node{ 
struct Node_base base; 
int size; 
int *address; 
}Node; 


Node_base *head[2] ={NULL, NULL}; 

//頭[0]商店int長度和頭[1]它對應的地址

節點有權,左和父鏈接,都是嵌套的,例如node-> left-> link.parent = node。我必須維護所有鏈接(父,左和右)並刪除節點。 我已經嘗試了很多案例,仍然缺少一些。有人能告訴我什麼情況下我需要使用?或者轉介我一些材料?我搜索了很多,但沒有成功。 我的插入功能如下:

Node_base * insert(Node_base *location, Node_base *n) { 
    if (head[0]==NULL) 
    head[0]=n; 
else 
{ 
if (location==NULL){ 
    location=n; 
    return location; 
    } 
else{ 

     if(((Node *)n)->size < ((Node *)location)->size){ 
      if(location->link[0].left==NULL) 
      { 
      location->link[0].left=n; 
      location->link[0].left->link[0].parent=location; 
      } 
      else 
       location->link[0].left=insert(location->link[0].left,n); 
      return location; 
    } 
} 

我已經爲頭相同的嵌套插入功能[1],其存儲在插入頭部節點的大小[0]。

+0

你的結構看起來對我來說都是錯的。你的插入函數不應該接受節點類型嗎?爲什麼你甚至需要Node和Node_base之間的分離?爲什麼Node_base需要2個Node_link結構?這是給你什麼部分,你自己實現了什麼? –

回答

0

很難說出這裏發生了什麼。你的代碼看起來不像我見過的任何BST實現。爲什麼需要Node_Link結構? Node結構中的指針應該定義鏈接的內容。爲什麼是父指針?這在標準BST實施中不應該需要。所有你需要的是:

struct node { 
    node *left; 
    node *right; 
    void *data; 
    int size; 
}; 

struct bst { 
    node *root; 
};