0
이처럼 내 구조체 모습입니다의 루트/부모를 호출하는 방법 :이 추천하고 어떤 점에서 많은 구조체의 트리를 만드는 중이라서 내가 데이터를 확인하고 싶은구조체
struct Coordinate{
int x;
int y;
int steps;
struct Coordinate *left;
struct Coordinate *right;
struct Coordinate *up;
struct Coordinate *down;
}*root;
typedef struct Coordinate *Coor;
을 (x, y) 구조체의 루트.
어떻게이 구조체 루트 (부모)의 데이터를 가져 옵니까?
* 편집이 (그렇게 오래되지 않음) 내 모든 코드
입니다 * :
//
// main.c
// C-3
//
// Created by Nimrod Shai on 6/21/13.
// Copyright (c) 2013 Nimrod Shai. All rights reserved.
//
#include <stdio.h>
#define length 2
struct Coordinate{
int x;
int y;
int steps;
struct Coordinate *left;
struct Coordinate *right;
struct Coordinate *up;
struct Coordinate *down;
}*root;
typedef struct Coordinate *Coor;
int isValidCoordinate(struct Coordinate *aCoordinate, int x, int y, int map[length][length]){
if ((x >= 0) && (x <= length) && (y >= 0) && (y <= length) && !map[y][x]) {
for (int i = 0; i < aCoordinate -> steps; i++) {
aCoordinate = aCoordinate -> father;
if (aCoordinate->x == x && aCoordinate->y == y) {
return 0;
}
}
return 1;
}else{
return 0;
}
}
Coor insertDataToTree(Coor root, int x, int y, int map[length][length], int steps){
steps++;
if (root == NULL) {
root = (Coor)malloc(sizeof(Coor));
root->x = x;
root->y = y;
root->steps = steps;
root -> left = root -> right = root -> up = root -> down = NULL;
}
//left
if (isValidCoordinate(root,root -> x - 1, root -> y, map)) {
printf("f");
root->left = insertDataToTree(root -> left, x - 1, y, map,steps);
}
//right
if (isValidCoordinate(root,root -> x + 1, root -> y, map)) {
printf("f");
root->right = insertDataToTree(root -> right, x + 1, y, map,steps);
}
//up
if (isValidCoordinate(root,root -> x, root -> y - 1, map)) {
printf("f");
root->up = insertDataToTree(root -> up, x, y - 1, map,steps);
}
//down
if (isValidCoordinate(root,root -> x, root -> y + 1, map)) {
printf("f");
root->down = insertDataToTree(root -> down, x, y + 1, map,steps);
}
Coor ggg = NULL;
return ggg;
}
int main(int argc, const char * argv[])
{
int map[length][length] = {
{0,0},
{0,0}
};
struct Coordinate startPoint;
startPoint.x = 0;
startPoint.y = 0;
startPoint.steps = -1;
insertDataToTree(root, startPoint.x, startPoint.y, map, startPoint.steps);
// insert code here...
printf("Hello, World!\n");
return 0;
}
이 코드의 목적으로 특정 시점에서 매트릭스 내부의 전체 경로를 매핑한다
가지가있는 나무 = 가능한 모든 점에 대한 경로.
문제는 - 내가이 트리에 추가하는 지점에서 수행해야하는 검사 중 하나는 좌표가 해당 분기에서 반복되지 않는다는 것입니다 (다른 분기에 나타나면 상관 없습니다).
하지만 특정 구조체의 루트 값을 얻는 방법을 모르겠습니다.
나는 지금 내가 더 분명해졌고, 누군가가이 끔찍한 구문으로 나를 도울 수 있기를 바란다. (나는 대개 C 언어로 프로그램한다.)
감사합니다.
C 프로그램에서'malloc'의 리턴 값을 캐스트 할 필요가 없습니다. –
'Coor * '를 받았다면'Coor' 구조의 데이터로부터 부모 노드를 찾을 방법이 없습니다. 필요한 경우 struct에 struct Coordinate * parent를 추가하고 적절하게 초기화해야합니다. –
그 일에 대해 생각했지만 그 부모 변수를 초기화하는 올바른 위치가 어디인지 몰랐습니다. –