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

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

GDScript: Binary Search Tree Node Insertion

GDScript

Goal -- WPM

Ready
Exercise Algorithm Area
1class Node:
2var value
3var left = null
4var right = null
5
6func _init(val):
7value = val
8
9
10class BinarySearchTree:
11var root = null
12
13func insert(value):
14if root == null:
15root = Node.new(value)
16return
17_insert_recursive(root, value)
18
19
20func _insert_recursive(current_node, value):
21if value == current_node.value:
22# Value already exists, do not insert duplicates
23return
24
25if value < current_node.value:
26if current_node.left == null:
27current_node.left = Node.new(value)
28else:
29_insert_recursive(current_node.left, value)
30else: # value > current_node.value
31if current_node.right == null:
32current_node.right = Node.new(value)
33else:
34_insert_recursive(current_node.right, value)
35
36
37# Helper function to visualize the tree (in-order traversal)
38func print_in_order():
39var result = []
40_print_in_order_recursive(root, result)
41print("In-order traversal: ", result)
42
43
44func _print_in_order_recursive(node, result_array):
45if node != null:
46_print_in_order_recursive(node.left, result_array)
47result_array.append(node.value)
48_print_in_order_recursive(node.right, result_array)
49
50
51# Example Usage:
52# var bst = BinarySearchTree.new()
53# bst.insert(50)
54# bst.insert(30)
55# bst.insert(70)
56# bst.insert(20)
57# bst.insert(40)
58# bst.insert(60)
59# bst.insert(80)
60# bst.insert(30) # Duplicate, should not be inserted
61# bst.print_in_order()
Algorithm description viewbox

GDScript: Binary Search Tree Node Insertion

Algorithm description:

This GDScript code defines a Binary Search Tree (BST) with a recursive insertion method. The BST is a data structure where each node has at most two children, referred to as the left child and the right child. For any given node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater than the node's value. This structure is fundamental for efficient searching, insertion, and deletion operations, often used in databases and symbol tables.

Algorithm explanation:

The `BinarySearchTree` class uses a `Node` class to represent each element in the tree, storing a `value` and references to its `left` and `right` children. The `insert` method is the public interface, handling the initial case where the tree is empty by creating the root node. For non-empty trees, it delegates the insertion logic to the private recursive helper function `_insert_recursive`. This helper function takes the current node and the value to be inserted. It first checks if the value already exists in the tree; if so, it returns without inserting to prevent duplicates. Otherwise, it compares the value with the current node's value. If the value is smaller, it attempts to insert into the left subtree; if the left child is null, a new node is created there. If the left child exists, the function recursively calls itself on the left child. A similar process occurs for values greater than the current node's value, targeting the right subtree. The recursion naturally handles traversing down the tree until an appropriate null child pointer is found for insertion. The time complexity for insertion in a balanced BST is O(log N), where N is the number of nodes, because the search path is logarithmic. In the worst case (a skewed tree), it degrades to O(N). The space complexity is O(log N) for a balanced tree and O(N) for a skewed tree due to the recursion depth.

Pseudocode:

class Node:
    value
    left = null
    right = null

function Node(val):
    initialize node with val

class BinarySearchTree:
    root = null

function insert(value):
    if root is null:
        set root to new Node(value)
        return
    call _insert_recursive(root, value)

function _insert_recursive(current_node, value):
    if value is equal to current_node.value:
        return # Duplicate, do nothing

    if value is less than current_node.value:
        if current_node.left is null:
            set current_node.left to new Node(value)
        else:
            call _insert_recursive(current_node.left, value)
    else: # value is greater than current_node.value
        if current_node.right is null:
            set current_node.right to new Node(value)
        else:
            call _insert_recursive(current_node.right, value)