[英]Creating binary tree and passing node as pass by reference
我試圖創建一個二叉樹,我是新來的數據結構。 我要做的是:
(1)取終端處樹的大小(節點總數)。
(2)然后在終端上最大讀取用戶的節點
(3)然后創建二叉搜索樹。
注意:我必須在函數調用中僅通過引用傳遞節點(沒有其他選項)。
它編譯沒有錯誤,但我猜有任何邏輯問題。 當我嘗試在for循環中插入第二個節點時,它會產生分段錯誤(首先它可以正常工作),但是編譯時沒有錯誤 。我無法預測是由於哪一行代碼引起的? 當我做:
How many nodes are to be inserted ?
5
enter the nodes
1 //I am not able to add more nodes
Segmentation fault (core dumped)
歡迎使用任何語言的c / c ++甚至算法回答。
我這樣做的代碼是:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct node
{
int freq;
struct node * left, * right;
};
typedef struct node node;
/////////////////////////////////////////////////////////////Function definitions //////////////////////////////////////////////////
insert_first_node(int data, node * * Node)
{
node * temp1 ;
temp1 = Node;
temp1= (node * ) malloc(sizeof(node));
temp1 -> freq = data;
temp1 -> left = NULL;
temp1 -> right = NULL;
}
////////////////////////////////////////////////////////////////////
insert_beginning(int data, node * * Node)
{
root = * Node;
root = (node * ) malloc(sizeof(node));;
if (root ==NULL)
{
insert_first_node(data, & root);
}
if (data <= root -> freq)
{
insert_beginning(data, & root -> left);
} else
{
insert_beginning(data, & root -> right);
}
*Node = root;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
main()
{
int i, size, data;
node * head;
head = NULL;
printf("How many nodes are to be inserted ?\n");
scanf("%d", & size);
for (i = 1; i <= size; i++)
{
printf("enter the nodes \n");
scanf("%d", & data);
insert_beginning(data, & head);
}
}
我會這樣寫(盡管我不一定要使用遞歸,但也許您應該...):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
int freq;
struct Node *left;
struct Node *right;
} Node;
///////////////////////////////////////////////////////////
// Function definitions //
///////////////////////////////////////////////////////////
int insert_leaf_node(int freq, Node **parent_ptr)
{
Node *node;
if ((node = malloc(sizeof *node)) == NULL)
return -1;
node->freq = freq;
node->left = NULL;
node->right = NULL;
*parent_ptr = node;
return 0;
}
///////////////////////////////////////////////////////////
int insert_node(int freq, Node **parent_ptr)
{
Node *node = *parent_ptr;
if (node == NULL) {
return insert_leaf_node(freq, parent_ptr);
}
else if (freq <= node->freq) {
return insert_node(freq, &node->left);
}
else {
return insert_node(freq, &node->right);
}
}
///////////////////////////////////////////////////////////
int main()
{
int i, size, freq;
Node *root = NULL;
printf("How many nodes are to be inserted ?\n");
scanf("%d", &size);
for (i = 0; i < size; i++) {
printf("enter the freq of node %d\n", i+1);
scanf("%d", &freq);
insert_node(freq, &root);
}
return 0;
}
這是我不遞歸地編寫insert_node
:
int insert_node(int freq, Node **parent_ptr)
{
Node *node;
while ((node = *parent_ptr) != NULL) {
parent_ptr = (freq <= node->freq) ? &node->left : &node->right;
}
return insert_leaf_node(freq, parent_ptr);
}
您只能從第一個輸入開始出現分段錯誤。 讓我澄清原因。
在insert_beginning
函數中,第一行是root = * Node;
。 這里*Node
已經為NULL
。 因此, root
也將具有NULL
值。 您希望root也指向與*Node
相同的地址,但事實並非如此,因為*Node
沒有指向任何內容,因此root
和*Node
仍然無關。 現在,您已經在上一行中將內存分配給了root
,但是現在您將NULL
分配給了root
。 因此,先前分配給root
地址會丟失。 達利博爾正在談論的就是泄漏記憶。
讓我們繼續。
現在檢查root==NULL
,這是真的,因此insert_first_node
。 有temp1=Node
,這在語法上是錯誤的。 我認為您打算使用temp1 = *Node
。 但這仍然是錯誤的,因為*Node
為NULL
,因此將為temp1
。 現在,您正在將值分配給NULL
對象。 所以下一行給出了分割錯誤。
工作代碼可以是
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct node
{
int freq;
struct node * left, * right;
};
typedef struct node node;
/////////////////////////////////////////////////////////////Function definitions //////////////////////////////////////////////////
void insert_first_node(int data, node * * Node,int direction)
{
node * temp1 = (node * ) malloc(sizeof(node));
temp1 -> freq = data;
temp1 -> left = NULL;
temp1 -> right = NULL;
if(*Node == NULL)
*Node = temp1;
else if(direction == 1)
(*Node)->right = temp1;
else
(*Node)->left = temp1;
}
////////////////////////////////////////////////////////////////////
void insert_beginning(int data, node * * Node)
{
node *root;
root = * Node;
if (root == NULL)
{
insert_first_node(data,Node,0);
return;
}
if (data <= root -> freq)
{
if(root->left == NULL)
insert_first_node(data,&root,0);
else
insert_beginning(data,&root->left);
} else
{
if(root->right == NULL)
insert_first_node(data,&root,1);
else
insert_beginning(data,&root->right);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
main()
{
int i, size, data;
node * head;
head = NULL;
printf("How many nodes are to be inserted ?\n");
scanf("%d", & size);
for (i = 1; i <= size; i++)
{
printf("enter the nodes \n");
scanf("%d", & data);
insert_beginning(data, & head);
}
}
在插入第一個節點期間,它會發生段故障。 如果是在第二次插入過程中,您將看到兩次“輸入節點”消息。
現在到原因了。 在函數insert_beginning中,第一個if語句不將root與NULL比較,而是將其設置為NULL。 由於將NULL視為false,因此不會評估if中的代碼,並且執行將移至第二條if語句。 在其中,您嘗試訪問root的freq字段,該字段從第一個if語句設置為NULL。 因此,您嘗試取消引用NULL指針,這會導致seg-fault。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.