2013-06-20 62 views
2

添加節點,我想知道爲什麼我們使用,指針的指針,而在二叉樹插入節點的原因。 但是,在遍歷二叉樹時,我們只需通過指向根節點的簡單指針來引用樹。但爲什麼在插入節點?使用指針結構的指針,而在二叉樹

誰能幫我提供的理由或引用鏈接來了解爲什麼它是指針的指針。

/*This program clears out all the three methods of traversal */ 

#include<stdio.h> 
#include<stdlib.h> 

/* Let us basically describe how a particular node looks in the binary tree .... Every node in the tree has three major elements , left child, right child, and and the data. */ 

struct TreeNode { 
int data; 
struct TreeNode *leftChild; 
struct TreeNode *rightChild; 
}; 

void inorder(struct TreeNode *bt); 
void preorder(struct TreeNode *bt); 
void postorder(struct TreeNode *bt); 
int insert(struct TreeNode **bt,int num); 

main() 
{ 
int num,elements; 
struct TreeNode *bt; 
int i; 

printf("Enter number of elements to be inserted in the tree"); 
scanf("%d",&num); 

printf("Enter the elements to be inserted inside the tree"); 
for(i=0;i<num;i++) 
{ 
scanf("%d",&elements); 
insert(&bt,elements); 
printf("\n"); 
} 

printf("In Order Traversal \n"); 
inorder(bt); 

printf("Pre Order Traversal \n"); 
preorder(bt); 

printf("Post Order Traversal \n"); 
postorder(bt); 

return 0; 
} 

int insert(struct TreeNode **bt,int num) 
{ 
if(*bt==NULL) 
{ 
*bt= malloc(sizeof(struct TreeNode)); 

(*bt)->leftChild=NULL; 
(*bt)->data=num; 
(*bt)->rightChild=NULL; 

return; 
} 
else{ 
/* */ 
if(num < (*bt)->data) 
{ 
insert(&((*bt)->leftChild),num); 
} 
else 
{ 
insert(&((*bt)->rightChild),num); 
} 
} 
return; 
} 

void inorder(struct TreeNode *bt){ 
if(bt!=NULL){ 


//Process the left node 
inorder(bt->leftChild); 

/*print the data of the parent node */ 
//printf(" %d ", bt->data); 

/*process the right node */ 
inorder(bt->rightChild); 
} 

} 

void preorder(struct TreeNode *bt){ 
if(bt) 
{ 
//Process the parent node first 
printf("%d",bt->data); 

//Process the left node. 
preorder(bt->leftChild); 

//Process the right node. 
preorder(bt->rightChild); 


} 

} 


void postorder(struct TreeNode *bt){ 

if(bt) 
{ 
//process the left child 
postorder(bt->leftChild); 

//process the right child 
postorder(bt->rightChild); 


//process the parent node 
printf("%d",bt->data); 


} 
} 
+0

沒」你錯過這裏的'* /':'/ *處理正確的節點'? – soon

+0

喲得到它..我的vi編輯器是在一種顏色..沒有能夠弄清楚..但仍然是指針的問題指針仍然沒有解決 –

+0

現在好了,它現在是運行代碼!仍然我無法弄清楚它是插入節點時指針的指針..任何人都可以提供參考。或者原因 –

回答

3

"I want to know the reason why do we use, pointer to pointer while inserting nodes in the binary tree. But, While traversing the binary tree, we just refer the tree by simple pointer to the root node. But why while inserting node?"

事實上,我們甚至不需要代碼來回答這個問題。如果你想修改(寫入)數據在C中的外部函數,你需要有數據的地址。就像:

main() { 
    int x = 2; 
    change_me(x); 
    printf("%d\n", x); // prints 2 
} 

void change_me(int x){ 
    x++; 
} 

沒有意義。你(在這個例子中)獲得可變的本地副本,對值所做的任何更改都只在本地範圍內。如果您希望這些更改傳播回調用函數需要地址:

main() { 
    int x = 2; 
    change_me(&x); 
    printf("%d\n", x); // prints 3 
} 

void change_me(int* x){ 
    (*x)++; 
} 

這同樣適用於指針。在鏈表的例子,如果我想打印的價值觀,我需要遍歷樹和讀取數據。我不需要改變任何東西,只需要指針即可。但是,如果我想修改樹:

struct node{ 
    int val; 
    sturct node* next; 
}; 

main() { 
    struct node* head = malloc(sizeof(struct node)); 
    head->val = 3; 
    insert_a_node_in_front(head); 
} 

insert_a_node_in_front(node * ptr) { 
    struct node* temp = ptr; 
    ptr = malloc(sizeof(struct node)); 
    ptr->val = 5; 
    ptr->next = temp; 
} 

那麼,猜猜看是什麼?我們實際上並沒有插入那個節點,因爲head的值從未改變過。它仍然指向具有val==3的原始節點。原因與之前一樣,我們試圖改變參數本地副本的值。如果我們希望這些變化要堅持它所需要的原件的地址:

insert_a_node_in_front(&head); 
} 

insert_a_node_in_front(node ** ptr) { 
    struct node* temp = (*ptr); 
    (*ptr) = malloc(sizeof(struct node)); 
    (*ptr)->val = 5; 
    (*ptr)->next = temp; 
} 
+0

這顯然幫助我理解!謝謝 –

2

這是因爲插入的第一部分malloc一個新的struct treenode。如果僅在一個結構樹節點傳遞*這將是這個樣子:

int insert(struct TreeNode *bt,int num) 
{ 
    if(bt==NULL) 
    { 
     bt= malloc(sizeof(struct TreeNode)); 

     (bt)->leftChild=NULL; 
     (bt)->data=num; 
     (bt)->rightChild=NULL; 

    return; 
    } 
    ... 
} 

的問題,這將是英國電信是當地插入所以在主要的BT將保持不變。所以你傳入一個指向main的bt的指針,它允許insert來改變它。

+0

非常感謝主席先生! –