왜 이진 트리에 노드를 삽입하는 동안 우리는 포인터를 사용하는 이유를 알고 싶습니다. 그러나 이진 트리를 탐색하는 동안 트리를 루트 노드에 대한 간단한 포인터로 참조합니다. 하지만 왜 노드를 삽입하는 동안?이진 트리에서 노드를 추가하는 동안 구조체의 포인터에 포인터 사용
포인터가 포인터 인 이유를 이해하는 이유 또는 참조 링크를 제공하는 사람을 도와 줄 수 있습니까?
/*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);
}
}
하셨습니까 ' '* /'여기서 :'/ * 오른쪽 노드를 처리 하시겠습니까? '? – soon
옙. 알았어. 내 vi 편집기가 한 가지 색 이었어. 그걸 알아낼 수는 없었어.하지만 여전히 포인터에 대한 질문 포인터는 아직 풀리지 않았다. –
이제 코드가 실행 중이다 !! 여전히 노드를 삽입하는 동안 그것 포인터 포인터입니다 알아낼 수 없습니다. 아무도 참조를 제공 할 수 있습니다. 또는 이유 –