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 :: stack1_size, stack2_size, i, j
if (.not. associated(root1)) then
merged_values = inorderTraversal(root2)
return
end if
if (.not. associated(root2)) then
merged_values = inorderTraversal(root1)
return
end if
stack1_size = 0
stack2_size = 0
current1 => root1
current2 => root2
do while (associated(current1) .or. associated(current2) .or. stack1_size > 0 .or. stack2_size > 0)
do while (associated(current1))
stack1_size = stack1_size + 1
call reallocate_stack(stack1, stack1_size)
stack1(stack1_size) = current1
current1 => current1%left
end do
do while (associated(current2))
stack2_size = stack2_size + 1
call reallocate_stack(stack2, stack2_size)
stack2(stack2_size) = current2
current2 => current2%left
end do
if (stack2_size == 0 .or. (stack1_size > 0 .and. stack1(stack1_size)%val <= stack2(stack2_size)%val)) then
current1 => stack1(stack1_size)
stack1_size = stack1_size - 1
current2 => current1%right
i = size(merged_values) + 1
call reallocate_array(merged_values, i)
merged_values(i) = current1%val
else
current2 => stack2(stack2_size)
stack2_size = stack2_size - 1
current1 => current2%right
i = size(merged_values) + 1
call reallocate_array(merged_values, i)
merged_values(i) = current2%val
end if
end do
end function mergeBSTs
recursive function inorderTraversal(root) result(values)
type(TreeNode), pointer, intent(in) :: root
integer, allocatable :: values(:)
integer :: i
if (.not. associated(root)) then
allocate(values(0))
return
end if
values = [inorderTraversal(root%left), root%val, inorderTraversal(root%right)]
end function inorderTraversal
subroutine reallocate_stack(stack, new_size)
type(TreeNode), allocatable, intent(inout) :: stack(:)
integer, intent(in) :: new_size
type(TreeNode), allocatable :: temp(:)
integer :: i
if (allocated(stack)) then
allocate(temp(new_size))
do i = 1, min(size(stack), new_size)
temp(i) = stack(i)
end do
call move_alloc(temp, stack)
else
allocate(stack(new_size))
end if
end subroutine reallocate_stack
subroutine reallocate_array(array, new_size)
integer, allocatable, intent(inout) :: array(:)
integer, intent(in) :: new_size
integer, allocatable :: temp(:)
integer :: i
if (allocated(array)) then
allocate(temp(new_size))
do i = 1, min(size(array), new_size)
temp(i) = array(i)
end do
call move_alloc(temp, array)
else
allocate(array(new_size))
end if
end subroutine reallocate_array
end module bst_module
In the Fortran translation:
The TreeNode type is defined with an integer value (val) and pointers to the left and right child nodes.
The mergeBSTs function takes two pointers to the roots of the BSTs (root1 and root2) and returns an allocatable array of merged values.
The function uses two stacks (stack1 and stack2) to perform an iterative inorder traversal of both BSTs simultaneously.
The inorderTraversal function is a recursive helper function that performs an inorder traversal of a single BST and returns an array of values.
The reallocate_stack and reallocate_array subroutines are helper routines to dynamically resize the stack and array, respectively, when needed.
The merged values are yielded one by one during the traversal and stored in the merged_values array, which is reallocated as needed.
Note: The Fortran translation assumes that the necessary memory management and allocation routines are available, such as allocate, deallocate, and move_alloc.