#include <stdio.h>
#include<stdlib.h>
struct node{
struct node *left;
struct node *right;
int data;
int num;
};
void addn(struct node *ptr,struct node *new){
if(ptr==NULL) ptr=new;
else if(ptr->data>new->data) addn(ptr->left,new);
else addn(ptr->right,new);
}
int mxnm(struct node *ptr){
if(ptr->right==NULL) return ptr->num;
return mxnm(ptr->right);
}
int max(struct node *head,int *arr,int num){
for(int t=0;t<num;t++){
struct node *new=malloc(sizeof(struct node));
new->left=NULL;
new->right=NULL;
new->data=arr[t];
new->num=t;
addn(head,new);
}
return mxnm(head);
}
int main() {
struct node* head=malloc(sizeof(struct node));
head->left=NULL;
head->right=NULL;
head->data=-1;
head->num=-2;
int *arr=malloc(sizeof(int)*5);
arr[0]=1;
arr[1]=6;
arr[2]=3;
arr[3]=4;
arr[4]=2;
printf("%d",max(head,arr,5));
return 0;
}
대충 기능은 정수 배열 입력하면 그중에 몇번째 원소가 제일 숫자가 큰거인지 알려주는 건데 배열을 포인터로도 바꿔보고 head 자식노드 초기화도 해보고 했는데 계속 head->num만 출력이 됨