我需要在Fortran中为一个项目实现一个树结构,所以我在网上阅读了各种指南,解释了如何做到这一点.但是,我不断收到错误或奇怪的结果.
假设我想构建一个二叉树,每个节点存储一个整数值.我还希望能够将新值插入树中并打印树的节点.所以我写了一个类型"树",它包含一个整数,两个指向子子树的指针和一个我设置为.true的布尔值.如果没有子子树:
module class_tree
implicit none
type tree
logical :: isleaf
integer :: value
type (tree), pointer :: left,right
end type tree
interface new
module procedure newleaf
end interface
interface insert
module procedure inserttree
end interface
interface print
module procedure printtree
end interface
contains
subroutine newleaf(t,n)
implicit none
type (tree), intent (OUT) :: t
integer, intent (IN) :: n
t % isleaf = .true.
t % value = n
nullify (t % left)
nullify (t % right)
end subroutine newleaf …Run Code Online (Sandbox Code Playgroud)