Julia中使用结构数据类型,指针和this的树和图问题

问题描述 投票:0回答:1

我想编写使用Julia语言解决一些图/树问题的方法。这是一个很好的例子。在C语言中是这样完成的:递归C程序,用于遍历二叉树[]

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

/* A binary tree node has data, pointer to left child
   and a pointer to right child */
struct node
{
    int data;
    struct node *left, *right;
};

/* Function prototypes */
void printGivenLevel(struct node* root, int level);
int height(struct node* node);
struct node* newNode(int data);

/* Function to print level order traversal a tree*/
void printLevelOrder(struct node* root)
{
    int h = height(root);
    int i;
    for (i=1; i<=h; i++)
        printGivenLevel(root, i);
}

/* Print nodes at a given level */
void printGivenLevel(struct node* root, int level)
{
    if (root == NULL)
        return;
    if (level == 1)
        printf("%d ", root->data);
    else if (level > 1)
    {
        printGivenLevel(root->left, level-1);
        printGivenLevel(root->right, level-1);
    }
}

/* Compute the "height" of a tree -- the number of
    nodes along the longest path from the root node
    down to the farthest leaf node.*/
int height(struct node* node)
{
    if (node==NULL)
        return 0;
    else
    {
        /* compute the height of each subtree */
        int lheight = height(node->left);
        int rheight = height(node->right);

        /* use the larger one */
        if (lheight > rheight)
            return(lheight+1);
        else return(rheight+1);
    }
}

/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct node* newNode(int data)
{
    struct node* node = (struct node*)
                        malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;

    return(node);
}

/* Driver program to test above functions*/
int main()
{
    struct node *root = newNode(1);
    root->left        = newNode(2);
    root->right       = newNode(3);
    root->left->left  = newNode(4);
    root->left->right = newNode(5);

    printf("Level Order traversal of binary tree is \n");
    printLevelOrder(root);

    return 0;
}

我曾尝试以类似的方式在Julia中执行此操作,但是存在一些问题,尤其是在访问struct元素时(例如node-> right和node-> left)。或通过某种方式创建自引用结构和函数来分配节点。

struct node
  data::Int
  left::Ptr{node}
  right::Ptr{node}
end

# Compute the "height" of a tree -- the number of
#     nodes along the longest path from the root node
#     down to the farthest leaf node.
function height = (node::Ptr{node})
  if node === nothing
    return 0
  else
    # compute the height of each subtree 
    lheight = height(node->left)
    rheight = height(node->right)

    # use the larger one 
    if lheight > rheight
      return lheight+1
    else return rheight+1
    end

  end
end

根据我所见,尝试以C方式重新创建问题解决方案不是那种方式,但是这种结构类型应该很有用。我只需要知道如何创建自引用结构,如何在此节点中分配元素以及如何获取它们。

我想编写使用Julia语言解决一些图/树问题的方法。这是一个很好的例子。在C语言中是以这种方式完成的:递归C程序,用于遍历二叉树#include <...>] >>

algorithm data-structures struct julia graph-algorithm
1个回答
2
投票

首先,我强烈建议您通读https://docs.julialang.org/en/v1/,因为Julia在很多方面与C不同。这里有很多要指出的地方:

  • 默认情况下,Julia中的struct是不可变的,这意味着创建字段后便无法修改字段。由于它没有特定的内存地址,并且通常在堆栈上分配,因此它也总是通过复制而不是引用传递。实际上,这对编译器有很多好处,并且是Julia可以这么快的原因之一。在您的示例中,您可能需要mutable struct,与C中的struct更相似。
  • 在Julia中,除非要调用C代码,否则永远不必直接使用指针(Ptr)。由于Julia使用垃圾回收器,因此在内存管理方面,原始指针有很多陷阱,通常应避免使用。通常,您通常直接使用对象,或者,如果要通过引用传递不可变的对象,则可以将它们包装在Ref中。
© www.soinside.com 2019 - 2024. All rights reserved.