正如標題所說,我試圖從具有2例樹中刪除一個節點:麻煩試圖消除從樹中的節點(結構)
- 的節點是沒有連接到任何一個葉子
- 節點連接到另一個節點
我能夠做出第二種情況下工作的功能(對我來說最困難的),但第一個(似乎容易)引起分段錯誤沒有不管我嘗試什麼。
這裏是結構的定義:
struct node {
int value;
struct node *sx;
struct node *dx;
};
typedef struct node *tree;
這裏是執行消除操作的模塊:
void destroy_node(tree Y, int elem) {
if (Y->value < elem)
destroy_node(Y->dx, elem);
if (Y->value > elem)
destroy_node(Y->sx, elem);
else { //
if (Y->sx == NULL && Y->dx == NULL) {
free(Y); <-- seg fault
Y = NULL; <-- seg fault
}
if (Y->sx != NULL && Y->dx == NULL) {
Y->value = (Y->sx)->value;
free(Y->sx);
Y->sx = NULL;
}
if (Y->sx == NULL && Y->dx != NULL) {
Y->value = (Y->dx)->value;
free(Y->dx);
Y->dx = NULL;
}
if (Y->sx != NULL && Y->dx != NULL)
printf("Can't eliminate that!\n");
}
print_everything(Y);
}
這裏是main
電話:
tree Y = T;
printf("Which one you want to destroy? ");
scanf("%d", &elem);
destroy_node(Y, elem);
要編譯函數我使用命令
gcc -std=gnu99 -Wall -Wextra c.c
我的環境是虛擬機的Ubuntu 17.04與股票GCC
是構建樹
tree insert_node(tree T, int val) {
if (T == NULL) {
T = (tree)malloc(sizeof(struct node));
T->value = val;
T->sx = NULL;
T->dx = NULL;
} else {
if ((T->value) < (val)) {
T->dx = insert_node(T->dx, val);
}
if ((T->value) > (val)) {
T->sx = insert_node(T->sx, val);
}
}
return T;
}
在這裏有主的通話EDIT 1
模塊此模塊
printf("How many nodes your tree will have? ");
scanf("%d", &tot);
for (int i = 0; i < tot; i++) {
printf("What is the %d° node? ", (i + 1));
scanf("%d", &val);
T = insert_node(T, val);
}
P.S.如果有關於節目我會複製粘貼整個文件
你可以添加你建樹的地方嗎? – atru
@atru插入了您請求的其他函數,希望它有幫助 – Allen
您忘記檢查'Y'是否爲空。 –