포트란 77-1085 자
subroutine q(u,t)
implicit integer(i-z)
character*33 f,g
dimension t(u)
m=ceiling(log(real(u))/log(2.))
v=2**(m+1)-1
do l=1,m
n=2**(l-1)
k=2**(m-l+2)-3
w=(3+k)*2**(l-1)-k
p=1+(v-w)/2
if(l.ne.1)then
write(f,'(A,I3,A)')'(A',p,',$)'
print f,' '
write(f,'(A5,I3,A3)')'(A3,A',k,',$)'
do j=2**(l-1),2**l-1
if(t(j/2).lt.0.or.t(j).lt.0)then
print f,' ',' '
elseif(mod(j,2).eq.0)then
print f,' /',' '
else
print f,' \ ',' '
endif
enddo
print*
endif
write(f,'(A,I3,A)')'(A',p,',$)'
print f,' '
write(f,'(A5,I3,A3)')'(I3,A',k,',$)'
write(g,'(A2,I3,A3)')'(A',k+3,',$)'
do j=2**(l-1),2**l-1
if(t(j).ge.0)then
print f,t(j),' '
else
print g,' '
endif
enddo
print*
enddo
end
트리는 t
일반적인 방식으로 입력 배열 에 루트 1에서 루트-> 2에서 왼쪽, 루트-> 3에서 루트-> 왼쪽-> 4에서 왼쪽으로 표시됩니다.
출력은 최대 5 단계 깊이의 일반 터미널에 맞아야합니다.
각 노드 쌍 사이에 정확히 하나의 슬래시를 사용합니다. 4 개 이상의 레벨이 있으면 상단 근처에서 꽤 어리석게 보입니다. 최대 3 자리 노드를 허용했습니다.
의견과 발사 발판이있는 전체 프로그램 :
program tree
parameter (l=8) ! How many nodes to support
dimension i(l)
c Initialize the array to all empty nodes
do j=1,l
i(j)=-1
end do
c Fill in some values
i(1)=3
i(2)=1
i(3)=5
i(5)=2
i(6)=4
i(7)=7
c i(14)=6
c i(15)=8
c Call the printing routine
call q(l,i)
stop
end
c Print an ASCII representation of the tree
c
c u the length of the array containing the tree
c t an integer array representing the tree.
c
c The array contains only non-negative values, and empty nodes are
c represented in the array with -1.
c
c The printed representation uses three characters for every node,
c and places the (back) slash equally between the two node-centers.
subroutine q(u,t)
implicit integer(i-z)
character*33 f,g
dimension t(u)
m=ceiling(log(real(u))/log(2.)) ! maximum depth of the tree
v=2**(m+1)-1 ! width needed for printing the whole tree
! Optimized from 3*2**m + 1*((2**m)-1) at
! the bottom level
do l=1,m
n=2**(l-1) ! number of nodes on this level
k=2**(m-l+2)-3 ! internode spacing
w=(3+k)*2**(l-1)-k ! width needed for printing this row
! Optimized from 3*2**l + k*((2**l)-1) at
! the bottom level
p=1+(v-w)/2 ! padding for this row
c Print the connecting lines associated with the previous level
if(l.ne.1)then ! Write the connecting lines
write(f,'(A,I3,A)')'(A',p,',$)'
print f,' '
write(f,'(A5,I3,A3)')'(A3,A',k,',$)'
do j=2**(l-1),2**l-1
if(t(j/2).lt.0.or.t(j).lt.0)then
print f,' ',' '
elseif(mod(j,2).eq.0)then
print f,' /',' '
else
print f,' \ ',' '
endif
enddo
print*
endif
c Print the nodes on this level
write(f,'(A,I3,A)')'(A',p,',$)'
print f,' '
write(f,'(A5,I3,A3)')'(I3,A',k,',$)'
write(g,'(A2,I3,A3)')'(A',k+3,',$)'
do j=2**(l-1),2**l-1
if(t(j).ge.0)then
print f,t(j),' '
else
print g,' '
endif
enddo
print*
enddo
end
예제와 동등한 입력을 가진 출력 :
$ ./a.out
3
/ \
1 5
\ / \
2 4 7