Here's a Python function to merge two Binary Search Trees (BSTs) and its translation to Fortran:
Python:
python
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def mergeBSTs(root1, root2):
if not root1:
return root2
if not root2:
return root1
stack1 = []
stack2 = []
current1 = root1
current2 = root2
while current1 or current2 or stack1 or stack2:
while current1:
stack1.append(current1)
current1 = current1.left
while current2:
stack2.append(current2)
current2 = current2.left
if not stack2 or (stack1 and stack1[-1].val <= stack2[-1].val):
current1 = stack1.pop()
current2 = current1.right
yield current1.val
else:
current2 = stack2.pop()
current1 = current2.right
yield current2.val
Fortran:
fortran
module bst_module
type TreeNode
integer :: val
type(TreeNode), pointer :: left => null()
type(TreeNode), pointer :: right => null()
end type TreeNode
contains
recursive function mergeBSTs(root1, root2) result(merged_values)
type(TreeNode), pointer, intent(in) :: root1, root2
integer, allocatable :: merged_values(:)
type(TreeNode), pointer :: current1, current2
type(TreeNode), allocatable :: stack1(:), stack2(:)
integer :: i, j, k
if (.not. associated(root1)) then
merged_values = traverseBST(root2)
return
end if
if (.not. associated(root2)) then
merged_values = traverseBST(root1)
return
end if
allocate(stack1(0))
allocate(stack2(0))
current1 => root1
current2 => root2
i = 0
j = 0
k = 0
do while (associated(current1) .or. associated(current2) .or. i > 0 .or. j > 0)
do while (associated(current1))
i = i + 1
call append(stack1, current1)
current1 => current1%left
end do
do while (associated(current2))
j = j + 1
call append(stack2, current2)
current2 => current2%left
end do
if (j == 0 .or. (i > 0 .and. stack1(i)%val <= stack2(j)%val)) then
current1 => stack1(i)
i = i - 1
current2 => current1%right
k = k + 1
call append(merged_values, current1%val)
else
current2 => stack2(j)
j = j - 1
current1 => current2%right
k = k + 1
call append(merged_values, current2%val)
end if
end do
end function mergeBSTs
function traverseBST(root) result(values)
type(TreeNode), pointer, intent(in) :: root
integer, allocatable :: values(:)
type(TreeNode), pointer :: current
type(TreeNode), allocatable :: stack(:)
integer :: i, j
if (.not. associated(root)) then
allocate(values(0))
return
end if
allocate(stack(0))
current => root
i = 0
j = 0
do while (associated(current) .or. i > 0)
do while (associated(current))
i = i + 1
call append(stack, current)
current => current%left
end do
current => stack(i)
i = i - 1
j = j + 1
call append(values, current%val)
current => current%right
end do
end function traverseBST
subroutine append(arr, val)
type(TreeNode), allocatable, intent(inout) :: arr(:)
type(TreeNode), pointer, intent(in) :: val
type(TreeNode), allocatable :: temp(:)
integer :: n
if (allocated(arr)) then
n = size(arr)
allocate(temp(n+1))
temp(1:n) = arr
call move_alloc(temp, arr)
else
allocate(arr(1))
end if
arr(size(arr)) = val
end subroutine append
subroutine append_int(arr, val)
integer, allocatable, intent(inout) :: arr(:)
integer, intent(in) :: val
integer, allocatable :: temp(:)
integer :: n
if (allocated(arr)) then
n = size(arr)
allocate(temp(n+1))
temp(1:n) = arr
call move_alloc(temp, arr)
else
allocate(arr(1))
end if
arr(size(arr)) = val
end subroutine append_int
end module bst_module
The Python function mergeBSTs takes two BST roots as input and yields the values of the merged BST in ascending order. It uses an iterative approach with two stacks to traverse the BSTs in-order and merge them.
The Fortran translation follows a similar approach. The TreeNode type represents a node in the BST, and the mergeBSTs function takes two BST roots as input and returns an array of the merged values. The traverseBST function is used to traverse a single BST and return its values in ascending order. The append and append_int subroutines are helper functions to append elements to arrays.
Note that the Fortran code assumes the availability of a Fortran compiler that supports recursive functions and allocatable arrays.