ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Fortran Binary Search Tree Implementation

Fortran

Goal -- WPM

Ready
Exercise Algorithm Area
1PROGRAM bst_example
2IMPLICIT NONE
3
4TYPE node
5INTEGER :: key
6TYPE(node), POINTER :: left, right
7END TYPE node
8
9TYPE(node), POINTER :: root
10INTEGER :: i
11INTEGER :: keys_to_insert(7)
12INTEGER :: search_key
13
14! Initialize root to NULL
15root => NULL()
16
17! Keys to insert
18keys_to_insert = [50, 30, 20, 40, 70, 60, 80]
19
20PRINT *, "Inserting keys:"
21DO i = 1, SIZE(keys_to_insert)
22PRINT *, keys_to_insert(i)
23CALL insert(root, keys_to_insert(i))
24END DO
25PRINT *
26
27PRINT *, "In-order traversal of BST:"
28CALL inorder_traversal(root)
29PRINT *
30
31! Search for a key
32search_key = 40
33PRINT *, "Searching for key: ", search_key
34IF (search(root, search_key) /= NULL())
35PRINT *, "Key ", search_key, " found."
36ELSE
37PRINT *, "Key ", search_key, " not found."
38END IF
39
40search_key = 90
41PRINT *, "Searching for key: ", search_key
42IF (search(root, search_key) /= NULL())
43PRINT *, "Key ", search_key, " found."
44ELSE
45PRINT *, "Key ", search_key, " not found."
46END IF
47
48CONTAINS
49
50SUBROUTINE insert(node_ptr, key_val)
51IMPLICIT NONE
52TYPE(node), POINTER, INTENT(INOUT) :: node_ptr
53INTEGER, INTENT(IN) :: key_val
54TYPE(node), POINTER :: new_node
55
56IF (node_ptr => NULL()) THEN
57ALLOCATE(new_node)
58new_node%key = key_val
59new_node%left => NULL()
60new_node%right => NULL()
61node_ptr => new_node
62ELSE IF (key_val < node_ptr%key) THEN
63CALL insert(node_ptr%left, key_val)
64ELSE IF (key_val > node_ptr%key) THEN
65CALL insert(node_ptr%right, key_val)
66ELSE
67! Key already exists, do nothing or handle as needed
68PRINT *, "Warning: Duplicate key ", key_val, " ignored."
69END IF
70END SUBROUTINE insert
71
72SUBROUTINE inorder_traversal(node_ptr)
73IMPLICIT NONE
74TYPE(node), POINTER, INTENT(IN) :: node_ptr
75
76IF (node_ptr => NULL()) THEN
77RETURN
78END IF
79
80CALL inorder_traversal(node_ptr%left)
81PRINT *, node_ptr%key, " "
82CALL inorder_traversal(node_ptr%right)
83END SUBROUTINE inorder_traversal
84
85FUNCTION search(node_ptr, key_val)
86IMPLICIT NONE
87TYPE(node), POINTER, INTENT(IN) :: node_ptr
88INTEGER, INTENT(IN) :: key_val
89TYPE(node), POINTER :: search
90
91search => NULL()
92IF (node_ptr => NULL()) THEN
93RETURN
94END IF
95
96IF (key_val == node_ptr%key) THEN
97search => node_ptr
98ELSE IF (key_val < node_ptr%key) THEN
99search => search(node_ptr%left, key_val)
100ELSE IF (key_val > node_ptr%key) THEN
101search => search(node_ptr%right, key_val)
102END IF
103RETURN search
104END FUNCTION search
105
106END PROGRAM bst_example
Algorithm description viewbox

Fortran Binary Search Tree Implementation

Algorithm description:

This Fortran program implements a Binary Search Tree (BST), a fundamental data structure in computer science. It allows for efficient insertion, deletion, and searching of elements. Each node in the tree stores a key and has pointers to its left and right children, with the property that all keys in the left subtree are less than the node's key, and all keys in the right subtree are greater. BSTs are used in various applications, including symbol tables, dictionaries, and implementing sets.

Algorithm explanation:

The time complexity for insertion and searching in a BST is O(h), where 'h' is the height of the tree. In a balanced BST, h is O(log n), leading to O(log n) operations. However, in the worst case (a skewed tree), h can be O(n), resulting in O(n) complexity. The space complexity is O(n) to store the nodes. Edge cases include an empty tree (handled by checking for NULL pointers), inserting duplicate keys (typically ignored or handled by a counter), and searching for a non-existent key. Correctness is maintained by adhering to the BST property: left child < parent < right child, ensuring ordered traversal and efficient searching.

Pseudocode:

DEFINE node TYPE:
  key: integer
  left: pointer to node
  right: pointer to node

FUNCTION insert(node_ptr, key_val):
  IF node_ptr is NULL:
    Create new_node with key_val, left=NULL, right=NULL
    node_ptr = new_node
  ELSE IF key_val < node_ptr.key:
    insert(node_ptr.left, key_val)
  ELSE IF key_val > node_ptr.key:
    insert(node_ptr.right, key_val)
  ELSE:
    // Duplicate key, ignore or handle

FUNCTION inorder_traversal(node_ptr):
  IF node_ptr is NULL: RETURN
  inorder_traversal(node_ptr.left)
  PRINT node_ptr.key
  inorder_traversal(node_ptr.right)

FUNCTION search(node_ptr, key_val):
  IF node_ptr is NULL: RETURN NULL
  IF key_val == node_ptr.key: RETURN node_ptr
  ELSE IF key_val < node_ptr.key:
    RETURN search(node_ptr.left, key_val)
  ELSE IF key_val > node_ptr.key:
    RETURN search(node_ptr.right, key_val)