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

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

Lua Binary Search Tree Insertion

Lua

Goal -- WPM

Ready
Exercise Algorithm Area
1local TreeNode = {}
2TreeNode.__index = TreeNode
3
4function TreeNode:new(key, value)
5local node = setmetatable({}, TreeNode)
6node.key = key
7node.value = value
8node.left = nil
9node.right = nil
10return node
11end
12
13local BinarySearchTree = {}
14BinarySearchTree.__index = BinarySearchTree
15
16function BinarySearchTree:new()
17local tree = setmetatable({}, BinarySearchTree)
18tree.root = nil
19return tree
20end
21
22function BinarySearchTree:insert(key, value)
23if self.root == nil then
24self.root = TreeNode:new(key, value)
25else
26self:insert_recursive(self.root, key, value)
27end
28end
29
30function BinarySearchTree:insert_recursive(node, key, value)
31if key < node.key then
32if node.left == nil then
33node.left = TreeNode:new(key, value)
34else
35self:insert_recursive(node.left, key, value)
36end
37elseif key > node.key then
38if node.right == nil then
39node.right = TreeNode:new(key, value)
40else
41self:insert_recursive(node.right, key, value)
42end
43else
44-- Key already exists, update value or ignore based on requirements.
45-- For this example, we'll update the value.
46node.value = value
47end
48end
49
50-- Helper function to visualize the tree (in-order traversal)
51function BinarySearchTree:in_order_traversal(node, result_table)
52if node ~= nil then
53self:in_order_traversal(node.left, result_table)
54table.insert(result_table, {key = node.key, value = node.value})
55self:in_order_traversal(node.right, result_table)
56end
57end
58
59function BinarySearchTree:get_sorted_entries()
60local result = {}
61self:in_order_traversal(self.root, result)
62return result
63end
Algorithm description viewbox

Lua Binary Search Tree Insertion

Algorithm description:

This Lua code implements insertion into a Binary Search Tree (BST). It defines a `TreeNode` structure and a `BinarySearchTree` class. The `insert` method handles the initial empty tree case and then delegates to a recursive helper function `insert_recursive` to find the correct position for the new node. Duplicate keys result in value updates. A helper `in_order_traversal` is included to demonstrate the sorted nature of BSTs.

Algorithm explanation:

The `BinarySearchTree` implementation uses a recursive approach for insertion. The `TreeNode` metatable defines the structure of each node, containing a key, value, and references to left and right children. The `insert` method checks if the tree is empty; if so, it creates the root node. Otherwise, it calls `insert_recursive`. This recursive function compares the new key with the current node's key. If the new key is smaller, it attempts to insert into the left subtree; if larger, into the right subtree. If a child is `nil`, a new `TreeNode` is created and attached. If the key already exists, the node's value is updated. The time complexity for insertion is O(h), where h is the height of the tree. In the worst case (a skewed tree), h can be n, leading to O(n) complexity. In a balanced tree, h is O(log n), giving O(log n) complexity. The space complexity is O(h) due to the recursion stack. The `in_order_traversal` helper has O(n) time and O(h) space complexity. The BST property (left < parent < right) is maintained by the insertion logic.

Pseudocode:

define TreeNode with key, value, left, right
define BinarySearchTree with root

function BinarySearchTree.insert(key, value):
  if root is nil:
    root = new TreeNode(key, value)
  else:
    call insert_recursive(root, key, value)

function BinarySearchTree.insert_recursive(node, key, value):
  if key < node.key:
    if node.left is nil:
      node.left = new TreeNode(key, value)
    else:
      call insert_recursive(node.left, key, value)
  else if key > node.key:
    if node.right is nil:
      node.right = new TreeNode(key, value)
    else:
      call insert_recursive(node.right, key, value)
  else: -- key == node.key
    node.value = value -- update value for duplicate key

function BinarySearchTree.in_order_traversal(node, result_table):
  if node is not nil:
    call in_order_traversal(node.left, result_table)
    add {key=node.key, value=node.value} to result_table
    call in_order_traversal(node.right, result_table)